Computer Science, asked by sahanareddy4687, 9 months ago

write a java program to print sum of prime numbers ​

Answers

Answered by Chinmai35
0

To print the sum of all prime numbers up to N we have to iterate through each number up to the given number and check if the number is a prime or not if it is a prime number then simply sum it or add it in one temporary variable.

Answered by parneet465
2

Answer:

// Java Program to Print Prime Numbers from 1 to N

public class PrintPrimeNumbers1 {

public static void main(String[] args)  

{

 int i, number, count;  

 

 System.out.println(" Prime Numbers from 1 to 100 are : ");  

 for(number = 1; number <= 100; number++)

 {

  count = 0;

     for (i = 2; i <= number/2; i++)

     {

      if(number % i == 0)

      {

       count++;

       break;

      }

     }

     if(count == 0 && number != 1 )

     {

      System.out.print(number + " ");

     }  

 }

}

}

Similar questions