for loop

PHOTO EMBED

Sat Oct 14 2023 16:46:53 GMT+0000 (Coordinated Universal Time)

Saved by @Mohamedshariif #java

Write a program that generates 60 random integers in interval [0,100]. Your program computes the average of the numbers that are divisible by 3.

public static void main(String[] args) {
		
		int sum=0, count=0;
		
		 for(int i=1; i<=60; i++) {
			 
	     int num = (int) (Math.random()*101);      
		  
	     if(num%3==0) {
	       sum = sum+num; 
	       count = count+1;
	     }
		}
		 double avg=(double)sum/count;
		 System.out.println("Average: "+ avg);	
	 }
}

content_copyCOPY