In: Computer Science
JAVA
Write a program to sum the numbers from 1 to 100 that are divisible by 7, and compute the average of those numbers, print both the sum and the average with appropriate messages to the screen.
Run the program. Capture the console output.
Put the program code and console output at the end of your text file,
The following code gives the required functionality:
/* package whatever; // don't place package name! */
import java.util.*;
import java.lang.*;
import java.io.*;
/* Name of the class has to be "Main" only if the class is
public. */
class Ideone
{
public static void main (String[] args) throws
java.lang.Exception
{
// your code goes here
int count=0,sum=0;
double avg;
for(int i=1;i<=100;i++)
{
if(i%7 ==
0)
{
sum = sum + i;
count++;
}
}//end of for loop
System.out.println("Sum of numbers
between 1 and 100 that are divisible by 7 is:"+ sum);
avg = sum/count;
System.out.println("Avg. of numbers
between 1 and 100 that are divisible by 7 is:"+ avg);
}
}
Output of the given code:
Hope it helps.