0% found this document useful (0 votes)
21 views

Computer Science

documents uploaded directly by the professor

Uploaded by

tnimisha806
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views

Computer Science

documents uploaded directly by the professor

Uploaded by

tnimisha806
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 137

COMPUTER SCIENCE

PROJECT

NAME: NASHRA AKHLAQ ANSARI


CLASS: 12th
SECTION : B
ACKNOWLEGDEMENT
I would like to express my special thanks of gratitude to my computer teacher Mrs
S.Waqar who gave me the golden opportunity to do this wonderful project which
helped me in doing a lot of research and I came to know about so many new
things.
I am really thankful to my parents who provided me the environment for
completing my project and for being my support throughout.
Lastly but not least I want to thank my friends who were there for me everytime
and offered their help and support.
A heartfelt thanks to all those who believed in me and supported me through
thick and thin. This project helped me acquire a lot of knowledge.
-NASHRA AKHLAQ ANSARI
12th B
STRING PROGRAMS

• Converting uppercase to lowercase and lowercase to uppercase

ALGORITHM
STEP 1: START THE ALGORITHM.
STEP 2: CREATE CLASS Convert.
STEP 3: CREATE METHOD main.
STEP 4: TAKE INPUT FROM USER.
STEP 5: INITIALISE l AND STORE THE LENGTH OF STRING.
STEP 6: DECLARE s1.
STEP 7: START for x=0 to x<l.
STEP 8: DECLARE ch .
STEP 9: CHECK IF THE CHARACTER IS LETTER USING Character.isLetter().
STEP 10: CHECK IF THE CHARACTER IS IN LOWERCASE USING
Character.isLowerCase().
STEP 11: COMPUTE s1 = s1 + Character.toUpperCase(ch)
STEP 12: ELSE
COMPUTE s1=s1 + Character.toLowerCase(ch)
STEP 13: ELSE
COMPUTE s1=s1+ch
STEP 14: END for.
STEP 15: PRINT THE ORIGINAL STRING.
STEP 16: PRINT THE NEW STRING.
STEP 17: END METHOD main.
STEP 18: END CLASS.
STEP 19: END ALGORITHM.
PROGRAM
import java.util.Scanner;
class convert
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.println(" Enter a string ");
String s = sc.nextLine();
int l = s.length();
String s1 = " ";
for (int x=0;x<l;x++)
{
char ch = s.charAt(x);
if(Character.isLetter(ch)==true)
{
if(Character.isLowerCase(ch)==true)
s1=s1+Character.toUpperCase(ch);
else
s1=s1+ Character.toLowerCase(ch);
}
else
{
s1=s1+ch;
}
}
System.out.println("Original string" + s);

System.out.println(" New string " + s1);


}

VARIABLE DESCRIPTION

Variable Data Type Description

s String To store the input from


the user.

l int To store the length of


string entered by the
user.
s1 String To store the value of
modified string.
Accumulator
ch char To store each letter of
the string .

x int To iterate for loop.

• Removing duplicates in word.

ALGORITHM

STEP 1: START THE ALGORITHM.


STEP 2: CREATE CLASS RemoveDuplicate.
STEP 3: CREATE METHOD main.
STEP 4: TAKE INPUT FROM THE USER.
STEP 5: DECLARE s AND STORE THE INPUT.
STEP 6: INITIALISE l AND STORE THE LENGTH OF THE STRING.
STEP 7: DECLARE s1.
STEP 8: DECLARE ch1 AND ch2.
STEP 9: START for x=0 to x<l-1
STEP 10: IF ch1!=ch2
COMPUTE s1=s1+ch1
STEP 11: END for.
STEP 12: PRINT ORIGINAL WORD.
STEP 13: PRINT NEW WORD.
STEP 14: END METHOD main.
STEP 15: END class
STEP 16 : END ALGORITHM.

PROGRAM

import java.util.Scanner;
class RemoveDuplicate
{
public static void main (String args[])
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter a word");
String s = sc.next();
s=s.trim() + " ";
int l= s.length();
String s1= " ";
char ch1,ch2;
for(int x=0;x<l-1;x++)
{
ch1= s.charAt(x);
ch2= s.charAt(x+1);
if (ch1!=ch2)
{
s1= s1+ ch1;
}
}
System.out.println("The original word :" +s);
System.out.println("The new word:" +s1);
}
}

VARIABLE DESCRIPTION
Variable Data Type Description

s String To store the input


from user.

l int To store the length of


string entered by the
user.

s1 String To store the modified


string.

ch1 char To store the letters of


the string.

ch2 char To store the letters of


the string.

x int To iterate for loop.


• REPLACE THE VOWELS.

ALGORITHM
STEP 1: START THE ALGORITHM .
STEP 2: CREATE CLASS Replace.
STEP 3: CREATE METHOD main.
STEP 4: TAKE INPUT FROM THE USER.
STEP 5: DECLARE s AND STORE THE INPUT.
STEP 6: DECLARE s1.
STEP 7: START for.
STEP 8: DECLARE ch.
STEP 9:CHECK IF THE CHARACTER IS LETTER USING Character.isLetter().
STEP10: CHECK IF THE CHARACTER IS VOWEL.
STEP11: DECLARE ch1.
STEP12: COMPUTE s1=s1+ch1.
STEP13: ELSE
COMPUTE s1=s1+ch.
STEP14: END for.
STEP15: PRINT THE ORIGINAL STRING.
STEP16: PRINT THE NEW STRING.
STEP17: END METHOD main.
STEP18: END class.
STEP19: END ALGORITHM.
PROGRAM
import java.util.Scanner;
class Replace
{
public static void main (String args[])
{
Scanner sc= new Scanner(System.in);
System.out.println("Enter a sentence");
String s = sc.nextLine();
s= s.trim() + " ";
String s1 = " ";
for(int x=0;x<s.length();x++)
{
char ch = s.charAt(x);
if (Character.isLetter(ch) == true)
{

if(ch=='a'||ch=='e'||ch=='i'||ch=='o'||ch=='u'||ch=='A'||ch=='E'||ch=='I'||ch
=='O'||ch=='U')
{
char ch1 = (char)(ch+1);
s1= s1 + ch1;
}
else
{
s1 = s1 + ch;
}
}
}
System.out.println("Original string" + s);
System.out.println(" New string"+ s1);
}
}
VARIABLE DESCRIPTION
Variable Data Type Description

s String To store input from the


user.

s1 String To store the modified


string.

Ch char To check each letter of


the string.

ch1 char To compare each letters


of the string.

x int To iterate for loop.


• MERGING THE REVERSE OF THE STRING.
ALGORITHM
STEP 1: START THE ALGORITHM.
STEP 2: CREATE CLASS MergeRev.
STEP 3: CREATE METHOD main.
STEP 4: TAKE INPUT FROM THE USER.
STEP 5: INITIALISE n TO STORE THE INPUT.
STEP 6: DECLARE s.
STEP 7: START for.
STEP 8: PRINT SENTENCES.
STEP 9: COMPUTE s = s+ sc.nextLine().
STEP 10: END for.
STEP 11: DECLARE STRING TOKENIZER.
STEP 12: INITIALISE c ANDSTORE THE NO OF WORDS IN IT.
STEP 13: DECLARE w .
STEP 14: START for.
STEP 15: COMPUTE rev = w + “ “ + rev.
STEP 16: END for.
STEP 17: PRINT THE OUTPUT.
STEP 18: END METHOD main.
STEP 19: END class.
STEP 20: END THE ALGORITHM.
PROGRAM
import java.util.*;
class MergeRev
{
public static void main(String args[])
{
Scanner sc = new Scanner (System.in);
System.out.print(" Enter the no. of sentences");
int n = sc.nextInt();
sc.nextLine();
String s =" ";
for (int x=0;x<=n;x++)
{
System.out.print("Enter sentence"+ x+ ":");
s = s + sc.nextLine();
}
StringTokenizer str = new StringTokenizer(s,"; : !?");
int c = str.countTokens();
String w = " ", rev = " ";
for (int x =1; x<=c;x++)
{
w=str.nextToken();
rev= w + " " + rev;
}

System.out.print("output:" + rev);
}
}

VARIABLE DESCRIPTION

Variable Data Type Description

n int To store the number of


sentences entered by
the user.

s String To store the input from


the user.

x int To iterate for loop.

c int To store the no of words


in the string.

w String To store each word of


the string.

rev String To store the reversed


string.
NUMBER PROGRAMS
• PRIME PALLINDROME NUMBER.

ALGORITHM FOR isPrime FUNCTION


