Java Program to find the sum of numbers divisible by 7 between 100 to 200

Write a Java Program to find sum of numbers divisible by 7 between 100 to 200

Write a Java program to find the number of and the sum of all numbers greater than 100 and less than 200 which are divisible by 7.

Video Tutorial:

Steps (Program logic):

1. Declare two local variables sum and count both are set to 0 to store the sum of all numbers between 100 and 200 which are divisible by 7 and count of those numbers respectively.

2. Use a for loop a to loop over from 101 to 199.

3. Check all the numbers between 100 to 200, whether they are divisible by 7 using mod operator.

4. If number is divisible by 7, then add number to previous sum and increment the count.

5. after the loop, display the result, that is Sum of number between 100 to 200 which are divisible by 7 and count of numbers between 100 to 200 which are divisible by 7.

See also  Byte code Java's Magic - Java Tutorial

Source code of Java Program to find the sum of numbers divisible by 7 between 100 and 200

/*
 * Write a Java program to find the number of and the sum of  all  
 * numbers greater than 100 and less than 200 which are divisible by 7
 
 * Subscribe to Mahesh Huddar YouTube Channel for more Videos
 */
 public class SumCountDivisibleBy7 
 {
 public static void main(String args[])
 {
 int sum = 0, count = 0;
 for (int i = 101; i < 200; i++)
 {
     if (i % 7 == 0)
     {
         sum = sum + i;
         count++;
     }
 }
 System.out.println("The Sum of the number between 100 to 200 which are divisible by 7 is: "+sum);
 System.out.println("Total numbers between 100 to 200 which are divisible by 7 is: "+count);
 }
 } 

Output:

The Sum of the number between 100 to 200 which are divisible by 7 is: 2107

Total numbers between 100 to 200 which are divisible by 7 is: 14

If you like the tutorial share it with your friends. Like the Facebook page for regular updates and YouTube channel for video tutorials.

See also  Implementing multiple inheritances using interfaces in Java

Leave a Comment

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