Monday 31 August 2020

Count Prime nos from a Given Range

Write a program to find Number is Prime number or not from a given range 

Input: Enter the First number: 
20
Enter the Last number: 
50
Output: Prime nos from Given Range :
23
29
31
37
41
43
47
no of prime number in list: 7

Code In Java:
import java.util.Scanner;
public class Prime
{
public static void main(String[] args) 
{
Scanner scan = new java.util.Scanner(System.in);
System.out.println("Enter the First number: ");
int first=scan.nextInt();
System.out.println("Enter the Last number: ");
int last=scan.nextInt();
int count,count2=0,temp;
System.out.println("Prime nos from Given Range :");
//condition used to identify number is odd or even
for(temp=first;temp<=last;temp++)
{
count=0;
for(int j=1; j<=temp; j++)
{
if(temp%j==0)
{
count++;
}
}
if(count==2)

System.out.println(temp);
count2++;
}
}
System.out.println();
System.out.println("no of prime number in list: " + count2);
}
}
Output:
Enter the First number: 
20
Enter the Last number: 
50
Prime nos from Given Range :
23
29
31
37
41
43
47
no of prime number in list: 7


Write a JAVA program to Find the Sum Of First N Natural Numbers





No comments:

Post a Comment

If you have any doubts, please let me know

Pattern 31 & 32