1. C Programs
  2. Basic Programs

C Program to Find the Number of Integers Divisible by 5 and their sum

In this program, we will ask the user to provide the minimum value and a maximum value range to be added and find integers divisible by 5 and a total of those integers.

To find integers divisible by 5, we will first start a for loop with variable i, which is the minimum value N1 provided by the user and will increase it using i++ until the value of i is equal to the maximum value which is N2.

Inside the for-loop, we will use if-else condition to check if the provided value of i %5  gives result in zero or not. If it is zero it means the value is divisible.

We will add 1  in a variable COUNT whose initial value was zero.  With every true condition, count will work as follows –

  • count = 0
  • count = count ++  (current value is 1)
  • count  = count ++ (current value is 1+1 = 2)
  • count = count ++  (current value is 2+1 = 3)

Also, we will add the value of integer to another variable SUM.   It will give us the total sum of integers which are divisible by 5. For example, 5,10,15 are divisible by 5. So for them, it will work as follows –

  • sum  = 0
  • sum = sum + i      (current value of i is 5 so , sum = 0 + 5 = 5)
  • sum = sum + i     (current value of i is 10 so , sum = 5 + 10 = 15)
  • sum = sum + i     (current value of i is 15 so , sum = 15 + 15 = 30)

Lets try into live code –

#include <stdio.h>
  
#include <conio.h>
  
void main()
  
{
  
  int i, N1, N2, count = 0, sum = 0;   /* declares count, sum and two variables as integer */
  
  clrscr();
  
  printf ("Enter the value of N1 and N2\n");   /* user gives the value for lower and upper range */
  
  scanf ("%d %d", &N1, &N2);
  
  /*Count the number and compute their sum*/
  
  printf ("Integers divisible by 5 are\n");  
  
  for (i = N1; i < N2; i++) 
  
  {
  
     if (i%5 == 0)
  
     { 
  
    printf("%3d,", 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 */
  
  
}
Comments to: C Program to Find the Number of Integers Divisible by 5 and their sum

Your email address will not be published. Required fields are marked *

Attach images - Only PNG, JPG, JPEG and GIF are supported.

Login

Welcome to WikiCoders

Join the site and store the save posts for later, share your own articles
Join WikiCoder
Registration is closed.