94.2k views
3 votes
Write a java program with the following instruction:- class name: prime, instance variable: int n. Member methods:-

prime(): default constructor to initialize n
void input(int x): to assign n with x,
void display(): to check whether the number is prime or not

1 Answer

4 votes

class Prime {

int num = 1;

boolean flag = false;

public void input(int x) {

this.num = x;

}

public void display() {

for (int i = 2; i <= num / 2; ++i) {

// condition for nonprime number

if (num % i == 0) {

flag = true;

break;

}

}

if (!flag)

System.out.println(num + " is a prime number.");

else

System.out.println(num + " is not a prime number.");

}

}

May be some bugs as it is an untested code written using a mobile phone

User Jnns
by
5.9k points