C Program to find sum of numbers divisible by 5 and the total count is within a range

A number is said to be divisible by 5 or any other number if the remainder of that division is 0. For a better understanding of this C program example, we always recommend you to learn the basic topics of C programming listed below:

How do we check the given number is divisible by five and calculate its sum?

To determine all numbers in a given range are divisible by five, we take each number in that range and calculate that number mod 5. After the division we check whether the remainder is zero or not, if it is zero, then that number is divisible by 5. If the number is divisible by 5. we will add that number to the sum variable and increment the count variable as 1. 

Let us take an example "5, 10, 15, etc." take the Mod of these numbers with 5, we will get the remainder as zero, so these numbers are divisible by 5. this is the logic we can apply to find any number divisibility.

How do we determine numbers divisible by five in C and calculate their sum?

This is a C Program that calculates the number of integers divisible by 5 in the given range and calculates its count and their sum. Firstly we declare the variables count, Sum, etc. as integers.

Now we open a 'for loop' from the Lower limit to the Upper limit values, which we receive from the user. Inside the 'for loop', we take each number and do Mod operation; if the remainder is zero, increment count with one and add Sum equal to Sum + that number.  Print that number as an Integer divisible by five.

ALGORITHM

STEP 1: Include the Header files to include the Libraries which have built-in functions defined in the C program.

STEP 2: Start the program execution by using the main() function. We define the Main function operating Void, which means no return value.

STEP 3: Declare and Initialize the variables using int Data type in the C programming language. 

STEP 4: Accept the values from the user for the Lower and Upper limit using the printf and scanf.

STEP 5: Use a 'for loop' from Lower limit to Upper limit increment by 1 to check each element is a divisor of 5.

STEP 6: Use an 'if 'condition to check the number Mod 5 is zero or not.

STEP 7: If the remainder is zero, then increment the count by one, and print the number as the divisor of 5

STEP 8: add that number to the Sum in each iteration of the Loop. 

STEP 9: Print the Sum and the numbers and the Count using printf in the C programming language. 

C Source Code

                                          #include <stdio.h>


void main()
{
   int i, N1, N2, count = 0, sum = 0;                          /* declares count, sum and two variables as integer */
   printf("Enter the value of N1 and N2\n");              /* user gives the value for lower and upper range */
   scanf("%d %d", & N1, & N2);
   printf("Integers divisible by 5 are\n");                   /*Count the number and compute their sum*/
   for (i = N1; i < N2; i++) 
   {
      if (i % 5 == 0) 
      {
         printf("%d,", I);                    /*using mod operator check the number is divisible by 5*/
         count++;
         sum = sum + I;                    /*add the numbers divisible by 5 to sum variable*/
      }
   }
  printf("\nNumber of integers divisible by 5 between %d and %d = %d\n",
    N1, N2, count);
  printf("Sum of all integers that are divisible by 5 = %d\n", sum);              /* displays the output of program */
} 
                                      

OUTPUT

Enter the value of N1 and N2
2
27

Integers divisible by 5 are
5, 10, 15, 20, 25,

Number of integers divisible by 5 between 2 and 27 = 5
Sum of all integers that are divisible by 5 = 75