STEP 1: START THE ALGORITHM.
STEP2: DECLARE A VARIABLE c AND INITIALISE IT TO 0.
STEP 3: START for.
STEP 4: IF x%10 ==0 THEN UPDATE c.
STEP 5: IF c==1 RETURN TRUE.
STEP 6: ELSE
RETURN FALSE.
STEP 7: END THE FUNCTION isPrime().
ALGORITHM FOR isPallindrome FUNCTION
STEP 1: START THE ALGORITHM.
STEP 2: DECLARE A VARIABLE sum AND INITIALISE IT TO 0.
STEP 3: START while.
STEP 4: DECLARE A VARIABLE d = n%10.
STEP 5: COMPUTE sum = (sum*10)+d.
STEP 6: END while.
STEP 7: IF n==sum
RETURN TRUE.
STEP 8: ELSE
RETURN FALSE.
ALGORITHM FOR MAIN METHOD
STEP 1: START ALGORITHM.
STEP 2: TAKE INPUT FROM THE USER.
STEP 3: DECLARE VARIABLE m AND n AND TAKE INPUT FROM THE USER.
STEP 4: CHECK IF m>n OR m>3000 OR n>3000 , THEN PRINT invalid.
STEP 5: PRINT The prime palindrome numbers are:
STEP 6: START for.
STEP 7: CHECK IF x IS PRIME NUMBER BY CALLING isPrime() FUNCTION.
STEP 8: CHECK IF x IS PALLINDROME NUMBER BY CALLING isPallindrome()
FUNCTION.
STEP 9: IF x IS PRIME AS WELL AS PALLINDROME THEN PRINT x.
STEP 10: END for.
STEP 11: END METHOD main.
STEP 12: END class
STEP 13: END ALGORITHM.
PROGRAM
import java.util.Scanner;
class PrimePallindrome
{
public static boolean isPrime(int n)
{
int c=0;
for(int x=0;x<=n/2;x++)
{
if(n%x ==0)
{
c++;
}
}
if (c==1)
return true;
else
return false;
}
public static boolean isPallindrome(int n)
{
int temp = n;
int sum = 0;
while(n>0)
{
int d = n%10;
sum = (sum*10)+d;
n=n/10;
}
if(temp==sum)
return true;
else
return false;
}
public static void main (String args[])
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter the starting range");
int m =sc.nextInt();
System.out.println(" Enter the ending point");
int n = sc.nextInt();
if(m>n||m>3000||n>3000)
{
System.out.println("invalid");
}
System.out.println("The prime pallindrome numbers are:");
for(int x =m;x<=n;x++)
{
if(isPrime(x)==true)
{
if(isPallindrome(x)==true)
{
System.out.println(x);
}
}

}
}
}

VARIABLE DESCRIPTION

Variable Data Type Description

n int To store the value of


any integer and to
store the limit .

x int To iterate for loop.


Accumulator
c int It is used to check
prime numbers.
temp int To store the copy of n.

sum int To store the sum of


digits.

d int To store the remainder


of a number.

m int To store the limit of the


range.

• COMPOSITE MAGIC NUMBER

ALGORITHM

• ALGORITHM FOR CompMagic()


STEP 1: CREATE CONSTRUCTOR.
STEP 2: INITIALISE VARIABLE m to 0.
STEP 3: INITIALISE VARIABLE n TO 0.
STEP 4: INITIALISE VARIABLE count TO 0.
STEP 5: END CONSTRUCTOR.

• ALGORITHM FOR readLimits()


STEP 1: TAKE INPUT FROM THE USER.
STEP 2: STORE THE INPUT IN VARIABLE m.
STEP 3: TAKE INPUT FROM THE USER.
STEP 4: STORE INPUT IN VARIABLE n.
STEP 5: END THE FUNCTION.
STEP 6: END ALGORITHM FOR THIS FUNCTION.

• ALGORITHM FOR isComposite()


STEP 1: INITIALISE VARIABLE c TO 0.
STEP 2:START for.
STEP 3: CHECK IF x%10 = 0 THEN UPDATE c++ AND break.
STEP 4: END for.
STEP 5: CHECK IF c>2 RETURN 1.
STEP 6: ELSE RETURN 0.
STEP 7: END FUNCTION.
STEP 8: END ALGORITHM FOR THIS FUNCTION.

• ALGORITHM FOR sumDigit().


STEP 1: CREATE FUNCTION.
STEP 2: INITIALISE VARIABLE s TO 0.
STEP 3: START while (v!=0).
STEP 4: COMPUTE s = s+ v%10.
STEP 5: END while.
STEP 6: RETURN s.
STEP 7: END THE FUNCTION.
STEP 8: END ALGORITHM FOR THIS FUNCTION.

• ALGORITHM show()
STEP 1: CREATE METHOD show().
STEP 2: CHECK IF m>n THEN PRINT Invalid.
STEP 3: ELSE
STEP 4: START for.
STEP 5: INITIALISE A VARIABLE co AND STORE COMPOSITE NO. IN IT.
STEP 6: INITIALISE A VARIABLE sum AND STORE THE SUM OF DIGITS IN IT.
STEP 7: START while (sum>9)
STEP 8: UPDATE sum = sumDigit(sum).
STEP 9: END while.
STEP 10:CHECK IF co ==1 AND sum == 1 THEN UPDATE count++ AND
THEN PRINT THE NO.
STEP 11: END for.
STEP 12: PRINT THE NO OF COMPOSITE MAGIC NUMBERS.
STEP 13: END THE FUNCTION.
STEP 14: END ALGORITHM FOR THIS FUNCTION.

• ALGORITHM FOR main method


STEP 1: CREATE METHOD main.
STEP 2: CREATE OBJECT ob.
STEP 3: CALL THE FUNCTION readLimits().
STEP 4: CALL THE FUNCTION show().
STEP 5: END METHOD main.
STEP 6: END CLASS.
STEP 7: END ALGORITHM.

PROGRAM

import java.util.Scanner;
class CompMagic
{
int m,n,count;
public CompMagic()
{
m=0;
n=0;
count=0;
}
public void readLimits()
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter the start limit");
m=sc.nextInt();
System.out.println("Enter the ending limit");
n=sc.nextInt();
}
public int isComposite(int x)
{
int c=0;
for(int i=0;i<x/2;i++)
{

if((x%i) == 0)
{
c++;
break;
}
}
if(c>2)
return 1;
else
return 0;
}
public int sumDigit(int v)
{
int s=0;
while(v!=0)
{
s=s+v%10;
v=v/10;
}
return s;
}
public void show()
{
if(m>n)
{
System.out.println("Invalid range");
}
else
{
for(int x=m; x<=n;x++)
{
int co= isComposite(x);
int sum= sumDigit(x);
while(sum>9)
{
sum=sumDigit(sum);
}
if(co==1&& sum==1)
{
count++;
System.out.print(x + "\t");
}
}
}
System.out.println("Number of composite magic numbers =" +
count);
}
public static void main(String args[])
{
CompMagic ob = new CompMagic();
ob.readLimits();
ob.show();
}
}
VARIABLE DESCRIPTION

Variable Data Type Description

m int To store the starting


limit.

n int To store the ending limit.

count int To store the number of


composite magic
numbers.

x int To store an integer and


iterate for loop.

c int Accumulator to check


composite number.

i int To iterate for loop.


v int To store an integer.

s int To store the sum of


digits of an integer.

co int To store composite


number.

sum int To store the sum of digit.

• PRIME ADAM NUMBER.


ALGORITHM

• ALGORITHM FOR boolean isPrime()


STEP 1: CREATE FUNCTION boolean isPrime().
STEP 2: TAKE PARAMETERIZED INTEGER N.
STEP 3: INITIALISE A VARIABLE c TO 0.
STEP 4: START for.
STEP 5: CHECK IF ( N%i ==0) AND UPDATE c++.
STEP 6: END for.
STEP 7: RETURN c==2.
STEP 8: END THIS FUNCTION.
STEP 9: END THE ALGORITHM FOR THIS FUNCTION.

• ALGORITHM FOR FUNCTION int reverse().


STEP 1: CREATE A PARAMETERIZED FUNCTION int reverse().
STEP 2: INITIALISE A VARIABLE rev TO 0.
STEP 3: START while (N!=0)
STEP 4: INITIALISE A VARIABLE r AND STORE THE REMAINDER.
STEP 5: COMPUTE rev = rev*10 +r;
STEP 6: UPDATE N=N/10;
STEP 7: END while.
STEP 8: RETURN rev.
STEP 9: END THIS FUNCTION.
STEP 10: END THE ALGORITHM FOR THIS FUNCTION.

• ALGORITHM FOR boolean isAdam().


STEP 1: CREATE A PARAMETERIZED FUNTION boolean isAdam().
STEP 2: INITIALISE A VARIABLE sqN.
STEP 3: INITIALISE A VARIABLE revN.
STEP 4: INITIALISE A VARIABLE sqrevN.
STEP 5: INITIALISE A VARIABLE Rev.
STEP 6: RETURN Rev==sqrevN.
STEP 7: END THIS FUNCTION
STEP 8: END THE ALGORITHM FOR THIS FUNCTION.

• ALGORITHM FOR main method


