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

For a better understanding, we always recommend you to learn the basic topics of C programming listed below:

How to check given number is divisible by five?

To determine all numbers that are divisible by five means when we take a number mod 5. We have to get the remainder zero. 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 five.

How to determine numbers divisible by five in C?

This is a C Program which calculates the number of integers divisible by 5 in the given range, 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, 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 Integer divisible by five.

ALGORITHM

STEP 1: Include the Header files to includes 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 */
} /* End of main()*/
                                      

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