[go: up one dir, main page]

0% found this document useful (0 votes)
26 views51 pages

Class 9 Icse Number Base Program

The document describes various types of numbers including Ugly, Pronic, Magic, Tech, Disarium, Unique, Twisted Prime, Twin Prime, Spy, Special, Niven, Neon, Harshad, and Happy numbers, along with their definitions and examples. Each type of number is accompanied by a Java program that checks if a given number belongs to that category. The document provides a comprehensive overview of these mathematical concepts and their implementations in code.

Uploaded by

prad
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)
26 views51 pages

Class 9 Icse Number Base Program

The document describes various types of numbers including Ugly, Pronic, Magic, Tech, Disarium, Unique, Twisted Prime, Twin Prime, Spy, Special, Niven, Neon, Harshad, and Happy numbers, along with their definitions and examples. Each type of number is accompanied by a Java program that checks if a given number belongs to that category. The document provides a comprehensive overview of these mathematical concepts and their implementations in code.

Uploaded by

prad
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/ 51

A number is said to be an Ugly number if positive

numbers whose prime factors only include 2, 3, 5.


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

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
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
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)
{
// 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);
rightNumber = num / (int) Math.pow(10, digits / 2);

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
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
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)
{
// 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");
}
}
}
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 java.util.Scanner;
Public class KboatTwistedPrime
{
Public void twistedPrimeCheck() {
Scanner in = new Scanner(System.in);
System.out.print(“Enter number: “);
Int num = in.nextInt();

If (num == 1) {
System.out.println(num + “ is not a twisted prime number”);
}
Else {
Boolean isPrime = true;
For (int I = 2; I <= num / 2; i++) {
If (num % I == 0) {
isPrime = false;
break;
}
}

If (isPrime) {

Int t = num;
Int revNum = 0;

While (t != 0) {
Int digit = t % 10;
T /= 10;
revNum = revNum * 10 + digit;
}

For (int I = 2; I <= revNum / 2; i++) {


If (revNum % I == 0) {
isPrime = false;
break;
}
}
}

If (isPrime)
System.out.println(num + “ is a twisted prime number”);
Else
System.out.println(num + “ is not a twisted prime
number”);
}

}
}

Or using function
import java.util.Scanner;

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

Output:
Enter number=137
Twisted Prime
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 java.util.Scanner;

public class TwinPrime


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

Output:
Enter a=5
Enter b=7
Twin Prime
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)
{
// 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
{
System.out.println("Not Spy Number");
}
}
}

Output:
Enter number=123
Spy Number
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.
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 java.util.Scanner;

public class SpecialNumber


{
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();
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
a Niven number (or harshad 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 NivenNumber


{
public static void main(String[] args)
{
// TODO code application logic here
int n, num, r,
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("Niven Number");
}
else
{
System.out.println("Not Niven Number");
}
}
}

Output:
Enter number=204
Niven Number
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)
{
// 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();
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
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
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
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
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
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
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 java.util.Scanner;

/**
* @author AFAQ
*/
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
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
Two different numbers are said to be so Amicable Numbers if each sum of
divisors is equal to the other number.
Amicable Numbers are: (220, 284), (1184, 1210), (2620, 2924), (5020,
5564), (6232, 6368) etc.
Example– 220 and 284 are Amicable Numbers.
Divisors of 220 = 1, 2, 4, 5, 10, 11, 20, 22, 44, 55, 110
1+2+4+5+10+11+20+22+44+55+110=284
Divisors of 284 = 1, 2, 7, 71, 142
1+2+7+71+142=220
import java.util.Scanner;
public class AmicableNumber
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter 1st number= ");
int a = sc.nextInt();
System.out.print("Enter 2nd number: ");
int b = sc.nextInt();
int sumA = 0, sumB = 0;
for (int i = 1; i < a; i++)
{
if (a % i == 0)
{
sumA += i;
}
}
for (int i = 1; i < b; i++)
{
if (b % i == 0)
{
sumB += i;
}
}
if (sumA == b && sumB == a)
{
System.out.println("The numbers are Amicable Number.");
}
else
{
System.out.println("The numbers are not Amicable Number");
}
}
}

Output:
Enter 1st number= 284
Enter 2nd number: 220
The numbers are Amicable Number.

Loop based output questions

Int a, b;

For (a = 6, b = 4; a <= 24; a = a + 6)

If (a % b == 0)

Break;

System.out.println(a);

Output:

12
Char ch = ‘F’;