STEP 1: CREATE METHOD main.
STEP 2: TAKE INPUT FROM THE USER.
STEP 3: INITIALISE A VARIABLE n TO TAKE INPUT FROM THE USER.
STEP 4: INITIALISE A VARIABLE count.
STEP 5: START for.
STEP 6: DECLARE A VARIABLE adam
STEP 7: CHECK IF adam IS TRUE.
STEP 8: DECLARE A VARIABLE prime.
STEP 9: CHECK IF prime IS TRUE.
STEP 10: PRINT i.
STEP 11: UPDATE count++.
STEP 12: END for.
STEP 13: CHECK IF count==0 THEN PRINT none primeadam number
found.
STEP 14: END METHOD main.
STEP 15: END CLASS.
STEP 16: END ALGORITHM.
PROGRAM
import java.util.Scanner;
class PrimeAdam
{
public static boolean isPrime(int N)
{
int c=0;
for(int i=0;i<=N;i++)
{
if(N%i==0)
{
c++;
}
}
return c==2;
}
public static int reverse(int N)
{
int rev=0;
while(N!=0)
{
int r=N%10;
rev= rev*10 +r;
N=N/10;
}
return rev;
}
public static boolean isAdam(int N)
{
int sqN= N*N;
int revN= reverse(N);
int sqrevN = revN *revN;
int Rev = reverse(sqN);

return Rev==sqrevN;
}
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter the value of n");
int n = sc.nextInt();
int count =0;

System.out.println("The prime adam integers are:");


for (int i=0;i<=n;i++)
{
boolean adam = isAdam(i);
if(adam)
{
boolean prime = isPrime(i);
if (prime)
{
System.out.println(i + "\t");
count++;
}
}
}
if(count==0)
{
System.out.println("none prime adam number found");
}
}
}

VARIABLE DESCRIPTION
Variable Data Type Description

N int To store the value of


any integer.

c int Accumulator to check


prime number.

i int To iterate for loop.

rev int To store the reverse of


a number.

r int To store the remainder.

sqN int To store the square of


an integer.

revN int To store the reverse of


the number.

sqrevN int To store the square of


the reversed number.

rev int To store the reverse of


the squared number.

n int To store the input by


the user.

count Int To store the number of


prime adam numbers.
adam boolean To check adam number.

prime boolean To check prime


numbers.

• HAPPY NUMBER
ALGORITHM

• ALGORITHM FOR void Happy()


STEP 1: CREATE A CONSTRUCTOR
STEP 2: ASSIGN 0 TO A VARAIBLE n.
STEP 3: END CONSTRUCTOR.
STEP 4: END ALGORITHM FOR CONSTRUCTOR.

• ALGORITHM FOR void getnum().


STEP 1: CREATE A PARAMETERIZED FUNCTION void getnum().
STEP 2: COMPUTE n=nx.
STEP 3: END THIS FUNCTION.
STEP 4: END ALGORITHM FOR THIS FUNCTION.

• ALGORITHM FOR sumsqdigit().


STEP 1: CREATE A PARAMETERIZED FUNCTION sumsqdigit().
STEP 2: INITIALISE A VARIABLE s TO 0.
STEP 3: START while(d!=0)
STEP 4: COMPUTE s.
STEP 5: UPDATE d=d/10;
STEP 6: END while.
STEP 7: RETURN s.
STEP 8: END THIS FUNCTION.
STEP 9: END ALGORITHM FOR THIS FUNCTION.

• ALGORITHM FOR void isHappy().


STEP 1: CREATE A FUNCTION vois isHappy().
STEP 2: INITIALISE A VARIABLE num.
STEP 3: INITIALISE A VARIABLE sum.
STEP 4: START while(sum>9)
STEP 5: COMPUTE sum.
STEP 6: END while.
STEP 7: CHECK IF (sum==1) THEN PRINT it is happy number.
STEP 8: ELSE PRINT it is not.
STEP 9: END THIS FUNCTION.
STEP 10: END ALGORITHM FOR THIS FUNCTION.

• ALGORITHM FOR main method.


• STEP 1: CREATE METHOD main.
STEP 2: TAKE INPUT FROM THE USER AND STORE IT IN THE VARIABLE
n1.
STEP 3: CREATE OBJECT.
STEP 4: CALL getnum() FUNCTION.
STEP 5: CALL isHappy() FUNCTION.
STEP 6: END METHOD main.
STEP 7: END CLASS.
STEP 8: END ALGORITHM.

PROGRAM
import java.util.Scanner;
class Happy
{
int n;
public void Happy()
{
n=0;
}
public void getnum(int nx)
{
n=nx;
}
int sumsqdigit(int d)
{
int s=0;
while(d!=0)
{
s=s+ (int)Math.pow(d%10,2);
d=d/10;
}
return s;
}
public void isHappy()
{
int num = n;

int sum = sumsqdigit(num);


while(sum>9)
{
sum= sumsqdigit(sum);
}
if(sum ==1)
System.out.println("It is happy number");
else
System.out.println("It is not");
}
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter an integer");
int n1=sc.nextInt();
Happy ob = new Happy();
ob.getnum(n1);
ob.isHappy();
}

VARIABLE DESCRIPTION

Variable Data Type Description

n int To store any integer.

nx int To make copy of n.

d int To store any integer.

s int To sum of digit square.

num int Copy of n.

sum int To store sum of square


digits.

n1 int To store input from the


user.

RECURSION PROGRAMS
• CONVERTING TO BINARY EQUIVALENT NUMBERS
• ALGORITHM FOR CONSTRUCTOR
STEP 1: CREATE CONSTRUCTOR BinaryConverter().
STEP 2: ASSIGN THE PARAMETERIZED VALUE TO INSTANCE VARIABLE
num.
STEP 3: ASSIGN VALUE TO THE INSTANCE VARIABLE binnum BY
CALLING convert() FUNCTION.
STEP 4: END THE CONSTRUCTOR.
STEP 5: END ALGORITHM FOR THE CONSTRUCTOR.

• ALGORITHM FOR long convert() .


STEP 1: CREATE long convert() PARAMETERIZED FUNCTION.
STEP 2: CHECK IF THE PARAMETERIZED VALUE IS EQUAL TO 0 THEN
RETURN 0.
STEP 3: RETURN conver(n/2)*10 +(n%10).
STEP 4: END THE FUNCTION.
STEP 5: END ALGORITHM FOR THIS FUNCTION.

• ALGORITHM FOR void display().


STEP 1: CREATE A FUNCTION void display().
STEP 2: PRINT NUMBER.
STEP 3: PRINT THE BINARY EQUIVALENT OF THE NUMBER.
STEP 4: END THE FUNCTION.
STEP 5: END ALGORITHM FOR THIS FUNCTION.

• ALGORITHM FOR main method().


STEP 1: CREATE METHOD main.
STEP 2: TAKE INPUT FROM THE USER.
STEP 3: CREATE OBJECT ob.
STEP 4: CALL display() FUNCTION TO PRINT .
STEP 5: END METHOD main.
STEP 6: END CLASS.
STEP 7: END ALGORITHM.

PROGRAM

import java.util.Scanner;
class BinaryConverter
{
int num;
long binnum;
BinaryConverter(int n)
{
num=n;
binnum=convert(n);
}
long convert(int n)
{
if(n==0)
return 0;
return convert(n/2)*10 + (n%2);
}
void display()
{
System.out.println("Number:" + num);
System.out.println("Binary Equivalent:" + binnum);
}
public static void main (String args[])
{
Scanner sc = new Scanner(System.in);
System.out.print(" Enter number to be converted into binary equivalent:" );
int N = sc.nextInt();
BinaryConverter ob = new BinaryConverter(N);

ob.display();
}
}

VARIABLE DESCRIPTION

Variable Data Type Description

num int To store the number .

binnum long To store the binary


equivalent of the number.

n int To store any integer.


N int To store the input from
the user.

• COMPUTING THE APPROXIMATE VALUE USING FOLLOWING RECURSIVE


FORMULA f(N) =1 = 1+1/f(-1) if N=0 if N>0.

ALGORITHM

• ALGORITHM FOR double function().


STEP 1: CREATE double function() PARAMETERIZED FUNCTION.
STEP 2: CHECK IF THE PARAMETERIZED VALUE IS EQUAL TO 0 THEN
RETURN 1.
STEP 3: RETURN 1.0 + 1.0/ function(n-1).
STEP 4: END THIS FUNCTION.
STEP 5: END THE ALGORITHM FOR THIS FUNCTION.

• ALGORITHM FOR min method().


STEP 1: CREATE MATHOD main.
STEP 2: TAKE INPUT FROM THE USER.
STEP 3: PRINT THE APPROXIMATE VALUE.
STEP 4: END MATHOD main.
STEP 5: END CLASS.
STEP 6: END ALGORITHM.
PROGRAM

import java.util.Scanner;
class Approximation
{
public static double function(int n)
{
if(n==0)
return 1;
return 1.0+1.0/function(n-1);
}
public static void main (String args[])
{
Scanner sc= new Scanner(System.in);
System.out.println("Enter value of N");
int N = sc.nextInt();
System.out.println(" The approximate value is:" + function(N));
}
}

VARIABLE DESCRIPTION

Variable Data Type Description


N int To store the input from
the user.

n int To store any integer.

• TO FIND GCD .

ALGORITHM
• ALGORITHM FOR CONSTRUCTOR
STEP 1: CREATE THE CONSTRUCTOR BinaryGCD().
STEP 2: ASSIGN THE FIRST APARAMETERISED VALUE TO num1.
STEP 3: ASSIGN THE SECOND PARAMETERIZED VALUE TO num2.
STEP 4: COMPUTE THE THIRD INSTANCE VARIABLE gcd =
bingcd(num1,num2).
STEP 5: END THE CONSTRUCTOR.
STEP 6: END THE ALGORITHM FOR THE CONSTRUCTOR.

• ALGORITHM FOR THE FUNCTION int bingcd().


