C Program to Find the Number of Integers Divisible by 5

This is a C Program which calculates the number of integers divisible by 5 in the given range.

Problem Description

1. This program takes the range as input and finds the number of integers divisible by 5 in the given range.
2. Also finds the sum of all integers that are divisible by 5 in the given range.

Problem Solution

1. Take the range as input.
2. Find all the integers that gives remainder zero when divided by 5 and print them as output.
3. Add all the integers that are divisible by 5 and print the sum.
4. Also print the count of integers that are divisible by 5.

Program/Source Code

Here is source code of the C program to calculate the number of integers divisible by 5. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

  1. /*
  2.  * C program to find the number of integers divisible by 
  3.  * 5 between the given range num1 and num2, where num1 < num2.
  4.  *
  5.  * Also find the sum of all these integer numbers which are divisible
  6.  * by 5 and display the total.
  7.  */
  8. #include <stdio.h>
  9.  
  10. void main()
  11. {
  12.     int i, num1, num2, count = 0, sum = 0;
  13.  
  14.     printf("Enter the value of num1 and num2 \n");
  15.     scanf("%d %d", &num1, &num2);
  16.     /* Count the number and compute their sum*/
  17.     printf("Integers divisible by 5 are \n");
  18.     for (i = num1; i < num2; i++)
  19.     {
  20.         if (i % 5 == 0)
  21.         {
  22.             printf("%3d,", i);
  23.             count++;
  24.             sum = sum + i;
  25.         }
  26.     }
  27.     printf("\n Number of integers divisible by 5 between %d and %d =
  28.  %d\n", num1, num2, count);
  29.     printf("Sum of all integers that are divisible by 5 = %d\n", sum);
  30. }
Program Explanation

1. Take the range as input and store it in the variables num1 and num2 respectively.
2. Firstly initialize the variables count and sum to zero.
3. Using the for loop, find all the integers that gives remainder zero when divided by 5 and print them consecutively.
4. Along with this, increment both the variables i.e increment the variable count by 1 and variable sum by the number that is divisible by 5.
5. Print the variables count and sum as output.

advertisement
advertisement
Runtime Test Cases
Case:1
Enter the value of num1 and num2
12 17
Integers divisible by 5 are
 15,
Number of integers divisible by 5 between 12 and 17 = 1
Sum of all integers that are divisible by 5 = 15
 
Case:2
Enter the value of num1 and num2
1 10
Integers divisible by 5 are
 5,10
Number of integers divisible by 5 between 1 and 10 = 2
Sum of all integers that are divisible by 5 = 15

Sanfoundry Global Education & Learning Series – 1000 C Programs.

Here’s the list of Best Books in C Programming, Data-Structures and Algorithms

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!
If you wish to look at other example programs on Simple C Programs, go to Simple C Programs. If you wish to look at programming examples on all topics, go to C Programming Examples.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.