0% found this document useful (0 votes)
145 views21 pages

Different Types of Numbers

Uploaded by

tiasha.ten
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
145 views21 pages

Different Types of Numbers

Uploaded by

tiasha.ten
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 21

DIFFERENT TYPES OF NUMBERS

Armstrong Number Program in Java


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.
Armstrong Number Program is very popular in java, c language, python etc. Examples: 153 is
Armstrong, (1*1*1)+(5*5*5)+(3*3*3) = 153

public static boolean isArmstrong(int num)


{
int temp = num,n=0;
int result = 0;
while(temp>0)
{
n++;
temp=temp/10;
}
temp = num;

while (temp > 0)


{
int digit = temp % 10;
result = result + Math.pow(digit, n);
temp /= 10; // shorthand operator
}

if(result==num)
return true;
else
return false;
}

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.
2025 = 20 + 25 = 45 ^ 2 = 2025
If four digits
public static void main(String[] args)
{
// TODO code application logic here
int n, num, leftNumber, rightNumber, digits = 0,
sumSquare = 0;
Scanner sc = new Scanner(System.in);
System.out.print("Enter number=");
num = sc.nextInt(); // 2025
DIFFERENT TYPES OF NUMBERS

leftNumber = num % 100; // 2025 %100 = 25


rightNumber = num / 100; // 2025/100 = 20

sumSquare = (leftNumber + rightNumber) * (leftNumber + rightNumber);


//int s=leftNumber+rightNumber;
// sumSquare=(int)Math.pow(s,2);

if (num == sumSquare)
{
System.out.println("Tech Number");
}
else
{
System.out.println("Not Tech Number");
}
}

//If generalized program


public static void main(String[] args)
{
// TODO code application logic here
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); //123456%1000 = 456
rightNumber = num / (int) Math.pow(10, digits / 2); //123456/1000 = 123

sumSquare = (leftNumber + rightNumber) * (leftNumber + rightNumber);


if (n == sumSquare)
{
System.out.println("Tech Number");
}
else
{
System.out.println("Not Tech Number");
}
DIFFERENT TYPES OF NUMBERS

}
else
{
System.out.println("Not Tech Number");
}
}

Automorphic Number Program in Java


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; // 25 x 25 = 625
sqrNumRemainder= sqrNum%(int)Math.pow(10, c); // 625%100 = 25
if(sqrNumRemainder==n) // 25 == 25
{
System.out.println("Automorphic Number");
}
else
{
System.out.println("Not Automorphic Number");
}

}
}

Tritomorphic Number Program in Java


An Trimorphic number is a number whose cube “ends” in the same digits as the number itself.
Examples: 5*5*5 = 125, 6*6*6 = 216, 25*25*25 = 15625

import java.util.Scanner;
DIFFERENT TYPES OF NUMBERS

public class TrimorphicNumber


{
public static void main()
{
int n, cuNum, temp, cuNum Remainder,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++;
}
cuNum = n * n*n;
cuNumRemainder= cuNum %(int)Math.pow(10, c); // 125%10 = 5
if(cuNumRemainder==n) // 5==5
{
System.out.println("Trimorphic Number");
}
else
{
System.out.println("Not Trimorphic Number");
}

}
}

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.

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
{
DIFFERENT TYPES OF NUMBERS

System.out.println("Not Buzz number");


}
}
}

CoPrime Numbers Program in Java


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.*;
class CoPrimeNumbers
{
public static void main(String[] args)
{
int a, b,i, c1=0,c2=0;
Scanner sc = new Scanner(System.in);
System.out.print("Enter a=");
a = sc.nextInt();
System.out.print("Enter b=");
b = sc.nextInt();
for(i= 1;i<=a;i++)
{
if(a%i==0)
{
c1++;
}
}
for(i= 1;i<=b;i++)
{
if(b%i==0)
{
c2++;
}
}
if (c1==2 && c2==2)
{
System.out.println("Co Prime Numbers");
}
else
{
System.out.println("Not Co Prime Numbers");
}
}
}

Duck Number Program in Java


DIFFERENT TYPES OF NUMBERS

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

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; break’
}
num = num / 10;
}
if(flag==true)
{
System.out.println("Duck Number");
}
else
{
System.out.println("Not Duck Number");
}

Factorial Program in Java ( 5 = 1x2x3x4x5 = 120)


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

