0% found this document useful (0 votes)
15 views

Number Programs

The document contains various Java programming exercises and their solutions, including checking for Lead numbers, Pronic numbers, and Cyclo numbers, as well as concepts like function overloading and infinite loops. It also includes questions on data types, control structures, and algorithms related to arrays and string manipulation. Additionally, it covers topics such as constructors, abstraction, and specific programming tasks like calculating factorials and identifying Armstrong numbers.

Uploaded by

Aro Pio Jerry
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

Number Programs

The document contains various Java programming exercises and their solutions, including checking for Lead numbers, Pronic numbers, and Cyclo numbers, as well as concepts like function overloading and infinite loops. It also includes questions on data types, control structures, and algorithms related to arrays and string manipulation. Additionally, it covers topics such as constructors, abstraction, and specific programming tasks like calculating factorials and identifying Armstrong numbers.

Uploaded by

Aro Pio Jerry
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 44

1. Input a number and verify whether it is a Lead Number or not.

A
number is called a Lead number if the sum of the even digits is
equal to the sum of the odd digits. For example, 1452. [15]
Answer:

import java.util.*;
public class Q9
{
void main( )
{
Scanner sc = new Scanner (System.in);
int n = 0, sed=0, sod=0, d;
System.out.println("The number is :");
n = sc.nextlnt( );;
int cn=n;
while(cn > 0)
{
d=cn%10;
if ( d % 2== 0)
sed+=d;
else
sod+=d;
cn=cn/10;
}
if( sod==sed)
System.out.println("IT IS A LEAD NUMBER");
else
System.out.println("IT IS NOT A LEAD NUMBER");
}
}

2. Write a program in java to input a number and check whether it is a


pronic number or Heteromecic number or not. Pronic number: A pronic
number, oblong number, rectangular number or heteromecic number is a
number which is the product of two consecutive integers i.e n(n-t-l)
The first few Pronic numbers are: 0,2,6,12.20,30,42,56,72.
Answer:
import java.util.*;
class Main {
public static void main(String[ ] args)
{
Scanner sc = new Scanner(System.in);
System.out.print("lnput a number:");
int n = sc.nextlnt( );
int result = 0; for(int i=0; i<n; i++)
{
if(i*(i+1) == n)
{
result = 1; break;
}
}
if(result == 1)
System.out.println(''Pronic Number."+n);
else
System.out.println("Not a Pronic Number."+n);
}//end of main
}//end of class

ICSE Class 10 Computer Applications Sample Question Paper 2 with


Answers

Section – A [40 Marks]


Attempt all questions

Question 1
(a) State one difference between primitive literals float and double.
(b) What is an infinite loop. Write a statement for infinite loop.
(c) Arrange the operators in the order of higher precedence.
(1)++ (2)&& (3)>= (4)%
(d) What is final variable and static variable?
(e) What is number of bytes char occupies. Write its range also.
Answer:
(a)

Float Double

Float occupies 4 bytes in memory Double occupies 8 bytes in memory

(b) An infinite loop occurs when there is no end to the iteration and loop
continues to run indefinitely.
E.g. for
System.out.println(“10”);

(c) ++, % >=, &&

(d) When any variable is declared using final keyword it makes the value
of that variable as constant. It can’t be changed throughout the program.
E.g. final int temp = 90;
Static variable are those which can be used only by static methods. It has
only single copy throughout the class.
(e) 2 bytes and its range is 0 to 65,536

Question 2
(a) What is the difference between keyword parse and function valueOf( ).
Give example of each.
Answer:

Parse ValueOf

It converts a variable from string data type


It converts only from string to int. E.g.
to another.
string a = “12”;
E.g. String a = “12”;
Int b = Integer.valueOf(a);
int b = Integer.parselnt(a);

(b) Write a program code to accept a character input and check whether
its digit using its ASCII code. Print character using suitable message.
Answer:

class abc

public void main( )

scanner sc = new scanner(system.in);

System.out.println("Enter the character to be checked");

char ch = sc.next( ).

charAt(0);

if(ch >=91 && ch <= 97)

System.out.println("Digit is "+ch); else

System.out.println("lts not a digit");

}
(c) What do you mean by block. Give one example.
Answer:
Block is a set of statements in a program.
Eg
{
System.out.printlnC’example of block statement”);
System.out.println(“End of block statement”);
}