STEP 1: CREATE THE PARAMETERIZED FUNCTION int bingcd().
STEP 2: CHECK IF THE SECOND PARAMETERIZED VALUE q IS EQUAL TO
0 THE RETURN THE FIRST PARAMETERIZED VALUE p.
STEP 3: CHECK IF THE FIRST PARAMETERIZED VALUE p IS EQUAL TO 0
THE RETURN THE SECOND PARAMETERIZED VALUE q.
STEP 4: CHECK IF( (p&1)==0 &&(q&1)==0) THEN RETURN
bingcd(p>>1,q>>1)<<1.
STEP 5: CHECK ELSE IF(p&1)==0 THEN RETURN bingcd(p>>1,q).
STEP 6: CHECK ELSE IF (q&1)==0 THEN RETURN bingcd(p,q>>1).
STEP 7: CHECK ELSE IF (p>=q) THEN RETURN bingcd((p-q)>>1,q).
STEP 8: ELSE RETURN bingcd(p,(q-p)>>1).
STEP 9: END THIS FUNCTION.
STEP 10 : END THE ALGORITHM FOR THIS FUNCTION.

• ALGORITHM FOR int getgcd().


STEP 1: CREATE THE FUNCTION.
STEP 2: RETURN gcd.
STEP 3: END THE FUNCTION.
STEP 4: END ALGORITHM FOR THE FUNCTION.

• ALGORITHM FOR main method().


STEP 1: CREATE METHOD main.
STEP 2: TAKE INPUT FROM THE USER.
STEP 3: CREATE OBJECT ob.
STEP 4: PRINT THE GCD.
STEP 5: END METHOD main.
STEP 6: END CLASS.
STEP 7: END ALGORITHM.
PROGRAM

import java.util.Scanner;
class BinaryGCD
{
int num1,num2,gcd;
BinaryGCD(int a,int b)
{
num1=a;
num2=b;
gcd=bingcd(num1,num2);
}
public static int bingcd(int p, int q)
{
if(q==0)
return p;
if(p==0)
return q;
if((p&1)==0&&(q&1)==0)
return bingcd(p>>1,q>>1)<<1;
else if((p&1)==0)
return bingcd(p>>1,q);
else if((q&1)==0)
return bingcd(p,q>>1);
else if (p>=q)
return bingcd((p-q)>>1,q);
else
return bingcd(p,(q-p)>>1);
}
int getgcd()
{
return gcd;
}
public static void main (String args[])
{
Scanner sc = new Scanner( System.in);
System.out.print("Enter 2 numbers:");
int p = sc.nextInt();
int q=sc.nextInt();
BinaryGCD ob = new BinaryGCD(p,q);
System.out.print(" gcd("+ p+ " ," + q+ ")=" +ob.getgcd());
}
}

VARIABLE DESCRIPTION

Variable Data Type Description

num1 int To store the integer.

num2 int To store the integer.


gcd int To store the gcd.

a int To store the integer.

b int To store the integer.

p int To store the input.

q int To store the input

• PROGRAM TO PRINT PARTITIONS OF A NUMBER.

ALGORITHM

• ALGORITHM FOR THE FUNCTION void partition()..


STEP 1: CREATE A PARAMETERIZED FUNCTION.
STEP 2: CALL THE FUNCTION ITSELF partition (n,n, “’);
STEP 3: END THE FUNCTION.
STEP 4: END THE ALGORITHM FOR THIS FUNCTION.

• ALGORITHM FOR THE FUNCTION void partition()


STEP 1: CREATE THE PARAMETERIZED FUNCTION .
STEP 2: CHECK IF n==0 THEN PRINT THE PARAMETERIZED STRING
VALUE prefix AND RETURN.
STEP 3: START for.
STEP 4: COMPUTE pzartition(n-i,i,prefix+”” +i)
STEP 5: END for.
STEP 6: END THE FUNCTION .
STEP 7: END THE ALGORITHM FOR THIS FUNCTION.

• ALGORITHM FOR main method().


STEP 1: CREATE METHOD main.
STEP 2: TAKE IN PUT FROM THE USER.
STEP 3: PRINT ALL POSSIBLE PARTITIONS.
STEP 4: CALL THE FUNCTION partition().
STEP 5: END METHOD main.
STEP 6: END CLASS
Step 7: END ALGORITHM.

PROGRAM

import java.util.Scanner;
class Partition
{
public static void partition(int n)
{
partition(n,n,"");
}
public static void partition( int n, int max, String prefix)
{
if(n==0)
{
System.out.println(prefix);
return ;
}
for (int i = Math.min(max,n);i>=1;i--)
{
partition(n-i,i,prefix+ " " +i);
}
}
public static void main(String args[])
{
Scanner sc= new Scanner(System.in);
System.out.println(" Enter the number:");
int N= sc.nextInt();
System.out.println("All possible partition of "+ N+ "are:");
partition(N);
}
}

VARIABLE DESCRIPTION

Variable Data Type Description

n int To store the integer.

max int To store the integer.

prefix String To store the string


value.

i int To iterate for loop.

N int To store the input.


OPERATION ON FILES
• READ NAMES OF 5 NEW SUBJECTS THROUGH KEYWORDS AND STORE THEM
IN FILE subjects.txt.

ALGORITHM

STEP 1: START THE ALGORITHM.


STEP 2: CREATE METHOD main.
STEP 3: CREATE A FileWriter OBJECT AND PASS THE FILENAME.
STEP 4: CREATE A BufferedWriter OBJECT AND PASS THE FileWriter OBJECT
IN IT.
STEP 5: CREATE A PrintWriter OBJECT AND PASS THE BufferedWriter OBJECT
IN IT.
STEP 6: START for.
STEP 7: TAKE INPUT FROM THE USER.
STEP 8: END for.
STEP 9: CLOSE THE STREAM CHAIN USING close().
STEP 10: END METHOD main.
STEP 11: PRINT.
STEP 12: END THE CLASS
STEP 13: END ALGORITHM.

PROGRAM
import java.io.*;
class IO
{
static String filename = ("subject.txt");
static InputStreamReader isr = new InputStreamReader(System.in);
static BufferedReader stdin = new BufferedReader(isr);
public static void main(String[] args)
{
try
{
FileWriter fw = new FileWriter(filename);
BufferedWriter bw = new BufferedWriter(fw);
PrintWriter file = new PrintWriter(bw);
for(int i=0;i<5;i++)
{
System.out.print("Enter subject:");
String subject = stdin.readLine();
file.println(subject);
}
file.close();
}
catch(IOException e)
{
System.err.println(e);
}
}
}

VARIABLE DESCRIPTION
Variable Data Type Description
filename String To store subject
names.
i int To iterate for loop.

• CREATE A TEXT FILE “data.txt” TO INPUT AND STORE N SENTENCES.

ALGORITHM

STEP 1: START THE ALGORITHM.


STEP 2: CREATE CLASS CreateFile
STEP 3: CREATE METHOD main.
STEP 4: CREATE A FileWriter OBJECT AND PASS THE data.txt.
STEP 5: CREATE A BufferedWriter OBJECT AND PASS THE FileWriter OBJECT
IN IT.
STEP 6: CREATE A PrintWriter OBJECT AND PASS THE BufferedWriter OBJECT
IN IT.
STEP 7: TAKE INPUT FROM THE USER.
STEP 8: START for.
STEP 9: TAKE INPUT.
STEP 10: SEND THE VALUE OF THE INPUT FROM MEMORY TO TEXT FILE
data.txt. BY THE PrintWriter OBJECT.
STEP 11: END for.
STEP 12: CLOSE THE TEXT FILE USING FILE WRITER OBJECT.
STEP 13: END METHOD main.
STEP 14: END CLASS
STEP 15: END THE ALGORITHM.
PROGRAM

import java.util.*;
import java.io.*;
class CreateFile
{
public static void main (String args[]) throws IOException
{
Scanner sc = new Scanner(System.in);
String sent;
FileWriter std = new FileWriter("data.txt");
BufferedWriter mat = new BufferedWriter(std);
PrintWriter obj = new PrintWriter(mat);
System.out.print(" Input total number of sentences to store :");
int N = sc.nextInt();
for(int x=1;x<=N;x++)
{
System.out.print(" Input a sentence");
sent = sc.nextLine();
obj.println(sent);
}
obj.close();
}
}

VARIABLE DESCRIPTION

Variable Data Type Description

sent String To store sentences.

N int To store the number


of sentences.

i int To iterate for loop.

• CREATE A DATA FILE TO INPUT PRODUCT NAME , QUANTITY OF PRODUCT


AND UNIT PRICE OF PRODUCT FOR N PRODUCTS.

ALGORITHM
STEP 1: START THE ALGORITHM
STEP 2: CREATE CLASS Create
STEP 3: CREATE METHOD main.
STEP 4: CREATE FileOutputStream OBJECT AND PASS THE “ invoice.dat” .
STEP 5: CREATE DataOutputStream OBJECT AND PASS THE
FileOutputStream OBJECT .
STEP 6: TAKE INPUT FROM THE USER.
STEP 7: START for.
STEP 8: TAKE INPUT FROM THE USER.
STEP 9: SENDING VALUES OF name, qty, price FROM MEMORY TO BINARY
FILE “ invoice.dat”.
STEP 10: END for.
STEP 11: CLOSE BINARY FILE USING STREAM OBJECTS.
STEP 12: END METHOD main.
STEP 13: END CLASS.
STEP 14: END ALGORITHM.
PROGRAM

