Computer 25 Project
Computer 25 Project
SCIENCE
PROJECT
Programming
in BLUEJ
Anam Veqar
XII-B
City Montessori School
Station Road
ACKNOWLEDGEMENT
First and foremost,I would like to express my sincere
gratitude of thanks towards my computer teacher Mrs.Ekta
Sethi for her incommensurable guidance throughout the
session.
Anam Veqar
XII-B
City Montessori School
Station Road
PROGRAM 1
To Display Entered Number in
Words
ALGORITHM
STEP 1 - START
STEP 2 - INPUT amt
STEP 3 - z=amt%10 , g=amt/10
STEP 4 - IF g!=1 THEN GOTO STEP 5 OTHERWISE GOTO STEP 6
STEP 5 - PRINT x2[g-1]+" "+x1[z]
STEP 6 - PRINT x[amt-9]
STEP 7 – END
Solution
import java.util.*;
class Num2Words
{
public static void main()//main function
{
Scanner sc=new Scanner(System.in);
1|ISCComputerScienceProject
System.out.println("Enter any Number(less than 99)");
int amt= sc.nextInt(); //accepting number
int z,g;
String
x[]={"","Ten","Eleven","Twelve","Thirteen","Fourteen","Fifteen","Si
xteen","Seventeen","Eighteen","Nineteen"};
String
x1[]={"","One","Two","Three","Four","Five","Six","Seven","Eight","
Nine"};
String
x2[]={"","Twenty","Thirty","Fourty","Fifty","Sixty","Seventy","Eighty
","Ninety"}; z=amt%10; //finding the number in
words
g=amt/10;
if(g!=1)
System.out.println(x2[g-1]+" "+x1[z]);
else System.out.println(x[amt-9]);
}}
variable description
No. Name Type Method Description
1 sc Scanner main() Scanner object
2 z int main() amt%10
3 g int main() amt/10
4 x String[] main() String array storing no.in words
5 x1 String[] main() String array storing no.in words
6 x2 String[] main() String array storing no.in words
7 amt int main() input number
2|
output
3|ISCComputerScienceProject
PROGRAM 2
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
4|
STEP 25 - PRINT " "
STEP 26 – END
solution
import java.util.*;
class Calendar
{
public void main()//main() function
{
int i,count=0,b,d=1;
Scanner sc=new Scanner(System.in);
System.out.println("Enter month");//accepting month and year
int month= sc.nextInt();
System.out.println("Enter Year");
int year= sc.nextInt();
/* Computing and displaying calendar*/
String w="SMTWTFS";
int days[]={31,28,31,30,31,30,31,31,30,31,30,31};
String month1[]={"January","Fe
scuary","March","April","May","June","July","August","September","Octobe
r","November","December"};
if((year%100==0 && year%400==0) || (year%100!=0 && year%4==0))
days[1]=29;
System.out.println("================The Calendar of
"+month1[month-1]+" "+year+" is==================");
for(i=0;i<w.length();i++)
System.out.print(w.charAt(i)+"\t");
System.out.println(" "); for(i=1;i<year;i++)
5|ISCComputerScienceProject
if((year%100==0 && year%400==0) || (year%100!=0 && year%4==0))
count+=2; else count+=1; for(i=0;i<month;i++) count+=days[i];
count+=1; count%=7; b=7-count; if(b!=1 || b!=7) while(count>0)
{System.out.print(' '+"\t"); count--;
}
for(i=1;i<7;i++)
{while(b>0 && d<=days[month-1]) {System.out.print(d+"\t"); d++; b--;
}
b=7;
System.out.println(" ");
}}}
6|
Variable Description
No. Name Type Method Description
1 sc Scanner main() Scanner object
2 i int main() loop variable
3 count int main() counter
4 b int main() week counter
5 d int main() day counter
6 month int main() input month
7 year int main() input year
8 w String main() week days
9 days String[] main() array storing days
10 month1 String[] main() array storing months
out put
7|ISCComputerScienceProject
PROGRAM 3
To Calculate Factorial Using
Recursion
ALGORITHM
STEP 1 - START
STEP 2 - INPUT n
STEP 3 - IF(n<2) THEN return 1 OTHERWISE return (n * fact(n-1))
STEP 4 – END
solution
import
java.util.*; class
Factorial
{
public static void main() //main function
{
Scanner sc=new Scanner(System.in);
System.out.println("enter no =");
int n = sc.nextInt();//accepting no.
8|ISCComputerScienceProject
Factorial obj = new Factorial();
long f = obj.fact(n);
System.out.println("Factorial ="+f); //displaying factorial
}
public long fact(int n) //recursive fact()
{if(n<2)
return 1;
else return (n*fact(n-1));
}}
variable Description
No. Name Type Method Description
1 sc Scanner main() Scanner object
2 n int main() input number
3 obj Factorial main() Factorial object
4 f long main() variable storing factorial
5 n int fact() parameter in recursive function fact()
9|ISCComputerScienceProject
output
PROGRAM 4
To Display Fibonacci Series
Using Recursion
10 | I S C C o m p u t e r S c i e n c e P r o j e c t
ALGORITHM
STEP 1 - START
STEP 2 - INPUT n
STEP 3 - IF(n<=1) THEN return 1 OTHERWISE return (fib(n-1) +fib(n-2))
STEP 4 – END
Solution
import
java.util.*;class
Fibonacci
{public static void main( ) //main function
{Fibonacci obj = new Fibonacci();
Scanner sc=new
Scanner(System.in);System.out.println("enter no of term
="); //accepting no. of terms int n = sc.nextInt();
System.out.println();
for(int i=1;i<=n;i++) //Fibonacci
element display loop {int f = obj.fib(i);
System.out.print(f+" ");
}}
public int fib(int n) //Recursive function fib() for
calculation of Fibonacci element
{if(n<=1)
return n;
else
11 | I S C C o m p u t e r S c i e n c e P r o j e c t
return (fib(n-1) +fib(n-2));
}}
Variable Description
No. Name Type Method Description
1 sc Scanner main() Scanner object
2 obj Fibonacci main() Fibonacci object
3 n int main() input number
4 i int main() loop variable for Fibonacci element
5 f int main() Fibonacci element
6 n int fib() recursive function fib() parameter
Output
12 | I S C C o m p u t e r S c i e n c e P r o j e c t
PROGRAM 5
To Calculate GCD Using
Recursion
ALGORITHM
STEP 1 - START
STEP 2 - INPUT p,q
STEP 3 - IF(q=0) THEN return p OTHERWISE return calc(q,p%q)
STEP 4 – END
Solution
import
java.util.*; class
GCD
{public static void main( ) //main function
{ Scanner sc=new Scanner(System.in);
System.out.println("enter the numbers
=");
int p = sc.nextInt(); //accepting nos.
int q = sc.nextInt();
13 | I S C C o m p u t e r S c i e n c e P r o j e c t
GCD obj = new
GCD(); int g =
obj.calc(p,q);
System.out.println("GCD ="+g);
}
public int calc(int p,int q) //recursive function calculating GCD
{if(q==0)
return p;
else return calc(q,p%q);
}}
Variable Description
No. Name Type Method Description
1 sc Scanner main() Scanner object
2 p int main() ,calc() input number
3 q int main() ,calc() input number
4 obj GCD main() GCD object
5 g int main() variable storing the GCD
14 | I S C C o m p u t e r S c i e n c e P r o j e c t
Output
15 | I S C C o m p u t e r S c i e n c e P r o j e c t
PROGRAM 6
To Display Magic 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
16 |
ISCComputerScienceProject
Solution
/*A Magic Square is a square whose sum of diagonal elements, row
elements and coloumn elements is the same*/
import java.util.*;
class MagicSquare
{
public static void main( )throws Exception //main function
{
Scanner sc=new Scanner(System.in);
System.out.println("enter the dimension of magical
square=");
int n = sc.nextInt(); //accepting dimensions
int arr[][]=new int[n][n];
int c=n/2-1,r=1,num;
for(num=1;num<=n*n;num++) //loop for finding
magic square elements
{r--;
c++;
17 |
if(r==-1)
r=n-1;
if(c>n-1)
c=0;
if(arr[r][c]
!=0)
{r=r+2; c--
;
}
arr[r][c]=n
um;
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++)
//loop displaying magic square
{for(c=0;c<n;c++)
System.out.print(arr[r][c]+" ");
System.out.println();
}}}
18 |
Variable Description
No. Name Type Method Description
1 sc Scanner main() Scanner object
2 n int main() input dimensions
3 arr int[][] main() magic square matrix
4 num int main() loop variable for magic square
5 r int main() row
6 c int main() coloumn
Output
19 |
PROGRAM 7
To Search an Array Using
20 |
Linear Search
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 REPEAT STEP 7
STEP 7 - IF (a[i] == v) THEN flag =i
STEP 8 - IF (flag=-1) THEN GOTO STEP 9 OTHERWISE GOTO STEP 10
STEP 9 - PRINT “ not found”
STEP 10 - PRINT v+" found at position - "+flag
STEP 11 – END
ISCComputerScienceProject
Solution
import java.util.*;
class LinearSearch
{int n,i;
int a[] = new int[100];
21 |
static Scanner sc =new Scanner (System.in));
public LinearSearch(int nn)
{n=nn;
}
public void input() //function for obtaining values from user
{System.out.println("enter elements");
for(i=0;i<n;i++)
{a[i] = sc.nextInt();
}}
public void display() //function displaying array values
{System.out.println();
for(i=0;i<n;i++)
{System.out.print(a[i]+" ");
}}
public void search(int v) //linear search function
{int flag=-1;
for(int i=0; i<n
; i++)
{if(a[i] == v)
flag =i;
}
if(flag== -1 )
System.out.println("not found");
else System.out.println(v+" found at position - "+flag);
}
public static void main( ) //main function
{LinearSearch obj = new
LinearSearch(10); obj.input();
obj.display();
22 |
System.out.println("enter no. to be searched -"); //accepting
the values to be searched int v = sc.nextInt(); obj.search(v);
}}
Variable Description
No. Name Type Method Description
1 sc Scanner - Scanner object
2 n int - array length
3 i int - loop variable
4 a[] int[] - input array
5 nn int LinearSearch() parameter in constructor
6 v int search(), main() search element
7 flag int search() flag
8 obj LinearSearch main() LinearSearch object
23 |
Output
PROGRAM 8
To Search an Array Using
24 |
Binary Search
Solution
import java.util.*;
class BinarySearch
{int n,i;
int a[] = new int[100];
static Scanner sc =new Scanner (System.in);
public BinarySearch(int nn) //default constructor
{n=nn;
}
public void input() //function accepting array elements
{System.out.println("enter elements");
for(i=0;i<n;i++)
{a[i] = sc.nextInt();
}}
public void display() //displaying array elements
{System.out.println();
for(i=0;i<n;i++)
{System.out.print(a[i]+" ");
}}
public void search(int v) //function to search array
elements using binary search technique
{int l=0;
int u =
n-1; int
25 |
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( ) //main function
{BinarySearch obj = new
BinarySearch(10); obj.input();
obj.display();
System.out.println("enter no. to be searched -");
int v = sc.nextInt(); //accepting integer to be searched by binary
search
obj.search(v);
}}
26 |
Variable Description
No. Name Type Method Description
1 sc Scanner - Scanner object
2 n int - array length
3 i int - loop variable
4 a[] int[] - input array
5 nn int BinarySearch() parameter in constructor
6 v int search(), main() search element
7 flag int search() flag
8 l int search() lower limit
9 u int search() upper limit
10 m int search() middle index
11 obj BinarySearch main() BinarySearch object
27 |
out p
PROGRAM 9
To Sort an Array Using
Selection Sort
ALGORITHM
28 |
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
ISCComputerScienceProject
Solution
import
java.util.*; class
SelectionSort
{int n,i;
int a[] = new int[100];
public SelectionSort(int nn) //parameterized constructor
{n=nn;
}
public void input() //function accepting array elements
29 |
{ Scanner sc =new Scanner(System.in);
System.out.println("enter elements");
for(i=0;i<n;i++)
{a[i] = sc.nextInt();
}}
public void display() //function displaying array elements
{System.out.println();
for(i=0;i<n;i++)
{System.out.print(a[i]+" ");
}}
public void sort() //function using selection sort technique
{int j,temp,min;
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;
}}}
public static void main( ) //main function
{SelectionSort x = new SelectionSort(5); x.input();
System.out.print("Before sorting -
"); x.display();
30 |
System.out.print("After sorting -
"); x.sort();
x.display();
}}
Variable Description
No. Name Type Method Description
1 sc Scanner input() Scanner object
2 n int - array length
3 i int - loop variable
4 a[] int[] - input array
5 nn int SelectionSort() parameter in constructor
6 j int sort() sort index
7 temp int sort() temporary storage
8 min int sort() minimum value
9 x SelectionSort main() Selection Sort object
Output
31 |
PROGRAM 10
To Sort an Array Using
Bubble Sort
32 |
Solution
import
java.util.*;
class
BubbleSort
{
int n,i;
int a[] = new int[100];
public BubbleSort(int nn) //parameterized
constructor
{n=nn;
}
public void input() //function accepting array elements
{ Scanner sc =new Scanner(System.in);
System.out.println("enter elements");
for(i=0;i<n;i++)
{a[i] = sc.nextInt();
}}
public void display() //function displaying array
elements
{System.out.println();
for(i=0;i<n;i++)
{System.out.print(a[i]+" ");
}}
public void sort() //function sorting array elements
using Bubble Sort technique
33 |
{int j,temp;
for(i=0 ; i<n-1 ;
i++) {for(j=0 ;
j<n-1-i ; j++)
{if(a[j] >
a[j+1])
{temp =
a[j]; a[j]
=a[j+1];
a[j+1] = temp;
}}}}
public static void main( ) //main function
{BubbleSort x = new BubbleSort(5);
x.input();
System.out.print("Before sorting -
"); x.display();
System.out.print("After sorting -
"); x.sort();
x.display();}}
Variable Description
No. Name Type Method Description
1 sc Scanner input Scanner object
2 n int - array length
3 i int - loop variable
4 a[] int[] - input array
5 nn int BubbleSort() parameter in constructor
34 |
6 j int sort() sort index
7 temp int sort() temporary storage
8 x SelectionSort main() SelectionSort object
Output
PROGRAM 11
To Convert a Decimal no. Into
its Binary Equivalent
35 | I S C C o m p u t e r S c i e n c e P r o j e c t
Solution
import java.util.*;
class Dec2Bin
{int n,i;
int a[] = new int[100];
static Scanner sc =new Scanner(System.in);
public Dec2Bin(int nn) //parameterized contructor
{n=nn;
}
public void dectobin(int no) //function converting decimalto
binary number
{int c = 0; int
temp = no;
while(temp !=
0) {a[c++] =
temp % 2;
temp = temp / 2;
}
System.out.println("Binary eq. of "+no+" = ");
for( i = c-1 ; i>=0 ; i--) //Displaying binary
number System.out.print( a[ i ] );
}
public static void main( ) //main function
{Dec2Bin obj = new Dec2Bin(30);
System.out.println("enter
decimal no -"); int no =
sc.nextInt(); obj.dectobin(no); }}
36 |
Variable Description
No. Name Type Method Description
1 sc Scanner Scanner object
37 | I S C C o m p u t e r S c i e n c e P r o j e c t
Output
PROGRAM 12
To Display Date From Entered
Day Number
ALGORITHM
STEP 1 - START
STEP 2 - INITIALISE a[ ] , m[ ]
38 |
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.util.*;
class
Day2Date
{static Scanner sc =new Scanner(System.in);
public void calc(int n, int yr) //function to calculate date
{int a[ ] = { 31,28,31,30,31,30,31,31,30,31,30,31 } ;
String m[ ] = {"Jan", "Feb",
"Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"}; if
( yr % 4 == 0)
a[1] =29;
int t=0,s=0;
while( t < n) //loop calculating date
{t =t + a[s++];
}
39 | I S C C o m p u t e r S c i e n c e P r o j e c t
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( ) //main function
{Day2Date obj = new Day2Date();
System.out.println( "Enter day no = ");
//accepting day no. int n = sc.nextInt();
System.out.println( "Enter year = ");
//accepting year
int yr =sc.nextInt(); obj.calc(n,yr);
}}
Variable Description
No. Name Type Method Description
1 sc Scanner - Scanner object
2 n int calc(), main() Day number
3 yr int calc(), main() year
40 |
4 a int[] calc() array storing day
5 m int[] calc() array storing month
6 t int calc() array index
7 s int calc() array index
8 d int calc() n+a[--s]+t
9 obj Day2Date main() Day2Date object
Output
PROGRAM 13
41 | I S C C o m p u t e r S c i e n c e P r o j e c t
To Create a Star Pattern From
Entered String
Solution
import
java.util.*;
class Pattern
{public static void main ( )
{int i,sp,j,k,l;
Scanner sc=new Scanner(System.in);
System.out.println("enter the string ="); //accepting
string
String s = sc.nextLine();
l=s.length();
/*printing the pattern*/
for(i=0;i<l;i++)
if(i==l/2)
System.out.print
ln(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));
42 |
for(j=0;j<sp-
1;j++)
System.out.print
(" "); k++;
}
System.out.println(" ");
}}}
Variable Description
No. Name Type Method Description
1 sc Scanner main() Scanner object
2 s String main() input string
3 i int main() loop variable for printing the pattern
4 sp int main() Math.abs((l/2)-i)
5 j int main() loop variable for printing the pattern
6 k int main() loop variable for printing the pattern
7 l int main() length of string
43 | I S C C o m p u t e r S c i e n c e P r o j e c t
Output
44 |
PROGRAM 14
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.util.*;
class
Palindrome
{public static void main( ) //main function
45 | I S C C o m p u t e r S c i e n c e P r o j e c t
{ Scanner sc=new Scanner(System.in);
System.out.println("enter the string=");
String s = sc.nextLine(); //accepting the string
StringBuffer sb = new StringBuffer(s);
sb.reverse();
//reversing the string String rev = new
String(sb);
if(s.equalsIgnoreCase(rev))
//checking for palindrome
System.out.println("Palindrome " );
//displaying the result else System.out.println("Not
Palindrome " );
}}
Variable Description
No. Name Type Method Description
1 sc Scanner main() Scanner object
2 s String main() input string
3 sb StringBuffer main() StringBuffer object of s
4 rev String main() revese string
46 |
Output
PROGRAM 15
To Find a Word in Entered
String
ALGORITHM
STEP 1 - START
47 | I S C C o m p u t e r S c i e n c e P r o j e c t
STEP 2 - INPUT string s
STEP 3 - StringTokenizer st = s
STEP 4 - l =str.length()
STEP 5 - INPUT look
STEP 6 - flag = -1
STEP 7 - IF (st.hasMoreElements()) REPEAT STEP 8
STEP 8 - IF (look.equals(st.nextElement())) THEN flag =1
STEP 9 - IF flag = - 1 GOTO STEP 10 OTHERWISE STEP 11
STEP 10 - PRINT "word not found"
STEP 11 - PRINT "word found"
STEP 12 – END
Solution
import
java.util.StringTokenizer
; import
java.util.*;public class
WordSearch
{public static void main() //main function
{ Scanner sc=new Scanner(System.in);
System.out.println("enter the string=");
String s = sc.nextLine(); //accepting string
StringTokenizer st = new StringTokenizer(s," ");
//StringTokenizer initialization
System.out.println("enter the word to be searched =");
String look =
sc.nextLine(); int flag =
-1;
48 |
while(st.hasMoreElements())
//searching for word
{if(look.equals(st.nextEleme
nt())) flag =1;
}
if(flag ==-1)
{System.out.println("the word not found"); //displaying
the result
}
else {
System.out.println("the word found");
}}}
Variable Description
No. Name Type Method Description
1 sc Scanner main() Scanner object
2 s String main() input string
3 st StringTokenizer main() StringTokenizer object
4 look String main() word to be searched
5 flag int main() flag
49 | I S C C o m p u t e r S c i e n c e P r o j e c t
Output
PROGRAM 16
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
50 |
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
import
java.util.*;
class Decode
{public void compute() //compute() function
{ Scanner sc=new Scanner(System.in);
System.out.println(“Enter name:”);
String name= sc.nextLine();
System.out.println(“Enter
number:”); int n= sc.nextInt();
int j,i,l,c=0,y,n1;
l=name.length();
51 | I S C C o m p u t e r S c i e n c e P r o j e c t
System.out.println("original string is
"+name); for(i=0;i<l;i++)
{char c1=name.charAt(i);
try //trying for
NumberFormatException {c=(int)c1 ;
}
catch(NumberFormatException e)
{}
if(n>0)
{if((c+n)<=90)
/*Decoding String*/
System.out.print((char
)(c+n)); else {c=c+n;
c=c%10; c=65+(c-1);
System.out.print((char)(c));
}}
else if(n<0)
{n1=Math.abs(n);
if((c-n1) >=65)
System.out.print((char) (c-n1));
else
{if(c>65)
c=c-65;
else
c=n1;
System.out.print((char)(90-(c-1)));
}}
else if (n==0)
{System.out.println("no change
"+name);
52 |
}}}}
Variable Description
No. Name Type Method Description
1 sc Scanner compute() Scanner object
2 name String compute() input string
3 n int compute() decode number
4 j int compute() loop variable
5 i int compute() loop variable
6 l int compute() length of string
7 c int compute() A SCII of c1
8 y int compute()
9 n1 int compute()
Output
53 | I S C C o m p u t e r S c i e n c e P r o j e c t
PROGRAM 17
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.util.*;
class Alpha
{String str;
54 |
int l;
char c[] = new char[100];
public Alpha() //Alpha() constructor
{str = "";
l =0;
}
public void nextword() //function to next input string
{System.out.println("enter word - ");
Scanner sc =new Scanner(System.in); str = sc.nextLine();
l = str.length();
}
public void arrange() //function to arrange string
in a scending order
{int i,j;
char temp;
for(i=0;i<l;i++)
{c[i]=
str.charAt(i);
}
for(i=0;i<l-1;i++) //loops for swapping of
characters
{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;
}}}}
55 | I S C C o m p u t e r S c i e n c e P r o j e c t
public void display() //function to display the
rearranged string
{System.out.println()
; for(int i=0;i<l;i++)
{System.out.print(c[i
]);
}}
public static void main( ) //main function
{Alpha obj = new
Alpha();
obj.nextword();
obj.arrange();
obj.display();
}}
56 |
Variable Description
No. Name Type Method Description
1 sc Scanner nextword() Scanner object
2 str String - input string
3 l int - length
4 c char[] - character array
5 i int nextword() loop variable
6 j int nextword() loop variable
7 temp char nextword() temporary storage
8 obj Alpha main() Alpha object
Output
57 | I S C C o m p u t e r S c i e n c e P r o j e c t
PROGRAM 18
To Create a String and Count
Number of Vowels and
Consonants
Solution
import java.util.*;
class Vowels
{public static void main( ) //main function
{ Scanner sc=new Scanner(System.in);
System.out.println("Enter a string");
String a= sc.nextLine(); //Accepting string
int z=a.length(),y,x=0,b=0;
for(y=0;y<z;y++) //loop for counting number of vowels
{if(a.charAt(y)=='a'||a.charAt(y)=='e'||a.charAt(y)=='i'||a.charAt(y)=='o'||a.c
harAt(y)=='u') x++; else b++;
}
System.out.println("Number of vowels in string ="+x); //displaying
result System.out.println("Number of consonants in string ="+b);
}}
58 | I S C C o m p u t e r S c i e n c e P r o j e c t
Variable Description
No. Name Type Method Description
1 sc Scanner main() Scanner object
2 a String main() input string
3 z int main() length of string
4 y int main() loop variable
5 b int main() no. of consonants
6 x int main() no. of vowels
Output
59 | I S C C o m p u t e r S c i e n c e P r o j e c t
PROGRAM 19
To Create a String and Count
Number of Words
ALGORITHM
STEP 1 - START
STEP 2 - a = "Computer Applications"
STEP 3 - z = a.length()
STEP 4 - x= 0
STEP 5 - FROM y =0 to y<z REPEAT STEP 6
STEP 6 - IF (a.charAt(y)==' ' ) then x =x+1
STEP 7 - PRINT "Number of words in string ="+(x+1)
STEP 8 – END
Solution
import
java.util.*;
class
NoOfWords
60 | I S C C o m p u t e r S c i e n c e P r o j e c t
{public static void main( )
{ Scanner sc=new Scanner(System.in);
System.out.println("Enter Sentence");
String a= sc.nextLine();
//accepting string
System.out.println("The string is -
"+a); int z=a.length(),y,x=0;
for(y=0;y<z;y++) //loop for counting number of spaces
{if(a.charAt(y)==' ')
x=x+1;
}
System.out.println("Number of words in string ="+(x+1));
//displaying result
}}
Variable Description
No. Name Type Method Description
1 sc Scanner main() Scanner object
2 z int main() length of string
3 a String main() input string
4 x int main() space counter
5 y int main() loop variable
61 | I S C C o m p u t e r S c i e n c e P r o j e c t
Output
PROGRAM 20
To Generate Sum of All
Elements of a Double
Dimensional Array of 5*5
Sub scripts
ALGORITHM
62 | I S C C o m p u t e r S c i e n c e P r o j e c t
STEP 1 - START
STEP 2 - INPUT a[]
STEP 3 - FROM x =0 to x<5 REPEAT STEP 4
STEP 4 - FROM y =0 to y<5 REPEAT STEP 5
STEP 5 - PRINT (a[x][y]+" "
STEP 6 - FROM x =0 to x<5 REPEAT STEP 7
STEP 7 - FROM y =0 to y<5 REPEAT STEP 8
STEP 8 - Sum=Sum+a[x][y]
STEP 9 - PRINT Sum
STEP10 – END
Solution
import
java.util.*;
class MatrixSum
{public static void main( ) //main function
{ int a[][]=new int[5][5];
Scanner aa=new Scanner(System.in);
int x,y,z,Sum=0;
System.out.println("Enter the array");
for(x=0;x<5;x++)
{for(y=0;y<5;y++)
{ z=aa.nextInt(); //accepting array element
a[x][y]=z;
}}
System.out.println("Array -");
63 | I S C C o m p u t e r S c i e n c e P r o j e c t
for(x=0;x<5;x++) //loop for printing array
{for(y=0;y<5;y++)
{System.out.print(a[x][y]+" ");
}
System.out.print("\n");
}
for(x=0;x<5;x++) //loop for printing sum of array
elements
{for(y=0;y<5;y++)
{Sum=Sum+a[x][y];
}}
System.out.println("Sum of Array elements="+Sum);
//displaying sum
}}
Variable Description
No. Name Type Method Description
1 aa Scanner main() Scanner object
2 a int[][] main() input array
3 x int main() loop variable
4 y int main() loop variable
5 z int main() input element
6 Sum main() main() Sum of all elements
Output
64 | I S C C o m p u t e r S c i e n c e P r o j e c t
PROGRAM 21
To Find Sum of Each Column of
a Double Dimensional
Array
ALGORITHM
STEP 1 - START
STEP 2 - INPUT a[]
STEP 3 - FROM x =0 to x<4 REPEAT STEP 4
STEP 4 - FROM y =0 to y<4 REPEAT STEP 5
STEP 5 - PRINT (a[x][y]+" "
STEP 6 - FROM x =0 to x<4 REPEAT STEP 7 , STEP 9 and STEP 10
STEP 7 - FROM y =0 to y<4 REPEAT STEP 8
STEP 8 - Sum=Sum+a[x][y] ,
65 | I S C C o m p u t e r S c i e n c e P r o j e c t
STEP 9 - PRINT Sum
STEP 10 - Sum = 0
STEP11 – END
Solution
import
java.util.*;
class
ColoumnSum
{public static void main( ) //main function
{int a[][]=new int[4][4];
Scanner aa=new Scanner(System.in);
int x,y,z,Sum=0;
System.out.println("Enter the array");
for(x=0;x<4;x++)
{for(y=0;y<4;y++)
{z=aa.nextInt();
a[x][y]=z;
}}
System.out.println("Array -"); //printing the
array in matrix form for(x=0;x<4;x++) {for(y=0;y<4;y++)
{System.out.print(a[x][y]+" ");
}System.out.print("\n");
}
for(y=0;y<4;y++)
{for(x=0;x<4;x++)
66 | I S C C o m p u t e r S c i e n c e P r o j e c t
{Sum=Sum+a[x][y];
}
System.out.println("Sum of column "+(y+1)+" is "+Sum);
//printing sum of coloumn Sum=0;
}}}
Variable Description
No. Name Type Method Description
1 aa Scanner main() Scanner object
2 a int[][] main() input array
3 x int main() loop variable
4 y int main() loop variable
5 z int main() input element
6 Sum int main() Sum of each couloumn
Output
67 | I S C C o m p u t e r S c i e n c e P r o j e c t
PROGRAM 22
To Find Sum of Diagonal of a
Double Dimensional Array of
4*4 Sub scripts
Solution
import
java.util.*;
class
DiagonalSum
{public static void main( ) //main function
{int a[][]=new int[4][4];
Scanner aa=new Scanner(System.in); int x,y,z,Sum=0;
System.out.println("Enter the array");
for(x=0;x<4;x++) //Nexting array
{for(y=0;y<4;y++)
{z=aa.nextInt();
a[x][y]=z;
}}
System.out.println("Array -");
for(x=0;x<4;x++) //displaying array
68 | I S C C o m p u t e r S c i e n c e P r o j e c t
{
for(y=0;y<4;y++)
{
System.out.print(a[x][y]+" ");
}
System.out.print("\n");
}
y=0;
for(x=0;x<4;x++) //loop for finding sum of diagonal
{Sum=Sum+a[x][y];
y=y+1;
}
System.out.println("Sum of diagonal is "+Sum); //displaying the
sum of diagonal
Sum=0;
}}
Variable Description
No. Name Type Method Description
1 aa Scanner main() Scanner object
2 a int[][] main() input matrix
3 x int main() loop variable
4 y int main() loop variable
5 Sum int main() Sum of diagonals
6 z int main() input element
69 | I S C C o m p u t e r S c i e n c e P r o j e c t
Output
PROGRAM 23
To Calculate the Commission
of a Salesman as per the
Following Data
Sales Commission
>=100000 25% of sales
80000-99999 22.5% of sales
60000-79999 20% of sales
40000-59999 15% of sales
<40000 12.5% of sales
70 | I S C C o m p u t e r S c i e n c e P r o j e c t
ALGORITHM
STEP 1 - START
STEP 2 - INPUT sales
STEP 3 - IF (sales>=100000) THEN comm=0.25 *sales OTHERWISE GOTO STEP 4
STEP 4 - IF (sales>=80000) THEN comm=0.225*sales OTHERWISE GOTO STEP 5
STEP 5 - IF (sales>=60000) THEN comm=0.2 *sales OTHERWISE GOTO STEP 6
STEP 6 - IF (sales>=40000) THEN comm=0.15 *sales OTHERWISE GOTO STEP 7
STEP 7 - comm=0.125*sales
STEP 8 - PRINT "Commission of the employee="+comm
STEP 9 – END
Solution
import
java.util.*;
class Sale
scomission
{public static void main( ) //main function
{double sales,comm;
Scanner aa=new Scanner(System.in);
System.out.println(“Enter sales”);
sales=aa.nextDouble();
/*calculating commission*/
if(sales>=100000) comm=0.25*sales; else if(sales>=80000)
comm=0.225*sales;
71 | I S C C o m p u t e r S c i e n c e P r o j e c t
else
if(sales>=60000) comm=0.2*sales; else if(sales>=40000)
comm=0.15*sales;
else
comm=0.125*sales;
System.out.println("Commission of the employee="+comm);
//displaying commission
}}
Variable Description
No. Name Type Method Description
1 aa Scanner main() Scanner object
2 sales double main() sales
3 comm. double main() commision
Output
72 | I S C C o m p u t e r S c i e n c e P r o j e c t
PROGRAM 24
To check the validity of a date
Solution
import java.util.*;
class check
{public void main()
{Scanner sc=new Scanner(System.in);
System.out.println("enter DATE");
int dd=sc.nextInt();
int mm=sc.nextInt();
int yy=sc.nextInt();
int m[]={0,31,28,31,30,31,30,31,31,30,31,30,31};
int f=0;
if(((yy%4==0)&&(yy%100!=0))||yy%400==0)
{if(mm==2)
{m[mm]=m[mm]+1;}
}
if((mm<1)||(mm>12))
73 | I S C C o m p u t e r S c i e n c e P r o j e c t
{f=1;
}
else
if(dd>m[mm])
{ f=1;}
if(f==1)
System.out.println("invalid date");
else
System.out.println("valid date");
}}
Variable Description
No. Name Type Method Description
1 sc Scanner main() Scanner object
2 dd,mm,yy int main() Storing date
3 m int[] main() array storing number of days of months
4 f int main() Flag value
Output
74 | I S C C o m p u t e r S c i e n c e P r o j e c t
PROGRAM 25
To Convert Celsius into
Fahrenheit Using Inheritence
ALGORITHM
STEP 1 – START
STEP 2 -- Input temperature ‘celcius’ in celcius
STEP 3 – far=1.8*celcius + 32
STEP 4 – Display far
STEP 5 -- END
75 | I S C C o m p u t e r S c i e n c e P r o j e c t
Solution
import
java.util.*;
class C2F
{ public static void main( ) //main function
{Temperature ob= new Temperature();
Scanner sc=new Scanner(System.in);
System.out.println("Enter temperature in Celsius");
//accepting temperature
double temp=ob.convert(Double.parseDouble( sc.nextLine()));
System.out.println("The temperature in fahrenheit is = "+temp);
}}
class Temperature extends C2F
{double convert(double celcius) //function to convert Celsius to
fahrenheit
{double far=1.8*celcius+32.0;
return far;
}}
Variable Description
No. Name Type Method Description
1 sc Scanner main() Scanner object
2 ob C2F main() C2F object
76 | I S C C o m p u t e r S c i e n c e P r o j e c t
3 temp double main() calculated Fahrenheit temperature
4 celcius double convert() input temperature in Celsius
5 far double convert() Calculated Fahrenheit temperature
Output
77 | I S C C o m p u t e r S c i e n c e P r o j e c t