0% found this document useful (0 votes)
2 views30 pages

Number Programs in Java Colr

The document contains a collection of Java programs designed for Grade 9 and 10 Computer Applications, focusing on various number-related concepts such as Armstrong numbers, Automorphic numbers, and Prime numbers. Each section includes a brief explanation of the concept, example outputs, and the corresponding Java code. The document serves as a practical guide for students to learn and implement these mathematical concepts through programming.

Uploaded by

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

Number Programs in Java Colr

The document contains a collection of Java programs designed for Grade 9 and 10 Computer Applications, focusing on various number-related concepts such as Armstrong numbers, Automorphic numbers, and Prime numbers. Each section includes a brief explanation of the concept, example outputs, and the corresponding Java code. The document serves as a practical guide for students to learn and implement these mathematical concepts through programming.

Uploaded by

msd777
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

JAVA – USING

BLUEJ

VVHS – Grade 9 & 10 Computer Applications 0|PagePage |0


NUMBER PROGRAMS IN JAVA
Click on the topic to go directly to the program

Armstrong Number........................................................................................................... 2
Automorphic number ....................................................................................................... 3
Buzz Number .................................................................................................................... 3
Circular prime ................................................................................................................... 4
Coprime Numbers ............................................................................................................. 5
Duck number .................................................................................................................... 6
Factorial ........................................................................................................................... 7
Factor............................................................................................................................... 7
Fibonacci number ............................................................................................................. 8
Greatest common divisor (gcd) ......................................................................................... 9
Harshad number or Niven number .................................................................................. 10
Neon number ................................................................................................................. 11
Palindrome Number ....................................................................................................... 12
Perfect number............................................................................................................... 13
Prime number................................................................................................................. 14
Reverse .......................................................................................................................... 15
Spy number .................................................................................................................... 16
Twin Prime Program ....................................................................................................... 17
Twisted Prime ................................................................................................................ 18
Unique ........................................................................................................................... 19
Disarium number ............................................................................................................ 20
Tech Number .................................................................................................................. 21
Prime Number Up to N Terms ......................................................................................... 22
Magic number ................................................................................................................ 23
Pronic ............................................................................................................................. 24
Ugly number .................................................................................................................. 25
Evil Number Program in Java .......................................................................................... 26
Fascinating Number Program ......................................................................................... 27
Capricorn or Kaprekar Number ....................................................................................... 28
Krishnamurthy Number .................................................................................................. 29

VVHS – Computer Applications : Number Programs in Java Page |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.
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
import [Link];

public class ArmstrongNumber


{
public static void main(String[] args)
{
int n,
cubeSum = 0, num, r;
Scanner sc = new Scanner([Link]);
[Link]("Enter number=");
n = [Link]();
num = n;
while (num > 0)
{
r = num % 10;
cubeSum = cubeSum + (r * r * r);
num = num / 10;
}
if (n == cubeSum)
{
[Link]("Armstrong Number");
}
else
{
[Link]("Not Armstrong Number");
}
}
}

Output:
Enter number=153
Armstrong Number

VVHS – Computer Applications : Number Programs in Java Page |2


Automorphic number
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 [Link];
public class AutomorphicNumber
{
public static void main(String[] args)
{
int n, sqrNum, temp,sqrNumRemainder,c = 0;
Scanner sc = new Scanner([Link]);
[Link]("Enter number=");
n = [Link]();
temp=n;
while (temp > 0)
{
temp=temp/10;
c++;
}
sqrNum = n * n;
sqrNumRemainder= sqrNum%(int)[Link](10, c);
if(sqrNumRemainder==n)
{
[Link]("Automorphic Number");
}
else
{
[Link]("Not Automorphic Number");
}
}
}
Output:
Enter number=25
Automorphic Number

Buzz Number -if a no. ends with 7 or is divisible by 7. Example: 1007 is a Buzz Number..

import [Link];
public class BuzzNumber
{
public static void main(String[] args)
{
int n;
Scanner sc = new Scanner([Link]);
[Link]("Enter n=");
n = [Link]();
if (n % 10 == 7 || n % 7 == 0)
{
[Link]("Buzz number");
}
else
{
[Link]("Not Buzz number");
}
}
}
VVHS – Computer Applications : Number Programs in Java Page |3
Circular prime

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 [Link];

