Sie sind auf Seite 1von 104

Ques1-Write a program to calculate electricity bill

of a consumer according to given table:Unit consumed

Charge

Up to 100 units
paise/unit

80

For next 100 units

1 Re/unit

More than 200 units

1,25 Rs/unit

Every consumer has to pay additional charges of


Rs 50 as service charges.
import java.io.*;
public class electricity
{
void charge(int u)
{
float c,a;c=0;a=0;
if(u<=100)
{
c=(u*80)/100;
a=c+50;
}
if((u>100)&&(u<=200))
{
c=((100*80)+(u-100)*100)/100;
a=c+50;
}
if(u>200)

{
c=((100*80)+(100*100)+(u-200)*125)/100;
a=c+50;
}
System.out.println("unit consumed"+u);
System.out.println("charge=Rs"+c);
System.out.println("additional charges=Rs50");
System.out.println("amount to be paid=Rs"+a);
}
public static void main(String args[])throws IOException
{
int un;un=0;
InputStreamReader read=new InputStreamReader(System.in);
BufferedReader in=new BufferedReader(read);
electricity obj=new electricity();
System.out.println("Enter the unit consumed");
un=Integer.parseInt(in.readLine());
obj.charge(un);
}}}

Algorithm1. A function is created named charge.

2. Formal argument are taken as int u.


3. if(u<=100)
to 100 then

// if value of u is less or equal


perform written below

calculations
{
c=(u*80)/100;

// store the money that is to be

charged
a=c+50;

// to store total amount to be

paid with extra


charges
}

4. Similar calculations are performed as stated in step 3


for different
values of u.
5. Main method is created.
6. Object of the class is createdelectricity obj=new electricity();

7. Function is called.
obj.charge(un);

8. Results are displayed.

Ques2-Write a program to input total cost and


compute and display the amount to be paid by the
customer after availing the discount according to
given table:Total cost in Rs
in %

discount

Less than 2000

5%

2001 to5000

25%

5001 to 10000

35%

Above 10000

50%

import java.io.*;
class discount
{
public static void main(String args[])throws IOException
{
InputStreamReader read=new InputStreamReader(System.in);
BufferedReader in=new BufferedReader(read);
int c;
double pay=0,d=0;
System.out.println("Enter total cost");
c=Integer.parseInt(in.readLine());
if(c<=2000)
d=(double)5/100*c;
if((c>2000)&&(c<=5000))
d=(double)25/100*c;
if((c>5000)&&(c<=10000))
d=(double)35/100*c;

if(c>10000)
d=(double)50/100*c;
pay=c-d;
System.out.println("discount"+d);
System.out.println("amount to be paid"+pay);
}
}

Algorithm1. Accept total cost from the user and store it in variable
c.
2. if(c<=2000)
2000 then written

// if total cost is less or equal to


below

calculations are

made
d=(double)5/100*c;

3. Similarly different calculations are made based on


different values of c.
4. Amount to be paid is calculated aspay=c-d;

5. Results are displayed.

Ques3- In an exam the grades are given according


to the marks obtained. Write a program to display
grades accordingly:Marks
Grades
80% and above
Distinction
60% and more but less than 80%
Division

First

45% and more but less than 60%


Division

Second

40% and more but less than 45%

Pass

Less than 40%


Promotion not granted
import java.io.*;
class exam
{
public static void main(String args[])throws IOException
{
InputStreamReader read=new InputStreamReader(System.in);
BufferedReader in=new BufferedReader(read);
int m;
System.out.println("Enter marks");
m=Integer.parseInt(in.readLine());

if(m>=80)
System.out.println("Distinction");
if((m>=60)&&(m<80))
System.out.println("First Division");
if((m>=45)&&(m<60))
System.out.println("Second Division");
if((m>=40)&&(m<45))
System.out.println("Promotion not granted");
}
}

Algorithm1. Accept the marks in the variable m.


2. if(m>=80)

// if marks are greater than or

equal to 80 then award


the category as given below
System.out.println("Distinction");

3. Similar procedure is adopted and category is given


according to values
Of m.
4. Results are displayed.

Ques4-State Bank offers following rate of discount


accordingly:Time
Rate
Up to 6 months
7%
More than 6 months and Up to 12 months

8%

More than 12 months and Up to 36 months


More than 36 months
10%
The amt. after m months is calculated by usingA=P(1+R/100)N
Write a program to compute above.
import java.io.*;
class interest

9%

