Do you know what is the sum of first 1000 prime numbers?
see how to find it easily using Java Programming, Here's the code :
OUTPUT :
see how to find it easily using Java Programming, Here's the code :
public class Main { public static int isPrime(int i){ int flag=0; for(int j=2;j<i;j++) { if(i%j==0) { flag=1; break; } } return flag; } public static void main (String[] args){ System.out.println("******* SUM OF PRIMES *******"); int sum=-1; for (int i=1,c=0;c<=1000;i++){ if(isPrime(i)==0){ sum=sum+i; c++; } } System.out.println("RESULT : "+sum); } }
OUTPUT :
2 Comments Leave new
can you please give me the code for this question???????????
ReplyQ:Check whether the given number is a prime number or not if it is not then print nearest prime number??????
Nice program.... Check the below Code:
Replyimport java.io.*;
public class series {
public static void main ( String arg[] ) throws IOException{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter Number: ");
int num=Integer.parseInt(br.readLine()); //Enter Numers >=2
if(isPrime(num)){
System.out.println(num + " is prime.");
} else{
System.out.print(num + " is not prime " + prePrime(num) + " is prime");
}
}
public static Boolean isPrime(int n){ //check for prome or not
for(int i=2;i<n;i++){
if(n%i==0)
return false;
}
return true;
}
public static int prePrime(int n){ //Find previous prime
int i , check;
for(i=n-1;i>2;i--){
check = 0;
for(int j=2;j<i;j++){
if(i%j==0){
check =1;
break;
}
}
if(check==0)
break;
}
return i;
}
}
Make sure you tick the "Notify Me" box below the comment form to be notified of follow up comments and replies.