0% found this document useful (0 votes)
22 views10 pages

Board Number Imp Practice Question

The document contains multiple Java programs that check for various types of numbers including twin primes, USHWA numbers, PalPrime numbers, Dudeney numbers, Super Palindromes, Duck numbers, EvenPal numbers, superspy numbers, magic numbers, happy numbers, amicable numbers, Adam numbers, cool numbers, and automorphic numbers. Each program includes user input, logic for determining the number type, and output statements indicating the result. The examples provided illustrate how each program works with specific input values.

Uploaded by

asha.bluebirds
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views10 pages

Board Number Imp Practice Question

The document contains multiple Java programs that check for various types of numbers including twin primes, USHWA numbers, PalPrime numbers, Dudeney numbers, Super Palindromes, Duck numbers, EvenPal numbers, superspy numbers, magic numbers, happy numbers, amicable numbers, Adam numbers, cool numbers, and automorphic numbers. Each program includes user input, logic for determining the number type, and output statements indicating the result. The examples provided illustrate how each program works with specific input values.

Uploaded by

asha.bluebirds
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Number program

Program 1
/*write a program in java to accept two numbers and check and display
* whether the numbers are twin prime or not .
* twin prime numbers are a pair of numbers whose difference is 2*/
import java.util.*;
public class twin_prime
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter first number");
int n1= sc.nextInt();
System.out.println("Enter Second number");
int n2= sc.nextInt();
int count1=0,count2=0;
for(int i=1;i<=n1;i++)
{
if(n1%i==0)
count1++;//counting factors of first number
}
for(int i=1;i<=n2;i++)
{
if(n2%i==0)
count2++;//counting factors of second number
}
if((count1==2 && count2==2) && (n1-n2)==-2)
System.out.println(n1+" "+n2+"are twin prime numbers");
else
System.out.println(n1+" "+n2+"are not twin prime numbers");
}
}
Program 2
/*Define a class to accept a four-digit number and check if it is a USHWA number or
not. The number is said to be USHWA
Accept a four digit number . If:
Sum of all digits= 2× ( sum of first and last)
Example: n =1234
Sum of first and last
= 1+4=5
Sum of all digits =1+2+3+4=10
Example 2
If the input value is 354, then a error message should be given as the number has only
3 digits. */
import java.util.*;
class ushwa
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.println("enter n");
int n=sc.nextInt();

if(n<1000||n>9999)
{
System.out. println("Invalid");
System.exit(0);
}
int first=n%10,rem=0,sum=0,n1=n;

while(n>0)
{
rem=n%10;
sum=sum+rem;
n=n/10;
}
int last=rem;
if(sum==2*(first+last))
System.out.println(n1+"Ushwa number");
else
System.out.println(n1+"not an ushwa number");
}
}
Program 3
/*PalPrime Number : If a number is simultaneously
palindromic and prime then it is said to be a
PalPrime (Palindromic Prime) Number.
Example : 7, 131, 313, etc*/
import java.util.*;
public class palprime
{
public static void main(String args[])
{
Scanner s=new Scanner(System.in);
System.out.println("Enter the number");
int n=s.nextInt();
int count=0,rem=0,rev=0,n1=n;

for(int i=1;i<=n;i++)
{
if(n%i==0)
{
count++;
}
}

while(n>0)
{
rem=n%10;
rev=rev*10+rem;
n=n/10;
}
if(count==2 && n1==rev)
System.out.println(n1+"is a palprime number");
else
System.out.println(n1+"is a palprime number");
}
}
Program 4
/*A Dudeney number is a positive integer that is a perfect cube such that the sum of its digits is equal to the cube
root of the number. Write a program to input a number and check and print whether it is a Dudeney number or not.
Example: Consider the number 512.*/

import java.util.*;
public class Dudeney
{
public static void main(String args[])
{
Scanner s =new Scanner(System.in);
System.out.println("Enter the number");
int n=s.nextInt();

int sum=0,rem=0,n1=n;
while(n>0)//512
{
rem=n%10;
sum=sum+rem;///8
n=n/10;
}
double cube_root=Math.cbrt(n1);//8
if(sum==cube_root)
System.out.println(n1+"is a Dudeney number");
else
System.out.println(n1+"is a Dudeney number");
}
}