{
public static void main(String args[])throws IOException
{
InputStreamReader read=new InputStreamReader(System.in);
BufferedReader in=new BufferedReader(read);
int p,m,ch;
float n;
System.out.println("Enter the principal");
p=Integer.parseInt(in.readLine());
System.out.println("Enter 1 for 6 months,2 for more than 6-12 months");
System.out.println("Enter 3 for 12-36 months,4 for more than 36 months");
System.out.println("Enter choice");
ch=Integer.parseInt(in.readLine());
m=Integer.parseInt(in.readLine());
n=(float)m/12;
switch(ch)
{
case 1:
System.out.println("Sum is"+p*(Math.pow(1.07,n)));
break;
case 2:
System.out.println("Sum is"+p*(Math.pow(1.08,n)));
break;
case 3:
System.out.println("Sum is"+p*(Math.pow(1.09,n)));
break;
case 4:
System.out.println("Sum is"+p*(Math.pow(1.1,n)));
break;

default:
System.out.println("Wrong choice");
}}

Algorithm1. Accept the principal and stored in variable p.


2. Then choice is asked based on different time period.
3. switch case starts.
4. case 1:
System.out.println("Sum is"+p*(Math.pow(1.07,n))); // calculations

are made and


results are displayed
based on different choices
break;

5. Similar steps are made based on different choices and


results are displayed.

Ques 5- Write a program to accept a number and


find the sum of its digit.
import java.io.*;
class sum
{
public static void main(String args[])throws IOException
{
InputStreamReader read=new InputStreamReader(System.in);
BufferedReader in=new BufferedReader (read);
int d; int s;s=0;
System.out.println("Enter a number");
int a =Integer.parseInt(in.readLine());
while(a>0)

{
d=a%10;
s=s+d;
a=a/10;
}
System.out.println("sum of the number is"+s);
}
}

Algorithm1. Accept a number from the user.


2. Loop executeswhile(a>0)

// loop runs till value of a is greater

than 0
{
d=a%10;

and stores ind

// last digit is taken from the number

s=s+d;

// adds d to s whose initial value is

0
a=a/10;

// number is taken after removing last

digit
}

3. After terminating the loop it displays the sum.

Ques6- Write a program to determine the root of


the coefficients of the equation given by the user
as a. b and c.
import java.io.*;
class root
{
public static void main(String args[])throws IOException
{
InputStreamReader read=new InputStreamReader(System.in);
BufferedReader in=new BufferedReader (read);
double d;
System.out.println("Enter a");
int a =Integer.parseInt(in.readLine());
System.out.println("Enter b");

int b =Integer.parseInt(in.readLine());
System.out.println("Enter c");
int c =Integer.parseInt(in.readLine());
d=Math.sqrt(b*b-4*a*c);
if(d>0)
System.out.println("Unequal");
if(d==0)
System.out.println("Equal");
if(d<0)
System.out.println("Imaginary");
}
}

Algorithm1. Accept the values from the user.


2. d=Math.sqrt(b*b-4*a*c);

// discriminant is found

3. Following conditions are checked and results are


displayed according to it.
if(d>0)

System.out.println("Unequal");
if(d==0)
System.out.println("Equal");
if(d<0)
System.out.println("Imaginary");

4. Stop.

Ques7- Write a program to calculate the charge on


parcels according toUpto 10 kg

Rs 20 / kg

For next 20 kg

Rs 10/kg

For next 20 kg

Rs 8/kg

More than 50 kg

Rs 5/kg

import java.io.*;
class parcel
{
public static void main(String args[])throws IOException
{
InputStreamReader read=new InputStreamReader(System.in);

BufferedReader in=new BufferedReader (read);


int c=0;
System.out.println("Enter weight of the parcel");
int w=Integer.parseInt(in.readLine());
if(w<=10)
c=w*20;
if((w>10)&&(w<=30))
c=200+(w=10)*10;
if((w>30)&&(w<=50))
c=200+200+(w=30)*8;
if(w>50)
c=c+200+200+260+(w-50)*5;
System.out.println("charge is "+c);
}
}

Algorithm1. Accept the weight from the user.

2. Calculate charge according to given conditions and then


display the resultif(w<=10)
c=w*20;
if((w>10)&&(w<=30))
c=200+(w=10)*10;
if((w>30)&&(w<=50))
c=200+200+(w=30)*8;
if(w>50)
c=c+200+200+260+(w-50)*5;
System.out.println("charge is "+c);

3. Stop

Ques8- 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 numberfor(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");

// if above condition is

false then print


not twin
prime
5. Stop.

Ques9- 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)
{
d=a%10;
r=r*10+d;
a=a/10;

digit
}

// last digit of the number is found


// last digit is converted to tens form
// number is taken after removing last

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.

Ques10- 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.

Ques11- 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)
0
{

// assigns the original number to m


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

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;

// then number is taken after removing last digit

}
}

3. Stop.

Ques12- Write a program to accept 10 different


numbers and display them in ascending order
using Selection Sort technique.
import java.io.*;
class sort
{
public static void main(String args[])throws IOException
{
InputStreamReader read=new InputStreamReader(System.in);
BufferedReader in=new BufferedReader(read);
int i,j; int min;
int t;
int m[]=new int[10];

for(i=0;i<10;i++)
{
System.out.println("Enter number");
m[i]=Integer.parseInt(in.readLine());
}
for(i=0;i<9;i++)
{
min =i;
for(j=i+1;j<10;j++)
{
if(m[min]>m[j])
min=j;
}
t=m[i];
m[i]=m[min];
m[min]=t;
}
System.out.println("numbers are in ascending order");
for(i=0;i<10;i++)
System.out.println(m[i]); }}

Algorithm1. Accept the numbers in an array m[].


2. First number is taken in variable min.
3. Then second loop runs to check the number which is
smaller than the number stored in min.
4. Then if the number is found then

min=j;

that number is stored in min.

5. Then swapping is executedt=m[i]


m[i]=m[min];
m[i]=m[min];

// and first number and number smaller

than it
interchange their position
m[min]=t;

6. Similarly again and again loop is executed till numbers


are arranged in ascending order.

Ques13- Write a program to accept 10 numbers


and arrange them in ascending order using Bubble
Sort technique.
import java.io.*;
class bubble
{
public static void main(String args[])throws IOException
{
InputStreamReader read=new InputStreamReader(System.in);
BufferedReader in=new BufferedReader(read);
int i,j,t;
int m[]=new int[10];
for(i=0;i<10;i++)
{
System.out.println("Enter number");
m[i]=Integer.parseInt(in.readLine());
}
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;
}
}
}

