मराठी

Write a program by using a class with the following specifications: Class name: Calculate Instance variables: int num, f, rev Member Methods: Calculate (int n): - Computer Applications

Advertisements
Advertisements

प्रश्न

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

Class name: Calculate
Instance variables: int num, f, rev 
Member Methods:
Calculate (int n): to initialize num with n, f and rev with 0 (zero)
int prime(): to return 1, if number is prime
int reverse(): to return reverse of the number
void display(): to check and display whether the number is a prime        palindrome or not

कोड लेखन

उत्तर

import java.util.*;
class Calculate
{int num, f, rev;

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

int prime()
{int cnt = 0;
for (int i = 1; i <= num; i++)
{ if (num % i == 0)
cnt;
}
if (cnt == 2) return 1;
else return 0;
}

int reverse()
{String sum = "";
int d, n = num;
do{
d = n % 10;
sum = d + sum; // To get the Palindrome of the number
n = n / 10;
}while (n > 0);
return Integer.parseInt(sum);
}

void display() 
{if (prime() == 1) && (reverse() == num))
System.out.println("The entered number is a prime number palindrome");
else
System.out.println("The entered number is not a prime number palindrome");
}

public static void main(String args|])
{Scanner ob = new Scanner(System.in);
System.out.println("Enter the number");
Calculate C = new Calculate(ob.nextInt());// Must match constructor
C.display();
}}
shaalaa.com
  या प्रश्नात किंवा उत्तरात काही त्रुटी आहे का?
पाठ 7: Constructors - EXERCISES [पृष्ठ ४१९]

APPEARS IN

अविचल Computer Applications [English] Class 10 ICSE
पाठ 7 Constructors
EXERCISES | Q VI. 3. | पृष्ठ ४१९
Share
Notifications

Englishहिंदीमराठी


      Forgot password?
Use app×
Our website is made possible by ad-free subscriptions or displaying online advertisements to our visitors.
If you don't like ads you can support us by buying an ad-free subscription or please consider supporting us by disabling your ad blocker. Thank you.