Computer Project Finals
Computer Project Finals
ACKNOWLEDGEMENT
I would like to express my
special thanks of gratitude to my
teacher (Swati Ma’am) as well as
our principal (Sangeeta ma’am)
who gave me the golden
opportuniuty to do this wonderful
project on the topic (JAVA
PROGRAMS),which also helped
me in doing a lot of research and
I came to know about so many
new things I am really thankful
to them. Secondly I would also
like to thanks my parents and
friends who helped me a lot in
finalizing this project within the
limited time frame.
Page 2 of 41
NAME :
AKARSH SRIVASTAVA
CLASS : XI-B
SUBJECT :
COMPUTER SCIENCE
ROLL NO. :
08
Page 3 of 41
Program-1
Data members :
n - stores the inputted number
Member Functions :
happy_recursion() - default constructor
void getnum(int nn) - for initializing n with nn
int sum_sq_digits(int x) - for calculating sum of
square of digits.
void ishappy() - for checking whether number is
happy number or not.
main() - for calling the functions
Algorithm :
(i) Start
(ii) Accept a digit number.
(iii) Pass the number through the parameterized
constructor for initialization
(iv) Extract last digit.
(v) Multiply the last digit with its position value and
then add it up.
(vi) Drop the last digit of the original number.
(vii) Check whether the sum calculated from above
three steps is divisible by 11.
Page 4 of 41
(viii) If the sum is divisible by 11 then, it is a valid
ISBN number.
(ix) Else it is not a valid ISBN number.
(x) End
Code :
import java.util.Scanner;
class happy_recursion
{
int n;
happy_recursion()
{
n=0;
}//default constructor
void getnum(int nn)
{
n=nn;
}
int sum_sq_digits(int x)
{
int sum=0;
while(x!=0)
{
int d=x%10;
sum += Math.pow(d,2);
x /= 10;
}//for calculating sum of digits
if(sum > 9)
return sum_sq_digits(sum);
else
return sum;
}
Page 5 of 41
void ishappy()
{
if(sum_sq_digits(n) == 1)
System.out.println("Happy number");
else
System.out.println("Not a happy number");
}//for checking for happy number
public static void main()
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter a number");
int a = sc.nextInt();
happy_recursionobj = new happy_recursion();
obj.getnum(a);
obj.ishappy();
}//main
}//class
VDC :
Input/Output :
1) INPUT :
Enter a number
100
OUTPUT :
Happy number
Page 6 of 41
2) INPUT :
Enter a number
121
OUTPUT :
Not a happy number
3)INPUT :
Enter a number
320
OUTPUT :
Happy number
Program-2
Page 7 of 41
Question: Write a program for finding a number in a
one dimensional array using recursion.
Class name : binary_search
Data Members :
A[] - 1D array for storing numbers in ascending
order
n - for storing the number to be searched
l - for storing the lower limit of the array
u - for storing the upper limit of the array
Member methods :
binary_search(int nn) - for storing the data members
with values.
void readdata() - for storing values in array and
number to be searched.
int binary(int v) - for searching the number in the
array using binary search.
main() - for calling the functions and accepting the
size of the array.
Algorithm :
(i) Start
(ii) Enter elements in array in ascending order.
(iii) Enter number to be searched.
(iv) Check for the number in the array and return the
index of the number.
(v) If the index is -1 then the number is not found in
the array, else it is found at the returned index.
(vi) Display the output.
(vii) End
Page 8 of 41
Code :
import java.util.Scanner;
class binary_search
{
int A[];int n,l,u;
binary_search(int nn)
{
n=nn;l=0;u=nn-1;
A = new int[n];
}//parameterized constructor
void readdata()
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter elements in ascending
order");
for(int i=0;i<n;i++)
A[i] = sc.nextInt();
System.out.println("Enter number to be searched");
int x = sc.nextInt();
int y = binary(x);//for storing the index of the
number to be searched
if(y==-1)
System.out.println("Number not found");
else
System.out.println("Number found at index = "+y);
}
int binary(int v)
{
int mid = l+u/2;
if(A[mid] == v)
return mid;
if(l>u)
Page 9 of 41
return -1;
if(A[mid]>v)
u = mid-1;
else
l = mid+1;
return binary(v);
}//binary search code using recursion
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter size of the array");
int nn = sc.nextInt();
binary_searchobj = new binary_search(nn);
obj.readdata();
}//end of main
}//end of class
VDC :
Page 10 of 41
Input/Output :
1) INPUT :
Enter size of the array
5
Enter elements in ascending order
2
4
6
7
8
Enter number to be searched
4
OUTPUT:
Number found at index = 1
Program-3
Question : Write a program to enter a string and
encrypt or decrypt the string as per the choice of the
user. The shift code for encryption /decryption is
3.for encryption increase each letter by 3 and
decrease by 3 for decryption.
Class name : cipher
Data Members:
n - for storing the string inputted by the user
a - for storing the encrypted/decrypted string.
Page 11 of 41
x - for storing the choice of the user
Member Methods :
void accept() - for entering the string and choice of
the user.
void encrypt() - for encrypting the string
void decrypt() - for decrypting the string
void display() - for printing the message.
Algorithm :
(i) Start
(ii) Enter a string.
(iii) Accept the choice of the user whether to encrypt
or decrypt the string.
(iv) If user chooses encryption then add shift code
to the letters of string.
(v) If the user chooses decryption then subtract shift
code from the letters of string.
(vi) Print the message.
(vii) End
Code :
import java.util.*;
class cipher
{
String n;Stringa;int x;
void accept()
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter string");
n = sc.nextLine();a="";
Page 12 of 41
System.out.println("Enter 1 to encrypt and 2 to
decrypt");
x = sc.nextInt();
switch(x)
{
case 1:
encrypt();break;
case 2:
decrypt();
}//switch case for deciding whether to encrypt
or decrypt
}
void encrypt()
{
for(int i=0;i<n.length();i++)
{
int p = (int)n.charAt(i);
a += (char)(p+3);
}
}//code for encryption
void decrypt()
{
for(int i=0;i<n.length();i++)
{
int p = (int)n.charAt(i);
a += (char)(p-3);
}
}//code for decryption
void display()
{
if(x==1)
System.out.println("Encrypted message = "+a);
else
Page 13 of 41
System.out.println("Decrypted message = "+a);
}//for displaying the encrypted/decrypted
message
public static void main()
{
cipher obj1 = new cipher();
obj1.accept();obj1.display();
}//main
}//class
VDC :
Input/Output :
Page 14 of 41
1) INPUT :
Enter string
act
Enter 1 to encrypt and 2 to decrypt
1
OUTPUT :
Encrypted message = dfw
2) INPUT :
Enter string
dfw
Enter 1 to encrypt and 2 to decrypt
2
OUTPUT :
Decrypted message = act
Program-4
Question : Write a program to enter a sentence and
print the words in the sentence in the lexicographic
order.
Data Members :
ar[] - array for storing the words in the sentence.
Algorithm :
(i) Start
(ii) Accept a sentence.
(iii) Calculate the number of words in the sentence.
(iv)Store the words of the sentence separately in a
string array.
(v)Sort the array lexicographically.
Page 15 of 41
(vi)Print the sorted array.
(vii)End
Code :
import java.io.*;
class lexicograph
{
public static void main(String args[])throws
IOException
{
BufferedReaderbr = new BufferedReader(new
InputStreamReader(System.in));
System.out.println("Enter a sentence");
String n = br.readLine();n += " ";
int x = 0;
for(int i=0;i<n.length();i++)
{
char p = n.charAt(i);
if(Character.isWhitespace(p))
x++;
}
int t=0,t1=0;
Page 16 of 41
String ar[] = new String[x];
for(int i=0;i<n.length();i++)
{
char p = n.charAt(i);
if(Character.isWhitespace(p))
{
ar[t] = n.substring(t1,i);
t++;t1=i;
}
}
ar[0] = " "+ar[0];//for maintaining the space
for(int i=0;i<x;i++)
{
for(int j=i+1;j<x;j++)
{
String p = ar[i].trim();
String q = ar[j].trim();
if(p.compareTo(q) > 0)
{
String temp = ar[i];
ar[i] = ar[j];
ar[j] = temp;
}
}
}//using bubble sort technique for sorting the
array
System.out.println("Sentence in lexicograph order");
for(int i=0;i<x;i++)
{
System.out.print(ar[i]);
}//for printing the lexicographically arranged
array
}//main
Page 17 of 41
}//class
VDC :
1) INPUT :
Enter number of words in the sentence
4
Enter words of the sentence one by one
my
name
is
parth
OUTPUT :
Sentence in lexicograph order
is my name parth
Page 18 of 41
Program-5
Question : Write a program to enter city names and
city codes in two separate single dimensional
arrays. According to the user’s choice, either enter
city name and find the city code for that city name
or enter the city code and find the name of the city.
Display appropriate message if the name/code not
found.
Data Members :
n - size of array
scode[] - array for storing city codes
city[] - array for storing the names of the city
Members Methods :
void city() - for finding the city name using city code
void code() - for finding the city code using the
name of the city
Page 19 of 41
Algorithm :
(i) Start
(ii) Accept the number of city names to be entered
(iii) Accept the names and codes of the cities and
store in the array.
(iv) Ask for user’s choice either to enter city name
and find the city code for that city name or enter the
city code and find the name of the city.
(v) If user asks for city name using city code then
find whether the city code exists or not. If exists
then print the city name on that index.
(vi) If user asks for city code using city name then
find whether the city name exists or not. If exists
then print the city code on that index.
(vii) End
Code :
import java.io.*;
class std
{
static int n;static int scode[];static String city[];
static BufferedReaderbr = new
BufferedReader(new
InputStreamReader(System.in));
public static void main(String args[])throws
IOException
{
System.out.println("Enter number of city names");
Page 20 of 41
n = Integer.parseInt(br.readLine());
scode = new int[n];
city = new String[n];
System.out.println("Enter city name alongwith std
code");
for(int i=0;i<n;i++)
{
city[i] = br.readLine();
scode[i] = Integer.parseInt(br.readLine());
}//for storing the city name alongwith its city
code
System.out.println("Enter choice");
System.out.println("1. enter the code and print the
city name");
System.out.println("2. enter city name and print the
city code");
int choice = Integer.parseInt(br.readLine());
std obj = new std();
switch(choice)
{
case 1:
obj.city();break;
case 2:
obj.code();break;
}//for deciding whether to find city name for city
code or vice versa
}
void city()throws IOException
{
System.out.println("Enter code of the city");
int code =
Integer.parseInt(br.readLine());boolean b=false;
for(int i=0;i<n;i++)
Page 21 of 41
if(code == scode[i])
{ System.out.println("City name = "+city[i]);
b=true;}
if(b == false)
System.out.println("City not found");
}//for finding city name using city code
void code()throws IOException
{
System.out.println("Enter name of the city");
String c = br.readLine();boolean b=false;
for(int i=0;i<n;i++)
if(c.equalsIgnoreCase(city[i]))
{ System.out.println("City code = "+scode[i]);
b=true;}
if(b == false)
System.out.println("Code not found");
}//for finding city code using city name
}//end of class
VDC :
Variable Data Type Description
br BufferedReader Object of Buffer class
socde[] int For storing the city
codes
city[] String For storing the city
names
Input/Output :
INPUT :
Enter number of city names
3
Enter city name alongwith std code
lko
Page 22 of 41
123
dhl
345
pune
456
Enter choice
1. enter the code and print the city name
2. enter city name and print the city code
2
Enter name of the city
dhl
OUTPUT :
City code = 345
Program-6
Question : Write a program which takes a string.
The words in this string are assumed to be
separated by one or more blanks.
Page 23 of 41
length words should be sorted alphabetically. Each
word must start with an
uppercase letter and the sentence should be
terminated by a full stop.
Algorithm :
(i) Start
(ii) Accept a sentence.
(iii) Calculate the number of words in the sentence.
(iv) Store the words of the sentence separately in a
string array.
(v) Sort the array according to the decreasing length
of the words.
Page 24 of 41
(vi) Print the sorted array.
(vii) End
Code :
import java.io.*;
class size
{
public static void main(String args[])throws
IOException
{
BufferedReaderbr = new BufferedReader(new
InputStreamReader(System.in));
System.out.println("Enter a string");
String n = br.readLine();n += " ";
int x = 0;
for(int i=0;i<n.length();i++)
{
char p = n.charAt(i);
if(Character.isWhitespace(p))
x++;
}//for number of words in the string
int t=0,t1=0;
String ar[] = new String[x];
for(int i=0;i<n.length();i++)
{
char p = n.charAt(i);
if(Character.isWhitespace(p))
{
ar[t] = n.substring(t1,i);
t++;t1=i;
}
}//storing words in array
Page 25 of 41
for(int i=0;i<x;i++)
{
for(int j=0;j<x;j++)
{
if(ar[i].length()<ar[j].length())
{
String temp = ar[i];
ar[i] =ar[j];
ar[j] = temp;
}
}
}//bubble sort
for(int i=0;i<x;i++)
{
System.out.print(ar[i]);
}
}//main
}//class
VDC :
Input/Output :
INPUT :
Page 26 of 41
Enter a string
a bb ccc ddddddeeee
OUTPUT :
a bb ccc eeeedddddd
Program-7
Question : Write a program to input a natural
number less than 1000 and then print it in words.
INPUT :29
OUTPUT : Twenty Nine
INPUT :123423
OUTPUT : OUT OF RANGE
INPUT :119
OUTPUT : ONE HUNDRED AND NINETEEN
Page 27 of 41
INPUT :225
OUTPUT : TWO HUNDRED AND TWENTY FIVE
INPUT :500
OUTPUT : FIVE HUNDRED
Algorithm :
(i) Start
(ii) Accept the number from the user.
(iii) Check for the validity of the number(<1000).
(iv) Convert the number into words.
(v) Print the converted words.
(vi) End
Code:
import java.io.*;
class num
{
public static void main(String args[])throws
IOException
{
BufferedReaderbr = new BufferedReader(new
InputStreamReader(System.in));
System.out.println("Enter the number");
int n = Integer.parseInt(br.readLine());String a
= "";
if(n>1000 || n<0)
{
System.out.println("Out of range");
System.exit(0);
Page 28 of 41
}//validity of the number
String h = "HUNDRED";
String digit[] =
{"ZERO","ONE","TWO","THREE","FOUR","FIVE","S
IX","SEVEN","EIGHT","NINE"};
String digit2[] =
{".","ELEVEN","TWELVE","THIRTEEN","FOURTEE
N","FIFTEEN","SIXTEEN","SEVENTEEN","EIGHTE
EN","NINETEEN"};
String tens[] =
{".","TEN","TWENTY","THIRTY","FOURTY","FIFTY"
,"SIXTY","SEVENTY","EIGHTY","NINETY","HUND
RED"};
if(n<10)
a = digit[n];//1-digit number
else if(n%10==0)
{
if(n<100)
a = tens[n/10];
else if(n%100==0)
a = digit[n/100]+" "+h;
else
a = digit[n/100]+" "+h+" AND "+tens[(n
%100)/10];
}//for all numbers ending with 0
else if(n>10 && n<20)
a = digit2[n-10];
else if(n>20 && n<100)
a = tens[n/10]+" "+digit[n%10];
else if((n%100)>10 && (n%100)<20)
a = digit[n/100]+" "+h+" AND "+digit2[n
%10];
else
Page 29 of 41
a = digit[n/100]+" "+h+" AND "+tens[(n
%100)/10]+" "+digit[n%10];
System.out.println(a);
}//main
}//class
VDC :
Input/Output :
1) INPUT :
Enter the number
23
OUTPUT :
TWENTY THREE
2) INPUT :
Enter the number
12344
OUTPUT :
Out of range
3) INPUT :
Enter the number
224
OUTPUT :
TWO HUNDRED AND TWENTY FOUR
4) INPUT :
Page 30 of 41
Enter the number
115
OUTPUT :
ONE HUNDRED AND FIFTEEN
5) INPUT :
Enter the number
500
OUTPUT :
FIVE HUNDRED
Program-8
Algorithm :
(i) Start
(ii) Declare a string variable.
(iii) Ask the user to initialize the string.
(iv) Call a function to check whether the string is
palindrome or not.
(v) If a string is empty, then it is a palindrome.
(vi) If the string is not empty, then call a recursive
function.
(vii) If there is only one character, then it is a
palindrome.
(viii) If the first and last characters do not match,
then it is not a palindrome.
Page 31 of 41
(ix) If there are multiple characters, check if the
middle substring is also palindrome or not using
the recursive function.
(x) Print the result.
(xi) Stop.
Code :
import java.io.*;
class palin_recursion
{
public static void main(String args[])throws
IOException
{
BufferedReaderbr = new BufferedReader(new
InputStreamReader(System.in));
System.out.println("Enter a string");
String str = br.readLine();
palin_recursionobj = new palin_recursion();
if(obj.isPalindrome(str))
System.out.println("It is palindrome");
else
System.out.println("It is not a palindrome");
}
booleanisPalindrome(String str)
{
if(str.length() == 0 || str.length()==1)
return true;//base case
Page 32 of 41
if(str.charAt(0) == str.charAt(str.length()-1))
return
isPalindrome(str.substring(1,str.length()-1));//checki
ng whether 1st and last characters are same
return false;
}//main
}//class
VDC :
Input/Output :
INPUT :
Enter a string
hannah
OUTPUT :
It is palindrome
Enter a string
parth
OUTPUT :
It is not a palindrome
Page 33 of 41
Program-9
Question : A class dec_bin has been defined to
convert a decimal number into its equivalent
binary number. Some of the members of the class
are given below:
Data members:
n - stores the decimal number
s - stores the binary equivalent of the number
Member functions:
dec_bin() - constructor to assign initial value to the
data members
void getdata() - to accept the value of n.
void recursive(int) - to calculate the binary
equivalent of ‘n’ using the recursive technique.
void putdata() - to display the decimal number ‘n’
and its binary equivalent.
Page 34 of 41
Algorithm :
(i) Start
(ii) Accept a number from the user.
(iii) Convert the number using recursion into its
binary equivalent.
(iv) Print the original number and its binary
equivalent.
(v) End
Code :
import java.io.*;
class dec_bin
{
int n;ints;inti;
dec_bin()
{
n=0;s=0;i=0;
}//default constructor
void getdata()throws IOException
{
BufferedReaderbr = new BufferedReader(new
InputStreamReader(System.in));
System.out.println("Enter a number");
n = Integer.parseInt(br.readLine());
recursive(n);
}//for accepting the number
void recursive(int x)
{
Page 35 of 41
int z=0;
if(x>0)
{
z = x%2;
recursive(x/2);
s = s*10+z;
}
}//for decimal binary conversion using recursion
void putdata()
{
System.out.println("Original number = "+n);
System.out.println("Binary equivalent = "+s);
}//for printing the numbers
public static void main(String args[])throws
IOException
{
dec_binobj = new dec_bin();
obj.getdata();obj.putdata();
}//main
}//class
VDC :
Input/Output :
1) INPUT :
Page 36 of 41
Enter a number
65
OUTPUT :
Original number = 65
Binary equivalent = 1000001
2) INPUT :
Enter a number
678
OUTPUT :
Original number = 678
Binary equivalent = 1010100110
Program-10
Question : A class dec_oct has been defined to
convert a decimal number into its equivalent octal
number. Some of the members of the class are
given below.
Page 37 of 41
Class name - dec_oct
Data members / instance variables:
n - stores the decimal number
oct - stores the octal equivalent number
Member functions :
dec_oct() - a constructor to initialize the data
members n=0, oct=0.
void getnum(intnn) - assig nn to n
void deci_oct ( ) - calculates the octal equivalent of
‘n’ and stores it in oct using the Recursive
Technique.
void show() - displays the decimal number ‘n’, calls
the function deci_oct() and displays its octal
equivalent.
Algorithm :
(i) Start
(ii) Accept a number from the user.
(iii) Convert the number using recursion into its octal
equivalent.
Page 38 of 41
(iv) Print the original number and its octal
equivalent.
(v) End
Code :
import java.util.*;
class dec_oct
{
int n;int oct;
dec_oct()
{
n=0;oct=0;
}//default constructor
void getnum(int nn)
{
n=nn;
}
void octal(int x)
{
int z;
if(x>0)
{
z = x%8;
octal(x/8);
oct = oct*10+z;
}
}//for decimal octal conversion using recursion
void show()
{
System.out.println("Original number = "+n);
octal(n);
System.out.println("Octal equivalent = "+oct);
Page 39 of 41
}//for printing the numbers
public static void main(String args[])
{
dec_octobj = new dec_oct();
Scanner sc = new Scanner(System.in);
System.out.println("Enter a number");
int nn = sc.nextInt();
obj.getnum(nn);obj.show();
}//main
}//class
VDC :
Input/Output :
1) INPUT :
Enter a number
214
OUTPUT :
Original number = 214
Octal equivalent = 326
2) INPUT :
Page 40 of 41
Enter a number
454
OUTPUT :
Original number = 454
Octal equivalent = 706
Page 41 of 41