QuestionAsk Questions?

Calculate and display the sum of all prime number from 1 to 100 covert in java system codes

View Answers

December 15, 2010 at 3:38 PM

Hi Friend,

Try the following code:

class PrimeNumbers{ public static void main (String args[]){ String primeNo = ""; int sum=0; int j=0; System.out.println("Prime nums are:" ); for (int i = 1; i < 100; i++){ { for ( j = 2; j < i; j++){ if (i % j == 0) { break; } } if (i == j) { primeNo += i + " "; sum+=i; } } } System.out.println(primeNo); System.out.println("Sum of Prime Numbers: "+sum); } }

Thanks

December 27, 2010 at 4:54 PM

Hi friend Try this code.......

import java.io.; class today2 { public static void main(String args[])throws IOException { BufferedReader obg=new BufferedReader(new InputStreamReader(System.in)); String str=obg.readLine(); int num=Integer.parseInt(str); int sum=0; //int test=5; for(int i=2;i<=num;i++) { if(num%i==0) { sum=sum+i; } } System.out.println("Result"+sum); } }

Related Tutorials/Questions & Answers: