Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
5 views76 pages

Computer Project 12

isc class 12 computer science project
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views76 pages

Computer Project 12

isc class 12 computer science project
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 76

PREFACE

Java is a popular third generation programming language. Java


serves as a platform for internet applets and stand alone
applications. It has contributed a lot towards its popularity. It
supports the Object-Oriented Programming methodology(OOP),
the latest in software development and the most near to real
world.
The following project file contains Java programs
which implement OOP Concepts like Data Abstraction, Data
Encapsulation, Modularity and Polymorphism. These programs
are some of our laboratory work done throughout our ISC
classes.
This project clearly depicts the programming in java ASPECTS OF
PROGRAMMING BY EXPLAINING VERIETY OF PROGRAMS THAT
CAN BE DESINED USING JAVA. Each program is completely
executed and its output is also provided after each program to
get clear explanation of program.
Algorithms and variables descriptions have also been placed in
the program where ever necessary to understand how the
program proceeds and explain the use of any variable or any
code in the program.
INDEX

S.N Topic Pg.N


o. o.
1 Program to bring all vowels at the beginning of
the word
2 Program to check perfect no. using recursion
3 Program to check equality of two matrices
4 Program to check palindrome no. using
recursion
5 Program to add any two accpted times
6 Program to perform string related operations
7 Program to check disarium no. using recursion
8 Program to perform string related operations
9 Program to concatenate two positive numbers
10 Program to count no. of words and consonants
in a string
11 Program to print fibonacci string
12 Program to store details of a stock and update
it
13 Program to check armstrong number using
recursion
14 Program to shuffle the matrix
15 Program to find radius and area of circle
16 Prorgam to find difference between Angle X
and Angle Y
17 Program to print frequency of each element of
array
18 Program to print each word along with no. of
vowels in it
19 Program to print sentence in alphabetical order
20 Program to print border elements of a matrix
21 Program to print prime elements in a matrix
22 Program to insert an element at a given
position
23 Program to remove consecutively repeating
characters from a string
24 Program to print numerical value of entered
roman no.
25 Program to print unique number between two
limits
26 Program to delete a given element from the
array
27 Program to print kaprekar no. between two
limits
28 Program to check special factorian no.
29 Prorgam to check date is valid or not
30 Program to check prime adam number
PROGRAM-1 [ISC 2019]