(d) State the difference between entry controlled loop and exit controlled
loop.
Answer:

Entry Controlled Loop Exit Controlled Loop

Here condition is checked before entering Here condition is checked after


the loop. entering the loop. First time entering
Eg. while(i<3) in the loop is must.
{ E.g.
//some statements {
} }while (i< 3);

(e) Write two advantages of using function in the program. And explain
role of void in declaring functions?
Answer:
Advantages:
It makes the program divides into multiple modules.
Each module is independent in executing its dedicated task.
It helps in reusability of the code once written.
void makes sure that function doesn’t return anything.

Question 3
(a) What do you mean by function overloading? Explain with example.
Answer:
Function overloading is called when function with the same name is used
multiple times with different arguments.
Example:

class abc

{
public: area(int a);

area(int a, int b);

area(int a, int b, float c);

(b) What is this keyword? Also discuss the significance of it.


Answer:

 This keyword is used as a reference to created object for calling its


methods or member functions.
 It differentiates between instance variables from the local variables
when they have the same names.
(c) Attempt the following
(i) State the two features of a constructor.
Answer:
Constructor is function which has the same name as that of class without
return type. It is called whenever the object is created for the class.

(ii) Write a valid java program code to print the following array in matrix
form with 2 rows and 2 columns.
int mat[ ][ ] = {{2,6},{10,20}};
Answer:

for(int i =0;i<=2;i++)

for(int j =0;j<=2;j++)

System.out.print(mat[i][j]);

System.out.println( );

(iii) State the total size in bytes of the array a[4] of char data type and
p[4] of float data type.
Answer:
char a[4] it occupies 8 bytes.
float p[4] it occupies 16 bytes.
(iv) String abc = “helloR”;
StringBuffer str = new StringBuffer(abc);
int num = str.capacity( );
what will variable num stores the value?
Answer:
22

(v) StringBuffer s1 = new StringBufferC’Robot”);


String s2 = s1 .reverse( );
System.out.println(“s2 =” +s2);
System.out.println(“s1 =” +s1);
Answer:
toboR
toboR

(d) Write a java statement for finding and displaying the position of last
space in string’str’.
Answer:
String str = “Blank”;
int a = str.lastlndexOf(‘ ‘));
System.out.println(a +” is the last position of”);

(e) Which one of the following returns the corresponding primitive data
type object from string object?
(i) Integer
(ii) readLine( )
(iii) valueOf( )
(iv) endsWith( )
Answer:
(i) int
(ii) String
(iii) int
(iv) boolean

(f) What do you mean by Abstraction and information hiding in java?


Answer:
Data abstraction is the act of representing the essential features of the
program without involving in its complexity.
Information hiding in java means data members which can’t be accessed
directly by objects instead it has access via its member functions.

Section – B (60 Marks)


Attempt any four questions from this Section
The answers in this Section should consist of the Programs in either Blue J
environment or any program environment with Java as the base. Each
program should be written using Variable descriptions/Mnemonic Codes
such that the logic of the program is clearly depicted.
Flow-Charts and Algorithms are not required.

Question 4
Write a program to input three sides of a triangle (s1, s2, s3). Using switch
case print whether a triangle is Equilateral, Isosceles, Right angled
triangle or scalene. The program should be used with menu and switch-
case.
Answer:

import java.util.*;

class Triangle

public static void main( )

Scanner sc = new Scanner(System.in);

System.out.println("Enter 3 sides of triangle ");

double s1 = sc.nextDouble( );

double s2 = sc.nextDouble( );

double s3 = sc.nextDouble( );

char ch ='0';

double sq 1 = si* si;

double sq2 = s2*s2;

double sq3 = s3 * s3;

double sum1 = sql + sq2;


double sum2 = sq2 + sq3;

double sum3 = sql + sq3;

if(s1 == s2 &&s2 == s3 )

ch = 'E';

if(s1 == s2 || s2 == s3 || s1 == s3)

ch = I;

if(sum1 == sq3 || sum2 == sq 1 || sum3 == sq2)

ch = 'R';

if(s1 != s2 && s2!=s3) ch = 'S'; switch(ch)

case 'E': System.out.println("Equilateral triangle"); break;

