Write a class program by using class with the following specifications: Class name: prime
Data members / instant variables : int n
Member functions: prime0 : constructor to initialize
void input (int x) : to assign n with x
void display(): to check and print whether number n is prime or not.
ICSE 10 th board
Answers
Answered by
7
Answer:
import java.util.*;
class prime{
int n;
prime(){
n=0;
}
void input(int x){
n=x;
}
void display(){
boolean flag = false;
for(int i = 2; i <= n/2; ++i)
{
if(n % i == 0)
{
flag = true;
break;
}
}
if (!flag)
System.out.println(n + " is a prime number.");
else
System.out.println(n + " is not a prime number");
}
}
Similar questions