+
ALGORITHM:
STEP 1:Start
STEP 2:Enter a word and convert it into upper case
STEP 3:Run loop for i=0 till i is less than length of the word
STEP 4:Count the no. of vowels and consonants
STEP 5:Again run the loop for i=0 till i is less than length of the word
STEP 6:Rearrange the words by bringing vowels at the beginning of the
word followed by constants
STEP 6:Print the original word and the rearranged word
STEP 7:Stop
import java.util.*;
public class Rearrange
{ String wrd,newwrd;
Scanner x=new Scanner(System.in);
Rearrange() //function to initialize variables to null
{ wrd=” “;
newwrd=” “;
}
void readword()
{ System.out.println("Enter a word" );
wrd=x.next();
wrd=wrd.toUpperCase(); //to convert word into upper case
void freq_vow_con()
{ int s=0,s1=0;
char ch;
for(int i=0;i<wrd.length();i++)
{ ch=wrd.charAt(i); //to extract characters
if("AEIOU".indexOf(ch)!=-1) //to check no. of vowels
s++; }
s1= wrd.length()-s; //to check no. of consonants
System.out.println("vowels = "+ s);
System.out.println("consonants = " + s1);
} void arrange()
{ char ch;
String p="",q="";
for(int i=0;i<wrd.length();i++)
{ ch=wrd.charAt(i);
if("AEIOU".indexOf(ch)!=-1)
p +=ch;
else
q +=ch;
} newwrd= p+q; //new word after rearranging of vowels &
consonants
} void display() //to display original word along with rearranged
word
{ System.out.println("Original word = "+ wrd);
System.out.println("Rearranged word = "+ newwrd);
} static void main()
{ Rearrange obj=new Rearrange();
obj.readword();
obj.freq_vow_con();
obj.arrange();
obj.display();
}}
VARIABLE DESCRIPTION:
VARIABLES DATA TYPE FUNCTION
wrd String to store original word
newwrd String to store rearranged word
i int counter variable
s int to count vowels
s1 int to count consonants
ch char to extract characters
p String to rearrange vowels
q String to rearrange consonants

INPUT AND OUTPUT:


PROGRAM-2 [ISC-
2018]

ALGORITHM:
STEP 1:Start
STEP 2:Input a number and initialize it to a data member
STEP 3:Return 0 if factor is the no. itself
STEP 4:If no. is divisible by factor,then return sum of factors
STEP 5:Call the function and check if the no. is perfect or not and print if
its perfect no. or not
STEP 6:Stop
import java.util.*;
class Perfect
{ int num;
Perfect(int nn)
{ num=nn; //to initialize data members
} int sum_of_factors(int i)
{ if (i == num) //to return if factor is no. itself
return 0;
else if (num%i==0)
return i + sum_of_factors(i+1); //to print sum of factors
else
return sum_of_factors(i+1);
} void check()
{ int s=sum_of_factors(1);
if (s==num) //to check if sum of factors is equal to the no.
System.out.println(num+"is a perfect number");
else
System.out.println(num+"is not a perfect number");
} public static void main()
{ Scanner sc=new Scanner(System.in);
System.out.println("Enter a number"); //to input the no.
int no=sc.nextInt();
Perfect ob=new Perfect(no);
Perfect ob.check();
} }
VARIABLE DESCRIPTION:
VARIABLE DATA TYPE FUNCTION
num int to store number
nn int to initialize num to nn
i int counter variable
s int to store sum of factors
INPUT AND OUTPUT:

program-3 [ISC 2018]


ALGORITHM:
STEP 1:Start
STEP 2:Input no. of rows and no. of columns and initialize them
STEP 3:Enter the elements in the array
STEP 4:Run loop for i=0 till i<no. of rows
STEP 5:Run loop for i=0 till i<no. of columns
STEP 6:Check if the two matrices are equal or not.If not equal return 0
and if equal then return 1
STEP 7: Print the array elements
STEP 8: Stop
import java.util.*;
class EqMat
{ int a[][];
int m;
int n;
Scanner sc=new Scanner(System.in);
EqMat(int mm,int nn) //to initialize data members
{ m=mm;
n=nn;
a=new int[m][n]; //to enter array elements
} void readarray()
{ System.out.println("enter" + (m*n) + "elements" );
for (int i=0;i<m;i++)
for (int j=0;j<n;j++)
a[i][j]=sc.nextInt();
} int check(EqMat P,EqMat Q)
{ for (int i=0;i<P.m;i++)
for (int j=0;j<P.n;j++)
{ if (P.a[i][j]!=Q.a[i][j]) //to check equality of two matrices
return 0;
} return 1;
} void print()
{ for (int i=0;i<m;i++)
{ System.out.println();
for (int j=0;j<n;j++)
System.out.print(a[i][j]+"\t"); //to print array elements
} }
public static void main()
{ EqMat ob1=new EqMat(3,3);
EqMat ob2=new EqMat(3,3);
System.out.println("enter nos for the Ist Matrix");
ob1.readarray();
System.out.println("enter nos for the 2nd Matrix");
ob2.readarray();
if (ob1.check(ob1,ob2)==1)
{ System.out.println("Equal Matrix");
ob1.print();
ob2.print();
}
else
System.out.println("not equal");
} }
VARIABLE DESCRIPTION:
VARIABLE DATA TYPE FUNCTION
a int to store array elements
m int to store no. of rows
n int to store no. of columns
mm int to initialize m
nn int to initialize n
i int counter variable
j int counter variable
P int to refer to current object
Q int to refer to current object
INPUT AND OUTPUT:

PROGRAM-4 [ISC 2017]

ALGORITHM:
STEP 1:Start
STEP 2:Input a number and initialize it
STEP 3:If y>0 then reverse y and store it in revnum using recursion
STEP 4:Check if the reverse of the no. is equal to the no. or not
STEP 5:If reverse is equal to the no. then print palindrome number else
print not palindrome
STEP 6: Stop
import java.util.*;
public class Palin
{ int num,revnum;
Scanner x=new Scanner(System.in);
Palin()
{ num=0;revnum=0;
} void accept()
{ System.out.println( "Enter a number");
num=x.nextInt();
} int reverse(int y)
{ if(y>0)
{ revnum =revnum * 10 + y%10;
return reverse(y/10);
} else
return revnum;
} void check()
{ int p=num;
if( num==reverse(p))
System.out.println("palindrome");
else
System.out.println("not a palindrome");
} static void main()
{ Palin obj=new Palin();
obj.accept();
obj.check();
}}
VARIABLE DESCRIPTION:
VARIABLE DATA FUNCTION
TYPE
num int to input a number
revnum int to reverse the number
p int to invoke the function reverse()
y int to reverse it and store it in
revnum
INPUT AND OUTPUT:
PROGRAM-5 [ISC 2017]

ALGORITHM:
STEP 1: Start
STEP 2:Input an array to store two elements
STEP 3 :Enter the array elements and assign 0 to them
STEP 4:Convert hours into minutes and add X and Y and store the sum in
current object
STEP 5:Print the array elements i.e. hours and minutes
STEP 6:End
import java.util.*;
public class Adder
{ int a[]=new int[2];
Scanner x=new Scanner(System.in);
Adder()
{ a[0]=0;a[1]=0;
} void readtime()
{ System.out.println("Enter hours and minutes");
a[0]=x.nextInt();
a[1]=x.nextInt();
} void disptime()
{ System.out.println("Hours=" + a[0]);
System.out.println("Minutes=" + a[1]);
} void addtime(Adder X,Adder Y)
{ a[1]=X.a[1] + Y.a[1];
a[0]=a[1]/60;
a[1]=a[1]%60;
a[0] += X.a[0] + Y.a[0];
} static void main()
{ Adder a=new Adder();
Adder b=new Adder();
Adder c=new Adder();
a.readtime();
b.readtime();
c.addtime(a,b);
c.disptime();
} }
VARIABLE DESCRIPTION:
VARIABLE DATA TYPE FUNCTION
a int to store array elements
X int current object
Y int current object

INPUT AND OUTPUT:


PROGRAM-6 [ISC 2017]

ALGORITHM:
STEP 1:Start
STEP 2:Input the word in upper case and calculate its length
STEP 3:Swap the first and the last character in the string
STEP 4:Run loop for i=65 till i<=90 and increase i by 1
STEP 5:Run loop for j=0 till j<length of word and increase j by 1
STEP 6:Sort the characters alphabetically
STEP 7:Print the original word,swapped word and sorted word
STEP 8:End
import java.util.*;
public class SwapSort
{ String wrd,swapwrd,sortwrd;
int len;
static Scanner x=new Scanner(System.in);
SwapSort()
{ swapwrd="";
sortwrd="";
} void readword()
{ System.out.println("Enter word in Upper case");
wrd=x.next();
len=wrd.length();
} void swapchar()
{ swapwrd=wrd.charAt(len-1) + wrd.substring(1,len-1) + wrd.charAt(0);
} void sortword()
{ char c;
for(int i=65;i<=90;i++)
{ for(int j=0;j<len;j++)
{ c=wrd.charAt(j);
if(c==i)
sortwrd += c;
} }
} void display()
{ System.out.println("Original word = " + wrd);
System.out.println("Swapped word = " + swapwrd);
System.out.println("Sorted word = " + sortwrd);
} static void main()
{ SwapSort x=new SwapSort();
x.readword();
x.swapchar();
x.sortword();
x.display();
}}
VARIABLE DESCRIPTION:
VARIABLE DATA TYPE FUNCTION
wrd String to store original word
len int to store length of word
swapwrd String to store swapped word
sortwrd String to store sorted word
i int loop variable
j int loop variable

INPUT AND OUTPUT:

PROGRAM-7 [ISC 2016]


ALGORITHM:
STEP 1:Start
STEP 2:Input the number and the size of the number
STEP 3:Initialize n=nn & size=0.Count the no. of digits present in them
STEP 4:Calculate the sum of digits of the number according to the
position of the digit using recursion
STEP 5:Print whether the number is disarium number or not
STEP 6:Stop
import java.util.Scanner;
public class Disarium
{ int num,size;
static Scanner sc=new Scanner(System.in);
Disarium(int nn)
{ num=nn;
size=0;
} void countDigits()
{ int a=num;
while(a!=0)
{ a=a/10;
size++;
} }
int sumofDigits(int n, int p)
{ return (n==0)? 0: sumofDigits(n/10,p-1) + (int)Math.pow(n%10,p);
} void check()
{ if(num==sumofDigits(num,size))
System.out.println(" Disarium Number");
else
System.out.println(" Not a Disarium Number");
} static void main()
{ System.out.println("Input a Number");
int m=sc.nextInt();
Disarium x= new Disarium(m);
x.countDigits();
x.check();
}}
VARIABLE DESCRIPTION:
VARIABLE DATA TYPE FUNCTION
num int to store a number
size int to store size of the number
p int to store power of digit
INPUT AND OUTPUT:
PROGRAM-8 [ISC 2016]

ALGORITHM:
STEP 1:Start
STEP 2:Accept the word in lower case and also the length of the word
STEP 3:Run loop for i=0 till i<length of word and increase i by 1
STEP 4:Check for the consonants present in the word and bring them to
the beginning of the word
STEP 5:Change all the consonants into upper case
STEP 6:Print the original word,shifted word and changed word
STEP 7:End
import java.util.Scanner;
public class ConsChange
{ String word;
int len;
static Scanner sc=new Scanner(System.in);
ConsChange()
{
len=0;
word= “”;
} void readword()
{ System.out.println("Enter word in Lower case");
word=sc.next();
len=word.length();
}
void shiftcons()
{ String s="";
char c;
for(int i=0;i<len;i++)
{ c=word.charAt(i);
if(c!='a' && c!='e' && c!='i' && c!='o' && c!='u')
s +=c;
} for(int i=0;i<len;i++)
{ c=word.charAt(i);
if(c=='a' || c=='e' || c=='i' || c=='o' || c=='u')
s +=c;
} System.out.print("\n Sorted Word="+s);
word=s;
} void changeword()
{ char c; String s="";
for(int i=0;i<len;i++)
{ c=word.charAt(i);
if(c!='a' && c!='e' && c!='i' && c!='o' && c!='u')
s +=(char)(c-32);
else
s +=c;
} System.out.println("\n Changed word= " + s);
}
void show()
{ System.out.print("\n Original word= " + word);
shiftcons();
changeword();
} static void main()
{ ConsChange X=new ConsChange();
X.readword();
X.show();
}}
VARIABLE DESCRIPTION:
VARIABL DATA TYPE FUNCTION
E
word String to store a word
len int to store length of word
c String to check constants in the word
s String to store changed and sorted
word
INPUT AND OUTPUT:
PROGRAM-9 [ISC 2015]

ALGORITHM:
STEP 1:Start
STEP 2:Input two numbers and initialize it to 0
STEP 3:Convert the numbers into string
STEP 4:Concatenate the two strings and again convert them into
numbers
STEP 5:Print the original numbers along with the merged numbers
STEP 6:End
import java.util.*;
public class Merger
{ long n1,n2,mergNum;
Merger()
{ n1= n2 = mergNum = 0;
} void readNum()
{ Scanner x=new Scanner(System.in);
System.out.println("Enter two numbers");
n1=x.nextLong();
n2=x.nextLong();
} void JoinNum()
{ String s=Long.toString(n1);
String s1=Long.toString(n2);
String s2=s+s1;
mergNum=Long.valueOf(s2);
} void show()
{ System.out.println("First Number= " + n1);
System.out.println("Second Number= " + n2);
System.out.println("Merged Number= " + mergNum);
} static void main()
{ Merger obj=new Merger();
obj.readNum();
obj.JoinNum();
obj.show();
}}
VARIABLE DESCRIPTION:
VARIABLE DATA FUNCTION
TYPE
n1 int to store first number
n2 int to store second number
mergNum int to store merged number
s1 String to convert first no. into string
s2 String to convert second no. into
string
s String to concatenate the two
numbers
INPUT AND OUTPUT:

PROGRAM -10 [ISC 2015]

ALGORITHM:
STEP 1:Start
STEP 2:Input a sentence and its length and initialize them
STEP 3:Run loop for i=0 till i<length of sentence and increase by 1
STEP 4:Count the no. of words present in the sentence
STEP 5:Check for the consonants present in the world and count them
STEP 6:Print the original string along with the no. of words and no. of
consonant
STEP 7:Stop
public class TheString
{String str
; int len,wordcount,cons;
TheString( )
{ str=””;
wordcount=0;
cons=0;
} TheString(String ds)
{ str=ds; len=str.length();
} void countFreq()
{ char c;
str=str.toLowerCase ();
for(int i=0;i<len;i++)
{ c=str.charAt(i);
if(c==32)
wordcount++;
if(c!='a' && c!='e' && c!='i' && c!='o' && c!='u')
cons++;
} cons = cons – wordcount;
++wordcount;
} void Display()
{ System.out.println(" Number of words=" + wordcount );
System.out.println(" Number of consonants=" + cons);
} static void main()
{ TheString x=new TheString("india is my country");
x.countFreq();
x.Display();
}}
VARIABLE DESCRIPTION:
VARIAB DATA FUNCTION
LE TYPE
str String to store a sentence
len int to store length of sentence
wordcoun int to store no. of words in sentence
t
cons int to store no. of consonants in
sentence

INPUT AND OUTPUT:


PROGRAM-11 [ISC 2014]

ALGORITHM:
STEP 1:Start
STEP 2:Input the first string,second string and the no. of terms
STEP 3:Assign x=a,y=b & z=ba
STEP 4:Print x+”,”+y and run loop for i=0 till i<=no.of terms-2 and
increase by 1
STEP 5:Print “,”+z and assign x=y,y=z & z=x+y
STEP 6:Print the fibonacci strings by calling the functions
STEP 7:End
import java.util.*;
class FiboString
{ String x,y,z;
int n;
FiboString( )
{ x="a"; y="b"; z="ba";
} void accept ()
{ Scanner Sc = new Scanner (System.in);
System.out.println ("Enter number of terms") ;
n = Sc.nextInt( );
} void generate()
{ System.out.print(x+","+y);
for(int i=0;i<=n-2;i++)
{ System.out.print(","+z);
x=y;
y=z;
z=y+x;
}}
static void main( )
{ FiboString obj=new FiboString( );
obj.accept( );
obj.generate();
}}

VARIABLE DESCRIPTION:
VARIA DATA FUNCTION
BLE TYPE
x String to store first string
y String to store second string
z String to store concatenation of previous two
strings
i int loop variable
INPUT AND OUTPUT:
PROGRAM-12 [ISC 2014]

ALGORITHM:
STEP 1:Start
STEP 2:In the first class,print the name of item,quantity,rate and amount
STEP 3:In the subclass,input unit price & purchased quantity.Also enter
item name,quantity ,rate,amount using super(a,b,c);
STEP 4:Add quantity and purchase quantity and amount=quantity*rate
STEP 5:Print stock details before and after updation
STEP 6:End
class Stock
{ String item;
double qty,rate,amt;
Stock(String a,double b,double c)
{ item=a;
qty=b;
rate=c;
amt=qty * rate;
} void display()
{ System.out.println("Name of the item : "+item);
System.out.println("Quantity: "+qty);
System.out.println("Rate per unit: "+rate);
System.out.println("Net value: "+amt);
} } class Purchase extends Stock
{ int pqty;
double prate;
Purchase(String a, double b, double c, int d, double e)
{ super(a,b,c);
pqty=d;
prate=e;
} void update()
{ qty += pqty;
if(prate!=rate)
rate=prate;
amt = qty * rate;
} void display()
{ super.display();
update();
super.display();
}}
VARIABLE DESCRIPTION:
VARIABL DATA FUNCTION
E TYPE
item String to store name of item
qty double to store quantity of item in stock
rate double to store unit price of an item
amt double to store net price of item
pqty int to store purchased quantity
prate double to store unit price of purchased
item
INPUT AND OUTPUT:

PROGRAM-13 [ISC-2019]
ALGORITHM:
STEP 1:Start
STEP 2:Input the number and the length of the number and initialize
n=nn
STEP 3:Return 0 if i==0 else return sum of each digit raised to the
power of length of the number
STEP 4:Check if this sum is equal to the no. or not
STEP 5:If equal then print armstrong number else print not armstrong
number
STEP 6:End
import java.util.*;
public class ArmNum
{ int n,l;
Scanner x=new Scanner(System.in);
ArmNum(int nn)
{ n=nn;
l=Integer.toString(n).length();
} int sum_pow(int i)
{ if(i==0)
return 0;
else
return (int)Math.pow(i%10,l) + sum_pow(i/10);
}
void isArmstrong()
{ if(sum_pow(n)==n)
System.out.println(n + "is an Armstrong number");
else
System.out.println(n + "is not an Armstrong number");
} static void main()
{ ArmNum obj=new ArmNum(371);
obj.isArmstrong();
}}
VARIABLE DESCRIPTION:
VARIABLE DATA TYPE FUNCTION
n int to store a number
l int to store length of number
nn int to initialize n=nn
INPUT AND OUTPUT:

PROGRAM-14 [ISC 2016]


ALGORITHM:
STEP 1:Start
STEP 2:Input the array elements,no. of rows & columns in the array
STEP 3:Initialize m=mm & n=nn
STEP 4:Run loop for i=0 till i<no. of rows and increase i by 1
STEP 5:Run loop for j=0 till j<no. of columns and increase j by 1
STEP 6:If i!=0 then mat[i-1][j]=P.mat[i][j];
STEP 7:Print the matrix elements
STEP 8:Stop
import java.util.Scanner;
public class Shift
{ static Scanner sc=new Scanner(System.in);
int mat[][];
int m,n;
Shift(int mm,int nn)
{ m=mm;
n=nn;
mat=new int[m][n];
} void input()
{ System.out.println("Enter elements");
for(int i=0;i<m;i++)
for(int j=0;j<n;j++)
mat[i][j]=sc.nextInt();
} void display()
{ for(int i=0;i<m;i++)
{ System.out.println();
for(int j=0;j<n;j++)
System.out.print(mat[i][j] +"\t");
} } void cyclic(Shift P)
{ for(int i=0;i<m;i++)
for(int j =0;j<n;j++)
{ if(i!=0)
mat[i-1][j]=P.mat[i][j];
else
mat[m-1][j]=P.mat[0][j];
} } static void main()
{ Shift x=new Shift(3,4);
Shift y=new Shift(3,4);
x.input();
y.cyclic(x);
x.display();
y.display();
}}
VARIABLE DESCRIPTION:
VARIABL DATA FUNCTION
E TYPE
mat int to store array elements
m int to store no. of rows
n int to store no. of columns
P Shift current object
INPUT AND OUTPUT:

PROGRAM-15 [ISC 2015]


ALGORITHM:
STEP 1:Start
STEP 2:Input the x & y coordinates of first end point and print them
STEP 3:Input x & y coordinates of second end point and also input radius
and area of circle in sub class
STEP 4:Assign initial value to data members of super class using
super(x1,y1) and also initialize data members of current class
STEP 5:Find length of radius using formula:(Math.sqrt(x2-x1)*(x2-x1)+
(y2-y1)*(y2-y1))/2 and area of circle:3.14*radius*radius
STEP 6:Print x2,y2,radius and area of circle
STEP 7:End
class Plane
{ int x1;
int y1;
Plane(int nx,int ny)
{x1=nx;
y1=ny;
}void show()
{System.out.println(x1);
System.out.println(y1);
}}
class Circle extends Plane
{ int x2;
int y2;
double radius;
double area;
Circle(int nx,int ny,int ox,int oy)
{ super(nx,ny);
x2=ox;
y2=oy;
}void findRadius()
{ radius=Math.sqrt((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1));
} void findArea()
{area=3.14*radius*radius;
} void Show()
{findRadius();
findArea();
System.out.println(x2);
System.out.println(y2);
System.out.println("Radius="+radius);
System.out.println("Area="+area);
}}
VARIABLE DESCRIPTION:
VARIABL DATA FUNCTION
E TYPE
x1 int to store x coordinate of first point
y1 int to store y coordinate of first point
x2 int to store x coordinate of second
point
y2 int to store y coordinate of second
point
radius double to store radius of circle
area double to store area of circle
INPUT AND OUTPUT:

PROGRAM-16

ALGORITHM
STEP 1-START
STEP 2-Take instance variables degree and min of integer type
STEP 3-Define a default constructor Angle() to initialize degree,min to 0
STEP 4-Define a parameterized constructor Angle(int d,int m) to initialize
degree to d and min to m
STEP 5-Define a function diffangle(Angle X,Angle Y) of Angle type to
calculate difference between two angles
STEP 6-Create an object Q of Angle class and Calculate difference of two
angles and store it in Q object and return it.
STEP 7-Define a function displayangle() of void datatype to print angles
in respect of degree and min.
STEP 8-Create main() method and Create object X and Y of Angle
datatype to accept first and second angle in respect of degree and min.
STEP 9-Create an object Z of Angle datatype and Call Angle() function.
STEP 10-Call diffangle(X,Y) and store the result in Z.
STEP 11-Call displayangle() function and print 1st,2nd and difference of
both angles.
STEP 12-STOP
class Angle
{int degree,min;
System.out.println(“Degree”+”\t”+”Min”);
Angle()
{degree=0;
min=0;
} Angle(int d,int m)
{ degree=d;
min=m;
} Angle diffangle(Angle X,Angle Y)
{ int a1,a2,a3,a,b;
Angle Q=new Angle();
a1=X.degree*60+X.min;
a2=Y.degree*60+Y.min;
a3=a1+a2;
a=a3/60;
b=a3%60;
Q.degree=a;
Q.min=b;
return Q;
} void displayangle()
{ System.out.println(degree+"\t"+min);
} void main()
{ Angle X=new Angle(43,12);
Angle Y=new Angle(56,32);
Angle Z=new Angle();
Z=Z.diffangle(X,Y);
X.displayangle();
Y.displayangle();
Z.displayangle();
}}
VARIABLE DESCRIPTION:
INPUT AND OUPUT:
VARIABLE DATATYPE PURPOSE
degree int To store degrees
min int To store minutes
d int Initialize degree
m int Initialize min
a1 int Stores first angle
a2 int Stores second angle
a3 int Adds both angles
a int Stores degrees of resultant angle
b int Stores minutes of resultant angle

PROGRAM-17
ALGORITHM:
STEP 1- Input the size of the array as n
STEP 2- Create an array of n elements
STEP 3- Run a loop i from 0 to n
STEP 4-Run a loop j from i+1 to n
STEP 5- If the elements a[i] and a[j] are equal and a[j] is not zero, count
and put zero in a[j]
STEP 6- Outside the inner loop, print frequency of a[i] if it is not zero
STEP 7-End
import java.util.*;
class Frequency
{public static void main()
{ Scanner sc=new Scanner(System.in);
System.out.println("Enter the number of terms: ");
int n=sc.nextInt();
int a[]=new int[n];
System.out.println("Enter "+n+ " integers");
int i;
for(i=0;i<n;i++)
a[i]=sc.nextInt();
int j,f;
for(i=0;i<n;i++)
{ f=1;
for(j=i+1;j<n;j++)
{ if(a[i]==a[j] && a[j]!=0)
{ f++;
a[j]=0;
}}
if(a[i]!=0)
System.out.println("Frequency of "+a[i]+" : "+f);
}
}}
VARIABLE DESCRIPTION:
VARIABLE DATA TYPE FUNCTION
n int to store no. of terms in array
i int loop variable
j int loop variable
f int to count frequency of each
element
INPUT AND OUTPUT:

PROGRAM-18
ALGORITHM:
Step-1: Enter a string and Find the length of the string
Step-2: Run a loop to access the string
Step-3: Extract a character from the string
Step-4: If it is not blank goto step 11
Step-5: Extract the word from the string and Run a loop to access the
word Step-6: Check for vowels.If vowel found, increment a counter
Step-7: Once the inner loop terminates display the word and the counter
Step-8: Keep checking the rest of the string until the end
Step-9: End
import java.io.*;
class Question13
{ public static void main(String fh[])throws IOException
{
BufferedReader br = new BufferedReader(new
InputStreamReader(System.in)); System.out.println("Enter a sentence : ");
String str=br.readLine();
str+=" ";
int l=str.length();
String t=" ";
int i,j,c=0,p=0;
for(i=0;i<l;i++)
{ char ch=str.charAt(i);
if(ch==' ')
{ t=str.substring(p,i+1);
c=0;
for(j=0;j<t.length();j++)
{ char k=t.charAt(j);
if(k=='a'||k=='e'|| k=='i' || k=='o' ||k=='u' || k=='A' || k=='E' || k=='I' ||
k=='O' || k=='U')
c++;
} System.out.println(“Word\tVowel”):
System.out.println(t+"\t"+c);
p=i+1;
}}
}}
VARIABLE DESCRIPTION:
VARIABLE DATA TYPE FUNCTION
str String to input a sentence
t String to extract words
l int to store length of sentence
c int to count vowels
i int loop variable
j int loop variable
INPUT AND OUTPUT:
PROGRAM-19

ALGORITHM:
Step-1: Enter a sentence and Convert it into lowercase
Step-2: Take the length of the string
Step-3: Run a loop to access the string
Step-4: Extract a character. If character is blank space or a period,
extract the word and store it in an array
Step-5: Continue the process until all the words are stored in the array
Step-6: Now run two loops i and j for sorting
Step-7: Compare the strings in the array.If ith string is greater than jth
string swap them
Step-8: Continue the process till the entire array is sorted in ascending
order Step-9: Run a loop and display array elements with a blank space.
Step-10: End
import java.io.*;
public class Q {
public static void main(String args[])throws IOException
{BufferedReader br = new BufferedReader(new
InputStreamReader(System.in));
System.out.println("Enter a string : ");
String str = br.readLine();
str=str.toLowerCase();
int l=str.length();
String a[]=new String[l];
int i,j,p=0,x=0;
for(i=0;i<l;i++)
{char ch=str.charAt(i);
if(ch==' ' || ch=='.'){
String temp=str.substring(p,i);
a[x++]=temp;
p=i+1;
}
}for(i=0;i<x;i++){
for(j=i+1;j<x;j++){
if(a[i].compareTo(a[j])>0){
String temp=a[i];
a[i]=a[j]; a[j]=temp;
}}}
System.out.println("Sentence in alphabetical order: ");
for(i=0;i<x;i++)
System.out.print(a[i]+" ");
}}
VARIABLE DESCRIPTION:
VARIABLE DATA TYPE FUNCTION
str String to input a sentence
l int to input length of sentence
i int loop variable
j int loop variable
temp String swapping variable
a String array to store words
INPUT AND OUTPUT:
PROGRAM-20

ALGORITHM:
Step-1: Input the number of rows ‘n’ for a square matrix
Step-2: Create an nxn matrix and Enter nxn elements from the user
Step-3: Run a loop for the number of rows and also for number of
columns
Step-4: If row is first or last or if column if first or last add the element
Step-5: Continue the process till the entire matrix is checked
Step-6: Display the sum
Step-7: End
import java.util.*;
public class Question18 {
public static void main(String args[])throws InputMismatchException
{ Scanner sc=new Scanner(System.in);
System.out.println("Enter the number of rows for a square matrix: ");
int n=sc.nextInt();
int a[][]=new int[n][n];
int i,j,s=0;
System.out.println("Enter "+(n*n)+" elements:");
for(i=0;i<n;i++){
for(j=0;j<n;j++)
{ a[i][j]=sc.nextInt();
}}
for(i=0;i<n;i++){
for(j=0;j<n;j++){
if(i==0 || j==0 || i==n-1 || j==n-1)
{ s=s+a[i][j];
}}}
System.out.println("Sum of border elements: "+s);
}
}
VARIABLE DESCRIPTION:
VARIABLE DATA TYPE FUNCTION
a int array to store elements
i int loop variable
j int loop variable
n int no. of rows for square matrix
s int to store sum of border elements
INPUT AND OUTPUT:

PROGRAM-21
ALGORITHM:
Step-1: Input the number of /rows and columns as m and n
Step-2: Create a m x n matrixand Enter m x n elements in the matrix
Step-3: Run a loop for the rows m and also for the columns n
Step-4: Now run a loop to find the number of factors of the matrix
element
Step-5: If the number of factors is 2 print the element, its row index and
column index
Step-6: Continue process for all the elements
Step-7: End
import java.util.*;
public class Question20{
public static void main(String args[])throws InputMismatchException
{ Scanner sc=new Scanner(System.in);
System.out.println("Enter the number of rows and columns of a matrix: ");
int m=sc.nextInt();
int n=sc.nextInt();
int a[][]=new int[m][n];
int i,j,k,s=0;
System.out.println("Enter "+(m*n)+" elements:");
for(i=0;i<m;i++){
for(j=0;j<n;j++)
{ a[i][j]=sc.nextInt();
}}
System.out.println("Prime Element\t Row\t Column");
for(i=0;i<m;i++){
for(j=0;j<n;j++){
int c=0;
for(k=1;k<=a[i][j];k++){
if(a[i][j]%k==0)
{ c++;
}}
if(c==2)
{ System.out.println(a[i][j]+"\t\t"+i+"\t\t"+j);
}
}}
}}
VARIABLE DESCRIPTION:
VARIABLE DATA TYPE FUNCTION
m int to input no. of rows
n int to input no. of columns
a int array to store elements
i int loop variable
j int loop variable
c int counter variable
INPUT AND OUTPUT:
PROGRAM-22

ALGORITHM:
Step-1: Enter the size in n and Create an array of size (n+1)
Step-2: Enter n elements from the user
Step-3: Enter the element(e) and the position(p)
Step-4: Run a loop i to access array
Step-5: Check if the position is invalid by comparing it with i+1. If match
not found keep checking with the rest of the array
Step-6: If match found run a loop from n to p and shift all the elements
one place to the right, store the element e in the desired position, raise a
flag and break out of loop
Step-7: Outside the loop, check if the flag is raised
Step-8: If it is display the modified array else display error message
Step-9: End
import java.util.*;
public class Question10{
public static void main(String args[])throws InputMismatchException
{ Scanner sc=new Scanner(System.in);
int i,j,k;
System.out.println("Enter the size of the array: ");
int n=sc.nextInt();
int a[]=new int[n+1];
System.out.println("Enter "+n+" integers:");
for(i=0;i<n;i++){
a[i]=sc.nextInt();
}System.out.println("Enter the element to be inserted: ");
int e=sc.nextInt();
System.out.println("Enter the position: ");
int p=sc.nextInt();
boolean flag=false;
for(i=0;i<n;i++)
{ if(i+1==p){
for(j=n;j>i;j--){
a[j]=a[j-1];
}a[i]=e; flag=true; n++;
break;
}}
if(flag){
for(i=0;i<n;i++)
System.out.print(a[i]+" ");
}else
{System.out.print("Position Invalid");
}} }
VARIABLE DESCRIPTION:
VARIAB DATA FUNCTION
LE TYPE
n int to store size of array
a int array to store elements
e int to input the element to be inserted
p int to input the position for inserting the
element
INPUT AND OUTPUT:

PROGRAM-23
Write a program to enter a string and remove consecutively repeating characters from a
string
Algorithm:-
Step-1: Enter a string
Step-2: Find the length of the string
Step-3: Copy the string to a character array Step-4: Run a loop ‘i’ to access the array
Step-4: Compare ith and (i+1)th element of the array Step-6: If both the elements are not
equal go to step 9 Step-7: Run a loop from i to the end
Step-5: Shift all the characters one place to the left Step-9: Decrease the length of the array
Step-6: Keep checking the rest of the string until the end Step-11: Set original string to null
Step-7: Run a loop and add all the elements of the array to the string Step-13: Display the
string
Step-8: End
import java.io.*;
public class Question14
{public static void main()throws IOException
{BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter a string : ");
String str = br.readLine();
char a[]=str.toCharArray();
int i,j,l=a.length;
for(i=0;i<l-1;i++)
{if(a[i]==a[i+1])
{for(j=i;j<l-1;j++)
{a[j]=a[j+1];
}l--;
i--;}
}
str="";
for(i=0;i<l;i++){
str+=a[i];
}System.out.println("The modified string is: "+str);
}}

VARIABLE DESCRIPTION:
Name Type Function
str String to input a sentence
i int loop variable
j int loop variable
l int to input length of string
a[] int array for storing words

INPUT AND OUTPUT:

PROGRAM-24
Program to print the numerical value of the entered Roman Number.

ALGORITHM:
STEP 1-Start.
STEP 2-Accept a String x.
STEP 3-Create a String a to store roman number.
STEP 4-Create a array b[] to values corresponding to roman number.
STEP 5-Extract each element of x and match it with the corresponding values a[] and b[].
STEP 6-Do the sum of the corresponding values.
STEP 7-Stop.
class Roman
{public static void main(String x)
{String a="IVXLCDM";
int b[]={1,5,10,50,100,500,1000};
int c,d,e=0,f,n=0,i=0;
char ch;
int g[]=new int[100];
for(c=0;c<x.length();c++)
for(d=0;d<a.length();d++)
if(x.charAt(c)==a.charAt(d))
{e=d;
g[c]=b[e];
}for(f=0;f<g.length-1;f++)
if(g[f]<g[f+1])
g[f]=-g[f];
for(int p=0;p<a.length();p++)
n=n+g[p];
System.out.println("The integer value of the roman no. "+x+" is "+n);
}}

VARIABLE DESCRIPTION:
NAME TYPE FUNCTION
x String To enter a string
a String To store roman nos. in the array
c int Loop variable
e int Loop variable

f int Loop variable


n int To sum up the numbers

INPUT AND OUTPUT:


PROGRAM-25
Program to print unique nos. between two limits . A unique no. is one in which none of
the digits gets repeated.

ALGORITHM:
STEP 1-Start
STEP 2-Create a function to accept a number
STEP 3-Create an array of 10 elements
STEP 4-Generate a loop which takes out each digit
of the number.
STEP 5-Update index no. of array Of which digit is Obtained
STEP 6-Check each element of array &check if any>1
STEP 7-Return true if none>1&false if any>1
STEP 8-Generate main() accepting limits a & b(STEP 9&10)
STEP 9-Check uniqueness of each Number
STEP 10-Print the unique no.
STEP 11-End
Class Unique
{public static boolean uni(int n)
{
int f[]=new int[10],b;
boolean x=true;
for(b=n;b>0;b/=10)
f[b%10]++;
for(b=0;b<10;b++)
if(f[b]>1)
x=false;
return x;
}public static void main(int a,int b)
{
for(int c=a;c<=b;c++)
if(uni(c)==true)
System.out.println(c+”is unique”);
}
}

VARIABLE DESCRIPTION:
Name Type Function
a Integer Lower limit
b Integer Upper limit
n Integer Number
f Integer Array
x Integer Stores result

c Integer Loop variable

INPUT AND OUTPUT:


PROGRAM-26

ALGORITHM:
Step-1: Enter the size in n and Create an array of size (n+1)
Step-2: Enter n elements from the user
Step-3: Enter the element(e) to be deleted
Step-4: Run a loop i to access array
Step-5: Check if the element(e) is present in the array. If match not found goto step 11
Step-6: If match found run a loop from i to n-1
Step-7: Shift all the elements one place to the left
Step-8: decrease the size of loop, raise a flag and break out of loop Step-11: Keep checking
the rest of the array until the end
Step-9: If the flag is raised display the modified array else display error message
Step-10: End
import java.util.*;
public class Q1{
public static void main()
{ Scanner sc=new Scanner(System.in);
System.out.println("Enter the size of the array: ");
int n=sc.nextInt();
int a[]=new int[n];
int i,j,k;
System.out.println("Enter "+n+" integers:");
for(i=0;i<n;i++){
a[i]=sc.nextInt();
}System.out.println("Enter the element to be deleted: ");
int e=sc.nextInt();
boolean flag=false;
for(i=0;i<n;i++)
{ if(a[i]==e){
for(j=i;j<n-1;j++){
a[j]=a[j+1];
}flag=true; n--;
break;
}
}if(flag)
{ for(i=0;i<n;i++)
System.out.print(a[i]+" ");
}else
{System.out.print("Element not found");
}} }

VARIABLE DESCRIPTION:
VARIABLE DATA TYPE FUNCTION
n int to store size of array
a int array to store elements
i int loop variable
j int loop variable
e int element to be deleted
INPUT AND OUTPUT:
PROGRAM-27
Program to print Kaprekar number between two limits.
e.g. 1,9,45,55,99
45: 45^2=2025=20+25=45 9: 9^2=81=8+1=9
Algorithm: -
STEP 1-Start
STEP 2-Take two limits
STEP 3-Run for a loop between two limits & call work (int n).
STEP 4-Store no. of digits of n in f & s=10^f & s=10^F & sq=n*n.
STEP 5-Using s, store first half digits of sq in I & remaining digits in r.
STEP 6-If r+1==n, then print n, the passed no.
STEP 7-End.
import java.io. * ;
class Kaprekar
{
void work(int n)
{
int c=n, f=0, s, r, l;
do {
f++;c/=10;
}
while(c>0);
s=(int)Math.pow (10, f);
r=(n*n) %s;
l=(n*n)/s;
if(r+I==n)
System.out.println(n);
}
void main()throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println(“Enter two limits”);
int l, u;
I=Integer.parseInt(br.readLine());
u=Integer.parseInt(br.readLine());
for(int i=1;i<=u;i++)
work(i);
}}