Int m = ch;

M=m+5;

System.out.println(m + “ “ + ch);

Output:

75 F

Int i = 1;

Int d = 5;

Do

D = d * 2;

System.out.println(d);

I++;

While (i <= 5);

Output:

10

20

40

80

160

For (int i = 3; i <= 4; i++)

For (int j = 2; j < i; j++)

{
System.out.print(“”);

System.out.println(“WIN”);

Output:

WIN

WIN

String arr[]= {“DELHI”, “CHENNAI”, “MUMBAI”, “LUCKNOW”, “JAIPUR”};

System.out.println(arr[0].length()> arr[3].length());

System.out.print(arr[4].substring(0,3));

Output:

False

JAI

Int i;

For ( i = 5 ; i > 10; i ++ )

System.out.println( i );

System.out.println( i * 4 );

Output:

20

Int i;
For( i=5 ; i>=1 ;i--)

If(i%2 ==1)

Continue;

System.out.print( i+ “”);

Output:

42

Public class Test

Public static void main(String[] args)

For (int i = 0; i < 10; i++)

Int x = 10;

Compile time error

Explanation: Curly braces are optional and without curly braces we can take
only one statement under for loop which should not be declarative
statement. Here we are declaring a variable that’s why we will get compile
time error saying error: variable declaration not allowed here.

Public class Test

{
Public static void main(String[] args)

Int i = 0;

For (System.out.println(“HI”); i < 1; i++)

System.out.println(“HELLO GEEKS”);

Compile time error

Explanation: Initialization part of the for loop will be executed only once in
the for loop life cycle. Here we can declare any number of variables but
should be of same type. By mistake if we are trying to declare different data
types variables then we will get compile time error saying error: incompatible
types: String cannot be converted to int.

Public class Test

Public static void main(String[] args)

Int i = 0;

For (System.out.println(“HI”); i < 1; i++)

System.out.println(“HELLO ”);

}
Output:

HI

HELLO

Explanation:I n the initialization section we can take any valid java statement
including System.out.println(). In the for loop initialization section is executed
only once that’s why here it will print first HI and after that HELLO

Public class Test

Public static void main(String[] args)

For (int i = 0;; i++)

System.out.println(“HELLO ”);

Output:

HELLO (Infinitely)

Explanation: In the conditional check we can take any valid java statement
but should be of type Boolean. If we did not give any statement then it
always returns true.

Public class Test

Public static void main(String[] args)


{

For (int i = 0; i < 1; System.out.println(“WELCOME”))

System.out.println(“GEEKS");

Output:

GEEKS WELCOME(Infinitely)

Explanation: In increment-decrement section we can take any valid java


statement including System.out.println(). Here in the increment/decrement
section, a statement is there, which result the program to go to infinite loop.

Fascinating Numbers: Some numbers of 3 digits or more exhibit a very


interesting property. The property is such that, when the number is
multiplied by 2 and 3, and both these products are concatenated with the
original number, all digits from 1 to 9 are present exactly once, regardless of
the number of zeroes.

Let’s understand the concept of Fascinating Number through the following


example:

Consider the number 192

192 x 1 = 192

192 x 2 = 384

192 x 3 = 576

Concatenating the results: 192 384 576

It could be observed that ‘192384576’ consists of all digits from 1 to 9


exactly once. Hence, it could be concluded that 192 is a Fascinating Number.
Some examples of fascinating Numbers are: 192, 219, 273, 327, 1902, 1920,
2019 etc.
import java.util.Scanner;

public class KboatFascinatingNumber


{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.print("Enter the number to check: ");
int num = in.nextInt();

if (num < 100) {


System.out.println(num + " is not a Fascinating Number");
return;
}

int num2 = num * 2;


int num3 = num * 3;

boolean isFascinating = true;


String str = "" + num + num2 + num3;
for (char i = '1'; i <= '9'; i++) {
int idx1 = str.indexOf(i);
int idx2 = str.lastIndexOf(i);
if (idx1 == -1 || idx1 != idx2) {
isFascinating = false;
break;
}
}

if (isFascinating)
System.out.println(num + " is a Fascinating Number");
else
System.out.println(num + " is not a Fascinating Number");
}
}
A number is said to Bouncy number if the digits of the
number are unsorted.
For example,
22344 - It is not a Bouncy number because the digits are
sorted in ascending order.
774410 - It is not a Bouncy number because the digits are
sorted in descending order.
155349 - It is a Bouncy number because the digits are
unsorted.
A number below 100 can never be a Bouncy number.
Write a program in java to accept a number. Check and
display whether it is a Bouncy number or not.
import java.util.Scanner;

public class KboatBouncyNumber


{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.print("Enter a number: ");
int n = in.nextInt();

if (n < 100) {
System.out.println(n + " is not a Bouncy Number.");
return;
}

int t = n;
boolean isIncreasing = true, isDecreasing = true;

int prev = t % 10;


while (t != 0) {
int d = t % 10;
if (d > prev) {
isIncreasing = false;
break;
}
prev = d;
t /= 10;
}

t = n;
prev = t % 10;
while (t != 0) {
int d = t % 10;
if (d < prev) {
isDecreasing = false;
break;
}
prev = d;
t /= 10;
}

if (!isIncreasing && !isDecreasing)


System.out.println(n + " is a Bouncy Number.");
else
System.out.println(n + " is not a Bouncy Number.");
}
}
An Evil number is a positive whole number which has even
number of 1's in its binary equivalent. Example: Binary
equivalent of 9 is 1001, which contains even number of 1's.
A few evil numbers are 3, 5, 6, 9…. Design a program to
accept a positive whole number and find the binary
equivalent of the number and count the number of 1's in it
and display whether it is a Evil number or not with an
appropriate message. Output the result in format given
below:
Example 1
Input: 15
Binary Equivalent: 1111
No. of 1's: 4
Output: Evil Number
import java.util.Scanner;

public class KboatEvilNumber


{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.print("Enter a positive number: ");
int n = in.nextInt();
if (n < 0) {
System.out.println("Invalid Input");
return;
}

int count = 0;
int p = 0;
int binNum = 0;

while (n > 0) {
int d = n % 2;
if (d == 1)
count++;
binNum += (int)(d * Math.pow(10, p));
p++;
n /= 2;
}

System.out.println("Binary Equivalent: " + binNum);


System.out.println("No. of 1's: " + count);

if (count % 2 == 0)
System.out.println("Output: Evil Number");
else
System.out.println("Output: Not an Evil Number");
}
}
A Smith number is a composite number, whose sum of the
digits is equal to the sum of its prime factors. For example:
4, 22, 27, 58, 85, 94, 121 ………. are Smith numbers.
Write a program in Java to enter a number and check
whether it is a Smith number or not.
Sample Input: 666
Sum of the digits: 6 + 6 + 6 = 18
Prime factors are: 2, 3, 3, 37
Sum of the digits of the prime factors: 2 + 3 + 3 + (3 + 7) =
18
Thus, 666 is a Smith Number.
import java.util.Scanner;

