Board Number Imp Practice Question
Board Number Imp Practice Question
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
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.*;
while (n> 0)
{
rem = n%10;
rev = rev*10 +rem;
sum = sum+rem;
n= n/10;
}
}
}
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.*;
}
}
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
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;
}
}
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");
}
}