0% found this document useful (0 votes)
8 views2 pages

Prime

The document describes a Java program that takes a number from the user between 1 and 1000 and checks if it is a prime number. It includes features like only allowing valid input, writing a function that checks primality, and validating the program. The program uses loops and conditionals to check each number entered by the user for primality.

Uploaded by

Snehal Wakchaure
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views2 pages

Prime

The document describes a Java program that takes a number from the user between 1 and 1000 and checks if it is a prime number. It includes features like only allowing valid input, writing a function that checks primality, and validating the program. The program uses loops and conditionals to check each number entered by the user for primality.

Uploaded by

Snehal Wakchaure
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

Que 5:

Write a program to take an input number from the user between the range of 1 and
1000 and return true if
the integer is prime number.
Implement the following features
a. The user is only allowed to enter numbers within the range and user can enter 0
to exit the program.
Otherwise it keeps on running.
b. You need to write a function which takes a single integer parameter and returns
a boolean result.
This function evaluates if the argument is a prime between1 and 1000.
c. You will be evaluated for c
ompleteness and validations.
<------------------------------->
package Demo;
import java.util.Scanner;

public class PrimeNo


{
PrimeNo(int num)
{
Scanner sc=new Scanner(System.in);
//int num;
int n=1000;
int cnt=0 ;
if(num==0) {
System.out.println("Number is zero");
}
else do
{
if(num>n)
{
System.out.println("Number is an out of range");
System.out.println("Enter another number:");
num=sc.nextInt();
do{
System.out.println("null1");
for(int i=1;i<=n;i++) {
if(num%i==0) {
cnt++;
if(cnt>2) {
System.out.println(num+" is prime");
}
else {
System.out.println(num+" is not prime");
}
}
}
}while(cnt!=0);
}//if(num>n)
else {
for(int i=1;i<=n;i++) {
if(num%i==0) {
cnt++;
if(cnt>2) {
System.out.println(num+" is prime");
}
else {
System.out.println(num+" is not prime");
}
}
}//for
}//else

}while(0<n); //else if(0<n)

}
}

/////Main class

package Demo;

import java.util.Scanner;

public class TestPrimeNo {

public static void main(String[] args) {


// TODO Auto-generated method stub
Scanner sc=new Scanner(System.in);
System.out.println("Enter no. to check prime or not");
int num = sc.nextInt();
PrimeNo p=new PrimeNo(num);
}

You might also like