Number Based Programs in Java
Number Based Programs in Java
Armstrong Number :
Armstrong Number is a positive number if it is equal to the sum of cubes of its digits is called Armstrong
number and if its sum is not equal to the number then its not a Armstrong number.
import java.util.Scanner;
Question 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;
}
}
Question 3:
Binary to Decimal Number
import java.util.Scanner;
public class BinaryToDecimal
{
while (binary>0)
{
int temp = binary % 10;
decimal += temp * Math.pow(2, n);
binary = binary / 10;
n++;
}
System.out.println("Decimal="+decimal);
}
}
Output:
Enter binary number=101010
Decimal=42
Question 4:
Buzz Number
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
Question 5:
2025
All parts for 2025:-
202 + 5 = 207 (not 45)
20 + 25 = 45
2 + 025 = 27 (not 45)
Above we can see one combination is equal to number so that 45 is Capricorn or Kaprekar number.
import java.util.Scanner;
int square = n * n;
int temp = square;
int contDigits = 0;
Output:
Enter a number=297
Capricorn/Kaprekar number
Question 1:
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;
}
}
Output:
Enter number=205
Duck Number
Question 2:
CoPrime Numbers
Two integers a and b are said to be relatively prime, mutually prime, or coprime if the only positive integer
that divides both of them is 1. Example: 13 and 15 are co prime.
import java.util.Scanner;
public class CoPrimeNumbers
{
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;
}
}
if (gcd == 1)
{
System.out.println("Co Prime Numbers");
}
else
{
System.out.println("Not Co Prime Numbers");
}
}
}
Output:
Enter a=13
Enter b=15
Co Prime Numbers
Question 3:
Circular Prime
A circular prime is a prime number with the property that the number generated at each intermediate step
when cyclically permuting its digits will be prime. For example, 1193 is a circular prime, since 1931, 9311
and 3119 all are also prime.
import java.util.Scanner;
if(flag)
{
System.out.println("Circular Prime");
}
else
{
System.out.println("Not Circular Prime");
}
}
static boolean prime(int n)
{
// TODO code application logic here
int i = 2;
boolean flag = true;
while (n > i)
{
if (n % 2 == 0)
{
flag = false;
break;
}
i++;
}
return flag;
}
}
Output:
Enter number=137
Question 4:
Factorial Program in Java
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;
Question 5:
Digit to Word Program in Java
import java.util.Scanner;
Output:
Enter number=1234
Digit=1234
Words=One Two Three Four
https://www.efaculty.in/number-based-programs-in-java/