import java.io.*;
import java.util.*;
class Create
{
public static void main(String args[])throws IOException
{
Scanner sc = new Scanner(System.in);
BufferedReader inp = new BufferedReader(new
InputStreamReader(System.in));
String name;
int qty;
double price;
FileOutputStream std = new FileOutputStream("invoice.dat");
DataOutputStream mat = new DataOutputStream(std);
System.out.print(" Input the total number of products:");
int N = sc.nextInt();
for(int x=1;x<=N;x++)
{
System.out.print("Input product name:");
name = sc.nextLine();
System.out.print("Input quantity:");
qty = sc.nextInt();
System.out.print(" Input unit price:");
price = sc.nextDouble();

mat.writeUTF(name);
mat.writeInt(qty);
mat.writeDouble(price);
}
std.close();
mat.close();
}
}

VARIABLE DESCRIPTION

Variable Data Type Description

Name String To store the product


name.

Qty int To store the quantity of


the product.

Price double
To store the price of the
product.

x int To iterate for loop.

N int To store the no of


products.

• UPDATE THE BINARY FILE BY APPENDING N MORE PRODUCT INFORMATION.


ALGORITHM
STEP 1: START THE ALGORITHM
STEP 2: CREATE CLASS Append
STEP 3: CREATE METHOD main.
STEP 4: CREATE BufferedReader OBJECT.
STEP 5: : CREATE FileOutputStream OBJECT AND PASS THE “ invoice.dat”
.true.
STEP 6: CREATE DataOutputStream OBJECT AND PASS THE
FileOutputStream OBJECT .
STEP 7: TAKE INPUT FROM THE USER.
STEP 8: START for.
STEP 9: TAKE INPUT FROM THE USER.
STEP 10: SENDING VALUES OF name, qty, price FROM MEMORY TO BINARY
FILE “ invoice.dat”.
STEP 11: END for.
STEP 12: CLOSE BINARY FILE USING STREAM OBJECTS.
STEP 13: END METHOD main.
STEP 14: END CLASS.
STEP 15: END ALGORITHM.
PROGRAM

import java.io.*;

class Append
{
public static void main(String args[])throws IOException
{
Scanner sc = new Scanner(System.in);
BufferedReader inp = new BufferedReader(new
InputStreamReader(System.in));
String name;
int qty;
double price;
FileOutputStream std = new FileOutputStream("invoice.dat". true);
DataOutputStream mat = new DataOutputStream(std);
System.out.print(" Input the total number of products:");
int N = sc.nextInt();
for(int x=1;x<=N;x++)
{
System.out.print("Input product name:");
name = sc.nextLine();
System.out.print("Input quantity:");
qty = sc.nextInt();
System.out.print(" Input unit price:");
price = sc.nextDouble();

mat.writeUTF(name);
mat.writeInt(qty);
mat.writeDouble(price);
}
std.close();
mat.close();
}
}

VARIABLE DESCRIPTION

Variable Data Type Description

name String To store the product


name.

qty int To store the quantity of


the product.
price double To store the price of the
product.

x int To iterate for loop.

N int To store the no of


products.
DATA STRUCTURE

• ALGORITHM TO DELETE A NODE IN A LIST.

STEP 1: save = start,ptr = start


STEP 2: REPEAT STEP 3 THROUGH 7 UNTIL ptr = NULL
STEP 3: IF ptr.INFO==ITEM then
STEP 4: BREAK.
STEP 5: ELSE
STEP 6: save=ptr
STEP 7: ptr=ptr.LINK.
STEP 8: IF ptr ==NULL then
STEP 9: PRINT item not found
STEP 10: ELSE
STEP 11: IF ptr==start then
STEP 12: start = ptr.LINK.
STEP 13: ELSE
STEP 14: save.LINK=ptr.LINK
STEP 15: END OF STEP 11
STEP 16: END OF STEP 8
STEP 17: END
PROGRAM
import java.io.*;
class Node
{
protected int data;
protected Node link;
public Node()
{
link=null;
data=0;
}
public Node(int d, Node n)
{
data=d;
link=n;
}
public void setlink(Node n)
{
link =n;
}
public void setData(int d)
{
data=d;
}
public Node getlink()
{
return link;
}
public int getData()
{
return data;
}
}
class linkedList
{
protected Node start;
public linkedList()
{
start=null;
}
public boolean isEmpty()
{
return start==null;
}
public void insert(int val)
{
Node nptr,ptr,save=null;
nptr = new Node(val,null);
boolean ins = false;
if(start==null)
start=nptr;
else if (val<=start.getData())
{
nptr.setlink(start);
start=nptr;
}
else
{
save=start;
ptr= start.getlink();
while(ptr!=null)
{
if(val>=save.getData()&&val<=ptr.getData())
{
save.setlink(nptr);
nptr.setlink(ptr);
ins=true;
break;
}
else
{
save=ptr;
ptr=ptr.getlink();
}
}
if(ins==false)
{
save.setlink(nptr);
}
}
}
public boolean Delete(int val)
{
boolean res = false;
if(start.getData()==val)
{
start= start.getlink();
res=true;
}
else
{
Node ptr,save;
save=start;
ptr=start.getlink();
while(ptr!=null)
{
if(ptr.getData()==val)
{
Node next = ptr.getlink();
save.setlink(next);
res=true;
break;
}
else
{
save=ptr;
ptr=ptr.getlink();
}
}
}
return res;
}
System.out.print("Enter number to be deleted:");
try
{
num=Integer.parseInt(br.readLine());
boolean res = S.Delete(num);
if(res==true)
System.out.println(num + "deleted successfully");
else
System.out.println("Sorry!no such number found in the list:");
}
catch (Exception e)
{
System.out.println(e);
}
System.out.println("\n Now the list is:");
S.display();
System.out.println("\n---list test over---");
}
}

VARIABLE DESCRIPTION

Variable Data Type Description


To store any integer.
val int
res boolean To return the true or
false.
a int To iterate for loop

num int To store the integer.

• TO INSERT A NODE IN A SORTED LIST.


ALGORITHM
STEP 1: ptr=START.
STEP 2: NEWPTR = new Node.
STEP 3: IF NEWPTR=NULL
STEP 4: PRINT no space available
STEP 5: ELSE
STEP 6: NEWPTR.INFO=ITEM
STEP 7: NEWPTR.LINK =NULL
STEP 8: IF START = NULL then
STEP 9: START = NEWPTR
STEP 10: ELSE IF ITEM<START.INFO then
STEP 11: save=START
STEP 12: START = NEWPTR
STEP 13: NEWPTR.LINK= save
STEP 14: save = START
STEP 15: REPEAT STEP 16 THROUGH 22 UNTIL ptr=NULL
STEP 16: IF NEWPTR.INFO>ptr.INFO then
STEP 17: save=ptr
STEP 18: ptr=ptr.LINK
STEP 19: ELSE
STEP 20: save.LINK=NEWPTR
STEP 21: NEWPTR.LINK=ptr
STEP 22: BREAK
STEP 23: IF ptr=NULL then
STEP 24: save.LINK =NEWPTR
STEP 25: NEWPTR.LINK =NULL
STEP 26: END.

PROGRAM

import java.io.*;
class Node1
{
protected int data;
protected Node1 link;
public Node1()
{
link=null;
data=0;
}
public Node1(int d, Node1 n)
{
data=d;
link=n;
}
public void setlink(Node1 n)
{
link =n;
}
public void setData(int d)
{
data=d;
}
public Node1 getlink()
{
return link;
}
public int getData()
{
return data;
}
}
class linkedList
{
protected Node1 start;
public linkedList()
{
start=null;
}
public boolean isEmpty()
{
return start==null;
}
public void insert(int val)
{
Node1 nptr,ptr,save=null;
nptr = new Node1(val,null);
boolean ins = false;
if(start==null)
start=nptr;
else if (val<=start.getData())
{
nptr.setlink(start);
start=nptr;
}
else
{
save=start;
ptr= start.getlink();
while(ptr!=null)
{
if(val>=save.getData()&&val<=ptr.getData())
{
save.setlink(nptr);
nptr.setlink(ptr);
ins=true;
break;
}
else
{
save=ptr;
ptr=ptr.getlink();
}
}
if(ins==false)
{
save.setlink(nptr);
}
}
}
public void display()
{
Node1 ptr = start;
System.out.print(start.getData()+"-->");
ptr=start.getlink();
while(ptr.getlink()!=null)
{
System.out.print(ptr.getData()+"-->");
ptr=ptr.getlink();
}
System.out.print(ptr.getData()+ "!!!!");
System.out.println();
}
}
class linkedTest
{
protected static linkedList S;
public static void main(String args[])
{
int num ;
S=new linkedList();
BufferedReader br = new BufferedReader(new
InputStreamReader(System.in));
System.out.println(".....Starting list for insertion.....\n");
for(int a=0;a<5;++a)
{
System.out.print("Enter a number:");
try
{
num=Integer.parseInt(br.readLine());
S.insert(num);
System.out.println("Inserted:"+ num);
}
catch(Exception e)
{
System.out.println(e);
}
}
System.out.println("\n Created list is:");
S.display();
System.out.println("\n----List test over----");
}
}