System.out.println("numbers are in ascending order");


for(i=0;i<10;i++)
System.out.print(m[i]+",");
}
}

Algorithm1. Accept the numbers in an array m[].


2. Loop startsfor(i=0;i<9;i++)

// takes the first value of the array

{
for(j=0;j<(9-i);j++)

the array

// takes the values one by one from

{
if(m[j]>m[j+1])

// checks whether the first number is

greater or not
3. If the above condition is true then interchanges the
position of first
element with second otherwise pass on to next
element
{
t=m[j];
m[j]=m[j+1];
m[j+1]=t;
}}}

4. The loop runs till all elements are sorted.

Ques14-Write a program to accept 10 different


numbers and search whether a given is present or
not by Linear Search.
import java.io.*;
class linear
{
public static void main(String args[])throws IOException
{
InputStreamReader read=new InputStreamReader(System.in);
BufferedReader in=new BufferedReader(read);
int i;int j;int f=0;
int m[]=new int[10];
for(i=0;i<10;i++)
{
System.out.println("Enter a number");
m[i]=Integer.parseInt(in.readLine());
}
System.out.println("Enter the number to be searched");
int a=Integer.parseInt(in.readLine());
for(j=0;j<10;j++)
{
if(a==m[j])
f=1;
}
if(f==1)
System.out.println("number is searched");

else
System.out.println("number is not searched");
}
}

Algorithm1. Accepts 10 numbers from the console in an


array m[].
2. Accepts the number to be searched and stores it
in variable a.
3. Loop runsfor(j=0;j<10;j++)
{

if(a==m[j])

// checks the number present in array

is equal to
number to be searched
f=1;

// acts as a flag and if the above

condition is true then


assigns 1 to f
}

4.

if(f==1)

// checks if f is equal to 1 or not

5. If above condition is true then display (number


is searched)
else (number is not searched).
6. Stop.

Ques15- Write a program to accept the states and


capitals and then accept the state from the user
the display its capital.
import java.io.*;
class cpital
{
public static void main(String args[])throws IOException
{
InputStreamReader read=new InputStreamReader(System.in);
BufferedReader in=new BufferedReader(read);
int i;int k;k=0;int a;a=0;
String n[]=new String[3];
String m[]=new String[3];
for(i=0;i<3;i++)
{
System.out.println(" Enter the state");
m[i]= in.readLine();
System.out.println(" Enter the capital");
n[i]= in.readLine();
}

System.out.println("Enter state name");


String st=in.readLine();
for(i=0;i<3;i++)
{
if(m[i].equals(st))
{k=1;a=i;}
}
if(k==1)
System.out.println("The capital is "+ n[a]);
else
System.out.println("Not found");
}
}

Algorithm1. Accept all states and capitals from the user.


2. Accept the state of which the user wants to know the
capital.
3. Loop executesfor(i=0;i<3;i++)
{
if(m[i].equals(st))

// finds the state entered by

the user
{k=1;a=i;}

// if above condition is true

then assign k to 1
and a to I
}

4. if(k==1)

// checks whether k is 1 or not

System.out.println("The capital is "+ n[a]);

// display if above

condition is true
else
System.out.println("Not found");

found
5. Stop.

// else display not

Ques16- Write a program to accept numbers in a


D.D.A.. Check whether the array is symmetric or
not.
import java.io.*;
class symetric
{
public static void main(String args[])throws IOException
{
InputStreamReader read=new InputStreamReader(System.in);
BufferedReader in=new BufferedReader(read);
int i;int k;int j;k=0;
int m[][]=new int[4][4];
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
System.out.println("Enter the numbers");
m[i][j]=Integer.parseInt(in.readLine());

}
}
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
if(m[i][j]!=m[j][i])
k=k+1;
}
}
if(k==0)
System.out.println("Symmetric");
else
System.out.println("Not Symmetric");
}
}

Algorithm1. Accept the numbers in DDA.


2. Loop executesfor(i=0;i<4;i++)
{
for(j=0;j<4;j++)

{
if(m[i][j]!=m[j][i])
// checks whether element of i th row and j th
column is not equal to j th row and i th column
k=k+1;

// if above condition is true then increment in k

}
}

3. if(k==0)

// checks whether k is 0 or not

4. Display the results.

Ques17- Write a program to accept 3x3 matrix and


display the sum of left and right diagonal.
import java.io.*;
class diagonal
{
public static void main(String args[])throws IOException
{
InputStreamReader read=new InputStreamReader(System.in);
BufferedReader in=new BufferedReader(read);
int i;int s;s=0;int sum;sum=0;int j;
int m[][]=new int[3][3];
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
System.out.println("Enter number");
m[i][j]=Integer.parseInt(in.readLine());
}
}
for(i=0;i<3;i++)
{
s=s+m[i][i];
}
System.out.println("Sum of left diagonal "+s);
for(j=0;j<3;j++)

{
sum=sum+m[j][2-j];
}
System.out.println("Sum of right diagonal "+sum);
}
}

Algorithm1. Accept the numbers from the user.


2. Loop executesfor(i=0;i<3;i++)
{

s=s+m[i][i];
diagonal

// to find the sum of left

}
System.out.println("Sum of left diagonal "+s);

// print the sum

3. Second loop executesfor(j=0;j<3;j++)