case T: System.out.println("lsoscless triangle"); break;

case 'R': System.out.printlnfRight triangle"); break;

case 'S': System.out.printlnC'Scalene triangle"); break;

default: break;

Question 5
Write a program to find the sum of the following series:
x+x22!+x23!+x44!+…. nterms
Answer:
import java.util.*;
class series

public static void main( )

Scanner sc = new Scanner(System.in);

System.out.println("Enter a number and number of terms");

int x = sc.nextlnt( );

int n = sc.nextlnt( );

int a = 1; int f;

double sum = 0.0;

forfint i =1; i<= n; i++)

f = 1;

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

f = f*j;

sum = sum+(Math.pow(x,a)/f);

a++;

System.out.printlnC'sum: "+sum);

}
Question 6
A number is said to be NEON number if sum of digits of square of a
number is equal t:o the number itself.
Example:
INPUT N = 9, Output Square: 81 (where 8 + 1 = 9 so 9 is NEON number)
Write a program to find such numbers between 10 and 10000.
Answer:

import java.util.*;

class Neon

public static void main( )

int a = 10;

for(int i =10;i<=10000; i++)

intal =a; long sq = a1*a1;

int sum =0;

while(sq >=0)

int k = (int)sq % 10;

sum = sum + k;

sq = sq/10;

if(sum == a)

System.out.println(a);

a++,
}

Question 7
Write a program to perform binary search on the list of 10 integers
entered by user in ascending order to search for an element input by user,
if it’s found display the element along with its position else display the
message “search element not found”.
Answer:

import java.util.*;
class binsearch
{
public static void main( )
{
Scanner sc = new Scanner(System.in);
System.out.priintlnfEnter the 10 no.s in ascending order to be put in the
array");
int[ ] a = new int[10];
for(int i = 0; i< 10; i++)
a[i] = sc.nextlnt( );
System.out.println('Enter the no. to be searched in the array");
int n = sc.nextlnt( );
int start =0,pos=0;
int last =a.length - 1;
int mid = (start + last) / 2;
int found = 0;
while(start <= last)
{
if(n == a[mid])
{
pos = mid; found = 1; break;
}
if(n < a[mid])
mid = last - 1;
if(n > a[mid])
mid = start + 1;
}
if(found == 1)
System.out.println(n+" found at position "+pos);
else
System.out.println(n+" not found");
}
}

Question 8
Write a program to accept a word and convert into lowercase if it is in
uppercase and display the new word by replacing the VOWELS with the
character following it.
ex: Sample intput: VOWEL
Sample output: vpwfi
Answer:
import java.util.*;
class strlol
{
public static void main( )
{
Scanner sc = new Scanner(System.in);
Systenn.out.println("Enter a word");
String strl = sc.next( );
String str = strl .toLowerCase( );
String word = " ";
for(int i = 0; i <- str.length( )-1; i++)
{
char ch = str.charAt(i);
if (ch != 'a' && ch !='e' && ch != T && ch != 'o' && ch!='u')
word = word + ch;
if(ch=='a' || ch == 'e'|| ch == 'i' || ch == 'o' || ch == 'u')
{
ch ++;
word = word + ch;
}
}
System.out.println(word);
}
}

Question 9
Write a program in java to print the following output.

Answer:
import java.util.*;

class pattern

public static void main()

for(int i = 5; i>= 1; i- -)

int a = i-1;

for(int k = 1; k <= 5-i; k++)

System.out.print(" ");

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

System.out.println(j+" ");

if(a > 0)

System.out.println("*");

a- -;

System.out.println ( );

}
Variable Data Type Description

sq1, sq2, sq3 double To find squares of each side

ch char To store the type of triangle.

x, n int Number and number of terms.

f Int Factorial storing

sum double To store sum of series.

sum Int To store sum of digits of neon number.

a[ ] int To store integers in ascending order.

n int Element to be searched.

start,last mid int Positions of index of array

found int Flag to check element is found or not.

strl string For storing string.

ch char To take out each character of string.

word String For storing the required string.

a int Number of rows.


A number is called a Cyclo number if its first and last digit is same. Input a
number and verify whether it is a Cyclo Number or not. For example
52145
Answer:

import java.util.*;

public class Q 8

