Mock Practical Question Class 12 Isc Students Handbook
Mock Practical Question Class 12 Isc Students Handbook
PROJECT
CLASS: 12-C
ROLL NO: 32
TABLE OF CONTENTS
Algorithm:
Solution:
import java.io.*;
class Money
{
public static void main(String arg[])throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int amt;
System.out.print("ENTER AMOUNT ");
amt=Integer.parseInt(br.readLine());
int an=amt;
int a[]=new int[8];
int b[]={500,100,50,20,10,5,2,1};
for(int i=0;i<8;i++)
{
a[i]=an/b[i];
an=an-(b[i]*a[i]);
}
for(int i=0;i<8;i++)
{
if(a[i]!=0)
System.out.println(" NOTES OF "+b[i]+" = "+a[i]);
}
}
}
Algorithm :
STEP 1 - START
STEP 2 - a = d = 0
STEP 3 - IMPORT a, d
STEP 4 - this.a = a & this.d = d
STEP 5 - IMPORT n
STEP 6 - RETURN (a+(n-1)*d)
STEP 7 - IMPORT n
STEP 8 - RETURN (n*(a+nTHTerm(n))/2)
STEP 9 - IMPORT n
STEP 10 - PRINT \n\tSeries\n\t"
STEP 11 - IF i=1;i<=n;i++ GOTO STEP 12
STEP 12 - PRINT nTHTerm(i)+" "
STEP 13 - i++ & IF i<=n GOTO STEP 12
STEP 14 - PRINT n\tSum : "+Sum(n)
STEP 15 - END
SOLUTION:
class APSeries
{
private double a,d; APSeries()
{
a = d = 0;
}
APSeries(double a,double d)
{
this.a = a; this.d = d;
}
double nTHTerm(int n)
{
return (a+(n-1)*d);
}
double Sum(int n)
{
return (n*(a+nTHTerm(n))/2);
}
void showSeries(int n)
{
System.out.print("\n\tSeries\n\t"); for(int i=1;i<=n;i++)
{
System.out.print(nTHTerm(i)+" ");
}
System.out.print("\n\tSum : "+Sum(n));
}
}
Output:
a = 5 d=2 n=10
Series:5.0 7.0 9.0 11.0 13.0 15.0 17.0 19.0 21.0 23.0
Sum : 140.0
PROGRAM 4 - To display calendar of any month of any year
Algorithm :
STEP 1 - START
STEP 2 - INPUT int month,int year
STEP 3 - int i,count=0,b,c,d=1 & String w="SMTWTFS"
STEP 4 - IF (year%100==0 && year%400==0) || (year%100!=0 && year
%4==0)
STEP 5 - days[1]=29
STEP 6 - PRINT "================The Calendar of
"+month1[month-1]+" "+year+"
is==================")
STEP 7 - IF i=0 THEN GOTO STEP 8
STEP 8 - PRINT (i)+"\t" & " "
STEP 9 - IF i=1 GOTO STEP 10
STEP 10 - IF (year%100==0 && year%400==0) || (year%100!=0 && year
%4==0)THEN GOTO STEP 11OTHERWISE GOTO STEP 12
STEP 11 - count+=2
STEP 12 - count+=1
STEP 13 - IF i=0 GOTO STEP 14
STEP 14 - count+=days[i] , count+=1, count%=7 & b=7-count
STEP 15 - IF b!=1 || b!=7 GOTO STEP 16
STEP 16 - IF count>0 GOTO STEP 17,18
STEP 17 - PRINT ' '+"\t")
STEP 18 - count--
STEP 19 - IF i=1 GOTO STEP 20
STEP 20 - IF b>0 && IF d<=days[month-1] GOTO STEP 21,22
STEP 21 - PRINT d+"\t"
STEP 22 - d++ & b--
STEP 23 - b=7
STEP 24 - i++ & IF i<MONTH GOTO STEP14
STEP 25 - PRINT " "
STEP 26 - END
SOLUTION:
class calendar
{
public void dee(int month,int year)
{
int i,count=0,b,c,d=1; String
w="SMTWTFS";
int days[]={31,28,31,30,31,30,31,31,30,31,30,31}; String
month1[]={"January","February","March","April","May","June","July",
"August","September","October","November","December"}
;
If((year%100==0 && year%400==0) || (year%100!=0 && year%4==0)) days[1]=29;
Output:
month = 10 year = 2009
=====The Calendar of October 2009 is====
S M T W T F S
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
SOLUTION:
import java.io.*;
class factorial
{
public static void main(String args[]) throws IOException
{
BufferedReader br = new BufferedReader(new
InputStreamReader(System.in)); System.out.println("enter no
=");
int n = Integer.parseInt(br.readLine()); factorial obj = new
factorial();
long f = obj.fact(n); System.out.println("factotial ="+f);
}
Output:
enter no = 5
factorial =120
SOLUTION:
import java.io.*;
class fibonacci
{
public static void main(String args[]) throws IOException
{
fibonacci obj = new fibonacci(); BufferedReader br = new
BufferedReader(new
InputStreamReader(System.in)); System.out.println("enter no of term
=");
int n = Integer.parseInt(br.readLine()); System.out.println();
for(int i=1;i<=n;i++)
{
int f = obj.fib(i); System.out.print(f+" ");
}
}
public int fib(int n)
{
if(n<=1) return n;
else
return (fib(n-1) +fib(n-2));
}
}
Output:
enter no of terms = 5
11235
SOLUTION:
import java.io.*;
class SmithNo
{
public static void main(String arg[])throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int n,i=2,s1=0,s2=0:
System.out.print("ENTER NO ");
n=Integer.parseInt(br.readLine());
int m=n;
while(m>1)
{
if(m%i==0)
{
int j=i;
while(j!=0)
{
s1=s1+(j%10);
j=j/10;
}
m=m/i;
}
else
i++;
}
m=n;
while(m!=0)
{
s2=s2+(m%10);
m=m/10;
}
if(s1==s2)
System.out.print("IS A SMITH NO. ");
else
System.out.print(" NOT A SMITH NO. ");
}
}
Output:
ENTER NO . 30
NOT A SMITH NO.
ENTER NO 666
IS ASMITH NO.
ENTER NO 4937775
IS A SMITH NO.
SOLUTION:
import java.io.*; class spiralsm
{
public static void main(String[] args) throws IOException
{
int a[][],r,c,k1=2,k2=3,p=0,co=0,re=0;
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
system.out.println("enter the dimension of matrix ="); int l = Integer.parseInt(br.readLine());
a=new int[l][l]; r=l/2;c=r-1; if(l%2==0)
{
System.out.println("wrong entry for spiral path"); System.exit(0);
}
while(p!=(int)Math.pow(l,2))
{
if(co!=0)
re=1;
for(int ri=1;ri<=k1-re;ri++) {p++;c++;if(c==l)break;a[r][c]=p;}
if(c==l)break;
for(int dw=1;dw<=k1-1;dw++) {p++;r++;a[r][c]=p;}
for(int le=1;le<=k2-1;le++) {p++;c--;a[r][c]=p;}
for(int up=1;up<=k2-1;up++) {p++;r--;a[r][c]=p;} k1=k1+2;
k2=k2+2;
co++;
}
for(int y=0;y<l;y++)
{
for(int yy=0;yy<l;yy++) System.out.print("\t"+a[y][yy]);
System.out.println(); System.out.println();
}
}
}
Output:
21 22 23 24 25
20 7 8 9 10
19 6 1 2 11
18 5 4 3 12
17 16 15 14 13
PROGRAM 9 - To display magical square
Algorithm:
STEP 1 - START
STEP 2 - arr[][]=new int[n][n],c=n/2-1,r=1,num
STEP 3 - IF num=1;num<=n*n;num++ GOTO STEP 4
STEP 4 - r--,c++
STEP 5 - IF r==-1 GOTO STEP 6
STEP 6 - r=n-1
STEP 7 - IF c>n-1 GOTO STEP 8
STEP 8 - c=0
STEP 9 - IF arr[r][c]!=0 GOTO STEP 10
STEP 10 - r=r+2 & c--
STEP 11 - num++ & IF num<=n*n GOTO STEP 4
STEP 12 - arr[r][c]=num
STEP 13 - IF r==0&&c==0 GOTO STEP 14
STEP 14 - r=n-1, c=1 & arr[r][c]=++num
STEP 15 - IF c==n-1&&r==0 GOTO STEP 16
STEP 16 - arr[++r][c]=++num
STEP 17 - PRINT ()
STEP 18 - IFr=0 GOTO STEP 19
STEP 19 - IF c=0 GOT STEP 20
STEP 20 - PRINT arr[r][c]+" " & ()
STEP 21 - c++ & IF c<n GOTO STEP 20
STEP 21 - r++ & r<n GOTO STEP 19
STEP 22 - END
SOLUTION:
import java.io.*; class magicalsquare
{
public static void main(String args[])throws Exception
{
BufferedReader br = ew BufferedReader (new
InputStreamReader(System.in));
System.out.println("enter the dimension of magical square="); int n =
Integer.parseInt(br.readLine());
int arr[][]=new int[n][n],c=n/2-1,r=1,num; for(num=1;num<=n*n;num++)
{
r--; c++; if(r==-1)
r=n-1; if(c>n-1)
c=0;
if(arr[r][c]!=0)
{
r=r+2; c--;
}
arr[r][c]=num;
if(r==0&&c==0)
{
r=n-1; c=1;
arr[r][c]=++num;
}
if(c==n-1&&r==0) arr[++r][c]=++num;
}
System.out.println();
for(r=0;r<n;r++)
{
for(c=0;c<n;c++) System.out.print(arr[r][c]+" ");
System.out.println();
}
Output :
17 24 1 8 15
23 5 7 14 16
4 6 13 20 22
10 12 19 21 3
11 18 25 2 9
SOLUTION:
import java.io.*;
class linear_search
{
int n,i;
int a[] = new int[100];
static BufferedReader br =new BufferedReader(new
InputStreamReader(System.in));
public linear_search(int nn)
{
n=nn;
}
Output :
enter elements 5 3 8 4 1 6 4 7 9
5384164795
enter no. to be searched - 1
1 found at position - 4
SOLUTION:
import java.io.*;class binary_search
{
int n,i;
int a[] = new int[100];
static BufferedReader br =new BufferedReader(new InputStreamReader(System.in));
public binary_search(int nn)
{n=nn;}
public void input() throws IOException
{
System.out.println("enter elements"); for(i=0;i<n;i++)
{
a[i] = Integer.parseInt(br.readLine());
}
}
public void display()
{
System.out.println();
for(i=0;i<n;i++)
{
System.out.print(a[i]+" ");
}
}
public void search(int v)
{
int l=0;
int u = n-1; int m;
int flag=-1;
while( l<=u && flag == -1)
{
m = (l+u)/2;
if(a[m] == v) flag = m;
else if(a[m] < v)
l = m+1;
else
u = m-1;
}
if(flag== -1 ) System.out.println("not found");
else
System.out.println(v+" found at position - "+flag);
}
public static void main(String args[]) throws IOException
{
binary_search obj = new binary_search(10); obj.input();
obj.display();
System.out.println("enter no. to be searched -"); int v = Integer.parseInt(br.readLine());
obj.search(v);
}
}
Output :
enter elements
5
3
8
4
1
6
4
7
9
5
5384164795
enter no. to be searched -1
1 found at position - 4
PROGRAM 12 - To sort an array using Selection sort
Algorithm:
STEP 1 - START
STEP 2 - INPUT a[]
STEP 3 - FROM i=0 to i<n REPEAT STEP 4
STEP 4 - PRINT a[i]+" "
STEP 5 - flag=-1
STEP 6 - FROM i=0 to i<n-1 REPEAT STEP 7 to STEP 11
STEP 7 - min =i
STEP 8 - FROM j=i+1 to j<n REPEAT STEP 8
STEP 9 - IF(a[j]<a[min]) then min =j
STEP 10 - IF (min!=i) GOTO STEP 11
STEP 11 - temp = a[i], a[i] =a[min], a[min] = temp
SOLUTION:
import java.io.*;
class selection_sort
{
int n,i;
int a[] = new int[100];
}
}
for(i=0;i<n-1;i++)
{
min =i;
for(j=i+1;j<n;j++)
{
if(a[j]<a[min]) min =j;
}
if(min!=i)
{
temp = a[i]; a[i] =a[min]; a[min] = temp;
}
}
}
Output:
enter elements 4 6 1 2 9
Before sorting - 4 6 1 2 9
After sorting - 1 2 4 6 9
Algorithm:
STEP 1 - START
STEP 2 - INPUT a[]
STEP 3 - FROM i=0 to i<n REPEAT STEP 4
STEP 4 - PRINT a[i]+" "
STEP 5 - flag=-1
STEP 6 - FROM i=0 to i<n-1 REPEAT STEP 7 to STEP 9
STEP 7 - FROM j=i+1 to j<n REPEAT STEP 8
STEP 8 - IF(a[j] > a[j+1]) THEN GOTO STEP 9
STEP 9 - temp = a[i], a[i] =a[min], a[min] = temp
STEP 10 - END
SOLUTION:
import java.io.*;
class bubble_sort
{
int n,i;
int a[] = new int[100];
}
}
}
}
}
}
Output:
enter elements 4 6 1 2 9
Before sorting - 4 6 1 2 9
After sorting - 1 2 4 6 9
PROGRAM 14 - To convert a decimal no into it binary equivalent
Algorithm:
STEP 1 - START
STEP 2 - n = 30
STEP 3 - INPUT int no
STEP 4 - c =0 , temp = no
STEP 5 - IF (temp!=0) REPEAT STEP 6
STEP 6 - a[c++] = temp%2, temp = temp / 2
STEP 7 - FROM i=c-1 to i>0 REPEAT STEP 8
STEP 8 - PRINT a[i]
STEP 9 - END
SOLUTION:
import java.io.*;class dec_bin
{
int n,i;
int a[] = new int[100];
static BufferedReader br =new BufferedReader(new InputStreamReader(System.in));
public dec_bin(int nn)
{n=nn;}
public void dectobin(int no)
{
int c = 0;
int temp = no;
while(temp != 0)
{
a[c++] = temp % 2; temp = temp / 2;
}
Algorithm:
STEP 1 - START
STEP 2 - INITIALISE a[ ] , m[ ]
STEP 3 - INPUT n , yr
STEP 4 - IF ( yr%4=0) THEN a[1] = 29
STEP 5 - t =0 , s = 0
STEP 6 - IF ( t<n) REPEAT STEP 7
STEP 7 - t =t + a[s++]
STEP 8 - d = n + a[--s] - t
STEP 9 - IF ( d ==1|| d == 21 || d == 31 ) then PRINT d + "st" + m[s] + "
, "+yr
STEP 10 - IF ( d ==2|| d == 22 ) then PRINT d + "nd" + m[s] + " , "+yr
STEP 11 - IF ( d ==3|| d == 23 ) then PRINT d + "rd" + m[s] + " , "+yr
OTHERWISE GOTO STEP 12
STEP 12 - PRINT d + "th" + m[s] + " , "+yr
STEP 13 - END
SOLUTION:
import java.io.*;
class daytodate
{
static BufferedReader br =new BufferedReader(new
InputStreamReader(System.in));
public void calc(int n, int yr)
{
int a[ ] = { 31,28,31,30,31,30,31,31,30,31,30,31 } ;
if ( yr % 4 == 0) a[1] =29;
int t=0,s=0;
while( t < n)
{
t =t + a[s++];
}
int d = n + a[--s] - t;
if( d == 1|| d == 21 || d == 31 )
{
System.out.println( d + "st" + m[s] + " , "+yr);
}
if( d == 2 || d == 22 )
{
System.out.println( d + "nd" + m[s] + " , "+yr);
}
if( d == 3|| d == 23 )
{
System.out.println( d + "rd" + m[s] + " , "+yr);
}
else
{
System.out.println( d + "th" + m[s] + " , "+yr);
}
}
public static void main(String args[]) throws IOException
{
daytodate obj = new daytodate(); System.out.println( "Enter day no = ");
int n = Integer.parseInt(br.readLine()); System.out.println( "Enter year = ");
int yr = Integer.parseInt(br.readLine()); obj.calc(n,yr);
}
}
Output:
Enter day no =192
Enter year = 2015
11th Jul , 2015
PROGRAM 16 – To create a pattern from entered string
SOLUTION:
import java.io.*;
class pattern
{
public static void main (String args[]) throws IOException
{
int i,sp,j,k,l;
BufferedReader br = new BufferedReader(new
InputStreamReader(System.in));
System.out.println("enter the string ="); String s = br.readLine();
l=s.length();
for(i=0;i<l;i++)
if(i==l/2)
System.out.println(s); else
{
sp=Math.abs((l/2)-i); for(j=sp;j<l/2;j++)
System.out.print(" "); k=0;
while(k<3)
{
System.out.print(s.charAt(i)); for(j=0;j<sp-1;j++)
System.out.print(" ");
k++;
}
System.out.println(" ");
}
}
}
Output:
enter the string = india
i i i nnn india iii
aaa
PROGRAM 17 - To check if entered string is palindrome or not
Algorithm:
STEP 1 - START
STEP 2 - INPUT string s
STEP 3 - StringBuffer sb = s
STEP 4 - sb.reverse
STEP 5 - String rev = sb
STEP 6 - IF rev = s GOTO STEP 7 OTHERWISE GOTO STEP 8
STEP 7 - PRINT " Palindrome"
STEP 8 - PRINT " Not Palindrome"
STEP 9 - END
Solution:
import java.io.*;
class palindrome
{
public static void main(String args[]) throws IOException
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("enter the string="); String s = br.readLine();
StringBuffer sb = new StringBuffer(s); sb.reverse();
String rev = new String(sb);
if(s.equals(rev)) System.out.println("Palindrome " ); else
Output :
Algorithm :
STEP 1:Input no. of users and create an array of its size.
STEP 2:Input time and date of login and logout.
STEP 3:Calculate the difference in dates.
STEP 4:If month changes, then calculate difference by calculating no. of days left for month to finish
and no. of days passed in logout date and add them.
STEP 5:If difference is 0, then take out difference between time.
STEP 6:If difference is 1, then take out time left for day to finish and time passed in logout time and
add them.
STEP 7:If difference is 2, then take out time left for day to finish and time passed in logout time and
add them and add 24 hrs to it.
STEP 8:Store the index no. of user who login for maximum time.
STEP 9:Print the details of all users and user who login for maximum time.
Solution:
import java.io.*;
class Duration
{
int id,h1,h2,m1,m2,mon1,mon2,d1,d2,dif;
public void input(int i)throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("\n USER "+(i+1));
System.out.print("ENTER ID ");
id=Integer.parseInt(br.readLine());
System.out.println("ENTER TIME OF LOGIN ");
System.out.print("ENTER HOUR ");
h1=Integer.parseInt(br.readLine());
System.out.print("ENTER MINUTE ");
m1=Integer.parseInt(br.readLine());
System.out.println("ENTER DATE OF LOGIN ");
System.out.print("ENTER DATE ");
d1=Integer.parseInt(br.readLine());
System.out.print("ENTER MONTH ");
mon1=Integer.parseInt(br.readLine());
System.out.println("ENTER TIME OF LOGOUT ");
System.out.print("ENTER HOUR ");
h2=Integer.parseInt(br.readLine());
System.out.print("ENTER MINUTE ");
m2=Integer.parseInt(br.readLine());
System.out.println("ENTER DATE OF LOGOUT ");
System.out.print("ENTER DATE ");
d2=Integer.parseInt(br.readLine());
System.out.print("ENTER MONTH ");
mon2=Integer.parseInt(br.readLine());
}
public int calDiff()
{
int mon=mon1,d=0,md=0,c=0;
if(mon1!=mon2)
{
d=d1;
if(mon1==1 || mon1==3||mon1==5||mon1==7
||mon1==8 ||mon1==10||mon1==12)
md=31;
if(mon1==4 || mon1==6||mon1==9||mon1==11)
md=30;
if(mon1==2)
md=28;
while(d!=md )
{
c++;
d++;
}
d=c+d2;
}
else
d=d2-d1;
if(d==1)
dif=(24*60)-((h1*60)+m1)+((h2*60)+m2);
else if(d==2)
dif=(48*60)-((h1*60)+m1)+((h2*60)+m2);
else if(d==0)
dif=((h2*60)+m2)-((h1*60)+m1);
int d1=dif;
return d1;
}
public void print()throws IOException
{
int h=dif/60;
int m=dif-h*60;
System.out.println(id+" \t\t"+h1+":"+m1+" "+d1+"-"+mon1+"
\t\t"+h2+":"+m2+" "+d2+"-"+mon2+"\t\t"+h+":"+m);
}
public void printMax(int max)throws IOException
{
int h=max/60;
int m=max-h*60;
System.out.println(id+" \t\t"+h1+":"+m1+" "+d1+"-"+mon1+"
\t\t"+h2+":"+m2+" "+d2+"-"+mon2+"\t\t"+h+":"+m);
}
public static void main(String arg[])throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int n,max=0,j=0;
System.out.print("ENTER NO OF USERS ");
n=Integer.parseInt(br.readLine());
Duration obj[]=new Duration[n];
for(int i=0;i<n;i++)
{
obj[i]=new Duration();
obj[i].input(i);
int d1=obj[i].calDiff();
if(d1>max)
{
max=d1;
j=i;
}
}
System.out.println(" User id \t Login time & Date \t Logout time & Date \t Duration");
for(int i=0;i<n;i++)
obj[i].print();
System.out.println("\nMAXIMUM");
obj[j].printMax(max);
}
}
Output :
ENTER NO OF USERS 4
USER 1
ENTER ID 1111
ENTER TIME OF LOGIN
ENTER HOUR 12
ENTER MINUTE 30
ENTER DATE OF LOGIN
ENTER DATE 1
ENTER MONTH 9
ENTER TIME OF LOGOUT
ENTER HOUR 12
ENTER MINUTE 30
ENTER DATE OF LOGOUT
ENTER DATE 3
ENTER MONTH 9
USER 2
ENTER ID 2222
ENTER TIME OF LOGIN
ENTER HOUR 4
ENTER MINUTE 0
ENTER DATE OF LOGIN
ENTER DATE 28
ENTER MONTH 2
ENTER TIME OF LOGOUT
ENTER HOUR 3
ENTER MINUTE 59
ENTER DATE OF LOGOUT
ENTER DATE 1
ENTER MONTH 3
USER 3
ENTER ID 3333
ENTER TIME OF LOGIN
ENTER HOUR 1
ENTER MINUTE 10
ENTER DATE OF LOGIN
ENTER DATE 23
ENTER MONTH 3
ENTER TIME OF LOGOUT
ENTER HOUR 2
ENTER MINUTE 0
ENTER DATE OF LOGOUT
ENTER DATE 23
ENTER MONTH 3
USER 4
ENTER ID 4444
ENTER TIME OF LOGIN
ENTER HOUR 6
ENTER MINUTE 25
ENTER DATE OF LOGIN
ENTER DATE 8
ENTER MONTH 9
ENTER TIME OF LOGOUT
ENTER HOUR 6
ENTER MINUTE 25
ENTER DATE OF LOGOUT
ENTER DATE 8
ENTER MONTH 9
User id Login time & Date Logout time & Date Duration
1111 12:30 1-9 12:30 3-9 48:0
2222 4:0 28-2 3:59 1-3 23:59
3333 1:10 23-3 2:0 23-3 0:50
4444 6:25 8-9 6:25 8-9 0:0
MAXIMUM
1111 12:30 1-9 12:30 3-9 48:0
PROGRAM 19 - To decode the entered string
Algorithm :
STEP 1 - START
STEP 2 - INPUT name, n
STEP 3 - l=name.length()
STEP 4 - PRINT original string is "+name
STEP 5 - IF i=0 THEN GOTO STEP 6
STEP 6 - char c1=name.charAt(i)
STEP 7 - c=(int)c1
STEP 8 - IF n>0 THEN GOTO STEP 9 THERWISE GOTO STEP 12
STEP 9 - IF (c+n)<=90 THEN GOTO STEP 10 OTHERWISE GOTO STEP 11
STEP 10 - PRINT (char)(c+n)
STEP 11 - c=c+n;c=c%10,c=65+(c-1) & PRINT (char)(c)
STEP 12 - ELSE IF n<0 THEN GOTO STEP 13 OTHERWISE GOTO STEP 19
STEP 13 - n1=Math.abs(n)
STEP 14 - IF (c-n1) >=65 THEN GOTO STEP 15 OTHERWISE GOTO STEP 16
STEP 15 - DISPLAY (char) (c-n1)
STEP 16 - IF c>65 THEN GOTO STEP 17 OTHERWISE GOTO STEP 18
STEP 17 - c=c-65,
STEP 18 - c=n1 & PRINT (char)(90-(c-1))
STEP 19 - ELSE IF n==0
STEP 20 - DISPLAY "no change "+name
STEP 21 - END
Solution :
class decode
{
public void compute(String name,int n)
{
int j,i,l,c=0,y,n1;
l=name.length();
Output:
original string is ABCDE n = 4
decoded string is EFGHI
PROGRAM 20 - To display the entered string in alphabetical order.
Algorithm :
STEP 1 - START
STEP 2 - str = "" , l = 0
STEP 3 - INPUT string str
STEP 4 - l =str.length()
STEP 5 - FROM i=0 to i<l REPEAT STEP 6
STEP 6 - c[i] = str.charAt(i)
STEP 7 - FROM i=0 to i<l-1 REPEAT STEP 8
STEP 8 - FROM j=0 to i<l-1 REPEAT STEP 9
STEP 9 - temp =c[j], c[j] = c[j+1] , c[j+1] = temp
STEP 10 - FROM i=0 to i<l REPEAT STEP 11
STEP 11 - PRINT c[i]
STEP 12 - END
Solution:
import java.io.*;
class Alpha
{
String str;
int l;
char c[] = new char[100];
public Alpha()
{
str = ""; l
=0;
}
for(i=0;i<l-1;i++)
{
for(j=0;j<l-1-i;j++)
{
if(c[j] > c[j+1])
{
temp = c[j]; c[j] = c[j+1]; c[j+1] = temp;
}
}
}
}
Output:
enter word - window
dinoww
PROGRAM 21 - To input a string and decode it by first reversing it and then
converting by ASCII value
Algorithm:
STEP 1:Input the nos. in string.
STEP 2:Reverse the string.
STEP 3:Execute a while loop j till string length-2.
STEP 4:Extract two characters and convert into no. and check that it is between 65-90 or 97-99 or
32 .If it satisfies the condition ,then increase value of j by 2.
STEP 5:If it does not belong to that range then extract 3 characters then increase value of j by 3.
STEP 6:Concat the nos. by changing into characters by ASCII value.
Solution:
import java.io.*;
class Decode
{
public static void main(String arg[])throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
String s,str="",n="",sen="";
int j=0,no=0;
System.out.print("ENTER STRING ");
s=br.readLine();
for(int i=s.length()-1;i>=0;i--)
str=str+s.charAt(i);
while(j<=str.length()-2)
{
n=str.substring(j,j+2);
no=Integer.parseInt(n);
if((no>=97 && no<=99)||(no>=65 && no<=90)||no==32)
j=j+2;
else
{
n=str.substring(j,j+3);
j=j+3;
no=Integer.parseInt(n);
}
sen=sen+(char)no;
}
System.out.println(sen+" ");
}
}
Output:
ENTER STRING 2312179862310199501872379231018117927
Have a Nice Day
PROGRAM 22-To input a sentence and find occurrence of each word and print the
word occurring maximum no of times.
Algorithm:
STEP 1:Input a sentence.
STEP 2:Count no. of words.
STEP 3:Store each word in an array.
STEP 4:Compare a word with all other words, and if found same increase the counter.
STEP 5:Check that it has already occurred or not, if not then print the occurrence.
STEP 6:Check occurrence of each word with max value, if it exceeds max value, then that word has
max occurred.
STEP 7:Print the max occurring word.
Solution:
import java.io.*;
class EachWord
{public static void main(String arg[])throws IOException
{BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
String s,w="";
int c=0,a=0,max=0,p=0;
System.out.println("ENTER TEXT --> ");
s=br.readLine();
System.out.println();
int m=s.length();
for(int i=0;i<m;i++)
{
if(s.charAt(i)!=' ' && (i==m-1 || s.charAt(i+1)==' '))
c++;
}
String str[]=new String[c];
for(int i=0;i<m;i++)
{
if(s.charAt(i)!=' ' && (i==m-1 || s.charAt(i+1)==' '))
{
str[a]=s.substring(p,i+1);
a++;
}
if(s.charAt(i)!=' ' && (i==0|| s.charAt(i-1)==' '))
p=i;
}
for(int i=0;i<c;i++)
{ int d=0,f=0;
for(int j=0;j<c;j++)
{ if(str[i].equalsIgnoreCase(str[j]))
d++;
if(str[i].equalsIgnoreCase(str[j]) && i>j)
f++;
}
if(d>max)
{ max=d;
w=str[i];
}
if(f==0)
System.out.println(str[i]+" = "+d);
}
System.out.println("\nHIGHEST OCCURING WORD "+w);
}
}
Algorithm:
STEP 1:Input the limit.
STEP 2:Execute a loop from 1-n for printing lines.
STEP 3:Execute another loop from 65-65+n/2 and print character.
STEP 4:If i=1 or n, then decrease value of j by 2 else by 1.
STEP 5:Execute a loop and print blank spaces.
STEP 6:Now print again characters.
STEP 7:If it’s upper half or lower half of loop, then increase value and decrease value of counter for
blank spaces respectively.
STEP 8:Change the line.
Solution:
import java.io.*;
class Pattern
{
public static void main(String arg[])throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int n;
System.out.print("ENTER NO ");
n=Integer.parseInt(br.readLine());
if(n%2==0)
{
System.out.print("NO SHOULD BE ODD ");
System.exit(0);
}
int m=65+n/2,b=-1,j;
for(int i=1;i<=n;i++)
{
for(j=65;j<=m;j++)
System.out.print((char)j);
if(i==1 || i==n)
j=j-2;
else
j=j-1;
for(int k=1;k<=b;k++)
System.out.print(" ");
for(int l=j;l>=65;l--)
System.out.print((char)l);
if(i<=n/2)
{
b=b+2;
m=m-1;
}
else
{
b=b-2;
m=m+1;
}
System.out.println();
}
}
}
Output:
ENTER NO 12
NO. SHOULD BE ODD
ENTER NO 19
ABCDEFGHIJIHGFEDCBA
ABCDEFGHI IHGFEDCBA
ABCDEFGH HGFEDCBA
ABCDEFG GFEDCBA
ABCDEF FEDCBA
ABCDE EDCBA
ABCD DCBA
ABC CBA
AB BA
A A
AB BA
ABC CBA
ABCD DCBA
ABCDE EDCBA
ABCDEF FEDCBA
ABCDEFG GFEDCBA
ABCDEFGH HGFEDCBA
ABCDEFGHI IHGFEDCBA
ABCDEFGHIJIHGFEDCBA
SOLUTION:
import java.io.*;
class Transpose
{
public static void main(String arg[])throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int m,n;
System.out.print("ENTER ROWS ");
m=Integer.parseInt(br.readLine());
System.out.print("ENTER COLUMNS ");
n=Integer.parseInt(br.readLine());
int a[][]=new int[m][n];
if(m==n)
{
for(int i=0;i<m;i++)
{
for(int j=0;j<m;j++)
{
System.out.print((i+1)+"."+(j+1)+"ENTER NO ");
a[i][j]=Integer.parseInt(br.readLine());
}
}
}
else
{
System.out.println("NOT SYMETRIC ");
System.exit(0);
}
int f=1;
loop1:
for(int i=0;i<m;i++)
{
loop2:
for(int j=0;j<m;j++)
{
if(a[i][j]!=a[j][i])
{
f=0;
break loop1;
}
}
}
System.out.println();
for(int i=0;i<m;i++)
{
for(int j=0;j<m;j++)
System.out.print(a[i][j]+" ");
System.out.println();
}
System.out.println();
if(f==1)
System.out.println("SYMMETRIC ");
else
System.out.println("NOT SYMMETRIC ");
}
}
Output:
ENTER ROWS 4
ENTER COLUMNS 4
1.1 ENTER NO 18
1.2 ENTER NO 47
1.3 ENTER NO 13
1.4 ENTER NO 31
2.1 ENTER NO 47
2.2 ENTER NO 22
2.3 ENTER NO 42
2.4 ENTER NO 67
3.1 ENTER NO 13
3.2 ENTER NO 42
3.3 ENTER NO 98
3.4 ENTER NO 15
4.1 ENTER NO 31
4.2 ENTER NO 67
4.3 ENTER NO 15
4.4 ENTER NO 96
18 47 13 31
47 22 42 67
13 42 98 15
31 67 15 96
SYMMETRIC
PROGRAM 25:To input a string and print no. of each combination of vowel.
Algorithm:
STEP 1:Input a string.
STEP 2:Store AEIOU in a string.
STEP 3:Create an array of [5][5].
STEP 4:Execute two loops and compare character of string with AEIOU.
STEP 5:Increase the counter if condition matches.
STEP 6:Store it in the array.
STEP 7:Print the array.
SOLUTION:
import java.io.*;
class VowelCombination
{
public static void main(String arg[])throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
String v="AEIOU";
String str;
int a[][]=new int[5][5];
System.out.print("ENTER STRING:- ");
str=br.readLine();
System.out.println();
System.out.println("\tA \t E \t I \t O \t U ");
for(int i=0;i<v.length();i++)
{
System.out.print(v.charAt(i)+"\t");
for(int j=0;j<v.length();j++)
{
int d=0;
char c1=v.charAt(i);
char c2=v.charAt(j);
for(int k=0;k<str.length()-1;k++)
{
if(str.charAt(k)==c1 && str.charAt(k+1)==c2)
d++;
}
a[i][j]=d;
System.out.print(a[i][j]+"\t");
}
System.out.println();
} }
}
Output:
ENTER STRING: - AILKSTEENHGFDSX CBAOKJUIJIOKJH GCDAAGTHTEEBH
GXUUYTRUEMK JIOJHFUA
NHGS UAOKJUDKRLFHFII LFHFOOKDGDUUE EEKAFSEEJFBU
UHAAKJDEEBHGEENB VCZAASDFREERTYU
UYTREIH JKOOLKJIUYUG AJHUABGHUOMNBOIJ GFEADFGEIJHGEI
KMBEOKLJOELKHOA LJGIOLHGUOL
JGFIAKREA
A E I O U
A 3 0 1 2 0
E 2 7 3 1 0
I 1 0 1 3 1
O 1 1 1 2 0
U 3 2 1 2 3