{
sum=sum+m[j][2-j];
diagonal

// to find the sum of right

}
System.out.println("Sum of right diagonal "+sum);

// print the sum

4. Stop.

Ques 18- Write a program to swap the numbers in


third row with first row in DDA.

import java.io.*;
class swap
{
public static void main(String args[])throws IOException
{
InputStreamReader read=new InputStreamReader(System.in);
BufferedReader in=new BufferedReader(read);
int i;int k;int t;int j;
int m[][]=new int[4][4];
System.out.println("Enter the numbers");
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
m[i][j]=Integer.parseInt(in.readLine());
}
}
System.out.println("Input is-");
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
System.out.print(m[i][j]+" ");
}
System.out.println();

}
for(k=0;k<4;k++)
{
t=m[0][k];
m[0][k]=m[3][k];
m[3][k]=t;
}
System.out.println("Matrix is-");
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
System.out.print(m[i][j]+" ");
}
System.out.println();
}
}
}

Algorithm1- Store numbers in an array.


2- Display the array in DDA form.
3-

for(k=0;k<4;k++)
{
t=m[0][k];

m[0][k]=m[3][k];
m[3][k]=t;

[to swap the elements]

Ques 19- Write a program to store numbers in two


array and merge them and display.
import java.io.*;
class merge
{
public static void main(String args[])throws IOException
{
InputStreamReader read=new InputStreamReader(System.in);
BufferedReader in=new BufferedReader(read);
int i;int k;int t;int j;t=6;
int r[]=new int [10];
int m[]=new int[6];
System.out.println("Enter the numbers");
for(i=0;i<6;i++)
{
m[i]=Integer.parseInt(in.readLine());
}
int n[]=new int[4];
System.out.println("Enter the numbers");
for(j=0;j<4;j++)
{
n[j]=Integer.parseInt(in.readLine());
}
for(k=0;k<6;k++)
{

r[k]=m[k];
}
for(i=0;i<4;i++)
{
r[t]=n[i];
t=t+1;
}
System.out.println("Merged array");
for(i=0;i<10;i++)
{
System.out.println(+r[i]);
}
}
}

Algorithm1- Store numbers in two arrays.


2- Firstly store numbers of first array in a new array then
store the other
array from the point it was left to be filled.
3- Display the new array.

Ques 20- Write a program to store numbers in an


array and find the frequency of a number entered
by the user in that array.
import java.io.*;
class frequency
{
public static void main(String args[])throws IOException
{
InputStreamReader read=new InputStreamReader(System.in);
BufferedReader in=new BufferedReader(read);
int i;int j;int t;t=0;
int m[]=new int[10];
System.out.println("Enter the numbers");
for(i=0;i<6;i++)
{
m[i]=Integer.parseInt(in.readLine());
}
System.out.println("Enter the number whose frequency is to be find");
int n=Integer.parseInt(in.readLine());
for(j=0;j<10;j++)
{
if(m[j]==n)
{

t=t+1;
}
}
System.out.println("Frequency of "+n+" is "+t);
}
}

Algorithm1- Store numbers in an array.


2- Accept the number from user whose frequency is to be
find.
3-With the help of a loop find how many times a number
had occurred in array.
4- Display results.

Ques 21- Write a program to store numbers in an


array and find the pallindrome words in it and
display them.
import java.io.*;
class pallindrome
{
public static void main(String args[])throws IOException
{
InputStreamReader read=new InputStreamReader(System.in);
BufferedReader in=new BufferedReader(read);
int i;int j;int t;int p;int r;r=0;t=0;
int m[]=new int[6];
System.out.println("Enter the numbers");
for(i=0;i<6;i++)
{
m[i]=Integer.parseInt(in.readLine());
}
System.out.println("Pallindrome numbers are-");
for(j=0;j<6;j++)
{
t=m[j];
while(t>0)
{
p=t%10;

r=(r*10)+p;
t=t/10;
}
if(m[j]==r)
System.out.print(r+" ");
r=0;
}
}
}

Algorithm1- Store numbers in an array.


2-One by one check whether a number is palindrome or
not with the help of a loop.
3-If found then display it.

Ques 22- Write a program to store names in an


array and arrange names in alphabetical order.
import java.io.*;
class arrange
{
public static void main(String args[])throws IOException
{
InputStreamReader read=new InputStreamReader(System.in);
BufferedReader in=new BufferedReader(read);
int i;int j;String t;int min;min=0;
String m[]=new String[6];
System.out.println("Enter names");
for(i=0;i<6;i++)
{
m[i]=in.readLine();
}
for(i=0;i<5;i++)
{
for(j=i+1;j<6;j++)
{
if(m[j].compareTo(m[min])<0)
min=j;
}
t=m[i];

m[i]=m[min];
m[min]=t;
}
for(i=0;i<6;i++)
{
System.out.println(m[i]);
}
}
}

Algorithm1- Store names in an array.


2-Arrange them in alphabetical order using selection sort
technique by just using a compareTo function instead of
< ; >.
3-Display the new array.

Ques 23- Write a program to accept a number in


decimal and convert it into hexadecimal.
import java.io.*;
class convert
{
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 k;i=0;k=0;
int m[]=new int[20];
System.out.println("Enter a decimal number");
int n=Integer.parseInt(in.readLine());
System.out.println("The equivalent hexadecimal number is ");
while(n>0)
{
p=n%16;
m[i]=p;
n=n/16;
i++;k++;
}
for(j=k-1;j>=0;j--)
{
if(m[j]>=10&&m[j]<=15)
{

if(m[j]==10)
System.out.print("A");
if(m[j]==11)
System.out.print("B");
if(m[j]==12)
System.out.print("C");
if(m[j]==13)
System.out.print("D");
if(m[j]==14)
System.out.print("E");
if(m[j]==15)
System.out.print("F");
}
else
System.out.print(m[j]);
}
}
}

