Write a Java Program to find the Sum Of Prime Numbers From Given Range
Basic Java programs. All programs done in very simple ways. It helps beginners to grow their programming skills and build a programming logic.
Write a Java Program to find the Sum Of Prime Numbers From Given Range
import java.util.Scanner; public class SumOfPrimrNumbersFronGivenRange { public static void main(String[] args) { Scanner scan = new java.util.Scanner(System.in); System.out.println("Enter the starting number: "); int n1=scan.nextInt(); System.out.println("Enter the end number: "); int n2=scan.nextInt(); int count,sum=0; System.out.println("Prime nos from Given Range :"); //condition used to identify number is odd or even for(int i=n1; i<=n2; i++) { count=0; for(int j=1; j<=i; j++) { if(i%j==0) { count++; } } if(count==2) { System.out.println(i); sum=sum+i; } } System.out.println("Sum of Prime Numbers From Given Range= "+sum); } }
No comments:
Post a Comment
If you have any doubts, please let me know