Program 5
/*Super Palindrome Number: A Super Palindrome Number is a Palindrome Number whose square is also Palindrome.

Example:
Input : 11
Square : 11 X 11 = 121
Output : It is a Super Palindrome Number

Input : 22
Square : 22 X 22 = 484
Output : It is a Super Palindrome Number

Input : 44
Square : 44 X 44 = 1936

Output : It is not a Super Palindrome Number*/


import java.util.*;
public class super_palindrome
{
public static void main(String args[])
{
Scanner s=new Scanner(System.in);
System.out.println("Enter the number");
int n=s.nextInt();
int sq=n*n;
int rem=0,rev1=0,rev2=0,n1=n,sq1=sq;

while(n>0)
{
rem=n%10;
rev1=rev1*10+rem;
n=n/10;
}
while(sq>0)
{
rem=sq%10;
rev2=rev2*10+rem;
sq=sq/10;
}
if(n1==rev1 && sq1==rev2)
System.out.println(n1+"is a super palindrome number");
else
System.out.println(n1+"is not a super palindrome number");

}
}

Question 6
Define a class to accept a 3 digit number and check whether it is a duck number or not. Note: A number
is a duck number if it has zero in it [15]
Example1:
Input: 2083
Output: Invalid Example 2:
Input: 103
Output: Duck number

import java.util.*;
class Duck_number
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.println("enter the number");
int n=sc.nextInt();

if(n<100||n>999)
{
System.out.println("Invalid input");
System.exit(0);
}

int rem=0,temp=0,n1=n;
while(n>0)
{
rem=n%10;
if(rem==0)
{
temp=1;
break;
}
n=n/10;
}
if(temp==1)
System.out.println(n1+" is a duck number");
else
System.out.println(n1+" is not a duck number");
}
}
Question 7
Define a class to accept a number from user and check if it is an EvenPal number or not. (The number is said to be
EvenPal number when number is palindrome number (a number is palindrome if it is equal to its reverse) and sum of its
digits is an even number.) Example: 121 – is a palindrome number
Sum of the digits – 1+2+1 = 4 which is an even number

import java.util.*;

public class EvenPal


{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);

System.out.print("Enter a number: ");


int n = sc.nextInt();
int rem=0, sum = 0,rev=0,n1=n;

while (n> 0)
{
rem = n%10;
rev = rev*10 +rem;
sum = sum+rem;
n= n/10;
}

if (n1 == rev && sum % 2 == 0)


System.out.println(n1 + " is an EvenPal number.");
else
System.out.println(n1 + " is not an EvenPal number.");

}
}
Question 8 :Define a class to accept a number and check whether it is a superspy number or not . A super spy number is a number
where sum of digit is equal to number of digit
Example:
N=1021
Sum of digit=1+0+2+1=4
Number of digit=4
Output: 1021 is a superspy number
import java.util.*;

public class superspy


{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);

System.out.print("Enter a number: ");


int n = sc.nextInt();
int sum = 0,rem=0, count = 0,n1=n;
while(n>0)
{
rem=n%10;
sum=sum+rem;
count++;
n=n/10;
}
if(sum == count)
System.out.println(n1 + " is an superspy number.");
else
System.out.println(n1 + " is not an superspy number.");

}
}
Question 9
/* A Magic number is a number or not. A number is said to be magic number if sum of the digits is calculated
* repeatedly till a single digit is obtained and if that single digit number is equal to 1.
Example:n=199
1+9+9=19
1+9=10
1+0=1

output: 199 is a Magic number*/


import java.util.*;
public class magic_number
{
public static void main (String args[])
{ Scanner s = new Scanner (System.in);
System.out.println("Enter the number ");
int n=s. nextInt();
int sum=0,rem=0,n1=n;
while(n>9)
{
sum=0;
while(n>0)
{
rem=n%10;
sum=sum+rem;
n=n/10;
}
n=sum;
}
if(n==1)
System.out.println(n1+" is a Magic number");
else
System.out.println(n1+" is not a Magic number");
}}
Question 10
A happy number is a number in which the eventual sum of the square of the digits of the number is equal to 1.
Example:
28 = 22 + 8 2 = 4 + 64 = 68 68= 6 2 + 82 = 36 + 64 = 100
100= 1 2 + 0 2 + 0 2= 1 + 0 + 0 = 1 Hence, 28 is a happy number.
Design a class happy to check if a given number is a happy number.
import java.util.*;
public class happy_number
{
public static void main (String args[])
{
Scanner s = new Scanner (System.in);
System.out.println("Enter the number ");
int n=s. nextInt();
int sum=n,rem=0,n1=n;
while(sum>9)
{
n=sum;
sum=0;
while(n>0)
{
rem=n%10;
sum=sum+(rem*rem);
n=n/10;
}
}

if(sum==1)
System.out.println(n1+"is a happy number");
else
System.out.println(n1+"is not a happy number");
}
}
Question 11
Write a program in Java to check whether two numbers are amicable or not.