Factors Program in Java


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.

System.out.print("Enter number=");
DIFFERENT TYPES OF NUMBERS

n = sc.nextInt();
for (int i = 1; i <= n; i++)
{
if (n % i == 0)
{
System.out.println(i);
}
}

(Not Now)
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.

Scanner sc = new Scanner(System.in);


System.out.print("Enter number of series=");
n = sc.nextInt();
int a = 0,
int b = 1, c;
for (int i = 1; i <= n; i++)
{
System.out.println(a);
c = a + b;
a = b;
b = c;
}
}

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.

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;
}
// min = (a<b)?a:b;
// max=(a>b)?a:b;
DIFFERENT TYPES OF NUMBERS

while (max > min)


{
int r = max % min;
if (r == 0)
{
gcd = min; break;
}
else
{
max = min; min = r;
}
}
System.out.println("GCD=" + gcd);
}
}

OR

if (min > b)
{
min = b; max = a;
}
else
{
min = a; max = b;
}
for(i=min;i>=1;i--)
{
if(a%i==0 && b%i==0)
{
gcd=i; break;
}
}
System.out.println("GCD=" + gcd);

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.
28 = 2*2+8*8 = 4+64= 68
68= 6x6+8x8 = 36+64 =100
100 = 1x1+0x0+0x0 =1
DIFFERENT TYPES OF NUMBERS

class abc
{
public static void happy()
{
int n, r = 1, num, sum = 0;
Scanner sc = new Scanner(System.in);
System.out.print("Enter number=");
n = sc.nextInt();
num = n; //28
while (num > 9) //28>9 68>9 100>9
{
while (num > 0) // 28>0 68>0 100>0
{
r = num % 10;
sum = sum + (r * r); // 68 100 1
num = num / 10;
}
num = sum;
sum = 0;
}
if (num == 1) //1==1
{
System.out.println("Happy Number");
}
else
{
System.out.println("Not Happy Number");
}
}

Niven / Harshad 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. (111 = 1+1+1 = 3 , 111%3==0)
class prog
{
public static void niven()
{
int r, n, num,
sum = 0;
Scanner sc = new Scanner(System.in);
System.out.print("Enter number=");
n = sc.nextInt();
num = n; // 111
while (num > 0)
{
r = num % 10;
sum = sum + r; // 3
num = num / 10;
DIFFERENT TYPES OF NUMBERS

}
if (n % sum == 0) // 111%3==0
{
System.out.println("Harshad Number");
}
else
{
System.out.println("Not Harshad Number");
}
}
}

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.

First find gcd then write


lcm = (a * b) / gcd;
System.out.println("LCM:" + lcm);

Multiply Of Digit Program in Java


If a number=1234, then 1*2*3*4 ,Multiply of digit=24, Multiply Of Digit Program in Java

public static void main(String[] args)


{
// TODO code application logic here
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);
}

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.

public static void main(String[] args)


{
DIFFERENT TYPES OF NUMBERS

// TODO code application logic here


int n,
sqr = 1,
sum = 0, r;
Scanner sc = new Scanner(System.in);
System.out.print("Enter number=");
n = sc.nextInt(); // 9
sqr = n * n; // 81
while (sqr > 0) //81>0
{
r = sqr % 10;
sum = sum + r; // 9
sqr = sqr / 10;
}
if (n == sum) // 9 ==9
{
System.out.println("Neon Number");
}
else
{
System.out.println("Not Neon Number");
}
}
Palindrome Number Program in Java
A palindromic number is a number that remains the same when its digits are reversed. Like 16461, for
example,

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) // 21>0
{
r = num % 10;
rev = (rev * 10) + r; // 0*10+1=1 *10+2=12
num = num / 10;
}
if (n == rev)
{
System.out.println("Palindrome Number");
}
else
{
System.out.println("Not Palindrome Number");
DIFFERENT TYPES OF NUMBERS

}
}

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.

public static void main(String[] args)


{
int n,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;
}
}
if (n== sum) // if(sum>n)
{
System.out.println("Perfect Number"); // Abandant Number
}
else
{
System.out.println("Not Perfect Number");
}
}

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.

public static void main(String[] args)


{
// TODO code application logic here
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)
{
DIFFERENT TYPES OF NUMBERS

flag = false;
break;
}
i++;
}
if (flag)
{
System.out.println("Number is prime.");
}
else
{
System.out.println("Number is not prime.");
}
}