VARIABLE DESCRIPTION

Variable Data Type Description


To store any integer.
val int
ins boolean To return the true or
false.
a int To iterate for loop

num int To store the integer.

• TRAVERSE A LIST
ALGORITHM
STEP 1: ptr=start
STEP 2: REAPEAT STEPS 3 AND 4 UNTIL ptr = NULL
STEP 3: PRINT ptr.INFO
STEP 4: ptr=ptr.LINK
STEP 5: END

PROGRAM

import java.io.*;
class Node2
{
protected int data;
protected Node2 link;
public Node2()
{
link=null;
data=0;
}
public Node2(int d, Node2 n)
{
data=d;
link=n;
}
public void setlink(Node2 n)
{
link =n;
}
public void setData(int d)
{
data=d;
}
public Node2 getlink()
{
return link;
}
public int getData()
{
return data;
}
}
class linkedList
{
protected Node2 start;
public linkedList()
{
start=null;
}
public boolean isEmpty()
{
return start==null;
}
public void insert(int val)
{
Node2 nptr,ptr,save=null;
nptr = new Node2(val,null);
boolean ins = false;
if(start==null)
start=nptr;
else if (val<=start.getData())
{
nptr.setlink(start);
start=nptr;
}
else
{
save=start;
ptr= start.getlink();
while(ptr!=null)
{
if(val>=save.getData()&&val<=ptr.getData())
{
save.setlink(nptr);
nptr.setlink(ptr);
ins=true;
break;
}
else
{
save=ptr;
ptr=ptr.getlink();
}
}
if(ins==false)
{
save.setlink(nptr);
}
}
}
public void traverse()
{
Node2 ptr =start;
System.out.print(start.getData()+ " --->");
ptr= start.getlink();
while(ptr.getlink()!=null)
{
System.out.print(ptr.getData() + " --->");
ptr = ptr.getlink();
}
System.out.print(ptr.getData() + "!!!!");
System.out.println();
}
}
class linkedTest
{
protected static linkedList S;
public static void main(String args[])
{
int num ;
S=new linkedList();
BufferedReader br = new BufferedReader(new
InputStreamReader(System.in));
System.out.println(".....Starting list for traversal.....\n");
for(int a=0;a<7;++a)
{
System.out.print("Enter a number:");
try
{
num=Integer.parseInt(br.readLine());
S.insert(num);

}
catch(Exception e)
{
System.out.println(e);
}
}
System.out.println("\n Now list is:");
S.traverse();
System.out.println("\n----List test over----");
}
}

VARIABLE DESCRIPTION

Variable Data Type Description


To store any integer.
val Int
ins boolean To return the true or
false.
a Int To iterate for loop

num int To store the integer.


• TO SEARCH AN ITEM IN ALINKED LIST

ALGORITHM
STEP 1: SET ptr=START,pos=0
STEP 2: REPEAT STEP 3 THROUGH 7 UNTIL ptr=NULL
STEP 3: IF ITEM = ptr.INFO then
STEP 4: pos = pos+1
STEP 5: PRINT ITEM found at node,pos
STEP 6: BREAK
STEP 7: ptr=ptr.LINK
STEP 8: IF ptr=NULL then
STEP 9: PRINT unsuccessful search
STEP 10: END.

PROGRAM
import java.io.*;
class Node3
{
protected int data;
protected Node3 link;
public Node3()
{
link=null;
data=0;
}
public Node3(int d, Node3 n)
{
data=d;
link=n;
}
public void setlink(Node3 n)
{
link =n;
}
public void setData(int d)
{
data=d;
}
public Node3 getlink()
{
return link;
}
public int getData()
{
return data;
}
}
class linkedList
{
protected Node3 start;
public linkedList()
{
start=null;
}
public boolean isEmpty()
{
return start==null;
}
public void insert(int val)
{
Node3 nptr,ptr,save=null;
nptr = new Node3(val,null);
boolean ins = false;
if(start==null)
start=nptr;
else if (val<=start.getData())
{
nptr.setlink(start);
start=nptr;
}
else
{
save=start;
ptr= start.getlink();
while(ptr!=null)
{
if(val>=save.getData()&&val<=ptr.getData())
{
save.setlink(nptr);
nptr.setlink(ptr);
ins=true;
break;
}
else
{
save=ptr;
ptr=ptr.getlink();
}
}
if(ins==false)
{
save.setlink(nptr);
}
}
}
public void traverse()
{
Node3 ptr =start;
System.out.print(start.getData()+ " --->");
ptr= start.getlink();
while(ptr.getlink()!=null)
{
System.out.print(ptr.getData() + " --->");
ptr = ptr.getlink();
}
System.out.print(ptr.getData() + "!!!!");
System.out.println();
}
public void search(int val)
{
Node3 ptr = start;
int pos=1;
while(ptr!=null)
{
if (ptr.getData()==val)
{
System.out.println("Item"+val + "found at position" + pos);
break;
}
else
{
ptr=ptr.getlink();
pos++;
}
}
if(ptr==null)
{
System.out.println("Item"+val +"is not found");
}
}
}
class linkedTest
{
protected static linkedList S;
public static void main(String args[])
{
int num ;
S=new linkedList();
BufferedReader br = new BufferedReader(new
InputStreamReader(System.in));
System.out.println(".....Starting list for searching.....\n");
for(int a=0;a<7;++a)
{
System.out.print("Enter a number:");
try
{
num=Integer.parseInt(br.readLine());
S.insert(num);

}
catch(Exception e)
{
System.out.println(e);
}
}
System.out.println("\n Now list is:");
S.traverse();
System.out.print("\n\nEnter number to be searched:");
try
{
num=Integer.parseInt(br.readLine());
S.search(num);
}
catch(Exception e)
{
System.out.println(e);
}
System.out.println("\n----List test over----");
}
}

VARIABLE DESCRIPTION

Variable Data Type Description


To store any integer.
val Int
ins boolean To return the true or
false.
a Int To iterate for loop

num int To store the integer.


INHERITANCE

• PROGRAM TO ILLUSTRATE THE COCEPT OF A HIDDEN MEMBER


VARIABLE AND AN OVERRRIDDEN METHOD
ALGORITHM
STEP1: CREATE CLASS SuperClass
STEP 2: CREATE A SUBCLASS
STEP 3: CREATE A FUNCTION void test()
STEP 4: END

PROGRAM
class SuperClass
{
String id= "id of SuperClass";
void identify()
{
System.out.println("This is identify() of superclass");
}
}
class SubClass extends SuperClass
{
String id = "id of SubClass";
void identify()
{
System.out.println("This is identify() of subclass");
}
void test()
{
System.out.println(id);
identify();
System.out.println(super.id);
super.identify();
}
}

VARIABLE DESCRIPTION
VARIABLE DATA TYPE DESCRIPTION
Id String To sore the string
• METHOD OVERRIDING

ALGORITHM
STEP 1: CREATE CLASS Animal
STEP 2: CREATE FUNCTION eat()
STEP 3: END THIS FUNCTION
STEP 4: CREATE A SUBCLASS dog
STEP 5: OVERRIDING eat() METHOD.
STEP 6: CREATE bark() METHOD IN SUBCLASS
STEP 7: CREATE CLASS Main
STEP 8: CREATE METHOD main.
STEP 9: CREATE AN OBJECT OF THE SUBCLASS
STEP 10: CALL THE eat() METHOD
STEP 11: END

PROGRAM

class Animal
{
public void eat()
{
System.out.println("i can eat");
}
}
class Dog extends Animal
{

public void eat()


{
System.out.println("i eat dog food");
}
public void bark()
{
System.out.println("i can bark");
}
}
class Main
{
public static void main(String args[])
{
Dog labrador= new Dog();
labrador.eat();
labrador.bark();
}
}

NO VARIABLE.
• HIERARCHICAL INHERITANCE

ALGORITHM
STEP 1: : CREATE CLASS Animal
STEP 2: CREATE FUNCTION eat()
STEP 3: END THIS FUNCTION
STEP 4: CREATE A SUBCLASS Dog
STEP 5: CREATE bark() METHOD IN SUBCLASS
STEP 6: CREATE A SUBCLASS Cat
STEP 7: CREATE A METHOD meow() IN THE SUBCLASS
STEP 8: CREATE CLASS Test
STEP 9: CREATE METHOD main.
STEP 10: CREATE OBJECT
STEP 11: END
PROGRAM

class Animal1
{

void eat()
{
System.out.println("eating...");
}
}
class Dog extends Animal1
{
void bark()
{
System.out.println("barking...");
}
}
class Cat extends Animal1
{
void meow(){
System.out.println("meowing...");
}
}
class Test
{
public static void main(String args[])
{
Cat c=new Cat();
c.meow();
c.eat();
//c.bark();//C.T.Error
}
}

NO VARIABLE.
• CLASS HEIRARCHY