Algorithm-

1- Take an array of finite size.


2-Store the numbers which were got on dividing the
number by 16 .
3- Match whether it is between 10 to 15 and then assign
them A,B,C..
4-Display results.

Ques24 - Write program to accept two numbers to


find H.C.F and L.C.M of them using function.
import java.io.*;
public class cal
{
int hcf(int a,int b)

{
int i; int p;int h=0;p=a*b;
for(i=1;i<p;i++)
{
if(a%i==0 && b%i==0)
{
h=i;
}
}
return(h);
}
public static void main(String args[])throws IOException
{
InputStreamReader read=new InputStreamReader(System.in);
BufferedReader in=new BufferedReader(read);
int m, n,p,k,c;k=0;c=0;
cal obj=new cal();
System.out.println("Enter two numbers");
m=Integer.parseInt(in.readLine());
n=Integer.parseInt(in.readLine());
k=obj.hcf(m,n);
p=m*n;
c=p/k;
System.out.println("H.C.F.is"+k);
System.out.println("L.C.M.is"+c);
}
}

Algorithm1. A function cal is created to calculate H.C.F.


2. Formal arguments are taken as a and b of int type.
3. Product of both value are stored in variable p.
4. Loop executesfor(i=1;i<p;i++)
{

//checks whether numbers are

if(a%i==0 && b%i==0)

divisible by any
other number
{
h=i;

// holds the last and highest

value divisible by
both numbers
}
}
return(h); }

// returns the value stored in h

5. Main method is created.


6. Actual arguments are taken from the console.
7. cal obj=new cal();

// object of the class is created

8. k=obj.hcf(m,n);
given by it is

// function cal is called and value


stored in k

9. L.C.M. is takenp=m*n;
c=p/k;

10. Results are displayed.

Ques25- Write a program to check a number is


palindrome or not by using function.
import java.io.*;
class functionpallindrome
{
int reverse(int n)
{
int d,r;r=0;
do
{
d=n%10;
r=r*10+d;
n=n/10;
}

while(n!=0);
return(r);
}
public static void main(String args[])throws IOException
{
InputStreamReader read=new InputStreamReader(System.in);
BufferedReader in=new BufferedReader (read);
int k,r,d,c;r=0;
System.out.println("Enter a number");
int a =Integer.parseInt(in.readLine());
c=a;
functionpallindrome ob= new functionpallindrome();
k=ob.reverse(a);
if(k==c)
System.out.println("Pallindrome");
else
System.out.println("Not pallindrome");
}
}

Algorithm1. A function reverse is created.


2. It reverses the number.
3. functionpallindrome ob= new functionpallindrome();

// object of the

class is created

4. k=ob.reverse(a);
5. if(k==c)

// function reverse is called


// checks whether k is equal to original number

6. Display the results accordingly.

Oues26 -Write a program to accept a string and


count number of vowels in it.
import java.io.*;
class vowel

{
public static void main(String args[])throws IOException
{
InputStreamReader read=new InputStreamReader(System.in);
BufferedReader in=new BufferedReader(read);
int x,y,v;v=0;
String a;
char b;
System.out.println("Enter a string");
a=in.readLine();
x=a.length();
for(y=0;y<x;y++)
{
b=(a.charAt(y));
if((b=='a')||(b=='i')||(b=='o')||(b=='o')||(b=='u')||(b=='A')||(b=='I')||
(b=='O')||(b=='U')||(b=='E'))
v=v+1;
}
System.out.println("number of vowels are"+v);
}
}

Algorithm1. Accept a string from the user.

2. x=a.length();

// to find length of the string

3. Loop executesfor(y=0;y<x;y++)
{
b=(a.charAt(y));

// find each character of the

string
if((b=='a')||(b=='i')||(b=='o')||(b=='o')||(b=='u')||

// to check

character is
Vowel or not
(b=='A')||(b=='I')||(b=='O')||(b=='U')||(b=='E'))
v=v+1;

// to add 1 each time vowel is

found
}

4. Display the results.

Ques27- Write a program to accept a string and


calculate number of uppercase, lowercase, special
character and digits present in it.
import java.io.*;
class change
{
public static void main(String args[])throws IOException

{
InputStreamReader read=new InputStreamReader(System.in);
BufferedReader in=new BufferedReader(read);
String name;
int up,low,digit,spl,l,i;up=0;low=0;spl=0;digit=0;
char chr;
System.out.println("Enter a string");
name=in.readLine();
l=name.length();
for(i=l-1;i>=0;i--)
{
chr=name.charAt(i);
if(chr>='A'&&chr<='Z')
up=up+1;
else if(chr>='a'&&chr<='z')
low=low+1;
else if(chr>='0'&&chr<='9')
digit=digit+1;
else
spl=spl+1;
}
System.out.println("No. of uppercase"+up);
System.out.println("No. of lowercase"+low);
System.out.println("No. of digits"+digit);
System.out.println("No. of special character"+spl);
}
}

Algorithm1. Accept a string from the user.


2. l=name.length();
string

// to find the length of the

