The document discusses various number programs in Java including programs to check Armstrong, Automorphic, Buzz, Duck, Factorial, Prime and other types of numbers. Each program section includes the Java code to check for a particular number type and sample input/output.
The document discusses various number programs in Java including programs to check Armstrong, Automorphic, Buzz, Duck, Factorial, Prime and other types of numbers. Each program section includes the Java code to check for a particular number type and sample input/output.
Examples: 153 is Armstrong (1*1*1)+(5*5*5)+(3*3*3) = 153 import java.util.Scanner; public class ArmstrongNumber { public static void main(String[] args) { int n, cubeSum = 0, num, r; Scanner sc = new Scanner(System.in); System.out.print("Enter number="); n = sc.nextInt(); num = n; while (num > 0) { r = num % 10; cubeSum = cubeSum + (r * r * r); num = num / 10; } if (n == cubeSum) { System.out.println("Armstrong Number"); } else { System.out.println("Not Armstrong Number"); } } Output: Enter number=153 Armstrong Number 2. Automorphic number An Automorphic number is a number whose square “ends” in the same digits as the number itself. Examples: 5*5 = 25, 6*6 = 36, 25*25 = 625 import java.util.Scanner; public class AutomorphicNumber { public static void main(String[] args) { int n, sqrNum, temp,sqrNumRemainder,c = 0; Scanner sc = new Scanner(System.in); System.out.print("Enter number="); n = sc.nextInt(); temp=n; while (temp > 0) { temp=temp/10; c++; } sqrNum = n * n; sqrNumRemainder= sqrNum%(int)Math.pow(10, c); if(sqrNumRemainder==n) { System.out.println("Automorphic Number"); } else { System.out.println("Not Automorphic Number"); }
}} Output: Enter number=25 Automorphic Number 3. Buzz Number Program in Java
A number is said to be Buzz Number if it ends with 7 or is divisible by 7.
Example: 1007 is a Buzz Number. import java.util.Scanner; public class BuzzNumber { public static void main(String[] args) { int n; Scanner sc = new Scanner(System.in); System.out.print("Enter n="); n = sc.nextInt(); if (n % 10 == 7 || n % 7 == 0) { System.out.println("Buzz number"); } else { System.out.println("Not Buzz number"); } } } Output: Enter n=147 Buzz number 4. Duck Number A Duck number is a number which has zeroes present in it, but there should be no zero present in the beginning of the number. For example 3210 import java.util.Scanner; public class DuckNumber { public static void main(String[] args) { int r, n, num; boolean flag=false; Scanner sc = new Scanner(System.in); System.out.print("Enter number="); n = sc.nextInt(); num = n; while (num > 0) { r = num % 10; if(r==0) { flag=true; } num = num / 10; } if(flag) { System.out.println("Duck Number"); } else { System.out.println("Not Duck Number"); } } } Output: Enter number=205 Duck Number 5. Factorial of a number Factorial is the product of all positive integers less than or equal to n. Examples: 4! = 4 × 3 × 2 × 1 = 24 import java.util.Scanner; public class Factorial { public static void main(String[] args) { int n, fact = 1; Scanner sc = new Scanner(System.in); System.out.print("Enter number of series="); n = sc.nextInt(); for (int i = 1; i <= n; i++) { fact = fact * i; } System.out.println("Factorial=" + fact); } } Output: Enter number of series=5 Factorial=120 6. Factor of a number Factor a number or algebraic expression that divides another number or expression evenly—i.e., with no remainder. For example, 3 and 6 are factors of 12 because 12 ÷ 3 = 4 exactly and 12 ÷ 6 = 2 exactly. The other factors of 12 are 1, 2, 4, and 12. Factors of 12: 1, 2, 3, 4, 6, 8, 12. import java.util.Scanner; public class Factors { public static void main(String[] args) { int n; Scanner sc = new Scanner(System.in); System.out.print("Enter number="); n = sc.nextInt(); for (int i = 1; i <= n; i++) { if (n % i == 0) { System.out.println(i); } } } } Output: Enter number=6 1 2 3 6 7.Fibonacci Series Program in Java A series of numbers in which each number ( Fibonacci number ) is the sum of the two preceding numbers. The simplest is the series 0, 1, 1, 2, 3, 5, 8, etc. import java.util.Scanner; public class FibonacciSeries { public static void main(String[] args) { int n; Scanner sc = new Scanner(System.in); System.out.print("Enter number of series="); n = sc.nextInt(); int a = 0, b = 1, c; for (int i = 1; i <= n; i++) { System.out.println(a); c = a + b; a = b; b = c; } } } Output: Enter number of series=10 0 1 1 2 3 5 8 13 21 34 8. Greatest Common Divisor Program in Java the greatest common divisor (gcd) of two or more integers, which are not all zero, is the largest positive integer that divides each of the integers. For example, the gcd of 8 and 12 is 4. import java.util.Scanner; public class GCDProgram { public static void main(String[] args) { int a, b, gcd = 1; Scanner sc = new Scanner(System.in); System.out.print("Enter a="); a = sc.nextInt(); System.out.print("Enter b="); b = sc.nextInt(); int min, max; min = a; if (min > b) { min = b; max = a; } else { min = a; max = b; } while (max > min) { int r = max % min; if (r == 0) { gcd = min; break; } else { max = min; min = r; } } System.out.println("GCD=" + gcd); } } Output: Enter a=15 Enter b=18 GCD=3 9. Happy Number Program in Java A happy number is a natural number in a given number base that eventually reaches 1 when iterated over the perfect digital invariant function for. Those numbers that do not end in 1 are -unhappy numbers. import java.util.Scanner; public class HappyNumber { public static void main(String[] args) { int n, r = 1, num, sum = 0; Scanner sc = new Scanner(System.in); System.out.print("Enter number="); n = sc.nextInt(); num = n; while (num > 9) { while (num > 0) { r = num % 10; sum = sum + (r * r); num = num / 10; } num = sum; sum = 0; } if (num == 1) { System.out.println("Happy Number"); } else { System.out.println("Not Happy Number"); } } } Output: Enter number=31 Happy Number 10. Harshad Number (or Niven number)Program in Java In mathematics, a harshad number (or Niven number) in a given number base is an integer that is divisible by the sum of its digits when written in that base. import java.util.Scanner; public class HarshadNumber { public static void main(String[] args) { int r, n, num, sum = 0; Scanner sc = new Scanner(System.in); System.out.print("Enter number="); n = sc.nextInt(); num = n; while (num > 0) { r = num % 10; sum = sum + r; num = num / 10; } if (n % sum == 0) { System.out.println("Harshad Number"); } else { System.out.println("Not Harshad Number"); } } } Output: Enter number=6804 Harshad Number 11. Least Common Multiple Program in Java The least common multiple, lowest common multiple, or smallest common multiple of two integers a and b, usually denoted by LCM(a, b), is the smallest positive integer that is divisible by both a and b. import java.util.Scanner; public class LCMProgram { public static void main(String[] args) { int a, b, gcd = 1, lcm = 1; Scanner sc = new Scanner(System.in); System.out.print("Enter a="); a = sc.nextInt(); System.out.print("Enter b="); b = sc.nextInt(); int min, max; min = a; if (min > b) { min = b; max = a; } else { min = a; max = b; } while (max > min) { int r = max % min; if (r == 0) { gcd = min; break; } else { max = min; min = r; } } lcm = (a * b) / gcd; System.out.println("LCM:" + lcm); } } Output: Enter a=15 Enter b=18 LCM:90 12.Multiply Of Digit Program in Java If a number=1234, then 1*2*3*4 ,Multiply of digit=24 import java.util.Scanner; public class MultiplyOfDigit { public static void main(String[] args) { int r, n, num, mul = 1; Scanner sc = new Scanner(System.in); System.out.print("Enter number="); n = sc.nextInt(); num = n; while (num > 0) { r = num % 10; mul = mul * r; num = num / 10; } System.out.println("Multiply of digit=" + mul); } } Output: Enter number=1234 Multiply of digit=24 13. Neon Number Program in Java A neon number is a number where the sum of digits of square of the number is equal to the number. For example if the input number is 9, its square is 9*9 = 81 and sum of the digits is 9. i.e. 9 is a neon number. import java.util.Scanner; public class NeonNumber { public static void main(String[] args) { int n, sqr = 1, sum = 0, r; Scanner sc = new Scanner(System.in); System.out.print("Enter number="); n = sc.nextInt(); sqr = n * n; while (sqr > 0) { r = sqr % 10; sum = sum + r; sqr = sqr / 10; } if (n == sum) { System.out.println("Neon Number"); } else { System.out.println("Not Neon Number"); } } } Output: Enter number=9 Neon Number 14. Palindrome Number Program in Java A palindromic number is a number that remains the same when its digits are reversed. Like 16461, for example, import java.util.Scanner; public class PalindromeNumber { public static void main(String[] args) { int n, num, r, rev = 0; Scanner sc = new Scanner(System.in); System.out.print("Enter number="); n = sc.nextInt(); num = n; while (num > 0) { r = num % 10; rev = (rev * 10) + r; num = num / 10; } if (n == rev) { System.out.println("Palindrome Number"); } else { System.out.println("Not Palindrome Number"); } } } Output: Enter number=12321 Palindrome Number 15.Perfect Number Program in Java A perfect number is a positive integer that is equal to the sum of its positive divisors, excluding the number itself. For instance, 6 has divisors 1, 2 and 3, and 1 + 2 + 3 = 6, so 6 is a perfect number. import java.util.Scanner; public class PerfectNumber { public static void main(String[] args) { int n, mul = 1, sum = 0; Scanner sc = new Scanner(System.in); System.out.print("Enter number="); n = sc.nextInt(); for (int i = 1; i < n; i++) { if (n % i == 0) { sum = sum + i; mul = mul * i; } } if (mul == sum) { System.out.println("Perfect Number"); } else { System.out.println("Not Perfect Number"); } } } Output: Enter number=6 Perfect Number 16. Prime Number Program in Java A prime number is a natural number greater than 1 that cannot be formed by multiplying two smaller natural numbers. A natural number greater than 1 that is not prime is called a composite number. For example, 5 is prime because the only ways of writing it as a product, 1 × 5 or 5 × 1, involve 5 itself. import java.util.Scanner; public class PrimeNumber { public static void main(String[] args) { int n, i = 2; boolean flag = true; Scanner sc = new Scanner(System.in); System.out.print("Enter number="); n = sc.nextInt(); while (n > i) { if (n % 2 == 0) { flag = false; break; } i++; } if (flag) { System.out.println("Number is prime."); } else { System.out.println("Number is not prime."); } } } Output: Enter number=17 Number is prime. 17.Reverse Number Program in Java If a number=1234, then reverse of number is 4321. import java.util.Scanner; public class ReverceNumber { public static void main(String[] args) { int n, num, r, rev = 0; Scanner sc = new Scanner(System.in); System.out.print("Enter number="); n = sc.nextInt(); num = n; while (num > 0) { r = num % 10; rev = (rev * 10) + r; num = num / 10; } System.out.println("Reverce of Number=" + rev); } } Output: Enter number=1234 Reverce of Number=4321 18.Special Number Program in Java A number is said to be special number when the sum of factorial of its digits is equal to the number itself. Example- 145 is a Special Number as 1!+4!+5!=145. import java.util.Scanner; public class SpecialNumber { public static void main(String[] args) { int n, num, r, sumOfFactorial = 0; Scanner sc = new Scanner(System.in); System.out.print("Enter number="); n = sc.nextInt(); num = n; while (num > 0) { r = num % 10; int fact=1; for(int i=1;i<=r;i++) { fact=fact*i; } sumOfFactorial = sumOfFactorial+fact; num = num / 10; } if(n==sumOfFactorial) { System.out.println("Special Number" ); } else { System.out.println("Not Special Number" ); } } } Output: Enter number=124 Not Special Number 19.Spy Number Program in Java A spy number is a number where the sum of its digits equals the product of its digits. For example, 1124 is a spy number, the sum of its digits is 1+1+2+4=8 and the product of its digits is 1*1*2*4=8. import java.util.Scanner; public class SpyNumber { public static void main(String[] args) { int r, n, num, mul = 1, sum = 0; Scanner sc = new Scanner(System.in); System.out.print("Enter number="); n = sc.nextInt(); num = n; while (num > 0) { r = num % 10; sum = sum + r; mul = mul * r; num = num / 10; } if (mul == sum) { System.out.println("Spy Number"); } else { System.out.println("Not Spy Number"); } } } Output: Enter number=123 Spy Number 20.Unique Number Program in Java A number is said to be unique , if the digits in it are not repeated. for example, 12345 is a unique number. 123445 is not a unique number. import java.util.Scanner; public class UniqueNumber { public static void main(String[] args) { int r1, r2, n, num1, num2, c = 0; Scanner sc = new Scanner(System.in); System.out.print("Enter number="); n = sc.nextInt(); num1 = n; num2 = n; while (num1 > 0) { r1 = num1 % 10; while (num2 > 0) { r2 = num2 % 10; if (r1 == r2) { c++; } num2 = num2 / 10; } num1 = num1 / 10; } if (c == 1) { System.out.println("Unique Number"); } else { System.out.println("Not Unique Number"); } } } Output: Enter number=23456 Unique Number 21. Disarium Number Program in Java A number is called Disarium number if the sum of its power of the positions from left to right is equal to the number. Example: 11 + 32 + 53 = 1 + 9 + 125 = 135 import java.util.Scanner; public class DisariumNumber { public static void main(String[] args) { int r, n, num,digits=0, sum = 0; Scanner sc = new Scanner(System.in); System.out.print("Enter number="); n = sc.nextInt(); num = n; while (num > 0) { digits++; num = num / 10; } num = n; while (num > 0) { r = num % 10; sum = sum + (int)Math.pow(r, digits); num = num / 10; digits--; } if(n==sum) { System.out.println("Disarium Number"); } else { System.out.println("Not Disarium Number"); } } } Output: Enter number=135 Disarium Number 22.Tech Number Program in Java A tech number can be tech number if its digits are even and the number of digits split into two number from middle then add these number if the added number’s square would be the same with the number it will called a Tech Number. If the number is split in two equal halves,then the square of sum of these halves is equal to the number itself. Write a program to generate and print all four digits tech numbers. Note: If number of digits is not even then it is not a tech number. Example: 2025 => 20+25 => 552 => 2025 import java.util.Scanner; public class TechNumber { public static void main(String[] args) { int n, num, leftNumber, rightNumber, digits = 0, sumSquare = 0; Scanner sc = new Scanner(System.in); System.out.print("Enter number="); n = sc.nextInt(); num = n; while (num > 0) { digits++; num = num / 10; } if (digits % 2 == 0) { num = n; leftNumber = num % (int) Math.pow(10, digits / 2); num = num / (int) Math.pow(10, digits / 2); rightNumber = num; sumSquare = (leftNumber + rightNumber) * (leftNumber + rightNumber); if (n == sumSquare) { System.out.println("Tech Number"); } else { System.out.println("Not Tech Number"); } } else { System.out.println("Not Tech Number"); } } } Output: Enter number=2025 Tech Number 23.Prime Number Up to N Terms Program in Java import java.util.Scanner; public class PrimeNumberUptoN { public static void main(String[] args) { int size,c=0; Scanner sc = new Scanner(System.in); System.out.print("Enter size of prime="); size = sc.nextInt(); int n=2; while(c<=size) { boolean flag = true; for(int i=2;i < n;i++) { if (n % i == 0) { flag = false; break; } } if (flag) { System.out.println("Number is prime="+n); c++; } n++; } } } Output: Enter size of prime=5 Number is prime=2 Number is prime=3 Number is prime=5 Number is prime=7 Number is prime=11 Number is prime=13 24.Magic Number Program in Java Magic number is the if the sum of its digits recursively are calculated till a single digit If the single digit is 1 then the number is a magic number. Magic number is very similar with Happy Number. Example- 226 is said to be a magic number 2+2+6=10 sum of digits is 10 then again 1+0=1 now we get a single digit number is 1.if we single digit number will now 1 them it would not a magic number. import java.util.Scanner; public class MagicNumber { public static void main(String[] args) { int n, r = 1, num, sum = 0; Scanner sc = new Scanner(System.in); System.out.print("Enter number="); n = sc.nextInt(); num = n; while (num > 9) { while (num > 0) { r = num % 10; sum = sum + r; num = num / 10; } num = sum; sum = 0; } if (num == 1) { System.out.println("Magic Number"); } else { System.out.println("Not Magic Number"); } } } Output: Enter number=226 Magic Number 25.Pronic Number Program in Java A number is said to be a pronic number if product of two consecutive integers is equal to the number, is called a pronic number. Example- 42 is said to be a pronic number 42=6×7, here 6 and 7 are consecutive integers import java.util.Scanner; public class PronicNumber { public static void main(String[] args) { int n; boolean flag=false; Scanner sc = new Scanner(System.in); System.out.print("Enter number="); n = sc.nextInt(); for(int i=0; i < n; i++) { if(i*(i+1) == n) { flag =true; break; } } if(flag) { System.out.println("Pronic Number"); } else { System.out.println("Not Pronic Number"); } } } Output: Enter number=42 Pronic Number 26. Ugly Number Program in Java A number is said to be an Ugly number if positive numbers whose prime factors only include 2, 3, 5. For example, 6(2×3), 8(2x2x2), 15(3×5) are ugly numbers while 14(2×7) is not ugly since it includes another prime factor 7. Note that 1 is typically treated as an ugly number. import java.util.Scanner; public class UglyNumber { public static void main(String[] args) { int n; boolean flag=true; Scanner sc = new Scanner(System.in); System.out.print("Enter number="); n = sc.nextInt(); while (n != 1) { if (n % 5 == 0) { n /= 5; } else if (n % 3 == 0) { n /= 3; } else if (n % 2 == 0) { n /= 2; } else { flag=false; break; } } if (flag) { System.out.println("Ugly number."); } else { System.out.println("Not Ugly number."); } } } Output: Enter number=15 Ugly Number