ALGORITHM
STEP 1: CREATE CLASS Bank
STEP 2: CREATE PARAMETERIZED CONSTRUCTOR
STEP 3: CREATE long getBalance() FUNCTION AND RETURN balance
STEP 4: CREATE void deposit() FUNCTION
balance=balance + amount
STEP 5: CREATE FUNCTION void withdraw()
CHECK IF (balance-amount)>1000
balance =balance -amount
ELSE
PRINT can’t withdraw
STEP 6: CREATE FUNCTION void display()
STEP 7: CREATE SUBCLASS Saving
STEP 8: CREATE void addInterest() FUNCTION IN THE SUBCLASS
STEP 9: CREATE APARAMETERIZED CONSTRUCTOR FOR THE SUBCLASS
STEP 10: CREATE METHOD main
STEP 11: COMPUTE
STEP 12: END METHOD main
STEP 13: END

PROGRAM

class Bank
{
protected static int nextAccno=1;
protected String firstname;
protected String surname;
protected String address;
protected int accNo;
protected long balance;
public Bank(String fn,String sn,String ad)
{
firstname =fn;
surname=sn;
address=ad;
balance=1000;
accNo= nextAccno;
++nextAccno;
}
public long getBalance()
{
return balance;
}
public void deposit(long amount)
{
balance=balance+amount;
}
public void withdraw(long amount)
{
if((balance -amount)>1000)
balance = balance-amount;
else
System.out.print("can't withdraw");
}
public void display()
{
System.out.println("\nCustomer" + firstname + " " + surname);
System.out.println("Account number=" + accNo);
System.out.println("Account balance = " + (balance/100.0));
}
}
class Saving extends Bank
{
private double rate;
public void addInterest(int month)
{
long interest;
interest = (long) (balance* rate/100.0*month/12.0);
balance= balance+ interest;
}
public Saving(String fn,String sn,String ad,double r)
{
super(fn,sn,ad);
rate=r;
}
public static void main(String args[])
{
Bank b1,b2;
b1=new Bank("Rubina","Khan","D.Park");
b2=new Bank("Tupur","Sen" ,"S.lake");
b1.deposit(15500);
b2.deposit(10000);
b1.display();
b2.display();
b1.withdraw(5500);
b2.withdraw(11000);
b1.display();
b2.display();
Saving depacc;
depacc= new Saving("Bula","Paul","P.Lane" ,9.0);
depacc.deposit(10000);
depacc.addInterest(10);
depacc.display();
}
}

VARIABLE DESCRIPTION
Variable Data Type Description

firstname String To store the first name.

surname String To store the surname.

address String To store the address.

accNo int To store the account


number.
balance long To store the balance of
that required person.
nextAccno int Initialise it with 1. An
accumulator.
fn String To make copy of
firstname.
sn String To make copy of
surname
ad String To make copy of
address
amount long To store the amount
entered by the user.
rate double To store the rate
percent.
month int To store the no of
months
interest long To store the interest

r double To make copy of the


rate.

2-D ARRAY PROGRAMS


• SADDLE POINT

ALGORITHM
STEP 1: START THE ALGORITHM
STEP 2: CREATE CLASS SaddlePoint
STEP 3: CREATE METHOD main.
STEP 4: TAKE INPUT FROM THE USER AND STORE IT IN THE
DECLARED VARIABLE n
STEP 5: CREATE ARRAY arr[][] OF nxn.
STEP 6: START for x=0 x<n
STEP 7: START for y=0 y<n
STEP 8: TAKE IN PUT FROM THE USER FOR THE ELEMENTS OF THE
ARRAY
STEP 9: DECLARE A VARIABLE found = false
STEP 10: START for x=0 x<n
STEP 11: INITIALISE A VARIABLE rMin AND STORE THE FIRST
ELEMENT IN IT.
STEP 12: INITIALISE A VARIABLE cIdx TO 0.
STEP 13 : START for y=0 y<n
STEP 14: CHECK IF arr[x][y] <rMin
STEP 15: THEN rMin = arr[x][y] AND cIdx =0
STEP 16: END for
STEP 17: START for k=0 k<n
STEP 18: CHECK IF arr[k][cIdx] >rMin THEN BREAK
STEP 19: END for.
STEP 20: CHECK IF k==n
STEP 21: found= true
STEP 22: PRINT Saddle point
STEP 23: END for
STEP 24: CHECK IF found== false
STEP 25: PRINT No saddle point
STEP 26: END METHOD main
STEP 27: END CLASS
STEP 28: END ALGORITHM.
PROGRAM
import java.util.Scanner;
class SaddlePoint
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter size:");
int n=sc.nextInt();
int arr[][]=new int[n][n];
System.out.println("Enter elements");
for(int x=0;x<n;x++)
{
for(int y=0;y<n;y++)
{
arr[x][y]=sc.nextInt();
}
}
boolean found = false;
for(int x=0;x<n;x++)
{
int rMin=arr[x][0];
int cIdx=0;
for(int y=0;y<n;y++)
{
if(arr[x][y]<rMin)
{
rMin = arr[x][y];
cIdx = y;
}
}
int k=0;
for(k=0;k<n;k++)
{
if(arr[k][cIdx]>rMin)
break;
}
if(k==n)
{
found = true;
System.out.println("Saddle point=" + rMin);
}
}
if(found==false)
{
System.out.print("No saddle point");
}
}
}

VARIABLE DESCRIPTION
Variable Data Type Description

n int To store the input.

arr[][] int To store the array

found boolean To initialise with false

x int To iterate for loop

y int To iterate for loop

rMin int To store the saddle


point
CIdx int To store the column
number
k int To iterate for loop

• SUM OF DIAGONALS

ALGORITHM
STEP 1: START THE ALGORITHM
STEP 2: CREATE CLASS SaddlePoint
STEP 3: CREATE METHOD main.
STEP 4: TAKE INPUT FROM THE USER AND STORE IT IN THE
DECLARED VARIABLE n
STEP 5: CREATE ARRAY arr[][] OF nxn.
STEP 6: START for x=0 x<n
STEP 7: START for y=0 y<n
STEP 8: TAKE IN PUT FROM THE USER FOR THE ELEMENTS OF THE
ARRAY
STEP 9: PRINT Original array
STEP 10: INITIALISE VARIABLE ld and rd TO 0
STEP 11: START for x=0 x<n
STEP 12: START for y=0 y<n
STEP 13: PRINT arr[x][y]
STEP 14: CHECK IF x==y THEN UPDATE ld+=arr[x][y]
STEP 15: ELSE IF CHECK x+y==m-1 THEN UPDATE rd+=arr[x][y]
STEP 16: END for
STEP 17: PRINT
STEP 18: END for
STEP 19: PRINT sum of left diagoanal
STEP 20: PRINT sum of right diagonal
STEP 21: END METHOD main.
STEP 22: END CLASS
STEP 23: END ALGORITHM
PROGRAM
import java.util.Scanner;
class SumDiagonal
{
public static void main(String args[])
{
Scanner sc= new Scanner(System.in);
System.out.println("Enter size");
int n = sc.nextInt();
int arr[][] = new int[n][n];
System.out.println("Enter elements:");
for (int x=0;x<n;x++)
{
for(int y=0;y<n;y++)
{
arr[x][y]= sc.nextInt();
}
}
System.out.print("Original array:");
int ld=0,rd=0;
for (int x=0;x<n;x++)
{
for(int y=0;y<n;y++)
{
System.out.print(arr[x][y] + "\t");
if(x==y)
ld +=arr[x][y];
else if(x+y == n-1)
rd +=arr[x][y];
}

System.out.println();
}
System.out.print(" sum of left diagonal" + ld);
System.out.print("sum of right diagoanl" + rd);
}
}

VARIABLE DESCRIPTION
Variable Data Type Description

n int To store the input.

arr[][] int To store the array

ld int To store the sum of


left diagonal
x int To iterate for loop

y int To iterate for loop


rd int To store the sum of
right diagonal

• SUM OF ODD NUMBERS OF EACH ROW AND PRODUCT OF EVEN


NUMBERS OF EACH ROW.

ALGORITHM
STEP 1: START THE ALGORITHM
STEP 2: CREATE CLASS SumO
STEP 3: CREATE METHOD main.
STEP 4: TAKE INPUT FROM THE USER AND STORE IT IN THE
DECLARED VARIABLE n
STEP 5: CREATE ARRAY arr[][] OF nxn.
STEP 6: START for x=0 x<n
STEP 7: START for y=0 y<n
STEP 8: TAKE IN PUT FROM THE USER FOR THE ELEMENTS OF THE
ARRAY
STEP 9: PRINT Array elements
STEP 10: START for
STEP 11: INITIALISE VARIABLE sumO and proE TO 0
STEP 12 : START for
STEP 13: CHECK IF arr[x][y] %2==0 THEN UPDATE proE
STEP 14: ELSE UPDATE sumO
STEP 15: PRINT ELEMENTS
STEP 16: PRINT proE AND sumO.
STEP 17: END for
STEP 18: PRINT
STEP 19: END for
STEP 20: END METHOD main
STEP 21: END class
STEP 22: END ALGORITHM
PROGRAM
import java.util.Scanner;
class SumO
{
public static void main(String args[])
{
Scanner sc= new Scanner(System.in);
System.out.println("Enter size");
int n = sc.nextInt();
int arr[][] = new int[n][n];
System.out.println("Enter elements:");
for (int x=0;x<n;x++)
{
for(int y=0;y<n;y++)
{
arr[x][y]= sc.nextInt();
}
}
System.out.print("Array elements");
for (int x=0;x<n;x++)
{
int sumO =0,proE = 1;
for(int y=0;y<n;y++)
{
if(arr[x][y]%2==0)
proE+=arr[x][y];
else
sumO +=arr[x][y];
System.out.print(arr[x][y] +"\t");
System.out.print(" product="+ proE + "\t" + "sum=" +
sumO);
}
System.out.println();
}
}
}
VARIABLE DESCRIPTION
Variable Data Type Description