Variable Description
Name Data Type Function
I,u Integer To store lower & upper limit
i Integer Loop variable

n Integer To store single number


f Integer To store no. of digits
r,l Integer To store parts of n^2

Input and Output:-


PROGRAM-28

Algorithm:-
STEP 1-Start
STEP 2-Initialize sum of factorials as 0
STEP 3-Add d! to sum of factorials
STEP 4-If sum of factorials is same as given no.,return true
STEP 5-Else return false
import java.util.*;
class CHECK
{ int n;
CHECK();
{n=0;
}
void input()
{Scanner sc=new Scanner(System.in);
System.out.println(“Enter the number to be checked”);
n=sc.nextInt();
} int fact(int X)
{
int dig=0,sum=0;
for(int i=X;i>0;i/=10)
{dig=i%10;
int factorial=1;
for(int j=1;j<=dig;j++)
{factorial *=j;
}
sum+=factorial;
}return sum;
}
void isspecial()
{int sumoffac=fact(n);
if(sumoffac==n)
System.out.println(n+” is a Special Factorian number”);
else
System.out.println(n+” is not a Special Factorian number”);
} void main()
{
CHECK obj=new CHECK();
obj.input();
obj.isspecial();
}
}

Variable Description
Name Data Type Function
n Integer To store a number
dig Integer To store extracted digits of the no.
sum Integer To calculate the sum of digits
i Integer Loop variable
factorial Integer To calculate factorial of the no.
Input and Output:-