public class CircularPrime


{
public static void main(String[] args)
{
boolean flag = true;
int n, num, r, c = 0;
Scanner sc = new Scanner([Link]);
[Link]("Enter number=");
n = [Link]();
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) [Link](10, c - 1)) + num;
if (!prime(num))
{
flag = false;
break;
}
}

VVHS – Computer Applications : Number Programs in Java Page |4


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 [Link];
public class CoPrimeNumbers
{
public static void main(String[] args)
{
int a, b, gcd = 1;
Scanner sc = new Scanner([Link]);
[Link]("Enter a=");
a = [Link]();
[Link]("Enter b=");
b = [Link]();
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)
{
[Link]("Co Prime Numbers");
}
else
{
[Link]("Not Co Prime Numbers");
}
}
}
Output:
Enter a=13
Enter b=15
Co Prime Numbers
VVHS – Computer Applications : Number Programs in Java Page |5
Duck number
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 [Link];

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([Link]);
[Link]("Enter number=");
n = [Link]();
num = n;
while (num > 0)
{
r = num % 10;
if(r==0)
{
flag=true;
}
num = num / 10;
}
if(flag)
{
[Link]("Duck Number");
}
else
{
[Link]("Not Duck Number");
}

}
}
Output:
Enter number=205
Duck Number

VVHS – Computer Applications : Number Programs in Java Page |6


Factorial
Factorial is the product of all positive integers less than or equal to n. Examples: 4! = 4 × 3 × 2 × 1 =
24

import [Link];
public class Factorial
{
public static void main(String[] args)
{
// TODO code application logic here
int n,
fact = 1;
Scanner sc = new Scanner([Link]);
[Link]("Enter number of series=");
n = [Link]();
for (int i = 1; i <= n; i++)
{
fact = fact * i;
}
[Link]("Factorial=" + fact);
}
}
Output:
Enter number of series=5
Factorial=120

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 [Link];
public class Factors
{
public static void main(String[] args)
{
int n;
Scanner sc = new Scanner([Link]);
[Link]("Enter number=");
n = [Link]();
for (int i = 1; i <= n; i++)
{
if (n % i == 0)
{
[Link](i);
}
}
}
}
Output:
Enter number=6
1
2
3
6
VVHS – Computer Applications : Number Programs in Java Page |7
Fibonacci number

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 [Link];

public class FibonacciSeries


{
public static void main(String[] args)
{
// TODO code application logic here
int n;
Scanner sc = new Scanner([Link]);
[Link]("Enter number of series=");
n = [Link]();
int a = 0,
b = 1, c;
for (int i = 1; i <= n; i++)
{
[Link](a);
c = a + b;
a = b;
b = c;
}
}
}
Output:
Enter number of series=10
0
1
1
2
3
5
8
13
21
34

VVHS – Computer Applications : Number Programs in Java Page |8


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 [Link];

public class GCDProgram


{
public static void main(String[] args)
{
int a, b, gcd = 1;
Scanner sc = new Scanner([Link]);
[Link]("Enter a=");
a = [Link]();
[Link]("Enter b=");
b = [Link]();
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;
}
}
[Link]("GCD=" + gcd);
}
}
Output:
Enter a=15
Enter b=18
GCD=3

VVHS – Computer Applications : Number Programs in Java Page |9


Harshad number or Niven number
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 [Link];

public class HarshadNumber


{
public static void main(String[] args)
{
int r, n, num,
sum = 0;
Scanner sc = new Scanner([Link]);
[Link]("Enter number=");
n = [Link]();
num = n;
while (num > 0)
{
r = num % 10;
sum = sum + r;
num = num / 10;
}
if (n % sum == 0)
{
[Link]("Harshad Number");
}
else
{
[Link]("Not Harshad Number");
}
}
}

Output:
Enter number=6804
Harshad Number

VVHS – Computer Applications : Number Programs in Java P a g e | 10


Neon number
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 [Link];

public class NeonNumber


