Unsolved Programs: Question 1 Write a program by using a class with the following specifications: Class name power Data members SID private int n Member functions: les void input to input a number void cheokprime to check and display whether the number is prime or not 12 Use a main function to create an object and call member methods of the class. with descriptive table
Answers
Answered by
0
Answer:
import java.util.*;
public class Main
{
public static void main(String[] args) {
User u = new User();
System.out.print("Enter a number:");
int x = u.input();
u.checkPrime(x);
}
}
class User{
public int n;
Scanner sc = new Scanner(System.in);
public int input(){
int j = sc.nextInt();
return j;
}
public void checkPrime(int n){
boolean ip = true;
for(int i = 2; i < n; i++){
if(n%i == 0){
ip = false;
}
}
if(ip){
System.out.println(n + " is a prime number");
}
else{
System.out.println(n + " is not a prime number");
}
}
}
Explanation:
Similar questions