Write a java Program to print N prime numbers and find sum and average

Write a java Program to print N prime numbers and find sum and average

Write a java Program to print N prime numbers and find sum and average

Program
public class Main 
{
    public static void main(String[] args) 
    {
        int num =0,counter=0, i =0,sum=0;
        double average=0;
        System.out.println("Prime numbers from 1 to 100 are :"); 
        for (i = 1; i <= 100; i++) 
        { 
            counter=0;
            for(num =i; num>=1; num--)
            { 
                if(i%num==0) 
                { 
                    counter = counter + 1; 
                } 
            } 
            if (counter ==2) 
            { 
                System.out.print(i+" "); 
                sum=sum+i;
            }
        }
        System.out.println("sum is "+sum);
        average=sum/counter;
        System.out.println("average is "+average);
    }
}


Output:

Prime numbers from 1 to 100 are :
2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97
sum is 1060
average is 117.0



More Questions


36 . Write a java Program to print palindrome number upto N numbers
37 . Write a java Program to print N prime numbers and find sum and average
38 . Write a Java Program to find largest number in an array
39 . Write a Program to find second largest number in an array
40 . Write a Program to Find largest and smallest number in an array in java
41 . Write a Java Program to find largest and second largest in an array
42 . Write a java program to find the index of the largest number in an array
43 . Write a java program to Find the index of the smallest number in an array
44 . Write a Program to remove duplicate element in an array
45 . Write a java Program to print odd and even numbers from an array
46 . Write a java program to add two matrix



For latest job updates join Telegram Channel: https://t.me/sateeshm

Apply For The Job

Apply For Job

Java Material

Python Material

PLSQL Material

.Net Material

Java Practice Programs

400 Java Programs