{
public static void main(String[] args)
{
// TODO code application logic here
int n,
sqr = 1,
sum = 0, r;
Scanner sc = new Scanner([Link]);
[Link]("Enter number=");
n = [Link]();
sqr = n * n;
while (sqr > 0)
{
r = sqr % 10;
sum = sum + r;
sqr = sqr / 10;
}
if (n == sum)
{
[Link]("Neon Number");
}
else
{
[Link]("Not Neon Number");
}
}
}

VVHS – Computer Applications : Number Programs in Java P a g e | 11


Palindrome Number

Palindromic number is a number that remains the same when its digits are reversed. Like 16461, for
example,

import [Link];

public class PalindromeNumber


{
public static void main(String[] args)
{
int n, num, r,
rev = 0;
Scanner sc = new Scanner([Link]);
[Link]("Enter number=");
n = [Link]();
num = n;
while (num > 0)
{
r = num % 10;
rev = (rev * 10) + r;
num = num / 10;
}
if (n == rev)
{
[Link]("Palindrome Number");
}
else
{
[Link]("Not Palindrome Number");
}
}
}
Output:
Enter number=12321
Palindrome Number

VVHS – Computer Applications : Number Programs in Java P a g e | 12


Perfect number

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 [Link];

public class PerfectNumber


{
public static void main(String[] args)
{
// TODO code application logic here
int n,
mul = 1,
sum = 0;
Scanner sc = new Scanner([Link]);
[Link]("Enter number=");
n = [Link]();
for (int i = 1; i < n; i++)
{
if (n % i == 0)
{
sum = sum + i;
mul = mul * i;
}
}
if (mul == sum)
{
[Link]("Perfect Number");
}
else
{
[Link]("Not Perfect Number");
}
}
}

Output:
Enter number=6
Perfect Number

VVHS – Computer Applications : Number Programs in Java P a g e | 13


Prime number

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 [Link];

public class PrimeNumber


{
public static void main(String[] args)
{
// TODO code application logic here
int n,
i = 2;
boolean flag = true;
Scanner sc = new Scanner([Link]);
[Link]("Enter number=");
n = [Link]();
while (n > i)
{
if (n % 2 == 0)
{
flag = false;
break;
}
i++;
}
if (flag)
{
[Link]("Number is prime.");
}
else
{
[Link]("Number is not prime.");
}
}
}
Output:
Enter number=17
Number is prime.

VVHS – Computer Applications : Number Programs in Java P a g e | 14


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

import [Link];

public class ReverceNumber


{
public static void main(String[] args)
{
// TODO code application logic here
int n, num, r,
rev = 0;
Scanner sc = new Scanner([Link]);
[Link]("Enter number=");
n = [Link]();
num = n;
while (num > 0)
{
r = num % 10;
rev = (rev * 10) + r;
num = num / 10;
}
[Link]("Reverse of Number=" + rev);
}
}

Output:
Enter number=1234
Reverse of Number=4321

VVHS – Computer Applications : Number Programs in Java P a g e | 15


Spy number
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 [Link];

public class SpyNumber


{
public static void main(String[] args)
{
// TODO code application logic here
int r, n, num, mul = 1, sum = 0;
Scanner sc = new Scanner([Link]);
[Link]("Enter number=");
n = [Link]();
num = n;
while (num > 0)
{
r = num % 10;
sum = sum + r;
mul = mul * r;
num = num / 10;
}
if (mul == sum)
{
[Link]("Spy Number");
}
else
{
[Link]("Not Spy Number");
}
}
}
Output:
Enter number=123
Spy Number

VVHS – Computer Applications : Number Programs in Java P a g e | 16


Twin Prime Program
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.

import [Link];

public class TwinPrime