public class KboatSmithNumber


{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.print("Enter number: ");
int n = in.nextInt();

if (n <= 0) {
System.out.println(n + " is not a Smith Number.");
return;
}

boolean isComposite = false;


for (int i = 2; i < n; i++) {
if (n % i == 0) {
isComposite = true;
break;
}
}

if (isComposite && n != 1) {
int sumDigits = 0;
int t = n;
while (t != 0) {
int d = t % 10;
sumDigits += d;
t /= 10;
}

int sumPrimeDigits = 0;
t = n;
for(int i = 2; i < t; i++) {
while(t % i == 0) {
t /= i;
int temp = i;
while (temp != 0) {
int d = temp % 10;
sumPrimeDigits += d;
temp /= 10;
}
}
}
if(t > 2) {
while (t != 0) {
int d = t % 10;
sumPrimeDigits += d;
t /= 10;
}
}

if (sumPrimeDigits == sumDigits)
System.out.println(n + " is a Smith Number.");
else
System.out.println(n + " is not a Smith Number.");
}
else {
System.out.println(n + " is not a Smith Number.");
}
}
}
An *Adam Number* is a number where the square of the
number and the square of its reverse, when reversed, are
equal.

✅ *Example*:
- Number: 12
- Reverse: 21
- Square: 144
- Reverse of (21² = 441) → 144
✔ Adam Number

Java Code (Without using a function):*


import java.util.Scanner;

