Q.9 Find the PrimeNumbers in between given Range,Print count and their sum?import java.util.Scanner;
public class PrimeNumbers {
public static void main(String[] args){
Scanner in =new Scanner(System.in);
System.out.println("Enter Starting Number");
int start=in.nextInt();
System.out.println("Enter Ending Number");
int end=in.nextInt();
int count=0;
int sum=0;
for(int i=start;i<=end;i++){
boolean isPrime=true;
for(int j=2;j<=i/2;j++){
if(i%j==0){
isPrime=false;
break;
}
}
if(isPrime&i>0)
{
System.out.print(i+" ");
count++;
sum=sum+i;
}
}in.close();
System.out.println();
System.out.println("Count = "+count);
System.out.println("Sum = "+sum);
}
}
No comments:
Post a Comment