{
public static void main(String[] args)
{
int a, b;
Scanner sc = new Scanner([Link]);
[Link]("Enter a=");
a = [Link]();
[Link]("Enter b=");
b = [Link]();
if (prime(a) && prime(b) && ([Link](a - b) == 2))
{
[Link]("Twin Prime");
}
else
{
[Link]("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;
}
}
Output:
Enter a=5
Enter b=7
Twin Prime

VVHS – Computer Applications : Number Programs in Java P a g e | 17


Twisted Prime
A number is called a twisted prime number if it is a prime number and reverse of this number is also
a prime number.

import [Link];

public class TwistedPrime


{
public static void main(String[] args)
{
// TODO code application logic here
int n, num, r,
rev = 0;
Scanner sc = new Scanner([Link]);
[Link]("Enter number=");
n = [Link]();
num = n;
while (num > 0)
{
r = num % 10;
rev = (rev * 10) + r;
num = num / 10;
}
if (prime(n) && prime(rev))
{
[Link]("Twisted Prime");
}
else
{
[Link]("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;
}
}

Output:
Enter number=137
Twisted Prime

VVHS – Computer Applications : Number Programs in Java P a g e | 18


Unique NUMBER
A number is said to be unique number , if the digits in it are not repeated. for example, 12345 is a
unique number. 123445 is not a unique number.

import [Link];

public class UniqueNumber


{
public static void main(String[] args)
{
// TODO code application logic here
int r1, r2, n, num1, num2, c = 0;
Scanner sc = new Scanner([Link]);
[Link]("Enter number=");
n = [Link]();
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)
{
[Link]("Unique Number");
}
else
{
[Link]("Not Unique Number");
}
}
}
Output:
Enter number=23456
Unique Number

VVHS – Computer Applications : Number Programs in Java P a g e | 19


Disarium number
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 [Link];

public class DisariumNumber


{
public static void main(String[] args)
{
int r, n, num,digits=0,
sum = 0;
Scanner sc = new Scanner([Link]);
[Link]("Enter number=");
n = [Link]();
num = n;
while (num > 0)
{
digits++;
num = num / 10;
}
num = n;
while (num > 0)
{
r = num % 10;
sum = sum + (int)[Link](r, digits);
num = num / 10;
digits--;
}

if(n==sum)
{
[Link]("Disarium Number");
}
else
{
[Link]("Not Disarium Number");
}

}
}
Output:
Enter number=135
Disarium Number

VVHS – Computer Applications : Number Programs in Java P a g e | 20


Tech Number
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.

Example:

2025 => 20+25 => 552 => 2025

import [Link];

public class TechNumber


{
public static void main(String[] args)
{
// TODO code application logic here
int n, num, leftNumber, rightNumber, digits = 0,
sumSquare = 0;
Scanner sc = new Scanner([Link]);
[Link]("Enter number=");
n = [Link]();
num = n;
while (num > 0)
{
digits++;
num = num / 10;
}
if (digits % 2 == 0)
{
num = n;
leftNumber = num % (int) [Link](10, digits / 2);
num = num / (int) [Link](10, digits / 2);
rightNumber = num;
sumSquare = (leftNumber + rightNumber) * (leftNumber + rightNumber);
if (n == sumSquare)
{
[Link]("Tech Number");
}
else
{
[Link]("Not Tech Number");
}
}
else
{
[Link]("Not Tech Number");
}
}
}
VVHS – Computer Applications : Number Programs in Java P a g e | 21
Output:
Enter number=2025
Tech Number

Prime Number Up to N Terms


import [Link];

public class PrimeNumberUptoN


{
public static void main(String[] args)
{
int size,c=0;
Scanner sc = new Scanner([Link]);
[Link]("Enter size of prime=");
size = [Link]();
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)
{
[Link]("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

VVHS – Computer Applications : Number Programs in Java P a g e | 22


Magic number
Magic number 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 [Link] we single digit
number will now 1 them it would not a magic number.

import [Link];

public class MagicNumber


{
public static void main(String[] args)
{
int n, r = 1, num, sum = 0;
Scanner sc = new Scanner([Link]);
[Link]("Enter number=");
n = [Link]();
num = n;
while (num > 9)
{
while (num > 0)
{
r = num % 10;
sum = sum + r;
num = num / 10;
}
num = sum;
sum = 0;
}
if (num == 1)
{
[Link]("Magic Number");
}
else
{
[Link]("Not Magic Number");
}
}
}

Output:
Enter number=226
Magic Number

VVHS – Computer Applications : Number Programs in Java P a g e | 23


Pronic Number

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 [Link];

public class PronicNumber


{
public static void main(String[] args)
{
int n;
boolean flag=false;
Scanner sc = new Scanner([Link]);
[Link]("Enter number=");
n = [Link]();
for(int i=0; i < n; i++)
{
if(i*(i+1) == n)
{
flag =true;
break;
}
}
if(flag)
{
[Link]("Pronic Number");
}
else
{
[Link]("Not Pronic Number");
}
}
}
Output:
Enter number=42
Pronic Number

VVHS – Computer Applications : Number Programs in Java P a g e | 24


Ugly number
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 [Link];

public class UglyNumber


{
public static void main(String[] args)
{
int n;
boolean flag=true;
Scanner sc = new Scanner([Link]);
[Link]("Enter number=");
n = [Link]();
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)
{
[Link]("Ugly number.");
}
else
{
[Link]("Not Ugly number.");
}
}
}
Output:
Enter number=15
Ugly Number

VVHS – Computer Applications : Number Programs in Java P a g e | 25


Evil Number Program in Java
import [Link];
public class EvilNumber {
public static void main(String[] args)
{
int n,num;
Scanner sc = new Scanner([Link]);
[Link]("Enter number=");
n = [Link]();

int binaryDigits=0;
String binary = "";
num=n;
while (num > 0)
{
binary= num % 2 + binary ;
if(num%2==1)
{
binaryDigits++;
}
num = num / 2;
}
if(binaryDigits%2==0)
{
[Link]("Binary of "+n+"="+binary);
[Link]("Evil Number");
}
else
{
[Link]("Binary of "+n+"="+binary);
[Link]("Not Evil Number");
}

}
}
Output:
Enter number=23
Binary of 23=10111
Evil Numbe

VVHS – Computer Applications : Number Programs in Java P a g e | 26


Fascinating Number Program
Example: 327
Multiply with 2:
327×2=654
Multiply with 3:
327×3=981
Now, concatenate the all number.
“327”+”654″+ “981”= 327654981

Here we can see that all digits from 1 to 9 are available in concatenating string

import [Link];
public class FascinatingNumber
{

public static void main(String args[])


{
int num, num2, num3;
Scanner sc = new Scanner([Link]);
[Link]("Enter any Number: ");
num = [Link]();
num2 = num * 2;
num3 = num * 3;
String concatNumbers = num + "" + num2 +""+ num3;
[Link]("Concated Number="+concatNumbers);
boolean flag = true;
for (char digit = '1'; digit <= '9'; digit++)
{
int count = 0;
for (int i = 0; i < [Link](); i++)
{
char ch = [Link](i);
if (ch == digit)
{
count++;
}
}
if (count > 1 || count == 0)
{
flag = false;
break;
}
}
if (flag)
{
[Link]("Fascinating Number.");
} else
{
[Link]("Not a Fascinating number.");
}
}
}
VVHS – Computer Applications : Number Programs in Java P a g e | 27
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.

import [Link];

public class CapricornNumber


{

public static void main(String[] args)


{
Scanner scanner = new Scanner([Link]);
[Link]("Enter a number=");
int n = [Link]();
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) [Link](10, i);
int quotient = square / divisor;
int remainder = square % divisor;
if (quotient + remainder == n)
{
isCapricorn = true;
}
}
if (isCapricorn)
{
[Link]("Capricorn/Kaprekar number");
} else
{
[Link]("Not Capricorn/Kaprekar number");
}
}
}

Output:
Enter a number=297
Capricorn/Kaprekar number
VVHS – Computer Applications : Number Programs in Java P a g e | 28
Krishnamurthy Number
A number is said to be Krishnamurthy Number when the sum of factorial of its digits is equal to the
number itself. Example- 145 is a Krishnamurthy Number as 1!+4!+5!=145.

import [Link];

public class SpecialNumber


{
public static void main(String[] args)
{
// TODO code application logic here
int n, num, r,
sumOfFactorial = 0;
Scanner sc = new Scanner([Link]);
[Link]("Enter number=");
n = [Link]();
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)
{
[Link]("Special Number" );
}
else
{
[Link]("Not Special Number" );
}
}
}
Output:
Enter number=124
Not Special Number

VVHS – Computer Applications : Number Programs in Java P a g e | 29

You might also like