3. Loop executesfor(i=l-1;i>=0;i--)
{
chr=name.charAt(i);

// to find each character of the

string one by
one
if(chr>='A'&&chr<='Z')

// to check it is in upper case

or not
up=up+1;

// increment in variable up if

character is in
upper case
else if(chr>='a'&&chr<='z')

// to check it is in lower case or

not
low=low+1;

// increment in variable low if

character is in
lower case

else if(chr>='0'&&chr<='9')

// to check it is in number or

not
digit=digit+1;

// increment in variable digit if

character any
digit
else
spl=spl+1;

// increment in variable spl if

character any
special symbol
}

4. Results are displayed.

Ques28-Write a program to enter 5 states and their


capital and display the output in following
manner:Assam
The capital is Dispur
import java.io.*;
class capital
{
public static void main(String args[])throws IOException
{
InputStreamReader read=new InputStreamReader(System.in);
BufferedReader in=new BufferedReader(read);
int i,a,k;k=0;a=0;
String ch;
String m[]=new String[5];
String n[]=new String[5];
for(i=0;i<5;i++)
{
System.out.println("Enter state");
m[i]=in.readLine();
System.out.println("Enter capital");
n[i]=in.readLine();
}
System.out.println("Enter the state whose capital you want to know");
ch=in.readLine();
for(i=0;i<5;i++)

{
if(m[i].equals(ch));
{k=1;a=i;}
}
if(k==1)
System.out.println("The capital is"+n[a]);
else
System.out.println("The stae entered does not exists in array");
}
}

Algorithm1. Accept all states and all capitals in two arrays such that
they each
should have same index number as its capital.
2. Accept the state whose capital user wants to search.
3. Loop executesfor(i=0;i<5;i++)
{
if(m[i].equals(ch));

// checks the city entered by user

is present or not
{k=1;a=i;}

// if above condition is true then

assign 1 to k and
store the index number of
the city
}

4. if(k==1)
found or not

// checks whether the city is

System.out.println("The capital is"+n[a]);

// if the above

condition is true then


print the
capital stored in other
array with same
index number as the city
else
System.out.println("The stae entered does not exists in array");

display this
5. Stop.

// else

Ques29- Write a program to accept a string and


find number of blank spaces, words and alphabets
present in it.
import java.io.*;
class count
{
public static void main(String args[])throws IOException
{
InputStreamReader read=new InputStreamReader(System.in);
BufferedReader in=new BufferedReader(read);
String name;
int a,i,b,w;b=0;w=0;
char chr;chr=0;
System.out.println("Enter string");
name=in.readLine();
a=name.length();
for(i=0;i<a;i++)
{
chr=name.charAt(i);
if(chr==' ')
b=b+1;
}
w=b+1;

System.out.println("No. of blank spaces" + b);


System.out.println("No. of words" + w);
System.out.println("No. of alphabets" + (a-b));
}
}

Algorithm1. Accept the string from the user.


2. a=name.length();
string

// to find the length of the

3. Loop executesfor(i=0;i<a;i++)
{
chr=name.charAt(i);

// to find the characters

present in the string


one by one
if(chr==' ')

// to check it is blank space

or not
b=b+1;

// if above condition is true

then increment in
variable b
}

4. w=b+1;
they will be one

// to find number of words as


greater than number

of spaces
5. System.out.println("No. of blank spaces" + b);

// to print no. of

spaces
System.out.println("No. of words" + w);

// to print no. of

words
System.out.println("No. of alphabets" + (a-b));

// to print no. of

alphabets as
they will be equal to the
difference between total no. of
characters and no. of spaces
}
}

6. Stop.

Ques30- Write a program to find the frequency of


the in a string.
import java.io.*;
class frequency
{
public static void main(String args[])throws IOException
{

InputStreamReader read=new InputStreamReader(System.in);


BufferedReader in=new BufferedReader(read);
int x,z,f=0;
String str,t="";
char p;
System.out.println("Enter a string");
str=in.readLine();
str=str+"";
x=str.length();
for(z=0;z<x;z++)
{
p=str.charAt(z);
if(p!=' ')
t=t+p;
else
{
if(t.compareTo("the")==0)
f=f+1;
t="";
}
}
System.out.println("The frequency of the is"+ f);
}
}

Algorithm1. Accept the string from the user.


2. x=str.length();

// to find the length of the string

3. Loop executesfor(z=0;z<x;z++)
{
p=str.charAt(z);

// to find each character of the

string one by one


if(p!=' ')

// to check it is not blank space

// store the word in t by adding


characters one by one
t=t+p;

until space comes


else
{
if(t.compareTo("the")==0)

// if the above condition is

false i.e. a space


comes then compare
the word before the
space(i.e. word stored
in t) with the

f=f+1;

// if above condition is true then

increment in f
t="";

// assign space to variable t

}
}

4. Display the results.

Ques31- Write a program to check whether a


character is in lowercase, uppercase, digit or
special character.
import java.io.*;
class uppercase
{

public static void main(String args[])throws IOException


{
InputStreamReader read=new InputStreamReader(System.in);
BufferedReader in=new BufferedReader(read);
char chr;
System.out.println("Enter a character");
chr=(char)(in.read());
if(Character.isLetter(chr))
{
System.out.println(chr+" is a letter");
if(Character.isUpperCase(chr))
System.out.println(chr+" is a uppercase letter");
if(Character.isLowerCase(chr))
System.out.println(chr+" is a lowercase letter");
}
else
{
if(Character.isDigit(chr))
System.out.println(chr+" is a digit");
else
System.out.println(chr+" is a special character");
}
}
}

