How to solve any Number program in java
- Categories Java, Iterative Statements, For & While
Watch our video on how to solve any Number Program – Click Here
1. Find if number is Deficient Number or not.
Deficient Number: Sum of factors is less than the number itself.
e.g. 21. Factors: 1,3,7 = 11<21.
Code:
import java.util.Scanner; public class DeficientNum { public static void main(String[] args) { System.out.println("Enter the number:"); Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int sum = 0; for(int i = 1; i < n; i++) { if (n % i == 0) sum = sum + i; } if (sum < n) System.out.println("Yes"); else System.out.println("No"); } }
2. Find if number is a Composite number. its is a number which has more that one factor(excl. 1,n).
e.g. 8=2,4=2 factors.
Code:
import java.util.Scanner; public class CompositeNum { public static void main(String[] args) { System.out.println("Enter the number:"); Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int count = 0; for(int i = 1; i <= n; i++) { if (n % i == 0) count++; } if (count >3) System.out.println("Yes"); else System.out.println("No"); } }
3. Find if given number is a Abundant number or not.
Abundant number: sum of factor is greater then the number itself.
e.g. 12. Factors: 1, 2, 3, 4, 6 = 16 >12
Code:
public class AbundantNum
{
public static void main(String[] args)
{
System.out.println("Enter the number:");
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int sum = 0;
for(int i = 1; i < n; i++)
{
if (n % i == 0)
sum = sum + i;
}
if (sum > n)
System.out.println("Yes");
else
System.out.println("No");
}
}
4. Find if a given number is Pronic number or not.
Pronic Number: it is the product of two consecutive integers, n(n+1).
e.g 56= 7 x 8 .
Code:
import java.util.Scanner;
public class PronicNum
{
public static void main(String[] args)
{
System.out.println("Enter the number:");
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int fact = 0;
for(int i = 1; i < n; i++)
{
if (n % i == 0)
if (i * (i + 1) == n)
fact = i;
}
if (fact != 0)
System.out.println("Yes");
else
System.out.println("No");
}
5. Accept a number from user and print if given number is Automorphic number or not.
Automorphic number: A Automorphic number is a number which is contained in the last digit(s) of its square.
For example: 25 in 625
import java.util.Scanner; public class AutomorphicNumber { public static void main(){ Scanner sc = new Scanner(System.in); int n; System.out.println("Enter any number:"); n= sc.nextInt(); int m = n; int flag = 0, q = n * n; while (n != 0) { int d = n%10; int d1 = q%10; if (d != d1) flag = 1; n = n / 10; q = q / 10; } if ( flag == 0) System.out.println ("yes"); else System.out.println ("no"); } }
6. Write Accept a number from user and print if given number is Krishnamurthy number or special number or not.
KN Special number: A special number is a number whose sum of factorial of digits is equal to the number
For example 145 = 1! + 4! + 5!
import java.util.Scanner; public class KNSpecialNumber { public static void main() { Scanner sc = new Scanner(System.in); int n; System.out.println("Enter any number:"); n= sc.nextInt(); int m = n; int sum = 0; while (n != 0) { int d = n%10; int fact = 1; for (int i= 1; i<= d; i++) fact = fact * i; sum = sum + fact; n = n / 10; } if ( sum == m) System.out.println ("yes"); else System.out.println ("no"); } }
7. Accept a number from user and print if given number is Neon number or not.
Neon number: Sum of digits of square of the number is equal to the number .
For example: 9: 9*9 = 81, 9= 8+1
import java.util.Scanner; public class NeonNumber { public static void main() { Scanner sc = new Scanner(System.in); int n; System.out.println("Enter any number:"); n= sc.nextInt(); int m = n; int sum = 0; n = n * n; while (n != 0) { int d = n%10; sum = sum + d; n = n / 10; } if (sum == m) System.out.println ("yes"); else System.out.println ("no"); } }
8. Accept a number from user and print if given number is duck number or not.
Duck number: A Duck number is a number which has zeroes present in it. e.g 402, 280.
Code:
import java.util.Scanner;
public class DuckNumber
{
public static void main(){
Scanner sc = new Scanner(System.in);
int n;
System.out.println("Enter any number:");
n= sc.nextInt();
int m = n;
int count = 0;
while (n != 0) {
int d = n%10;
if ( d == 0)
count++;
n = n / 10;
}
if ( count > 0)
System.out.println ("yes");
else
System.out.println ("no");
}
}
9. Accept a number from user and print if given number is spy number or not.
spy number: A spy number is a number which has sum its digits equals the product of the digits.
Example: 123. 1+2+3=1*2*3
Code:
import java.util.Scanner;
public class SpyNumber
{
public static void main(){
Scanner sc = new Scanner(System.in);
int n;
System.out.println("Enter any number:");
n= sc.nextInt();
int m = n;
int sum = 0, prod = 1;
while (n != 0) {
int d = n%10;
sum = sum + d;
prod = prod * d;
n = n / 10;
}
if (sum == prod)
System.out.println ("yes");
else
System.out.println ("no");
}
}
10. Accept a number from user and print if given number is special number or not.
Special number: A special number is a number whose Sum of digits plus product of digits it is equal to original number. e.g
For example 59 = 5 + 9 + (5×9)
=59.
Code:
import java.util.Scanner; public class SpecialNumber { public static void main() { Scanner sc = new Scanner(System.in); int n; System.out.println("Enter any number:"); n= sc.nextInt(); int m = n;
int sum = 0, prod = 1;
while (n != 0) {
int d = n%10;
sum = sum + d;
prod = prod * d;
n = n / 10;
}
if ( sum + prod == m)
System.out.println ("yes");
else
System.out.println ("no"); } }
11. Accept a number from user and print if given number is prime number or not?
Code:
import java.util.Scanner; public class primeNumber { public static void main(String[] args)
{
System.out.println("Enter the number:");
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int count=0;
for(int i=1; i<=n; i++)
{
if (n % i == 0)
count++;
}
if (count==2)
System.out.println("Yes");
else
System.out.println("No");
}
}
12. Accept a number from user and print if given number is Perfect number or not.
Perfect numbers : the sum of its positive divisors excluding the number itself is equal to that number.
For example, 28 is a perfect number because, 28 is divisible by 1, 2, 4, 7, 14 and 28 ,and the sum of these values is 1 + 2 + 4 + 7 + 14 = 28.
Code:
import java.util.Scanner; public class PerfectNumber { public static void main(String[] args) { int n, Sum = 0 ; Scanner sc = new Scanner(System.in); System.out.println("\n Please Enter any Number: "); n = sc.nextInt(); for(int i = 1; i < n; i++) { if (n % i == 0) sum = sum + i; } if (sum == n) System.out.println("Yes"); else System.out.println("No"); } }
13.Check whether a given number is palindrome number or not.
Palindrome means, If the reverse of the number is same of original number
Code:
import java.util.Scanner; public class palindrome { public static void main() { Scanner scan = new Scanner(System.in);
System.out.print("Enter any number: ");
int n = scan.nextInt();
int m = n;
int sum = 0;
while (n != 0) {
int d = n%10;
sum = sum * 10 + d;
n = n / 10;
}
if ( sum == m)
System.out.println ("yes");
else
System.out.println ("no");
}
}
14.Write a Program in Java to print GCD of two numbers.
The GCD (Greatest Common Divisor) of two numbers is the largest positive integer number that divides both the numbers without leaving any remainder.
For example. GCD of 30 and 45 is 15.
Code:
import java.util.Scanner; public class GCD { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int num1, num2, gcd = 1; System.out.println("Enter first number:"); num1= sc.nextInt(); System.out.println("Enter second number:"); num2= sc.nextInt(); for(int i = 1; i <= num1 && i <= num2; i++) { if(num1%i==0 && num2%i==0) gcd = i; } System.out.printf("GCD of "+num1+" and "+num2+" is= "+gcd); } }
15. Accept a number from user and print if given number is Harshad number or not.
A number is said to be the Harshad number if it is divisible by the sum of its digit.
For example, 156= 1 + 5 + 6 = 12.
Since 156 is divisible by 12. So, 156 is a Harshad number.
import java.util.Scanner; public class HarshadNumber { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("Enter any number:"); int n= sc.nextInt(); int m = n; int sum = 0; while (n != 0) { int d = n%10; sum = sum + d; n = n / 10; } if ( m%sum == 0) System.out.println ("yes"); else System.out.println ("no"); } }