public class AdamNumberCheck {


public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a number: ");
int num = sc.nextInt();

// Reverse the number


int original = num;
int reverse = 0;
while (num > 0) {
int digit = num % 10;
reverse = reverse * 10 + digit;
num /= 10;
}

// Square of original and reverse


int squareOriginal = original * original;
int squareReverse = reverse * reverse;

// Reverse the square of the reversed number


int revSquareReverse = 0;
while (squareReverse > 0) {
int digit = squareReverse % 10;
revSquareReverse = revSquareReverse * 10 + digit;
squareReverse /= 10;
}

// Check if it's an Adam Number


if (squareOriginal == revSquareReverse) {
System.out.println(original + " is an Adam Number");
A Composite Magic number is a positive integer which is
composite as well as a magic number.
Composite number: A composite number is a number which
has more than two factors.
For example:
Factors of 10 are: 1, 2, 5, 10
Magic number: A Magic number is a number in which the
eventual sum of the digit is equal to 1.
For example: 28 = 2+8=10= 1+0=1
Accept two positive integers 'm' and 'n', where m is less than
n. Display the number of composite magic integers that are
in the range between m and n (both inclusive) and output
them along with frequency, in the format specified below:
Sample Input:
m=10 n=100
Output: The composite magic numbers are
10,28,46,55,64,82,91,100
Frequency of composite magic numbers: 8
Sample Input:
m=120 n=90
Output: Invalid input
import java.util.Scanner;

public class KboatCompositeMagicNumber


{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.print("Enter m: ");
int m = in.nextInt();

System.out.print("Enter n: ");
int n = in.nextInt();

if (m < 1 || n < 1 || m > n) {


System.out.println("Invalid input");
return;
}

System.out.println("The composite magic numbers are:");


int count = 0;
for (int i = m; i <= n; i++) {

boolean isComposite = false;


for (int j = 2; j < i; j++) {
if (i % j == 0) {
isComposite = true;
break;
}
}

if (isComposite && i != 1) {
int num = i;
while (num > 9) {
int sum = 0;
while (num != 0) {
int d = num % 10;
num /= 10;
sum += d;
}
num = sum;
}

if (num == 1) {
count++;
System.out.print(i + " ");
}
}
}
System.out.println();
System.out.println("Frequency of composite magic numbers: " +
count);
}
}

Java Number Programs (ISC Classes 11 / 12)


A Goldbach number is a positive even integer that can be
expressed as the sum of two odd primes.
Note: All even integer numbers greater than 4 are Goldbach
numbers.
Example:
6=3+3
10 = 3 + 7
10 = 5 + 5
Hence, 6 has one odd prime pair 3 and 3. Similarly, 10 has
two odd prime pairs, i.e. 3 and 7, 5 and 5.
Write a program to accept an even integer 'N' where N > 9
and N < 50. Find all the odd prime pairs whose sum is equal
to the number 'N'.
Test your program with the following data and some random
data:
Example 1
INPUT:
N = 14
OUTPUT:
PRIME PAIRS ARE:
3, 11
7, 7
Example 2
INPUT:
N = 30
OUTPUT:
PRIME PAIRS ARE:
7, 23
11, 19
13, 17
Example 3
INPUT:
N = 17
OUTPUT:
INVALID INPUT. NUMBER IS ODD.
Example 4
INPUT:
N = 126
OUTPUT:
INVALID INPUT. NUMBER OUT OF RANGE.

ANSWER
import java.util.Scanner;

public class GoldbachNumber


{
public static boolean isPrime(int num) {
int c = 0;

for (int i = 1; i <= num; i++) {


if (num % i == 0) {
c++;
}
}

return c == 2;
}

public static void main(String args[]) {


Scanner in = new Scanner(System.in);
System.out.print("ENTER THE VALUE OF N: ");
int n = in.nextInt();

if (n <= 9 || n >= 50) {


System.out.println("INVALID INPUT. NUMBER OUT OF RANGE.");
return;
}

if (n % 2 != 0) {
System.out.println("INVALID INPUT. NUMBER IS ODD.");
return;
}

System.out.println("PRIME PAIRS ARE:");

int a = 3;
int b = 0;
while (a <= n / 2) {
b = n - a;
if (isPrime(a) && isPrime(b)) {
System.out.println(a + ", " + b);
}

a += 2;
}
}
}
A Prime-Adam integer is a positive integer (without leading
zeros) which is a prime as well as an Adam number.
Prime number: A number which has only two factors, i.e. 1
and the number itself.
Example: 2, 3, 5, 7 … etc.
Adam number: The square of a number and the square of its
reverse are reverse to each other.
Example: If n = 13 and reverse of 'n' = 31, then,
(13)2 = 169
(31)2 = 961 which is reverse of 169
thus 13, is an Adam number.
Accept two positive integers m and n, where m is less than n
as user input. Display all Prime-Adam integers that are in
the range between m and n (both inclusive) and output them
along with the frequency, in the format given below:
Test your program with the following data and some random
data:
Example 1
INPUT:
m=5
n = 100
OUTPUT:
THE PRIME-ADAM INTEGERS ARE:
11 13 31
FREQUENCY OF PRIME-ADAM INTEGERS IS: 3
Example 2
INPUT:
m = 100
n = 200
OUTPUT:
THE PRIME-ADAM INTEGERS ARE:
101 103 113
FREQUENCY OF PRIME-ADAM INTEGERS IS: 3
Example 3
INPUT:
m = 50
n = 70
OUTPUT:
THE PRIME-ADAM INTEGERS ARE:
NIL
FREQUENCY OF PRIME-ADAM INTEGERS IS: 0
Example 4
INPUT:
m = 700
n = 450
OUTPUT:
INVALID INPUT

1. Home
2. /
3. Java Number Programs (ISC Classes 11 / 12)
4. /
5. Prime-Adam Integer Java Program

Java Number Programs (ISC Classes 11 / 12)


A Prime-Adam integer is a positive integer (without leading
zeros) which is a prime as well as an Adam number.
Prime number: A number which has only two factors, i.e. 1
and the number itself.
Example: 2, 3, 5, 7 … etc.
Adam number: The square of a number and the square of its
reverse are reverse to each other.
Example: If n = 13 and reverse of 'n' = 31, then,
(13)2 = 169
(31)2 = 961 which is reverse of 169
thus 13, is an Adam number.
Accept two positive integers m and n, where m is less than n
as user input. Display all Prime-Adam integers that are in
the range between m and n (both inclusive) and output them
along with the frequency, in the format given below:
Test your program with the following data and some random
data:
Example 1
INPUT:
m=5
n = 100
OUTPUT:
THE PRIME-ADAM INTEGERS ARE:
11 13 31
FREQUENCY OF PRIME-ADAM INTEGERS IS: 3
Example 2
INPUT:
m = 100
n = 200
OUTPUT:
THE PRIME-ADAM INTEGERS ARE:
101 103 113
FREQUENCY OF PRIME-ADAM INTEGERS IS: 3
Example 3
INPUT:
m = 50
n = 70
OUTPUT:
THE PRIME-ADAM INTEGERS ARE:
NIL
FREQUENCY OF PRIME-ADAM INTEGERS IS: 0
Example 4
INPUT:
m = 700
n = 450
OUTPUT:
INVALID INPUT
import java.util.Scanner;

public class PrimeAdam


{
public static int reverse(int num) {
int rev = 0;
while (num != 0) {
int d = num % 10;
rev = rev * 10 + d;
num /= 10;
}

return rev;
}

public static boolean isAdam(int num) {


int sqNum = num * num;
int revNum = reverse(num);
int sqRevNum = revNum * revNum;
int rev = reverse(sqNum);

return rev == sqRevNum;


}

public static boolean isPrime(int num) {


int c = 0;

for (int i = 1; i <= num; i++) {


if (num % i == 0) {
c++;
}
}

return c == 2;
}

public static void main(String args[]) {


Scanner in = new Scanner(System.in);
System.out.print("Enter the value of m: ");
int m = in.nextInt();
System.out.print("Enter the value of n: ");
int n = in.nextInt();
int count = 0;

if (m >= n) {
System.out.println("INVALID INPUT");
return;
}

System.out.println("THE PRIME-ADAM INTEGERS ARE:");


for (int i = m; i <= n; i++) {
boolean adam = isAdam(i);
if (adam) {
boolean prime = isPrime(i);
if (prime) {
System.out.print(i + " ");
count++;
}
}
}
if (count == 0) {
System.out.print("NIL");
}

System.out.println();
System.out.println("FREQUENCY OF PRIME-ADAM INTEGERS IS: " + count);
}
}
Write a program to input a number and check and print
whether it is a Pronic number or not. [Pronic number is the
number which is the product of two consecutive integers.]
Examples:
12 = 3 * 4
20 = 4 * 5
42 = 6 * 7
Import java.util.Scanner;

Public class KboatPronicNumber

Public void pronicCheck() {

Scanner in = new Scanner(System.in);

System.out.print(“Enter the number to check: “);

Int num = in.nextInt();

Boolean isPronic = false;

For (int I = 1; I <= num – 1; i++) {

If (I * (I + 1) == num) {

isPronic = true;

break;

}
If (isPronic)

System.out.println(num + “ is a pronic number”);

Else

System.out.println(num + “ is not a pronic number”);

An Abundant number is a number for which the sum of its proper factors is
greater than the number itself. Write a program to input a number and check
and print whether it is an Abundant number or not.

Example:

Consider the number 12.

Factors of 12 = 1, 2, 3, 4, 6 Sum of factors = 1 + 2 + 3 + 4 + 6 = 16

As 16 > 12 so 12 is an Abundant number.

Import java.util.Scanner;

Public class KboatAbundantNumber

Public static void main(String args[]) {

Scanner in = new Scanner(System.in);

System.out.print(“Enter the number: “);


Int n = in.nextInt();

Int sum = 0;

For (int I = 1; I < n; i++) {

If (n % I == 0)

Sum += I;

If (sum > n)

System.out.println(n + “ is an abundant number”);

Else

System.out.println(n + “ is not an abundant number”);

Write a program to input a number. Check and display whether it is a Niven


number or not. (A number is said to be Niven which is divisible by the sum of
its digits).

Example: Sample Input 126

Sum of its digits = 1 + 2 + 6 = 9 and 126 is divisible by 9.

Import java.util.Scanner;

Public class KboatNivenNumber

Public void checkNiven() {


Scanner in = new Scanner(System.in);

System.out.print(“Enter number: “);

Int num = in.nextInt();

Int orgNum = num;

Int digitSum = 0;

While (num != 0) {

Int digit = num % 10;

Num /= 10;

digitSum += digit;

/*

* digitSum != 0 check prevents

* division by zero error for the

* case when users gives the number

* 0 as input

*/

If (digitSum != 0 && orgNum % digitSum == 0)

System.out.println(orgNum + “ is a Niven number”);

Else

System.out.println(orgNum + “ is not a Niven number”);

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 java.util.Scanner;

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;

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)

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

rightNumber = num / (int) Math.pow(10, digits / 2);

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
First 15 Narcissistic Numbers
Write a Java program to generate and show the first 15 narcissistic decimal numbers.

A Narcissistic decimal number is a non-negative integer, n that is equal to the sum of the
m-th powers of each of the digits in the decimal representation of n, where m is the
number of digits in the decimal representation of n.

 if n is 153

 then m , (the number of decimal digits) is 3

 we have 13 + 53 + 33 = 1 + 125 + 27 = 153

 and so 153 is a narcissistic decimal number .

Narcissistic numbers in various bases :


The sequence of base 10 narcissistic numbers starts: 0, 1, 2, 3, 4, 5, 6, 7, 8,
9, 153, 370, ……
The sequence of base 8 narcissistic numbers starts: 0, 1, 2, 3, 4, 5, 6, 7, 24,
64, 134, 205,….
Pictorial Presentation:

Sample Solution:

Java Code:
// https://rosettacode.org/

public class Example6 {

public static void main(String[] args){

for(long n = 0, ctr = 0; ctr < 15; n++){

if(is_narc_dec_num(n)){
System.out.print(n + " ");

ctr++;

System.out.println();

public static boolean is_narc_dec_num(long n){

if(n < 0) return false;

String str1 = Long.toString(n);

int x = str1.length();

long sum_num = 0;

for(char c : str1.toCharArray()){

sum_num += Math.pow(Character.digit(c, 10),


x);

return sum_num == n;

Copy

Sample Output:
0 1 2 3 4 5 6 7 8 9 153 370 371 407 1634
public class TriangleArea {

public static void main(String[] args) {

double sideA = 5;

double sideB = 6;

double sideC = 7;

double area = calculateTriangleArea(sideA, sideB, sideC);

System.out.println("The area of the triangle is: " + area);

public static double calculateTriangleArea(double a, double b, double c) {

double s = (a + b + c) / 2.0;

return Math.sqrt(s * (s - a) * (s - b) * (s - c));

Public class DistanceCalculator {

Public static void main(String[] args) {

Double x1 = 10;

Double y1 = 10;

Double x2 = 20;

Double y2 = 20;

Double distance = calculateDistance(x1, y1, x2, y2);

System.out.println(“The distance between the points is: “ + distance);

}
Public static double calculateDistance(double x1, double y1, double x2,
double y2) {

Return Math.sqrt(Math.pow(x2 – x1, 2) + Math.pow(y2 – y1, 2));

You might also like