Computer Applications

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

Class name — Calculate

Instance variables:

  1. int num
  2. int f
  3. int rev

Member Methods:

  1. Calculate(int n) — to initialize num with n, f and rev with 0 (zero)
  2. int prime() — to return 1, if number is prime
  3. int reverse() — to return reverse of the number
  4. void display() — to check and print whether the number is a prime palindrome or not

Java

Java Constructors

80 Likes

Answer

import java.util.Scanner;

public class Calculate
{
    private int num;
    private int f;
    private int rev;

    public Calculate(int n) {
        num = n;
        f = 0;
        rev = 0;
    }

    public int prime() {

        f = 1;

        if (num == 0 || num == 1)
            f = 0;
        else
            for (int i = 2; i <= num / 2; i++) {
                if (num % i == 0) {
                    f = 0;
                    break;
                }
            }

        return f;
    }

    public int reverse() {
        
        int t = num;
        
        while (t != 0) {
            int digit = t % 10;
            rev = rev * 10 + digit;
            t /= 10;
        }
        
        return rev;
    }
    
    public void display() {
        if (f == 1 && rev == num)
            System.out.println(num + " is prime palindrome");
        else
            System.out.println(num + " is not prime palindrome");
    }
    
    public static void main(String args[]) {
        
        Scanner in = new Scanner(System.in);
        System.out.print("Enter number: ");
        int x = in.nextInt();
        
        Calculate obj = new Calculate(x);
        obj.prime();
        obj.reverse();
        obj.display();
    }
}

Variable Description Table

Program Explanation

Output

Answered By

38 Likes


Related Questions