1.
Write a program to accept a number and check whether the number is
perfect or not.
// To check whether a number is perfect or not
import [Link].*;
public class Rahul1
{
public static void main(String args[])
{
Scanner in = new Scanner([Link]);
int i,n,s=0;
[Link]("Enter a number");
n=[Link]();
for(i=1;i<n;i++)
{
if(n%i==0)
s=s+i;
}
if(s==n)
[Link]( i + " is a perfect number");
else
[Link]( i + " is not a perfect number");
}
}
Output:-
2. Write a program in Java to enter a three digit number and check whether the
number is an Armstrong number or not.
// To check whether a number is Armstrong or not
public class Rahul2
{
public static void main(int n)
{
int i,num,p=0;
num=n;
while(n>0)
{
i=n%10;
p=p+i*i*i;
n=n/10;
}
if(num==p)
[Link]("The number " + num +" is an Armstrong Number");
else
[Link]("The number " + num +" is not an Armstrong Number");
}
}
Output:-
3. Write a program to input a number and check whether the number is an
Automorphic or not.
// To check Automorphic number
public class Rahul3
{
public static void main(int num)
{
int m,r=0,s;
s=num;
m=num*num;
do
{
if(num%10!=m%10)
{
r=1;
break;
}
else
{
num=num/10;
m=m/10;
}
}
while(num>0);
if(r==0)
[Link](s+" is an Automorphic Number");
else
[Link](s+ "is Not an Automorphic Number");
}
}
Output:-
4. Write a program to display all the prime numbers between 1 and 100.
// To display all prime numbers between 1 and 100
public class Rahul4
{
public static void main(String args[])
{
int i,p;
[Link]("The prime numbers between 1 and 100 are:");
for(i=1;i<=100;i++)
{
int c=0;
for(p=1;p<=i;p++)
{
if(i%p==0)
c=c+1;
}
if(c==2)
[Link](i);
}
}
}
Output:-
5. Write a program to display twenty simultaneous prime numbers, staring
from a given number, entered by the user.
// To display 20 prime numbers
import [Link].*;
public class public class Rahul5
{
public static void main(String args [])
{
Scanner in = new Scanner([Link]);
int a,b,c,n,k=1;
[Link]("Enter the first number");
n=[Link]();
[Link]("Twenty prime numbers from "+ n +":");
do
{
c=0;
for(a=1;a<=n;a++)
{
if(n%a==0)
c=c+1;
}
if(c==2)
{
[Link](n+" ");
k=k+1;
}
n=n+1;
}
while(k<=20);
}
}
Output:-
6. Write a program to accept 10 different numbers in a single dimensional
array (SDA). Now, enter a number and search whether the number is
present or not in the list of array elements by using the linear search
technique.
// To search a number in SDA by using Linear Search technique
import [Link].*;
public class Rahul6
{
public static void main(String args[])
{
Scanner in = new Scanner([Link]);
int i,k=0,ns;
int m[]=new int[10];
for(i=0;i<10;i++)
{
[Link]("Enter the number in the cell :");
m[i]=[Link]();
}
[Link]("Enter the number to be searched :");
ns=[Link]();
for(i=0;i<10;i++)
{
if(m[i]==ns)
k=1;
}
if(k==1)
[Link]("The number is present");
else
[Link]("The number is not present");
}
}
Output:-
[Link] a program to accept 10 different numbers in a single dimensional
array (SDA). If the number is present then display the message “Search
successful otherwise, display “ Search unsuccessful.
// To search a number in SDA by using Binary Search technique
import [Link].*;
public class Rahul7
{
public static void main(String args[])
{
Scanner in = new Scanner([Link]);
int i,k=0,p=0,ns,lb=0,ub=9;
int m[]=new int[10];
for(i=0;i<10;i++)
{
[Link]("Enter the number in ascending order :");
m[i]=[Link]();
}
[Link]("Enter the number to be searched :");
ns=[Link]();
while(lb<=ub)
{
p=(lb+ub)/2;
if(m[p]<ns)
lb=p+1;
if(m[p]>ns)
ub=p-1;
if(m[p]==ns)
{
k=1;
break;
}
}
if(k==1)
[Link]("The search successful and the number is present");
else
[Link]("The search successful and the number is not present");
}
}
Output:-
[Link] a program to accept 10 different numbers in a Single Dimensional
Array (SDA). Arrange the numbers in ascending order by using ‘Selection
Sort’ technique and display them.
// To arrange the numbers by using Selection Sort technique
import [Link].*;
public class Rahul8
{
public static void main(String args[])
{
Scanner in = new Scanner([Link]);
int i,j,t,min;
int m[]=new int[10];
for(i=0;i<10;i++)
{
[Link]("Enter the number in the cell :");
m[i]=[Link]();
}
for(i=0;i<9;i++)
{
min=i;
for(j=i+1;j<10;j++)
{
if(m[j]<m[min])
min=j;
}
t=m[i];
m[i]=m[min];
m[min]=t;
}
[Link]("The number is arranged in ascending order are: ");
for(i=0;i<10;i++)
[Link](m[i]);
}
}
Output:-
9. Write a program to accept a set of 10 integers in a Single Dimensional
Array (SDA). Sort the numbers in ascending order by using the ‘Bubble Sort’
technique. Display the sorted array.
// To arrange the numbers by using Bubble Sort technique
import [Link].*;
public class Rahul9
{
public static void main(String args[])
{
Scanner in = new Scanner([Link]);
int i,j,t;
int m[]=new int[10];
for(i=0;i<10;i++)
{
[Link]("Enter the number in the cell :");
m[i]=[Link]();
}
for(i=0;i<9;i++)
{
for(j=0;j<(9-i);j++)
{
if(m[j]>m[j+1]);
{
t=m[j];
m[j]=m[j+1];
m[j+1]=t;
}
}
}
[Link]("The number is arranged in ascending order are: ");
for(i=0;i<10;i++)
[Link](m[i]);
}
}
Output:-
10. Write a program to accept roll number and the marks secured in Computer
Application by students by 10 students of a class in a Single Dimension Array.
Sort the marks obtained in descending order.
// To display the marks in descending order
import [Link].*;
public class Rahul10
{
public static void main (String args[])
{
Scanner in = new Scanner([Link]);
int i,j,t;
int roll[] = new int[10];
int marks[] = new int[10];
[Link]("Enter roll number and marks");
for(i=0;i<10;i++)
{
roll[i] = [Link]();
marks[i] = [Link]();
}
for(i=0;i<9;i++)
{
for(j=0;j<(9-i);j++)
{
if(marks[j]<marks[j+1])
{
t=roll[j];
roll[j]=roll[j+1];
roll[j+1]=t;
t=marks[j];
marks[j]=marks[j+1];
marks[j+1]=t;
}
}
}
[Link]("Roll No.\t"+"Marks");
for(i=0;i<10;i++)
[Link](roll[i]+"\t"+marks[i]);
}
}
Output:-
11. Write program accept a String and change the case of each letter of the
String.
// To convert the case of the string
import [Link].*;
public class Rahul11
{
public static void main (String args[])
{
Scanner in = new Scanner([Link]);
int a,i,p;
String st,st1="";
char chr,chr1;
[Link]("Enter your string:");
st=[Link]();
p=[Link]();
for(a=0;a<p;a++)
{
chr=[Link](a);
if(chr>='a'&&chr<='z')
{
chr1=[Link](chr);
st1=st1+chr1;
}
else if(chr>='A'&&chr<='Z')
{
chr1=[Link](chr);
st1=st1+chr1;
}
else
st1=st1+chr;
}
[Link]("The new string after converting the case of each letter: ");
[Link](st1);
}
}
Output:-
12. Write a program to accept a name and display the initials along with the
surname.
//To enter a full name and to print initials with surname
import [Link].*;
public class Rahul12
{
public static void main (String args[])
{
String st,sn,st1="",st2="";
int a,b;
char chr;
Scanner in = new Scanner([Link]);
[Link]("Enter the full name: ");
st=[Link]();
st=' '+st;
b=[Link](' ');
sn=[Link](b);
for(a=0;a<b;a++)
{
chr=[Link](a);
if(chr==' ')
st1=st1+[Link](a+1)+'.';
}
st2=st1+sn;
[Link]("Initials with surname: ");
[Link](st2);
}
}
Output:-
13. Write a program to accept string and check whether the string is Unique or
not.
// A program to check Unique String
import [Link].*;
public class Rahul13
{
public static void main (String args[])
{
Scanner in = new Scanner([Link]);
String str;
int a,b,p,k=1;
[Link]("Enter a word: ");
str=[Link]();
str=[Link]();
p=[Link]();
for(a=0;a<p;a++)
{
for(b=a+1;b<p;b++)
if([Link](a)==[Link](b))
k=0;
}
if(k==1)
[Link](str+" is a Unique String");
else
[Link](str+" is not a Unique String");
}
}
Output:-
14. Write a program in Java to accept a word and check the whether the word
is Palindrome or not.
// To check whether a word is Palindrome or not
public class Rahul14
{
public static void main (String word)
{
String word1="";
int a,b;
char chr;
b=[Link]();
for(a=b-1;a>=0;a--)
{
chr=[Link](a);
word1=word1+chr;
}
if([Link](word1))
[Link]("Palindrome");
else
[Link]("Not a Palindrome");
}
}
Output:-
15. Write a program in Java to accept a word and display the same in Pig Latin
form.
// To display a Pig Latin word
public class Rahul15
{
public static void main (String str)
{
int i,j;
String str1,str2;
char b=0;
i=[Link]();
[Link]("The Pig Latin form of the given string: ");
for(j=0;j<i;j++)
{
b=([Link](j));
if(b=='a'||b=='e'||b=='i'||b=='o'||b=='u'||b=='A'||b=='E'||b=='I'||b=='O'||b=='U')
break;
}
str1=[Link](j,i);
str2=[Link](0,j);
[Link](str1+str2+"ay");
}
}
Output:-
16. Write a program to accept a number and check whether the number is
prime or not.
// To check a prime number by using method
import [Link].*;
public class Rahul16
{
int check(int n)
{
int i,c=0,f=0;
for(i=1;i<=n;i++)
{
if(n%i==0)
c=c+1;
}
if(c==2)
f=1;
return(f);
}
public static void main(String args[])
{
int b,c;
Scanner in = new Scanner([Link]);
[Link]("Enter your number");
b=[Link]();
Prime1 ob= new Prime1();
c=[Link](b);
if(c==1)
[Link](b + " is a prime number");
else
[Link](b + " is not a prime number");
}
}
Output:-
17. Write a program to accept a number and check whether the number is
palindrome or not by using the method name reverse (int n).
// To check a palindrome number by using function
import [Link].*;
public class Rahul17
{
int reverse(int n)
{
int d,r=0;
do
{
d=n%10;
r=r*10+d;
n=n/10;
}
while(n!=0);
return (r);
}
public static void main(String args[])
{
Scanner in = new Scanner([Link]);
int k,a,c;
Palin ob=new Palin();
[Link]("Enter your number");
a=[Link]();
c=a;
k=[Link](a);
if(k==c)
[Link]("The number " +c + " is a palindrome");
else
[Link]("The number " +c + " is not a palindrome");
}
}
Output:
18. Write a program to accept two numbers and check whether they are twin
prime or not.
// To check twin prime numbers by using function
import [Link].*;
public class Rahul18
{
int prime(int n)
{
int i,c=0,f=0;
for(i=1;i<=n;i++)
{
if(n%i==0)
c=c+1;
}
if(c==2)
f=1;
return(f);
}
public static void main(String args[])
{
Scanner in = new Scanner([Link]);
int a,b,d,e;
Twinp ob=new Twinp();
[Link]("Enter your first number");
a=[Link]();
[Link]("Enter your second number");
b=[Link]();
d=[Link](a);
e=[Link](b);
if((d==1&&e==1)&&(a-b==2||b-a==2))
[Link]("The number " + a + " and " + b + " are twin primes");
else
[Link]("The number " + a + " and " + b + " are not twin primes");
}
}
Output:-
19. Write a class with the name volume using function overloading that
computers the volume of a cube, a sphere and a cuboid.
// To find the volume of a cube, sphere and cuboid using function overloading
import [Link].*;
public class Rahul19
{
double vc=0.0D,vs=0.0D,vcd=0.0D;
void volume(int s)
{
vc=s*s*s;
[Link]("The volume of a cube =" +vc);
}
void volume(float r)
{
vs=4/3*22/7*r*r*r;
[Link]("The volume of a sphere =" +vs);
}
void volume(int l,int b,int h)
{
vcd=l*b*h;
[Link]("The volume of a cuboid =" +vcd);
}
public static void main(String args[])
{
Scanner in = new Scanner([Link]);
int s,l,b,h;
float r;
[Link]("Enter the value of side of a cube, radius of sphere, sides of a
cuboid");
s=[Link]();
r=[Link]();
l=[Link]();
b=[Link]();
h=[Link]();
Compute ob= new Compute();
[Link](s);
[Link](r);
[Link](l,b,h);
}
}
Output:-
20. Write a program to accept a numbers in a Single Dimension Array. Pass this
array to a method name sort (int m[]). Arrange all the numbers in ascending
order and display them.
// To arrange the numbers in ascending order by using function
import [Link].*;
public class Rahul20
{
void sort(int m[])
{
int i,j,t;
for(i=0;i<9;i++)
{
for(j=0;j<(9-i);j++)
{
if(m[j]>m[j+1])
{
t=m[j];
m[j]=m[j+1];
m[j+1]=t;
}
}
}
[Link]("The numbers are arranged in ascending order as");
for(i=0;i<10;i++)
[Link](m[i]);
}
public static void main(String args[])
{
Scanner in = new Scanner([Link]);
Sorting ob= new Sorting();
int i;
int n[]=new int[10];
for(i=0;i<10;i++)
{
[Link]("Enter your marks in the cell" +(i+1)+":");
n[i]=[Link]();
}
[Link](n);
}
}
Output:-