KnowledgeBoat Logo
|

Computer Applications

Write a program by using a class with the following specifications:

Class name — Prime

Data members — private int n

Member functions:

  1. void input() — to input a number
  2. void checkprime() — to check and display whether the number is prime or not

Use a main function to create an object and call member methods of the class.

Java

Encapsulation & Inheritance in Java

34 Likes

Answer

import java.util.Scanner;

public class Prime
{
    private int n;
    
    public void input() {
        Scanner in = new Scanner(System.in);
        System.out.print("Enter the number: ");
        n = in.nextInt();
    }
    
    public void checkprime() {
        boolean isPrime = true;
        if (n == 0 || n == 1)
            isPrime = false;
        else {
            
            for (int i = 2; i <= n / 2; i++) {
                if (n % i == 0) {
                    isPrime = false;
                    break;
                }
            }
        }
        
        if (isPrime)
            System.out.println("Prime Number");
        else
            System.out.println("Not a Prime Number");
    }
    
    public static void main(String args[]) {
        Prime obj = new Prime();
        obj.input();
        obj.checkprime();
    }
}

Variable Description Table

Program Explanation

Output

BlueJ output of Write a program by using a class with the following specifications: Class name — Prime Data members — private int n Member functions: (a) void input() — to input a number (b) void checkprime() — to check and display whether the number is prime or not Use a main function to create an object and call member methods of the class.

Answered By

17 Likes


Related Questions