n int To store the input.

arr[][] int To store the array

proE int To store the product

x int To iterate for loop


y int To iterate for loop

SumO int To store the sum


• PRINT BOUNDARY ELEMENTS
ALGORITHM
STEP 1: START THE ALGORITHM
STEP 2: CREATE CLASS SumO
STEP 3: CREATE METHOD main.
STEP 4: TAKE INPUT FROM THE USER AND STORE IT IN THE
DECLARED VARIABLE n
STEP 5: CREATE ARRAY arr[][] OF nxn.
STEP 6: START for x=0 x<n
STEP 7: START for y=0 y<n
STEP 8: TAKE IN PUT FROM THE USER FOR THE ELEMENTS OF THE
ARRAY
STEP 9: PRINT Array elements
STEP 10: START for x=0 x<n
STEP 11: START for y=0 y<n
STEP 12: PRINT arr[x][y]
STEP 13: END for
STEP 14: PRINT
STEP 15: END for
STEP 16: START for
STEP 17: START for
STEP 18: CHECK IF x==0
STEP 19: PRINT arr[x][y]
STEP 20: ELSE IF CHECK x==n-1
STEP 21: PRINT arr[x][y]
STEP 22: ELSE IF CHECK y==0
STEP 23: PRINT arr[x][y]
STEP 24: ELSE IF CHECK y=n-1
STEP 25: PRINT arr[x][y]
STEP 26: ELSE PRINT SPACE
STEP 27: END for
STEP 28: PRINT
STEP 29: END for
STEP 30 : END METHOD main
STEP 31: END ALGORITHM

PROGRAM
import java.util.Scanner;
class Boundary
{
public static void main(String args[])
{
Scanner sc= new Scanner(System.in);
System.out.println("Enter size");
int n = sc.nextInt();
int arr[][] = new int[n][n];
System.out.println("Enter elements:");
for (int x=0;x<n;x++)
{
for(int y=0;y<n;y++)
{
arr[x][y]= sc.nextInt();
}
}
System.out.print("Array elements");

for (int x=0;x<n;x++)


{

for(int y=0;y<n;y++)
{

System.out.print(arr[x][y] +"\t");

}
System.out.println();
}
for (int x=0;x<n;x++)
{
for(int y=0;y<n;y++)
{
if(x==0)
System.out.print(arr[x][y] +"\t");
else if (x==n-1)
System.out.print(arr[x][y] +"\t");
else if(y==0)
System.out.print(arr[x][y] +"\t");
else if(y==n-1)
System.out.print(arr[x][y] +"\t");
else
System.out.print(" ");
}
System.out.println();
}
}
}

VARIABLE DESCRIPTION
Variable Data Type Description

n int To store the input.

arr[][] int To store the array

x int To iterate for loop

y int To iterate for loop


DATE PROGRAMS
1. TO CHECK THE VALIDITY OF A DATE.

ALGORITHM
STEP 1: START THE ALGORITHM
STEP 2: CREATE CLASS CheckDate
STEP 3: CREATE METHOD main.
STEP 4: INITIALISE AN ARRAY days[]
STEP 5: TAKE INPUT FROM THE USER.
STEP 6: CHECK IF y>1900&&y<=9999
STEP 7: CHECK IF (y%4==0)&&(y%100!=0)||(y%400==0)
STEP 8: THEN UPDATE day[1]++
STEP 9: CHECK IF m>0 &&m<=12
STEP 10: CHECK IF d>0&&d<days[m-1]
STEP 11: THEN PRINT valid date
STEP 12: ELSE PRINT invalid date
STEP 13: END METHOD main
STEP 14: END CLASS
STEP 15: END ALGORITHM.
PROGRAM-

import java.util.*;
class CheckDate
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
int days[] = {31,28,31,30,31,30,31,31,30,31,30,31}
String input;
System.out.print(" enter date :");
input= sc.nextLine();
int d=Integer.parseInt(input.substring(0,2));
int m =Integer.parseInt(input.substring(3,5));
int y=Integer.parseInt(input.substring(6));
if(y>1900&&y<=9999)
{
if(((y%4==0)&&(y%100!=0))||(y%400==0)
{
days[1]++;
}
if(m>0&&m<=12)
{
if(d>0&&d<days[m-1]
{
System.out.print("Valid date");
}
else
{
System.out.print("Invalid date");
}
}
else
{
System.out.print("Invalid date");
}
}
else
{
System.out.print("Invalid date");
}
}
}

VARIABLE DESCRIPTION
Variable Data Type Description

days[] int To store the array

input String To store the input

d int To store the substring


of the input
m int To store the substring
of the input
y int To store the substring
of the input

2. FIND THE DAY OF THE ENTERED DATE

ALGORITHM
STEP 1: START THE ALGORITHM
STEP 2: CREATE CLASS FindDay
STEP 3: CREATE METHOD main
STEP 4: CREATE ARRAY month OF int TYPE
STEP 5: CREATE ARRAY names OF String TPE
STEP 6: TAKE INPUT FROM THE USER.
STEP 7: CREATE StringTokenizer OBJECT
STEP 8: CHECK IF (count<=0||count>3)
STEP 9: THEN PRINT invalid
STEP 10: RETURN
STEP 11: INITIALISE day ,m and year VARIABLES
STEP 12: DECLARE boolean leapyear VARIABLE
STEP 13: CHECK IF (leapyear)
STEP 14: THEN UPDATE month[1] =29
STEP 15: CHECK IF (m<1||m>12)
STEP 16: THEN PRINT Invalid AND RETURN.
STEP 17: CHECK IF day<1||day>month[m-1]
STEP 18: THEN PRINT Invalid AND RETURN
STEP 19: TAKE INPUT
STEP 20: START for
STEP 21: CHECK IF (names[i].equalsignorecase(startdayname))
THEN UPDATE Idx =i AND BREAK
STEP 22: CHECK IF Idx==-1 THEN PRINT Invalid AND RETURN
STEP 23: START for.
STEP 24: UPDATE tdays = tdays+month[i]
STEP 25: UPDATE tdays=day
STEP 26: IF CIdx >=7 THEN UPDATE CIdx -=7
STEP 27: PRINT THE DAY
STEP 28: END METHOD main
STEP 29: CREATE A FUNCTION boolean isLeapyear()
STEP 30: INITIALISE A VARIABLE ret TO false
STEP 31: CHECK IF (y%400==0) THEN ret=true
STEP 32: ELSE IF (y%100==0) THEN ret=false
STEP 33: ELSE IF(y%4==0) THEN ret =true
STEP 34: ELSE ret=false;
STEP 35: RETURN ret
STEP 36: END THIS FUNCTION
STEP 37: END CLASS
STEP 38: END ALGORITHM

PROGRAM-
import java.util.*;
class FindDay
{
public static void main(String args[])
{
int month[] = {
31,28,31,30,31,30,31,31,30,31,30,31};
String names[] = {
"MONDAY","TUESDAY","WEDNESDAY","THURSDAY","FRIDAY","SAT
URDAY","SUNDAY"};
Scanner sc= new Scanner(System.in);
System.out.print("enter date");
String date = sc.nextInt();
StringTokenizer st = new StringTokenizer(date,"/");
int count=st.countTokens();
if(count<=0 ||count>3)
{
System.out.print("Invalid");
return;
}
int day = Integer.parseInt(st.nextToken());
int m = Integer.parseInt(st.nextToken());
int year = Integer.parseInt(st.nextToken());
boolean leapyear = isLeapyear(year);
if (leapyear)
{
month[1] =29;
}
if(m<1||m>12)
{
System.out.println("invalid");
return;
}
if( day <1||day>month[m-1])
{
System.out.println("invalid");
return;
}
System.out.print("Day on 1st Jan:");
String startdayname=sc.nextLine();
int Idx = -1;
for(int i=0;i<names.length();i++)
{
if (names[i].equalsIgnoreCase(startdayname))
{
Idx = i;
break;
}
}
if(Idx==-1)
{
System.out.println("invalid");
return;
}
int tdays=0;
for (int i =0;i<m-1;i++)
{
tdays+=month[i];
}
tdays+=day;
int CIdx = tdays%7 + Idx -1;
if (CIdx >=7)
{
CIdx-=7;
}
System.out.print("day on "+ date+ ":"+ names[CIdx]);
}
public static boolean isLeapyear(int y)
{
boolean ret=false;
if(y%400==0)
{
ret=true;
}
else if(y%100==0)
{
ret=false;
}
else if(y%4==0)
{
ret=true;
}
else
{
ret = false;
}
return ret;
}
}

VARIABLE DESCRIPTION
Variable Data Type Description

month int To store the array

names String To store the array.

date String To store the date.

count int To store the words

day int To store the day

m int store the month

year int To store the year

leapyear boolean To check leapyear

startdayname String To store the day name

Idx int To store the start index

i int To iterate for loop

tdays int To store the total days

CIdx int To store the current


day index.
ret boolean To check leapyear

You might also like