NUMBER BASED PROGRAMS
CLASS IX AND X
1 /*
Write a program to input a number and print its factors
Eg
Input 6
Output:
1
2
3
6
*/
import java.util.*;
class Factors
{
public static void main()
{
Scanner sc=new Scanner (System.in);
System.out.println("Enter any number");
int a=sc.nextInt();
for(int i=1;i<=a;i++)
{
if(a%i==0)
System.out.println(i);
}
}
}
2 /*
Write a program to input a number and print sum of digits
Eg
Input 659
Output:
20
*/
import java.util.*;
class Sum_of_digits
{
public static void main()
{
Scanner sc=new Scanner (System.in);
System.out.println("Enter any number");
int a=sc.nextInt();
int s=0,d;
for(int t=a;t>0;t=t/10)
{
d=t%10;
s=s+d;
}
System.out.println(s);
}
}
3 /*
Write a program to input a number and print its factorial.
A factorial of a number is the product of all natural numbers till that
numbers.
Eg 6!=1*2*3*4*5*6=720
Input 6
Output:
720
*/
import java.util.*;
class Factorial
{
public static void main()
{
Scanner sc=new Scanner (System.in);
System.out.println("Enter any number");
int a=sc.nextInt();
int f=1;
for(int i=1;i<=a;i++)
f=f*i;
System.out.println(f);
}
}
4 /*
Write a program to input a number and count and print number of its
digits.
Input : 48523
Output
5
*/
import java.util.*;
class Count
{
public static void main()
{
Scanner sc=new Scanner (System.in);
System.out.println("Enter any number");
int a=sc.nextInt();
int c=0;
for(int t=a;t>0;c++,t=t/10);
System.out.println(c);
}
}
5 /*
Write a program to input a number and check whether it is a spy
number or not.
A number in which sum of digits is equal to product of its digits is called
spy number
Eg 132
Sum of digit=1+3+2=6
Product of digits=1*3*2=6
So, 132 is a spy number
*/
import java.util.*;
class Spy
{
public static void main()
{
Scanner sc=new Scanner (System.in);
System.out.println("Enter any number");
int a=sc.nextInt();
int s=0,p=1,d;
for(int t=a;t>0;t=t/10)
{
d=t%10;
s=s+d;
p=p*d;
}
if(s==p)
System.out.println(a+"Is a Spy number");
else
System.out.println(a+"Is not a Spy number");
}
}
6 /*
Write a program to input a number and check whether it is a perfect
number or not.
A number which is equal to sum of its factors excluding itself is called
Perfect number.
Eg 6
Sum of factors=1+2+3=6
So, 6 is a perfect number
*/
import java.util.*;
class Perfect
{
public static void main()
{
Scanner sc=new Scanner (System.in);
System.out.println("Enter any number");
int a=sc.nextInt();
int s=0;
for(int i=1;i<a;i++)
{
if(a%i==0)
s=s+i;
}
if(s==a)
System.out.println(a+"Is a Perfect number");
else
System.out.println(a+"Is not a Perfect number");
}
}
7 /*
Write a program to input a number and check whether it is a pronic
number or not.
A number which is equal to product of any two consecutive numbers is
called pronic number
Eg 6=2*3,12=3*4
*/
import java.util.*;
class Pronic
{
public static void main()
{
Scanner sc=new Scanner (System.in);
System.out.println("Enter any number");
int a=sc.nextInt();
int f=0;
for(int i=1;i<a;i++)
{
if(i*(i+1)==a)
{
f=1;
break;
}
}
if(f==1)
System.out.println(a+" Is a Pronic number");
else
System.out.println(a+" Is not a Pronic number");
}
}
8 /*
Write a program to input a number and print its reverse
Eg
Input :52885
Output
58825
*/
import java.util.*;
class Reverse
{
public static void main()
{
Scanner sc=new Scanner (System.in);
System.out.println("Enter any number");
int a=sc.nextInt();
int rev=0,d;
for(int t=a;t>0;t=t/10)
{
d=t%10;
rev=rev*10+d;
}
System.out.println(rev);
}
}
9 /*
Write a program to input a number and check whether it is a
palindrome number or not.
A number which is equal to its reverse is called Palindrome number.
Eg 121,3663
*/
import java.util.*;
class Palindrome
{
public static void main()
{
Scanner sc=new Scanner (System.in);
System.out.println("Enter any number");
int a=sc.nextInt();
int rev=0,d;
for(int t=a;t>0;t=t/10)
{
d=t%10;
rev=rev*10+d;
}
if(rev==a)
System.out.println(a+" is Palindrome");
else
System.out.println(a+" is not Palindrome");
}
}
10 /*
Write a program to input a number and check whether it is a armstrong
number or not.
A number which is equal to the sum of its digits raised to the power
equal to number of its digits is called Armstrong number
Eg 153=1^3+5^3+3^3=153
*/
import java.util.*;
class Armstrong
{
public static void main()
{
Scanner sc=new Scanner (System.in);
System.out.println("Enter any number");
int a=sc.nextInt();
int c=0;
for(int t=a;t>0;c++,t=t/10);
int s=0,d;
for(int t=a;t>0;t=t/10)
{
d=t%10;
s=s+(int)Math.pow(d,c);
}
if(s==a)
System.out.println(a+" is Armstrong number");
else
System.out.println(a+" is not Armstrong number");
}
}
11 /*
Write a program to input a number and check whether it is a
automorphic number or not.
A number which is present at the last of its square is called Automorphic
number
Eg 6 is present in 6^2=36, 25,625
*/
import java.util.*;
class Automorphic
{
public static void main()
{
Scanner sc=new Scanner (System.in);
System.out.println("Enter any number");
int a=sc.nextInt();
int c=0;
for(int t=a;t>0;c++,t=t/10);
int s=0,sq=a*a;
s=sq%(int)Math.pow(10,c);
if(s==a)
System.out.println(a+" is Automorphic number");
else
System.out.println(a+" is not Automorphic number");
}
}
12 /*
Write a program to input a number and check whether it is a magic
number or not.
If the eventual sum of digits of a number is 1 then that number is called
magic number.
Eg
91=9+1=10
10=1+0=1
So 91 is magic number
*/
import java.util.*;
class Magic
{
public static void main()
{
Scanner sc=new Scanner (System.in);
System.out.println("Enter any number");
int a=sc.nextInt();
int s=0,d;
while(a>9)
{
for(int t=a;t>0;t=t/10)
{
d=t%10;
s=s+d;
}
a=s;
s=0;
}
if(a==1)
System.out.println("It is Magic number");
else
System.out.println("It is not Magic number");
}
}
13 /*
Write a program to input a number and check whether it is a Happy
number or not.
If the eventual sum of square of digits of a number is 1 then that number
is called magic number.
Eg
28=4+64=68
68=36+64=100
100=1+0+0=1
So 28 is Happy number
*/
import java.util.*;
class Happy
{
public static void main()
{
Scanner sc=new Scanner (System.in);
System.out.println("Enter any number");
int a=sc.nextInt();
int s=0,d;
while(a>9)
{
for(int t=a;t>0;t=t/10)
{
d=t%10;
s=s+(d*d);
}
a=s;
s=0;
}
if(a==1)
System.out.println("It is Happy number");
else
System.out.println("It is not Happy number");
}
}
14 /*
Write a program to input a number and check whether it is a special
/Petersen/Krishnamurthy number or not.
A number which is equal to sum of factorial of its digits is called special
number
Eg 145=1!+4!+5!=1+24+120
*/
import java.util.*;
class Special
{
public static void main()
{
Scanner sc=new Scanner (System.in);
System.out.println("Enter any number");
int a=sc.nextInt();
int s=0,d;
for(int t=a;t>0;t=t/10)
{
d=t%10;
int f=1;
for(int i=1;i<=d;i++)
f=f*i;
s=s+f;
}
if(s==a)
System.out.println(a+" is a Special number");
else
System.out.println(a+" is not a Special number");
}
}
15 /*
Write a program to input a number and check whether it is a Niven
number or not.
A number which is divisible by its sum of digits is called Niven number.
Eg
126
Sum of digits=1+2+6=9
126 is divisble by 9 so 126 is Niven number.
*/
import java.util.*;
class Niven
{
public static void main()
{
Scanner sc=new Scanner (System.in);
System.out.println("Enter any number");
int a=sc.nextInt();
int s=0,d;
for(int t=a;t>0;t=t/10)
{
d=t%10;
s=s+d;
}
if(a%s==0)
System.out.println(a+ " is a Niven number");
else
System.out.println(a+ " is not a Niven number");
}
}
16 /*
Write a program to print all 4 digits tech numbers.
A tech number has even numbers of digits.If the number is split in two
equal halves, then the square of sum of these halves is equal to the
number itself.
Eg 3025
Square of (30+25)=55 square=3025
*/
class Tech
{
public static void main()
{
int s=0,f,d;
for(int i=1000;i<=9999;i++)
{
d=i%100;
f=i/100;
s=f+d;
if(s*s==i)
System.out.println(i);
}
}
}
17 /*
* Write a program to input a number and print its prime factors.
* Example:
* Input : 36
* Output :
*2
*2
*3
*3
*
*/
import java.util.*;
class PrimeFactors
{
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter any number");
int a=sc.nextInt();
for(int i=2;i<=a;i++)
{
while(a%i==0)
{
System.out.println(i);
a=a/i;
}
}
}
}
18 /*
* Write a program to input a number and check whether it is a unique
number or not.
* A number is said to be unique if all digits of the number is different i.e.
there is no repetition of any digit.
*
*/
import java.util.*;
class Unique
{
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter any number");
int a=sc.nextInt();
int c,d,f=0;
for(int i=0;i<=9;i++)
{
c=0;
for(int t=a;t>0;t=t/10)
{
d=t%10;
if(d==i)
c++;
}
if(c>1)
{
f=1;
break;
}
}
if(f==0)
System.out.println(a +" is a Unique number");
else
System.out.println(a +" is not a Unique number");
}
}
19 /*
* Write a program to input a number and check whether it is a bouncy
number or not.
A number is said to be a bouncy number if the digits of that number are
in random order that is not sorted. Example: 121575, 521634, etc.
In other words, a number that is neither an increasing number nor a
decreasing number is called a bouncy number.
Increasing number: The number whose current digit is greater than or
equal to the previous digit is called an increasing number. Example:
12345, 45678, etc.
Decreasing number: The number whose current digit is less than the
previous digit is called an decreasing number. Example: 54321, 98765,
etc.
*/
import java.util.*;
class Bouncy
{
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter any number ");
int n=sc.nextInt();
int m=n%10,ci=0,cd=0,c=0;
for(int t=n;t>0;t/=10,c++)
{
int d=t%10;
if(d>=m)
ci++;
if(d<=m)
cd++;
m=d;
}
if(ci==c || cd==c)
System.out.println("Not a Bouncy number");
else
System.out.println("Bouncy number");
}
}
20 /*Disarium number
A number is said to be the Disarium number when the sum of its digit
raised to the power of their respective positions is equal to the number
itself.
For example, 175 is a Disarium number
*/
/*
Write a program to input a number and check whether it is a armstrong
number or not.
A number which is equal to the sum of its digits raised to the power
equal to number of its digits is called Armstrong number
Eg 153=1^3+5^3+3^3=153
*/
import java.util.*;
class Disarium
{
public static void main()
{
Scanner sc=new Scanner (System.in);
System.out.println("Enter any number");
int a=sc.nextInt();
int c=0;
for(int t=a;t>0;c++,t=t/10);
int s=0,d;
for(int t=a;t>0;t=t/10)
{
d=t%10;
s=s+(int)Math.pow(d,c);
c--;
}
if(s==a)
System.out.println(a+" is a Disarium number");
else
System.out.println(a+" is not a Disarium number");
}
}
UNSOLVED QUESTIONS
1 Write a program to input a number and check whether it is a Neon
number or not.
Neon Number. A positive integer whose sum of digits of its square is
equal to the number itself is called a neon number. Example of Neon
Number.
2 A prime number is said to be 'Twisted Prime or Emirp number ', if the
new number obtained after reversing the digits is also a prime number.
Write a program to accept a number and check whether the number is
'Twisted Prime or Emirp number' or not.
Input : 167
Output : Twisted Prime, because reverse of 167 is 761, which is also
Prime
3 Balanced Number: A number is said to be balanced if the sum of digits in
the first half of it is equal to the sum of the digits in the second half.
Write a program to print all 6 digits Balanced numbers.
4 A Dudeney number is a positive integer that is a perfect cube such that
the sum of its decimal digits is equal to the cube root of the number.
Examples:
Input: N = 19683
Output: Yes
∛19683 = 27 and 1 + 9 + 6 + 8 + 3 = 27
Input: N = 75742
Output: No
Write a program to input a number and check whether it is a Dudeney
number or not.
5 Write a program to input a number and check whether it is a Mystery
number or not.
A mystery number is a number that can be expressed as the sum of two
numbers and those two numbers should be the reverse of each other.
Examples:
Input : n = 121
Since it can be expressed as sum of 19 and 91, So
Output :121 is a mystery number