PROGRAM-29
Program to check whether the date is valid or not. If the date is valid or invalid , print the
appropriate message.
Algorithm:-
STEP 1- Start.
STEP 2- Enter day,month and year.
STEP 3- Take an array to store last days of the 12 months.
STEP 4- Use ternary operator to check whether the year is divisible by 400 and 100 or 4 to
check leap year and add 1 int month of February.
STEP 5- Check whether the month is between 1-12 otherwise print month is invalid.
STEP 6- Check whether the number of days according to corresponding element of the
array.
STEP 7- Check whether the year>0 and if all the conditions are true then print date is valid.
STEP 8-End.
class Valid_Date
{
public static void main(int dd,int mm,int yy)
{
int k=0,e=0;
int m[]={30,28,31,30,31,30,31,31,30,31,30,31};
k=(yy%400==0)?yy%100==0?1:0:yy%4==0?1:0;
m[k]=m[k]+k;
if(mm<1 || mm>12)
{
e=1;
System.out.println(mm+"is invalid month");
}
if(dd<0 || dd>m[mm-1])
{e=1;
System.out.println(dd+" is invalid date");
}
if(yy<1)
{
e=1;
System.out.println(yy+" is invalid year");
}
if(e==0)
System.out.println(dd+"-"+mm+"-"+yy+" is valid date");
}
}
Variable Description :-
NAME TYPE FUNCTION
dd int To store day
mm int To store month
yy int To store year
K int To test condition

