Algorithm
- Start
- x:=0
- If x<=1, go to step 4 or else go to step 9
- Print "Enter a number"
- Read x
- If x<=1, go to step 7 or else go to step 8
- Print "Invalid Input. Try Again."
- Go to step 3
- Print "The Prime Numbers Are:"
- j=2
- If j<=x, go to step 12 or else go to step 22
- a=j
- count=0
- i=2
- If i<a, go to step 16 or else go to step 19
- If a%i==0, go to step 17 or else go to step 18
- count++
- i++. Go to step 15
- If count==0, go to step 20 or else go to step 21
- Print j
- j++. Go to step 11
- Stop
Program
Buffered Reader
import java.io.*;
class PrimeList
{
int x;
void accept() throws IOException
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
while(x<=1)
{
System.out.println("Enter a number");
x=Integer.parseInt(br.readLine());
if(x<=1)
{
System.out.println("Invalid Input. Try Again");
}
}
}
boolean isPrime(int a)
{
int count=0;
for(int i=2;i<a;i++)
{
if(a%i==0)
{
count++;
}
}
if(count==0)
{
return true;
}
else
{
return false;
}
}
void printList()
{
System.out.println("The Prime Numbers Are:");
for(int j=2;j<=x;j++)
{
if(isPrime(j)==true)
{
System.out.println(j);
}
}
}
void main() throws IOException
{
accept();
printList();
}
}
Scanner
import java.util.*;
class PrimeList
{
int x;
void accept()
{
Scanner sc=new Scanner(System.in);
while(x<=1)
{
System.out.println("Enter a number");
x=sc.nextInt();
if(x<=1)
{
System.out.println("Invalid Input. Try Again");
}
}
}
boolean isPrime(int a)
{
int count=0;
for(int i=2;i<a;i++)
{
if(a%i==0)
{
count++;
}
}
if(count==0)
{
return true;
}
else
{
return false;
}
}
void printList()
{
System.out.println("The Prime Numbers Are:");
for(int j=2;j<=x;j++)
{
if(isPrime(j)==true)
{
System.out.println(j);
}
}
}
void main()
{
accept();
printList();
}
}
Documentation
Variable Description
Data Type | Variable Name | Description |
int | x | To accept a number from the user. |
int | count | To count the number of factors except 1 and itself |
int | i | To act as a counter in loop |
int | j | To act as a counter in loop |
int | a | To parse value to method. |
Method Description
Return Type | Method Name | Description |
void | accept() | To accept data from the user |
boolean | isPrime(int) | To check if the argument is Prime or not |
void | printList() | To print the list of prime numbers |
void | main() | To act as an entry gateway for the compiler |
Output
Share with all ISC Computer Science Students you know.
Comment below with all your program requests and blog suggestions.
Follow us on social media by clicking on these links.
Instagram : @IscJavaCode
Twitter: @IscCode
Quora: ISC Java Code
Stay Healthy
Stay Happy
Stay Helpful
Comments
Post a Comment