Algorithm1. Accept a character from the user.


2. if(Character.isLetter(chr))
character is letter or not

// to check

{
System.out.println(chr+" is a letter");

// if above condition

is true then
display that
it is a letter
if(Character.isUpperCase(chr))

// to check it is in

upper case or not


System.out.println(chr+" is a uppercase letter");

// if above

condition is true then


print that
it is in upper case
if(Character.isLowerCase(chr))

// to check it is in

lower case or not


System.out.println(chr+" is a lowercase letter"); // if above condition

is true then
print that
it is in lower case

}
else
{
if(Character.isDigit(chr))

// to check it is digit or

not
System.out.println(chr+" is a digit");

// if above condition is

true then print


that it is a
digit
else
System.out.println(chr+" is a special character");

// if all above

conditions are false


then print that it
is a special character
}

3. Stop.

Ques32- Write a program to check whether a word


is palindrome or not.
import java.io.*;
class pallindrome
{
public static void main(String args[])throws IOException
{

InputStreamReader read=new InputStreamReader(System.in);


BufferedReader in=new BufferedReader(read);
String word1="";
int i,l;
char chr;
System.out.println("Enter a word");
String word=in.readLine();
l=word.length();
for(i=l-1;i>=0;i--)
{
chr=word.charAt(i);
word1=word1+chr;
}
if(word.equals(word1))
System.out.println("Pallindrome");
else
System.out.println("Not Pallindrome");
}
}

Algorithm1. Accept a word from the user.

2. l=word.length();
string or word

// to find the length of the

3. Loop executes// loop runs from last to first

for(i=l-1;i>=0;i--)
{
chr=word.charAt(i);

// to find character from the last

of the word
word1=word1+chr;

// add one by one character from

last to the
string word1
}

4. if(word.equals(word1))
by reversing the

// to check the string formed


string is equal to

the original word or not


System.out.println("Pallindrome");

// if above condition is true

then print it is
palindrome
else
System.out.println("Not Pallindrome");

// if above condition is

false then print


not
palindrome
5. Stop.

Ques 33- Write a program to accept a string from


the user and convert it in piglatin form.
import java.io.*;
class piglatin
{
public static void main(String args[])throws IOException
{
InputStreamReader read=new InputStreamReader(System.in);
BufferedReader in=new BufferedReader(read);
int l;char ch;int i;String st;String s;
System.out.println("Enter string");
String str=in.readLine();
l=str.length();
for(i=0;i<l;i++)
{
ch=str.charAt(i);
if(ch=='A'||ch=='E'||ch=='I'||ch=='O'||ch=='U'||ch=='a'||ch=='e'||ch=='i'||
ch=='o'||ch=='u')
break;
}
st=str.substring(i,l);
s=str.substring(0,i);
System.out.println(st+s+"ay");

}
}

Algorithm1. Accept a string from the user.


2. l=str.length();

// to the length of the string

3. Loop executesfor(i=0;i<l;i++)
{
ch=str.charAt(i);

// to find the characters present in

the string one


by one
if(ch=='A'||ch=='E'||ch=='I'||ch=='O'||ch=='U'

// to check the

character is vowel
or not
||ch=='a'||ch=='e'||ch=='i'||ch=='o'||ch=='u')

break;

// if above condition is true then

break the loop


}

4. st=str.substring(i,l);
after the first

// to take all the characters


vowel from the

string
s=str.substring(0,i);

// to take the characters from

starting to the
first vowel
System.out.println(st+s+"ay");

// combine all strings and

display them
5. Stop

Ques 34- Write a program to accept a string and


find number of A and a present in the string.
import java.io.*;
class freqaA
{
public static void main(String args[])throws IOException
{

InputStreamReader read=new InputStreamReader(System.in);


BufferedReader in=new BufferedReader(read);
int l;char ch;int i;String st;int f;f=0; int k;k=0;
System.out.println("Enter string");
String str=in.readLine();
l=str.length();
for(i=0;i<l;i++)
{
ch=str.charAt(i);
if(ch=='A')
f=f+1;
if(ch=='a')
k=k+1;
}
System.out.println("Frequency of 'A' is "+f);
System.out.println("Frequency of 'a' is "+k);
}
}

Algorithm1. Accept a string from the user.


2. l=str.length();

// to find the length of the string

3. Loop executesfor(i=0;i<l;i++)
{
ch=str.charAt(i);
one
if(ch=='A')
f=f+1;
in f by one
if(ch=='a')
k=k+1;
in k by one
}

4. Display the results.

// to find the characters in the string one by


// to check character is A or not
// if above condition is true then increment
// to check character is a or not
// if above condition is true then increment

Ques 35- Write a program to accept two words and


frame a new word by taking one by one character
from each word. Assuming both words have same
length.
import java.io.*;
class combine
{
public static void main(String args[])throws IOException
{
InputStreamReader read=new InputStreamReader(System.in);
BufferedReader in=new BufferedReader(read);
int l;int i;String s;s="";
System.out.println("Enter first string");
String str=in.readLine();
System.out.println("Enter second string");
String st=in.readLine();
l=str.length();
for(i=0;i<l;i++)
{
s=s+str.charAt(i)+st.charAt(i);
}
System.out.println(s);

}
}