Reverse Number Program in Java


If a number=1234, then reverse of number is 4321.

public static void main(String[] args)


{
// TODO code application logic here
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("Reverse of Number=" + rev);
}

Krishnamurthy / 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.

public static void main(String[] args)


{
// TODO code application logic here
int n, num, r,
sumOfFactorial = 0;
Scanner sc = new Scanner(System.in);
System.out.print("Enter number=");
n = sc.nextInt();
DIFFERENT TYPES OF NUMBERS

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

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.

public static void main(String[] args)


{
// TODO code application logic here
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
{
DIFFERENT TYPES OF NUMBERS

System.out.println("Not Spy Number");


}
}

Twin Prime Program in Java


A twin prime is a prime number that is either 2 less or 2 more than another prime number—for
example, either member of the twin prime pair (41, 43). In other words, a twin prime is a prime that
has a prime gap of two.

public static void main(String[] args)


{
int a, b;
Scanner sc = new Scanner(System.in);
System.out.print("Enter a=");
a = sc.nextInt();
System.out.print("Enter b=");
b = sc.nextInt();
if (prime(a) && prime(b) && (Math.abs(a - b) == 2))
{
System.out.println("Twin Prime");
}
else
{
System.out.println("Not Twin 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;
}

Twisted Prime Program in Java


A number is called a twisted prime number if it is a prime number and reverse of this number is also a
prime number. 13 is a prime number, 31 is also a prime number.

import java.util.Scanner;
DIFFERENT TYPES OF NUMBERS

public class TwistedPrime


{
public static void main(String[] args)
{
// TODO code application logic here
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 (prime(n) && prime(rev))
{
System.out.println("Twisted Prime");
}
else
{
System.out.println("Not Twisted 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;
}
}

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.
DIFFERENT TYPES OF NUMBERS

public static void main(String[] args)


{
// TODO code application logic here
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");
}
}

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: 1 + 3*3 + 5*5*5 = 1 + 9 + 125 = 135

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)
{
DIFFERENT TYPES OF NUMBERS

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

Prime Number Up to N Terms Program in Java


Prime Number Up to N Terms Program in Java. Ex: 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

public static void main(String[] args)


{
int size,c=0;
Scanner sc = new Scanner(System.in);
System.out.print("Enter size of prime=");
n = sc.nextInt(); // 5

for(j=1;j<=n;j++) // 1--5
{ c=0; // c=0
for(int i=1;i <= j;i++) // 1=1
{
if (n % i == 0)
{
C++;
}
}
if (c==2)
{
System.out.println("Number is prime="+n);
}
DIFFERENT TYPES OF NUMBERS

}
}

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.

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

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

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=1; i < n; i++)
DIFFERENT TYPES OF NUMBERS

{
if(i*(i+1) == n)
{
flag =true;
break;
}
}
if(flag)
{
System.out.println("Pronic Number");
}
else
{
System.out.println("Not Pronic Number");
}
}

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.

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(); // 8
while (n !=1) //8!=1 // 8>1 4>1 2>1 2>=1
{
if (n % 5 == 0) // 8%5 x 4%5x 2%5==0x 1%5x
{
n /= 5;
}
else if (n % 3 == 0) // 8%3 x 4%3x 2%2==0 1%3==0
{
n /= 3;
}
else if (n % 2 == 0) //8%2 ==0 4%2==02%2==0
{
n /= 2; // 8/2=4 4/2=2 2/2==1
}
else
{
flag=false;
break;
}
DIFFERENT TYPES OF NUMBERS

}
if (flag)
{
System.out.println("Ugly number.");
}
else
{
System.out.println("Not Ugly number.");
}
}

Composite Number

import java.util.Scanner;

public class CompositeNumber {

public static boolean isComposite(int number) {


if (number <= 1) {
return false; // 0 and 1 are not composite numbers
}

for (int i = 2; i < number; i++) {


if (number % i == 0) {
return true;
}
}
return false;
}

public static void main() {


Scanner sc = new Scanner(System.in);

System.out.print("Enter a number: ");


int number = sc.nextInt();

if (isComposite(number)) {
System.out.println(number + " is a composite number.");
} else {
System.out.println(number + " is not a composite number.");
}

}
}

You might also like