Amicable Number: If two numbers are such that the sum of the perfect divisors of one number is equal to the other
number and the sum of the perfect divisors of the other number is equal to the first number, then the numbers are
called Amicable Numbers.
Example : 220 and 284.
import java.util.*;
public class amicable
{
public static void main (String args[])
{
Scanner s = new Scanner(System.in);
System.out.println("Enter the first number ");
int n1=s.nextInt();
System.out.println("Enter the first number ");
int n2=s.nextInt();
int rev=0,sum1=0,sum2=0,rem=0;
for (int i=1; i<n1; i++)
{
if(n1%i==0)
{
sum1=sum1+i;
}
}

for (int i=1; i<n2; i++)


{
if(n2%i==0)
{
sum2=sum2+i;
}
}
if (n1==sum2 && n2==sum1)
System.out.println(n1+","+n2+"are amicable number");
else
System.out.println(n1+","+n2+"are not amicable number");
}
}
Question 12
Adam Number
Given a number n, write a program to check whether given number is Adam Number or not.Adam number is a
number when reversed, the square of the number and the square of the reversed number should be numbers
which are reverse of each other. Adam numbers upto 1000 are: 0, 1, 2, 3, 11, 12, 13, 21, 22, 31, 101, 102, 103,
111, 112 , 113, 121, 122, 201, 202, 211, 212, 221, 301, 311.
Examples :
Input : 12
Output : Adam Number
Explanation:
Square of 12 = 144
Reverse of 12 = 21
Square of 21 = 441
Now Square of 12 and square of reverse of 12 are
reverse of each other. Therefore 12 is Adam number.
Write a Java program to check whether a Given Number is Adam no. Or not.
import java.util.*;
public class adam
{ public static void main(String args[])
{ Scanner s = new Scanner (System.in);
System.out.println("Enter the number ");
int n=s.nextInt();
int rev=0,rev1=0,rem=0;
int n1=n;
int sq=n*n;
while(n>0)
{
rem=n%10;
rev=rev*10+rem;
n=n/10;
}
int sq1=rev*rev;
while(sq>0)
{
rem=sq%10;
rev1=rev1*10+rem;
sq=sq/10;
}
if(sq1==rev1)
System.out.println(n1+"is an adam number");
else
System.out.println(n1+"is not an adam number");
}
}

Question 13
A cool number is a number which has a perfect square as well as a perfect cube
for example 64,
8*8 is its perfect square
and4*4*4 is its perfect cube
Write a Java program to check whether a Given Number is cool no. Or not.
import java.util.*;
public class cool
{
public static void main(String args[])
{
Scanner s = new Scanner (System.in);
System.out.println("Enter the number ");
int n=s.nextInt();
int temp1=0,temp2=0;
for(int i=1;i<=n;i++)
{
if(i*i==n)
{
temp1=1;
break;
}
}
for (int i=1; i<=n; i++)
{
if(i*i*i==n)
{
temp2=1;
break;
}
}
if (temp1==1 && temp2==1)
System.out.println(n+"is a cool number ");
else
System.out.println(n+"is not a cool number ");
}
}
Question 14
/*A number is called an automorphic number if and only
if the square of the given number ends with the same number itself.
For example, 25, 76 are automorphic numbers because their square is 625 and 5776,
respectively and the last two digits of the square represent the number itself.*/
import java.util.*;
public class Automorphic
{
public static void main(String args[])
{
Scanner s=new Scanner(System.in);
System.out.println("Enter the number");
int n=s.nextInt();//25;
int sq=n*n;//625;
int count=0,rem=0,n1=n;
while(n>0)
{
rem=n%10;
count++;//count number of digits//2
n=n/10;
}
int p=(int)Math.pow(10,count);//100
if(sq%p==n1)
System.out.println(n1+"is an automorphic number");
else
System.out.println(n1+"is not an automorphic number");
}
}

You might also like