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

October 22, 2023, Learn eTutorial

A number or integer is said to be divisible by 5 or any other number 'n' if the remainder of that division operation is 0.

How do find the numbers which are divisible by five in a given range and their 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, if the remainder is zero, then that integer is divisible by 5. Then we will add that integer to the sum variable and increment the count

Let us take an example "5, 10, 15." take the Mod of these numbers with 5, and if we get the remainder as zero then that number is divisible by 5. The same logic can be applied to check any number divisibility.

Simple Program: C Program to check whether a number is divisible by 5 or not.

How do we apply the number divisibility logic using C program?

This is a C Program, 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 integer.  Print that as an Integer divisible by five.


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

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. 

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 integer Mod 5 is zero or not.

STEP 7: If the remainder is zero, then increment the count by one, and print the integer 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

To check the number divisibility program, we are using the below C topics, we recommend learning these topics for a better understanding

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*/
         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 */


Enter the value of N1 and N2

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