INPUT AND OUTPUT:

PROGRAM-30
STEP 1-START
STEP 2-Create main(int m,int n) method to accept two positive integers m
& n where m is less than n entered by user and to find prime adam
integers between m & n
STEP 3-Declare variables i,j,r,sq,k,b,a,l,p of int type and Declare and
initialize d of int type as 0
STEP 4-Print “THE PRIME-ADAM INTEGERS ARE:”
STEP 5-Repeat for i=m to n by 1
STEP 6-Declare and initialize variable c of int type as 0
STEP 7-Repeat for j=1 to i by 1
STEP 8-r=i%j.if r=0 then increase c by 1
STEP 9-if c==2 the find square of i and store it in sq
STEP 10-Declare and initialize variable x of int type as 0 and Repeat for
l=sq to l>0 by l/10
STEP 11-p=l%10
STEP 12-x=x*10+p
STEP 13-Declare and initialize variable t of int type as 0 and Repeat for
k=i to k>0 by k/10
STEP 14-b=k%10
STEP 15-t=t*10+b
STEP 16-Store square of t in a
STEP 17-if x=a then print i and increase d by 1
STEP 18- Print FREQUENCY OF PRIME-ADAM INTEGERS IS=d
STEP 19-STOP
class alpha
{void main(int m,int n)
{int i,j,r,sq,k,b,a,l,p;
int d=0;
System.out.println("THE PRIME-ADAM INTEGERS ARE:");
for(i=m;i<=n;i++)
{ int c=0;
for(j=1;j<=i;j++)
{ r=i%j;
if(r==0)
c++;
} if(c==2)
{ sq=i*i;
int x=0;
for(l=sq;l>0;l=l/10)
{ p=l%10;
x=x*10+p;
} int t=0;
for(k=i;k>0;k=k/10)
{ b=k%10;
t=t*10+b;
} a=t*t;
if(x==a)
{System.out.println(i);
d++;
}}}
System.out.println("FREQUENCY OF PRIME-ADAM INTEGERS
IS="+d);
}}
VARIABLE DESCRIPTION:
VARIA DATATY FUNCTION
BLE PE
m int Initialize minimum limit of checking prime-
adam integers
n int Initialize maximum limit of checking prime-
adam integers
i int Control variable
j int Control variable
r int Helps in finding factors
c int Counts number of factors

INPUT AND OUTPUT:

You might also like