You are on page 1of 23

Ques7- Write a program to check whether a

number is perfect or not.


import java.io.*;
class perfect
{
public static void main(String args[])throws IOException
{
InputStreamReader read=new InputStreamReader(System.in);
BufferedReader in=new BufferedReader(read);
System.out.println("Enter a number");
int a=Integer.parseInt(in.readLine());
int i; int s=0;
for(i=1;i<a;i++)
{
if(a%i==0)
{
s=s+i;
}
if(s==a)
{
System.out.println("number is perfect");
}
else
{
System.out.println("number is not perfect");
}
}
}
}

Algorithm-

1. Accept a number a from the console.


2. Loop startsfor(i=1;i<a;i++)
{
if(a%i==0)

// to find numbers divisible by console

number
{
s=s+i;

//to find sum of factors

3. if(s==a)
// to check the number formed by above
calculations is equal
to original number
4.If the condition is true then display(number is perfect)
else (number is
not perfect)
5. Stop.

Ques8- Write a program to check whether a


number is neon or not.
import java.io.*;
class neon
{
public static void main(String args[])throws IOException
{
InputStreamReader read=new InputStreamReader(System.in);
BufferedReader in=new BufferedReader(read);
System.out.println("Enter a number");
int a=Integer.parseInt(in.readLine());

int p;int s;s=0;int d;


p=a*a;
while(p!=0)
{
d=p%10;
s=s+d;
p=p/10;
}
if(s==a)
System.out.println("number is neon");
else
System.out.println("number is not neon");
}
}

Algorithm1. Accept a number a from the user.


2. p=a*a;
// to find the square of the number
3. Loop startswhile(p!=0)
{
d=p%10;

// to find last digit of the square of the

number
s=s+d;

the number
p=p/10;
the last digit

// to find sum of last digits of the square of


// to find the remaining digit after removing

4. if(s==a)
// to check the number got after above
calculations is equal to

original number
5. If the condition is true then display(number is neon)
else (number is
not neon).
6. Stop.

Ques9-Write a program to check whether a


number is automorphic or not.
Import java.io.*;
class automorphic
{
public static void main(String args[])throws IOException
{
InputStreamReader read=new InputStreamReader(System.in);
BufferedReader in=new BufferedReader(read);
int i; int p; int c=1;
System.out.println("Enter a number");
int a=Integer.parseInt(in.readLine());
p=a*a;
for(i=a;i>0;i/=10)
{
c=c*10;
}
if(p%c==a)
System.out.println("number is automorphic");
else
System.out.println("number is not automorphic");
}
}

Algorithm1. Accept a number a from the user.


2. p=a*a; // to find the square of the accepted number
3. Loop startsfor(i=a;i>0;i/=10)
{

// to multiply with 10 each time the number of


digits In original
number
c=c*10;

4. if(p%c==a) // to find and check the number formed by


the digit present at
last of the square of the number which
are equal to the
digits present in original number
5. If the above condition is true then display (number is
automorphic)
else (number is not automorphic).
6. Stop.

Ques-10 Write a program to check whether a


number is prime or not.
import java.io.*;
class prime
{
public static void main(String args[])throws IOException
{
InputStreamReader read=new InputStreamReader(System.in);
BufferedReader in=new BufferedReader(read);
int a,i,c;c=0;
System.out.println("Enter a number");
a=Integer.parseInt(in.readLine());
for(i=2;i<a;i++)
{
if(a%i==0)
c=1;
}
if(c==0)
System.out.println("number is prime");
else
System.out.println("number is not prime");
}
}

Algorithm1. Accept a number a from the user.


2. Loop startsfor(i=2;i<a;i++)

// to check the number is divisible by


number other than 1
and itself
c=1;
// acts as a flag
if(a%i==0)

3. if(c==0)
// to check the value of c
4. If the above condition is true then display (number is
prime) else
(number is not prime).
5. Stop.

Ques11-

Write a program to enter a number and


check whether a number is Armstrong or not . A
number is Armstrong if the sum of cubes of digits
is equal to the original number.
import java.io.*;
class Armstrong
{
public static void main(String args[])throws IOException
{
InputStreamReader read=new InputStreamReader(System.in);
BufferedReader in=new BufferedReader(read);
int b;int s;s=0;
System.out.println("Enter a number");
int a=Integer.parseInt(in.readLine());
int n;n=a;
while(a>0)
{
b=a%10;
s=s+b*b*b;
a=a/10;
}

if(n==s)
System.out.println("Armstrong Number");
else
System.out.println("Not Armstrong Number");
}
}

Algorithm1. Accept a number a from the console.


2. Transferring the value of a to a variable n .
3. Loop startswhile(a>0)
{
b=a%10;
s=s+b*b*b;
a=a/10;

// to find last digit of the number


// to find sum of cube of digits
// to find the remaining digit after
removing last digit

}
4. if(n==s)

// to check the no. formed after above


calculations is equal to original no.
5. If condition is true then display(Armstrong Number)
else (Not Armstrong Number)

6. Stop.

Ques 12- Write a program to accept a number and


check whether it is palindrome or not.
import java.io.*;
class nupallindrome
{
public static void main(String args[])throws IOException
{
InputStreamReader read=new InputStreamReader(System.in);
BufferedReader in=new BufferedReader (read);
int d; int r;r=0;
System.out.println("Enter a number");
int a =Integer.parseInt(in.readLine());
int k;k=a;
while(a>0)
{
d=a%10;
r=r*10+d;
a=a/10;
}
if(k==r)
System.out.println("Number is pallindrome");

else
System.out.println("Nubmer is not pallindrome");
}
}

Algorithm1. Accept a number from the user and stored in variable


a.
2. Value of a is assigned to k.
3. Loop executeswhile(a>0)

// loop runs till value of a is

greater than 0
{

// to find the last digit of the


number and stores in d
d=a%10;

r=r*10+d;

and d is added

// a variable r is multiplied with 10

to it so as to convert d to
tens digit
a=a/10;

// number is taken after removing

the last digit


}

4. if(k==r)
equal to new

// checks whether original value is


number stored in r

System.out.println("Number is pallindrome"); // if above condition

is true than
print it is
palindrome
else
System.out.println("Nubmer is not pallindrome"); // if condition is

false then print


number is not palindrome
5. Stop.

Ques13- Write a program to accept two numbers


and check whether they are twin prime or not. Two
numbers are said to be twin prime if they have no

factor other than 1 and the number itself and


differ by 2.
import java.io.*;
class twinprime
{
public static void main(String args[])throws IOException
{
InputStreamReader read=new InputStreamReader(System.in);
BufferedReader in=new BufferedReader (read);
int i; int j;int p; int t;p=0;t=0;
System.out.println("Enter first number");
int a =Integer.parseInt(in.readLine());
System.out.println("Enter second number");
int b =Integer.parseInt(in.readLine());
for(i=2;i<a;i++)
{
if(a%i==0)
p=1;
}
for(j=2;j<b;j++)
{
if(b%j==0)
t=1;
}
if(p==0 && t==0 && Math.abs(a-b)==2)

System.out.println("Twin Prime");
else
System.out.println("Not Twin Prime");
}
}

Algorithm1. Accept two numbers from the user.


2. Loop executes for first numberfor(i=2;i<a;i++)
{

// checks whether it divisible by number


other than 1 and
if(a%i==0)

itself
p=1;

// if above condition is true then assign 1

to p
}

3. Loop executes for second number-

for(j=2;j<b;j++)
{
if(b%j==0)

// checks whether it divisible by

number other than 1


and itself
t=1;

// if above condition is true then

assign 1 to t

4. if(p==0 && t==0 && Math.abs(a-b)==2)


numbers are

// checks whether
prime or not

because they will only be


prime when
value of p and t will be
original value i.e. 0 and
checks whether their
difference
is 2 or not
System.out.println("Twin Prime");

// if above condition is

true then print


twin prime
else
System.out.println("Not Twin Prime");

false then print

// if above condition is

not twin
prime
5. Stop.

Ques14- Write a program to accept a word and


display the new word after removing all the zeros.
import java.io.*;
class zero
{
public static void main(String args[])throws IOException
{
InputStreamReader read=new InputStreamReader(System.in);
BufferedReader in=new BufferedReader (read);
System.out.println("Enter a number");
int a =Integer.parseInt(in.readLine());
int p; int d; int r;r=0;
while(a>0)
{
d=a%10;
r=r*10+d;
a=a/10;
}
while(r>0)

{
p=r%10;
if(p!=0)
{
System.out.print(p);
}
r=r/10;
}
}
}

Algorithm1. Accept a number from the user.


2. Convert the number to its opposite i.e. 50090 to 09005
by executing the
loopwhile(a>0)
{

// last digit of the number is found

d=a%10;

// last digit is converted to tens form

r=r*10+d;
a=a/10;

// number is taken after removing last

digit
}

3. Second loop runswhile(r>0)

// loop runs till value of r is

greater than 0
{
p=r%10;
if(p!=0)

// last value of the number is taken


// checks whether last digit is not 0

{
System.out.print(p);

// if above condition is true than print

the number
}
r=r/10;

// find the remaining number after

removing the last


digit
}

4. Stop.

Ques15- Write a program to accept a number and


check whether it is special or not.
import java.io.*;
class special
{
public static void main(String args[])throws IOException
{
InputStreamReader read=new InputStreamReader(System.in);
BufferedReader in=new BufferedReader (read);
int d; int f; int i;int s; s=0;int n;f=1;
System.out.println("Enter a number");
int a =Integer.parseInt(in.readLine());
n=a;
while(n>0)
{
d=n%10;
for(i=1;i<=d;i++)
{
f=f*i;
}
s=s+f;
n=n/10;
f=1;
}
if(s==a)

System.out.println("Number is special");
else
System.out.println("Number is not special");
}
}

Algorithm1. Accept a number from the user.


2. Assign the number to a variable n.
3. Loop executeswhile(n>0)

// runs till value of n is greater

than 0
{
d=n%10;

// to find the last digit of the

number
for(i=1;i<=d;i++)
{

// for loop executes

// multiplies each time from 0 to the

f=f*i;

number stored
in d or finds the factorial of the
number stored in d
}

// adds and stores the factorial to s

s=s+f;

// finds the remaining number by


removing last digit of
n=n/10;

the original number


f=1;

// assign 1 to f

4. if(s==a)
stored in s is

// checks whether the number


equal to original

number or not
System.out.println("Number is special");

// if above condition is

true then print


number is
special
else
System.out.println("Number is not special"); // if above condition is

false then
print
number is not special
5. Stop.

Ques16- Write a program to accept a number and


display the new number in ascending order.
import java.io.*;
class nuascend
{
public static void main(String args[])throws IOException
{
InputStreamReader read=new InputStreamReader(System.in);
BufferedReader in=new BufferedReader (read);
int d; int i;int m;
System.out.println("Enter a number");
int a =Integer.parseInt(in.readLine());
for(i=0;i<10;i++)
{
m=a;
while(m>0)
{
d=m%10;
if(d==i)
{
System.out.print(d);
}
m=m/10;

}
}
}
}

Algorithm1. Accept a number from the user.


2. Loop executesfor(i=0;i<10;i++)
{
m=a;
while(m>0)

// assigns the original number to m


// while loop runs till value of m is greater than

0
{
d=m%10;

// last digit of the number is taken out

if(d==i)

// checks whether last digit is equal to i

{
System.out.print(d);
d
}

// if above condition is true then display value of

m=m/10;
}
}

3. Stop.

// then number is taken after removing last digit

You might also like