Algorithm1. Accept two string from the user having same length.
2. Find length of any of the string.
3. Loop executesfor(i=0;i<l;i++)
{
s=s+str.charAt(i)+st.charAt(i);

// takes one by one character

from each of the


strings and adds
them to variable s
}
System.out.println(s);

4. Stop.

// displays the final string

Ques36- Write a program to accept a string and


find the number of punctuations.
import java.io.*;
class punctuate
{
public static void main(String args[])throws IOException
{
InputStreamReader read=new InputStreamReader(System.in);
BufferedReader in=new BufferedReader (read);
int l;int i; char ch; int k; k=0;
System.out.println("Enter a string");
String str=in.readLine();
l=str.length();
for(i=0;i<l;i++)
{
ch=str.charAt(i);
if((ch==',')||(ch=='.')||(ch==';')||(ch=='?')||(ch=='!'))
{
k=k+1;
}
}
System.out.println("Number of punctuations are "+k);
}

Algorithm1. Accept a string from the user.


2. l=str.length();

// to find the length of the string

3. Loop executesfor(i=0;i<l;i++)
{
ch=str.charAt(i);
one

// to find each character of the string one by

if((ch==',')||(ch=='.')||(ch==';')||(ch=='?')||(ch=='!'))
above character is equal to any puncuation

// to check whether

{
k=k+1;
the value of k

// if above condition is true then increment in

4. Display the results.

Ques37- Write a program to accept a string and


convert it in alphabetical order and display the
new string.
import java.io.*;
class alphaorder
{
public static void main(String args[])throws IOException
{
InputStreamReader read=new InputStreamReader(System.in);
BufferedReader in=new BufferedReader(read);
int l;char ch;int i;String st;int j;
System.out.println("Enter string");
String str=in.readLine();
l=str.length();
for(j=65;j<=90;j++)
{
for(i=0;i<l;i++)
{
ch=str.charAt(i);
if(ch==(char)j||ch==(char)(j+32))
System.out.print(ch);
}

}
}
}

Algorithm1. Accept a string from the user.


2. l=str.length();
the string

// to find the length of

3. Loop executesfor(j=65;j<=90;j++)

// first loop runs from ASCII

code of A to Z
{
for(i=0;i<l;i++)

// second loop runs till

length of the string


{
ch=str.charAt(i);

// to find the characters of

the string
if(ch==(char)j||ch==(char)(j+32))

particular

// checks whether a

character of
the string is equal to
character
having ASCII code of j
System.out.print(ch);

// if above condition is

true then display it


}
}

4. Stop.

Ques38- Write a program to accept a name and


display the initials along with surname.
import java.io.*;
class initial
{
public static void main(String args[])throws IOException
{
InputStreamReader read=new InputStreamReader(System.in);
BufferedReader in=new BufferedReader(read);
int x; int y; int d; d=0;

String st;st="";char b;b=' ';


System.out.println("Enter string");
String str=in.readLine();
x=str.length();
st=st+str.charAt(0);
System.out.println("Name is ");
for(y=0;y<x;y++)
{
b=str.charAt(y);
if(b==' ')
{
d=d+1;
if(d==1)
st=st+"."+(str.charAt(y+1));
if(d==2)
st=st+"."+(str.substring(y,x));
}
}
System.out.println(st);
}
}

Algorithm1. Accept the string from the user.


2. Find length of the stringx=str.length();

3. Character at 0 position is stored in stst=st+str.charAt(0);

4. Loop executesfor(y=0;y<x;y++)
{
b=str.charAt(y);
if(b==' ')

// each character of the string is find


// checks whether the character is space

{
d=d+1;
if(d==1)

// increments in d if above condition is true


// checks the first space

st=st+"."+(str.charAt(y+1));
the space
if(d==2)

// adds the character to the string after


// checks the second space

st=st+"."+(str.substring(y,x));
second space

// adds the remaining string after

}
}

. 5. Display the results.

Ques39 Write a program to accept a


number(<1000) and display in words.
Sample input 25
Sample output- twenty five
import java.io.*;
class numbernames
{
public static void main(String args[])throws IOException
{
InputStreamReader read=new InputStreamReader(System.in);
BufferedReader in=new BufferedReader(read);
int p;int d;int v;int b;int n;int c;
System.out.println("Enter any number between 1 to 1000");
int a= Integer.parseInt(in.readLine());

Stringm[]={"","one","two","three","four","five","six","seven","eight","nine","t
en","eleven","twelve"thirteen","fourteen","fifteen","sixteen","seventeen","ei
ghteen","nineteen","twenty",
"thirty","forty","fifty","sixty","seventy","eighty","ninety","hundred","thousand
"};
if(a>0&&a<=20)
{
System.out.println(m[a]);
}
if(a>20&&a<=100)
{
p=a%10;
d=a/10;
System.out.println(m[20+(d-2)]+" "+m[p]);
}
if(a>100&&a<1000)
{
n=a%100;
b=n%10;
v=n/10;
c=a/100;
System.out.println(m[c]+" hundred "+m[20+(v-2)]+" "+m[b]);
}
}
}

Algorithm1- Accept a number.


2- Store required number names in an array.
3- Put three condition as given in program0<a<=20; 20<a<=100; 100<a<1000
4- In first condition the number matches with the index
number of its number name.
5- In second condition p is used to store last part of number which matches with the
index number in array of its number name and d stores first part of number.
6- Similarly third condition is performed.
7- Result is displayed after every condition.

Das könnte Ihnen auch gefallen