Write a program to find number and sum of all integer between 100 and 200 which are divisible by 9
This is a Java program to find and print all the numbers between 100 and 200 that are divisible by 9 and also calculate their sum. Here is a step-by-step explanation of the code:
- The first line of code declares a public class named "Sum_Divisible_Nine" which will contain the main method.
- The main method is declared with the following signature: public static void main(String[] args). It is the entry point of the program and will be executed first.
- Two integer variables, i and sum, are declared and initialized to 0.
- The program then prints the string "Numbers between 100 and 200, divisible by 9 : \n" using the System.out.println method. The \n character is used to add a newline after the string.
- The for loop starts from 101 and continues until 199 (excluding 200). It checks every integer value between 101 and 199, inclusive, using the variable i.
- Within the for loop, an if statement checks whether the current value of i is divisible by 9 by using the modulus operator %. If the remainder of i divided by 9 is zero, the if block is executed.
- Inside the if block, the current value of i is printed to the console using the System.out.println method.
- The value of i is added to the variable sum.
- After the for loop is complete, the program ends without any output.
Therefore, this program finds all the numbers between 100 and 200 that are divisible by 9 and also calculates their sum.
Source Code
class Sum_Divisible_Nine
{
public static void main(String[] args)
{
int i, sum=0;
System.out.println("Numbers between 100 and 200, divisible by 9 : \n");
for(i=101;i<200;i++)
{
if(i%9==0)
{
System.out.println(i);
sum+=i;
}
}
}
}
Output
Numbers between 100 and 200, divisible by 9 :
108
117
126
135
144
153
162
171
180
189
198