Computer Science, asked by vasurgukt4083, 1 year ago

Write a java program that prompts the user for an integer and then prints out all prime numbers up to that integer

Answers

Answered by Anonymous
9
Hey Friend,

Here's your program using Buffered Reader...

import java.io.*;
class Prime
{
public static void main () throws IOException
{
System.out.println("Enter a number");

BufferedReader br = new BufferedReader (new InputStreamReader (System.in));

int n = Integer.parseInt(br.readLine()); // to read the number
int a = 0; // to count the factors
int i = 0; // to move from 1 to n
int x = 0; // to shift to the next number 

System.out.println ("The prime numbers are");

for (i=1; i<=n; i++)
{
a=0;
for (x=1; x<=i; x++)
{
if (i%x == 0)
a++;
}
if (a==2)
System.out.println (i);
}
}
}

Hope it helps!
Attachments:
Similar questions