void main( )

Scanner sc new Scanner (System.in);

int n = 0, d = 0, p = 1;

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

n = sc.nextlnt( );

d = n%10; int cn = n; while ( cn>=10)

cn = cn/10;

if ( d == cn)

System.out.print(n + " is a Cyclo Number " );

else

{
System.out.print(n + " is NOT a Cyclo Number " );

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

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

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

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

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
Circular Prime

import java.util.Scanner;

public class DigitToWord


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

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

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, 6 and 12.
Factors of 12: 1, 2, 3, 4, 6, 12.

import java.util.Scanner;

public class Factors


{
public static void main(String[] args)
{
int n;
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)
{
System.out.println(i);
}
}
}
}
Output:
Enter number=6
1
2
3
6
What is Factors Program?
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, 6 and 12.
Factors of 12: 1, 2, 3, 4, 6, 12.

Floyd Triangle Program in Java


1
23
456
7 8 9 10
11 12 13 14 15

public class FloydTriangle


{
public static void main(String[] args)
{
int k = 1;
for (int i = 1; i <= 5; i++)
{
for (int j = 1; j <= i; j++)
{
System.out.print(k + " ");
k++;
}
System.out.println();
}
}
}

Output:
1
23
456
7 8 9 10
11 12 13 14 15
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.

import java.util.Scanner;

public class GCDProgram


{
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;
}
}
System.out.println("GCD=" + gcd);
}
}
Output:
Enter a=15
Enter b=18
GCD=3
What is Greatest Common Divisor?
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.

What is Greatest Common Divisor 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.

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
What is Happy 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.

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 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
What is Harshad 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.

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
What is Neon 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.

What isNeon Number 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.

In mathematics, 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
What is Niven Number?
In mathematics, 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.

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

import java.util.Scanner;

public class PalindromeNumber


{
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)
{
r = num % 10;
rev = (rev * 10) + r;
num = num / 10;
}
if (n == rev)
{
System.out.println("Palindrome Number");
}
else
{
System.out.println("Not Palindrome Number");
}
}
}

Output:
Enter number=12321
Palindrome Number
What is Palindrome Number?
A palindromic number is a number that remains the same when its digits
are reversed. Like 16461, for example,
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.

import java.util.Scanner;

public class PerfectNumber


{
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)
{
System.out.println("Perfect Number");
}
else
{
System.out.println("Not Perfect Number");
}
}
}

Output:
Enter number=6
Perfect Number
What is Perfect Number ?
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.

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

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(System.in);
System.out.print("Enter number=");
n = sc.nextInt();
while (n > i)
{
if (n % 2 == 0)
{
flag = false;
break;
}
i++;
}
if (flag)
{
System.out.println("Number is prime.");
}
else
{
System.out.println("Number is not prime.");
}
}
}

Output:
Enter number=17
Number is prime.
What is 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.

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


import java.util.Scanner;

public class ReverceNumber


{
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("Reverce of Number=" + rev);
}
}

Output:
Enter number=1234
Reverce of Number=4321
What is Reverse Number?
If a number=1234, then reverse of number is 4321.

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
What is Special 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 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
What is Spy Number?
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.

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
What is Twin 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.

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

Output:
Enter number=23456
Unique Number
What is Unique 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.

What is Unique Number 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.

___________________________________________________________________________
____________
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
What is 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.

___________________________________________________________________________
_______________

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
What is 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.

___________________________________________________________________________
_________

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
What is Magic 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.

What is Magic Number 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.

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
What isPronic 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.

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
What is Ugly Number?
A number is said to be an Ugly number if positive numbers whose prime
factors only include 2, 3, 5.

A number is called a Cyclo number if its first and last digit is same.
Input a number and verify whether it is a Cyclo Number or not. For example
52145
Answer:
import java.util.*;
public class Q 8
{
void main( )
{
Scanner sc new Scanner (System.in);
int n = 0, d = 0, p = 1;
System.out.print("Enter a number : ");
n = sc.nextlnt( );
d = n%10; int cn = n; while ( cn>=10)
{
cn = cn/10;
}
if ( d == cn)
{
System.out.print(n + " is a Cyclo Number " );
}
else
{
System.out.print(n + " is NOT a Cyclo Number " );
}
}
}

You might also like