100% found this document useful (5 votes)
8K views10 pages

Number Based Programs in Java

The document contains 5 questions about number-based programs in Java, including programs to check if a number is an Armstrong number, Automorphic number, Binary to Decimal conversion, Buzz number, and Capricorn/Kaprekar number. Each question provides the definition, an example, and the Java code to check for or convert that number type.

Uploaded by

SUSMIT SAHA
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
100% found this document useful (5 votes)
8K views10 pages

Number Based Programs in Java

The document contains 5 questions about number-based programs in Java, including programs to check if a number is an Armstrong number, Automorphic number, Binary to Decimal conversion, Buzz number, and Capricorn/Kaprekar number. Each question provides the definition, an example, and the Java code to check for or convert that number type.

Uploaded by

SUSMIT SAHA
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 10

Question 1 :

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.

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");
}
}
}

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;

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");
}

}
}

Question 3:
Binary to Decimal Number

import java.util.Scanner;
public class BinaryToDecimal
{

public static void main(String[] args)


{
int binary;
Scanner sc = new Scanner(System.in);
System.out.print("Enter binary number=");
binary = sc.nextInt();
int decimal = 0;
int n = 0;

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:

Capricorn or Kaprekar Number


A number is called Capricorn or Kaprekar number whose square is divided into two parts in any
conditions and parts are added, the additions of parts is equal to the number, is called Capricorn or
Kaprekar number.
Example: 45
45 = (45)2 = 2025

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;

public class CapricornNumber


{

public static void main(String[] args)


{
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a number=");
int n = scanner.nextInt();
boolean isCapricorn = false;

int square = n * n;
int temp = square;
int contDigits = 0;

while (temp > 0)


{
contDigits++;
temp /= 10;
}

for (int i = 1; i < contDigits; i++)


{
int divisor = (int) Math.pow(10, i);
int quotient = square / divisor;
int remainder = square % divisor;
if (quotient + remainder == n)
{
isCapricorn = true;
}
}
if (isCapricorn)
{
System.out.println("Capricorn/Kaprekar number");
} else
{
System.out.println("Not Capricorn/Kaprekar number");
}
}
}

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;

public class DuckNumber


{
public static void main(String[] args)
{
// TODO code application logic here
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

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;

public class CircularPrime


{
public static void main(String[] args)
{
boolean flag = true;
int n, num, r, c = 0;
Scanner sc = new Scanner(System.in);
System.out.print("Enter number=");
n = sc.nextInt();
num = n;
while (num > 0)
{
r = num % 10;
c++;
num = num / 10;
}
num = n;
for (int i = 1; i <= c; i++)
{
r = num % 10;
num = num / 10;
num = (r * (int) Math.pow(10, c - 1)) + num;
if (!prime(num))
{
flag = false;
break;
}
}

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;

public class Factorial


{
public static void main(String[] args)
{
// TODO code application logic here
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

Question 5:
Digit to Word Program in Java

import java.util.Scanner;

public class DigitToWord


{
public static void main(String[] args)
{
int r, n, num;
String digitWords = "";
Scanner sc = new Scanner(System.in);
System.out.print("Enter number=");
n = sc.nextInt();
num = n;
while (num > 0)
{
r = num % 10;
switch (r)
{
case 0:
digitWords = "Zero " + digitWords;
break;
case 1:
digitWords = "One " + digitWords;
break;
case 2:
digitWords = "Two " + digitWords;
break;
case 3:
digitWords = "Three " + digitWords;
break;
case 4:
digitWords = "Four " + digitWords;
break;
case 5:
digitWords = "Five " + digitWords;
break;
case 6:
digitWords = "Six " + digitWords;
break;
case 7:
digitWords = "Seven " + digitWords;
break;
case 8:
digitWords = "Eight " + digitWords;
break;
case 9:
digitWords = "Nine " + digitWords;
break;
}
num = num / 10;
}
System.out.println("Digit=" + n);
System.out.println("Words=" + digitWords);
}
}

Output:
Enter number=1234
Digit=1234
Words=One Two Three Four

https://www.efaculty.in/number-based-programs-in-java/

You might also like