ISC Computer Assignment
ISC Computer Assignment
Assignment
On
By-
Bhargav Parashar 12 S
Acknowledgement
Thank you.
NUMBER PROGRAMS
SOURCE CODE:
import java.util.*;
class CircularPrime
{
boolean isPrime(int n) // Function for checking whether a
number is prime or not
{
int c = 0;
for(int i = 1; i<=n; i++)
{
if(n%i == 0)
c++;
}
Bhargav Parashar 12 S Page | 7
if(c == 2)
return true;
else
return false;
}
int circulate(int n) //Function for circulating the digits to
form new number
{
String s = Integer.toString(n);
String p = s.substring(1)+s.charAt(0);
int a = Integer.parseInt(p);
return a;
}
void isCircularPrime(int n) //Function to check for circular
prime
{
int f = 0,a = n;
do
{
System.out.println(a);
if(isPrime(a)==false)
{
f = 1;
}
a = circulate(a);
}while(a!=n);
if(f==1)
System.out.println(n+" IS NOT A CIRCULAR PRIME");
else
System.out.println(n+" IS A CIRCULAR PRIME");
}
public static void main(String args[])
{
CircularPrime ob = new CircularPrime();
Scanner sc = new Scanner(System.in);
System.out.print("Enter a number : ");
Bhargav Parashar 12 S Page | 8
int n = sc.nextInt();
ob.isCircularPrime(n);
}
}
Output:
Enter a number : 87
87
87 IS NOT A CIRCULAR PRIME
import java.io.*;
public class EvilNumber
{
int n,d,count,val,x,i,y,k,sum,flag,d1,l;
public void main() throws IOException
{
BufferedReader obj=new BufferedReader (new
InputStreamReader(System.in));
System.out.println("Enter number to checked:");
n=Integer.parseInt(obj.readLine());
d=n;
d1=d;
while(n>0) //while loop to calculate the
required length of array
{
n=n/2;
count++;
}
int array[]=new int[count];
int array2[]=new int[count];
x=0;
while(d>0) //while loop to find and store the
remainders in 'array[count]'
{
val=d%2;
array[x]=val;
Bhargav Parashar 12 S Page | 11
x++;
d=d/2;
}
for(i=count-1; i>=0;i--) //for loop to reverse
'array[count]' and find the binary equivalent
{
array2[y]=array[i];
y++;
}
for(k=0; k<count;k++) //for loop to count the number
of 1's
{
if(array2[k]==1)
sum++;
}
if(sum%2==0) //if conditional to check if there
are even number of ones
{
flag++;
}
System.out.print("INPUT:"+ d1); // Displaying
the Binary equivalent and number of 1's
System.out.println();
System.out.print("BINARY EQUIVALENT:");
for(l=0; l<count;l++)
{
System.out.print(array2[l]);
}
System.out.println();
System.out.print("NO. OF 1's :" + sum);
System.out.println();
if(flag>0) //if conditional to check if there are even
number of ones or not
{
System.out.print("EVIL NUMBER"); // Displaying
output
}
else
Bhargav Parashar 12 S Page | 12
{
System.out.print("NOT AN EVIL NUMBER");
}
}
}
Output:
Enter a positive number : 26
Binary Equivalent = 11010
Number of Ones = 3
26 is Not an Evil Number.
SOURCE CODE:
import java.util.*;
public class Kaprekar
{
int n,p,q,i,count,num,num1,freq,power1,part1,part2,sum;
Bhargav Parashar 12 S Page | 14
double power;
public void main()
{
Scanner scan=new Scanner(System.in);
System.out.println("Enter begining limit");
p=scan.nextInt();
System.out.println("Enter end limit");
q=scan.nextInt();
if(p>5000||q>5000)
{
System.out.println("out of range");
}
else
{
calculate();
}
}
void calculate()
{
num=p;
while(num<=q)
{
p=num;
freq=0;
while(p!=0)
{
p=p/10;
freq++;
}
num1=num*num;
power=Math.pow(10,freq);
int power1=(int)power;
part1=num1%power1;
part2=num1/power1;
Bhargav Parashar 12 S Page | 15
sum=part1+part2;
if(sum==num)
{
count++;
System.out.println(num+" ");
}
num++;
}
System.out.println("Frequency : " +count);
}
}
Output:
Enter begining limit
1
Enter end limit
100
1
9
45
55
99
Frequency : 5
SOURCE CODE:
import java.util.*;
public class PrimePalindrome
{
public void main()
{
Scanner scan=new Scanner(System.in);
int m,n,i,j,t,c,r,a,freq;
System.out.println("Enter two positive integers m and
n, where m<3000 and n<3000");
m=scan.nextInt();
n=scan.nextInt();
if(m<3000 && n<3000)
{
freq=0;
System.out.println("The prime palindrome integers
are:");
Bhargav Parashar 12 S Page | 17
for(i=m;i<=n;i++)
{ t=i;
c=0;
for(j=1;j<=t;j++)
{
if(t%j==0)
c++;
}
if(c==2)
{
r=0;
while(t>0)
{
a=t%10;
r=r*10+a;
t=t/10;
}
if(r==i)
{
System.out.print(i+" ");
freq++;
}
}
}
System.out.println("\nFrequency of prime
palindrome integers:"+freq);
}
else
{
System.out.println("Out of Range");
}
}
}
Output:
m=100
n=100
N=1000
The prime palindrome integers are:
import java.util.*;
class PronicNumber
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter a number : ");
int n = sc.nextInt();
int flag = 0;
for(int i=0; i<n; i++)
{
if(i*(i+1) == n)
{
flag = 1;
break;
}
}
if(flag == 1)
System.out.println(n+" is a Pronic Number.");
else
System.out.println(n+" is not a Pronic Number.");
}
}
Output:
Enter a number : 110
110 is a Pronic Number.
SOURCE CODE:
import java.io.*;
class UniqueNumber
{
public static void main(String args[])throws IOException
{
BufferedReader br=new BufferedReader(new
InputStreamReader(System.in));
System.out.print("Enter any number : ");
int n=Integer.parseInt(br.readLine());
if(flag==0)
System.out.println("**** The Number is a Unique
Number ****");
else
System.out.println("**** The Number is Not a Unique
Number ****");
}
}
Output:
Output:
Enter a number : 195
195 is a Harshad Number.
Enter a number : 194
194 is not a Harshad Number.
SOURCE CODE:
import java.io.*;
class TwinPrimeRange
{
boolean isPrime(int n) //function for checking prime
{
int count=0;
for(int i=1; i<=n; i++)
{
if(n%i == 0)
count++;
}
if(count == 2)
return true;
else
return false;
}
public static void main(String args[]) throws IOException
{
TwinPrimeRange ob = new TwinPrimeRange();
BufferedReader br=new BufferedReader(new
InputStreamReader(System.in));
SOURCE CODE:
class Pattern1 //legs
{
void main(int n) //main fuction
{
int i,j;
System.out.println("Pattern :");
System.out.println();
int f=(n/2)+1; //for first line
int g=(n/2); //line of symmetry
for(i=1;i<=(n/2);i++)
{
for(j=1;j<=n;j++)
{
if((j>=f&&j<=g)&&(j!=1))
{
System.out.print(" ");
}
else
{
System.out.print("*");
}
}
f=f-1;
g=g+1;
System.out.println();
}
}
}
Bhargav Parashar 12 S Page | 30
OUTPUT
Pattern :
**********
**** ****
*** ***
** **
* *
SOURCE CODE:
public class Pattern2 //mouth
int i,j;
System.out.println("Pattern :");
System.out.println();
or star
if(j>=i)
System.out.print("*");
Bhargav Parashar 12 S Page | 32
}
else
System.out.print(" ");
System.out.println();
for(j=1;j<=n;j++)
if(j>=i)
System.out.print("*");
else
System.out.print(" ");
}
Bhargav Parashar 12 S Page | 33
}
System.out.println();
OUTPUT
Pattern : n=10
**********
*********
********
*******
******
*****
****
***
**
*
*
**
***
****
*****
******
*******
********
*********
**********
SOURCE CODE:
public class Pattern3
void main(int n)
int i,j;
System.out.println("Pattern :");
System.out.println();
for(j=1;j<=n;j++)
if(j>=f8&&j<=g8)
{
Bhargav Parashar 12 S Page | 36
System.out.print(" ");
else
System.out.print("*");
f8=f8+1;
g8=g8-1;
System.out.println();
for(j=1;j<=n;j++)
if((j>=f6&&j<=g6)&&(j!=1))
System.out.print(" ");
Bhargav Parashar 12 S Page | 37
}
else
System.out.print("*");
f6=f6-1;
g6=g6+1;
System.out.println();
OUTPUT
Pattern :
* *
** **
*** ***
**** ****
**********
**********
**** ****
*** ***
** **
* *
import java.io.*;
class Boundary_Element
{
Bhargav Parashar 12 S Page | 40
public static void main(String args[])throws IOException
{
int i,j,m,n;
BufferedReader br=new BufferedReader(new
InputStreamReader(System.in));
ALGORITHM:
/* Algorithm for main() method
* Step 1 : Start of algorithm
Bhargav Parashar 12 S Page | 42
* Step 2 : Input the size of the matri aand store it in
integer variable m
*Step 3 : Create an integer array A[][]=new int[m][m];
*Step 4 : Using for loops input the elements into the
matrix
for(int i=0;i<m;i++)
{
for(int j=0;j<m;j++)
{
System.out.print("Enter an element : ");
A[i][j]=sc.nextInt();
}
}
*Step 5 : Print the Matrix
*Step 6 : Create intgers p and q with initial values 0 and
x=A[0][0]
*Step 7 : Using for loops check if the matrix is diagonal:
for(int i=0;i<m;i++)
{
for(int j=0;j<m;j++)
{
/* Checking that the matrix is diagonal or
not */
if(i!=j && A[i][j]!=0) // All non-diagonal
elements must be zero
{
p=1;
break;
}
*Step 8 : Continue the loop body and check the matrix for
Scalarity
/* Checking the matrix for scalarity */
// All main diagonal elements must be
equal to 'x' and non-zero
if(i==j && (A[i][j]==0 || A[i][j]!=x))
{
q=1;
break;
Bhargav Parashar 12 S Page | 43
}
}
}
*Step 9 : If p==0 &&q==0, print: The matrix is scalar
else print: The matrix is not scalar
*Step10:End of Algorithm for void main()
SOURCE CODE:
import java.util.*;
class ScalarMatrix
{
public static void main(String args[])throws Exception
{
Scanner sc=new Scanner(System.in);
System.out.print("Enter the size of the matrix : ");
int m=sc.nextInt();
int A[][]=new int[m][m];
/* Inputting the matrix */
for(int i=0;i<m;i++)
{
for(int j=0;j<m;j++)
{
System.out.print("Enter an element : ");
A[i][j]=sc.nextInt();
}
}
Output:
int n=Integer.parseInt(br.readLine());
int A[][]=new int[n][n];
int k=1, c1=0, c2=n-1, r1=0, r2=n-1;
while(k<=n*n)
{
for(int i=c1;i<=c2;i++)
{
A[r1][i]=k++;
}
for(int j=r1+1;j<=r2;j++)
{
A[j][c2]=k++;
}
for(int i=c2-1;i>=c1;i--)
{
A[r2][i]=k++;
}
for(int j=r2-1;j>=r1+1;j--)
{
A[j][c1]=k++;
}
c1++; c2--; r1++; r2--;
Bhargav Parashar 12 S Page | 48
}
/* Printing the Circular matrix */
System.out.println("The Circular Matrix is:");
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
System.out.print(A[i][j]+ "\t");
}
System.out.println();
}
}}
Output:
Enter the number of elements : 4
The Circular Matrix is:
1 2 3 4
12 13 14 5
11 16 15 6
10 9 8 7
SOURCE CODE:
for(int i=0;i<m;i++)
{
for(int j=0;j<m;j++)
{
Bhargav Parashar 12 S Page | 52
if(i!=j && A[i][j]!=0) // Checking non-diagonal
elements
{
p=1;
break;
}
if(i==j && A[i][j]==0) // Checking diagonal
elements
{
q++;
}
}
}
Output:
Enter the size of the matrix : 4
Enter an element : 5
Enter an element : 0
Enter an element : 0
Enter an element : 0
Enter an element : 0
Enter an element : 1
Enter an element : 0
Enter an element : 0
Enter an element : 0
Enter an element : 0
Enter an element : 0
Enter an element : 0
Enter an element : 0
Enter an element : 0
Enter an element : 0
Enter an element : 7
Bhargav Parashar 12 S Page | 53
*************************
The Matrix is :
5000
0100
0000
0007
*************************
The matrix is Diagonal
SOURCE CODE:
import java.io.*;
class SymetricMatrix
{
public static void main(String args[])throws IOException
{
BufferedReader br=new BufferedReader(new
InputStreamReader(System.in));
System.out.print("Enter the number of elements : ");
int m=Integer.parseInt(br.readLine());
int A[][]=new int[m][m];
if(m>2 && m<10) // Checking for valid input of rows
and columns size
Bhargav Parashar 12 S Page | 55
{
System.out.println("\nInputting the elements in the
Matrix: n");
for(int i=0;i<m;i++)
{
for(int j=0;j<m;j++)
{
System.out.print("Enter the elements : ");
A[i][j]=Integer.parseInt(br.readLine());
}
}
/* Printing the Original Matrix */
System.out.println("\nThe Original Matrix is : ");
for(int i=0;i<m;i++)
{
for(int j=0;j<m;j++)
{
System.out.print(A[i][j]+"\t");
}
System.out.println();
}
/* Checking whether the matrix is symmetric or not */
int flag = 0;
for(int i=0;i<m;i++)
{
for(int j=0;j<m;j++)
{
if(A[i][j] != A[j][i])
{
flag = 1; // Setting flag = 1 when elements do
not match
break;
}
}
}
if(flag == 1)
}
else
System.out.println("The Matrix Size is Out Of
Range");
}
}
Output:
Enter the number of elements : 3
SOURCE CODE:
import java.io.*;
class SaddlePoint
{
public static void main(String args[])throws IOException
{
BufferedReader br=new BufferedReader(new
InputStreamReader(System.in));
System.out.print("Enter the order of the matrix : ");
int n=Integer.parseInt(br.readLine());
int A[][]=new int[n][n];
System.out.println("Inputting the elements in the
matrix");
System.out.println("******************************");
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
System.out.print("Enter Element at ["+i+"]["+j+"]
: ");
A[i][j]=Integer.parseInt(br.readLine());
}
}
System.out.println("******************************");
Bhargav Parashar 12 S Page | 60
System.out.println("The Original Matrix is");
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
System.out.print(A[i][j]+"\t");
}
System.out.println();
}
if(f==0)
{
System.out.println("********************");
System.out.println("No saddle point");
System.out.println("********************");
}
}
}
Output:
SOURCE CODE:
OUTPUT
Inputted matrix :
1 2 3
4 5 6
7 8 9
Transposed matrix :
1 4 7
2 5 8
3 6 9
OUTPUT
Enter the number of rows of Matrix A
2
Enter the number of columns of Matrix A
2
Enter the number of rows of Matrix B
2
Enter the number of columns of Matrix B
2
Printing Matrix A
1 2
3 4
Printing Matrix B
5 6
7 8
Printing product matrix
19 22
43 50
SOURCE CODE:
import java.io.*;
class diagonal_secondarydiagonal
{
int sum1 = 0; //value storage for summation of
diagonals
int sum2 = 0;
public void main() throws IOException
Bhargav Parashar 12 S Page | 71
{
int i, j, m;
BufferedReader br = new BufferedReader(new
InputStreamReader(System.in));
System.out.println("Enter the number of rows for
square matrix ");
m = Integer.parseInt(br.readLine());
int a[][] = new int[m][m]; // declarartion of array
System.out.println("Enter the elements of Matrix ");
for(i=0;i<m;i++) // inputting array a
{
for(j=0;j<m;j++)
{
System.out.println("Enter an Integer for row " +
(i+1) + " and column " + (j+1));
a[i][j] = Integer.parseInt(br.readLine());
}
}
System.out.println("Printing Matrix "); // Output for
matrix A
for(i=0;i<m;i++)
{
for(j=0;j<m;j++)
{
System.out.print(a[i][j] + "\t");
}
System.out.println();
}
for (i=0;i<m;i++)
{
sum1 = sum1 + a[i][i]; // calculating sum2 of
diagonal
}
int c = m-1;
for (j=0;j<m;j++) // calculating sum2 of secondary
diagonal
{
sum2 = sum2 +a[j][c];
c--;
Bhargav Parashar 12 S Page | 72
}
System.out.println("Diagonal summation of matrix is
"+sum1);
System.out.println("Secondary Diagonal summation of
matrix is "+sum2);
}
}
OUTPUT
Printing Matrix
1 3 4
7 9 11
13 15 17
SOURCE CODE:
import java.io.*; //importting package
classsort_columns
{
Bhargav Parashar 12 S Page | 74
void main()throws IOException
{
BufferedReader a = new BufferedReader(new
InputStreamReader(System.in));
int r, c, i, j, k, n, m, z, temp;
System.out.println("Enter number of rows for array :");
r=Integer.parseInt(a.readLine());
System.out.println("Enter number of columns for array :");
c=Integer.parseInt(a.readLine());
intarr[][]=new int[r][c]; //array framework initialization
System.out.println("Enter elements of array :");
for(i=0;i<r;i++) //Entering values
{
for(j=0;j<c;j++)
{
System.out.println("Row " +(i+1)+ " and Column " +(j+1));
arr[i][j]=Integer.parseInt(a.readLine());
}
}
System.out.println("Inputted array :");
System.out.println();
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
System.out.print(arr[i][j]+"\t");
}
System.out.println();
}
int arr2[]=new int[r]; //Creating single dimension array
for(i=0;i<c;i++) //master loop
{
k=0; //reset interval
for(j=0;j<r;j++)
{
arr2[k]=arr[j][i]; //Transfer of data to 1D array
k++;
}
for(n=0;n<r;n++)
Bhargav Parashar 12 S Page | 75
{
for(m=0;m<r-n-1;m++)
{
if(arr2[m]>arr2[m+1]) //Bubble sorting algorithm
{
temp=arr2[m];
arr2[m]=arr2[m+1]; //swapping places
arr2[m+1]=temp;
}
}
}
k=0;
for(z=0;z<r;z++)
{
arr[z][i]=arr2[k]; //Transfer of array data back to 2D array
k++;
}
}
System.out.println("Displaying array with Sorted
Columns:");
System.out.println();
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
System.out.print(arr[i][j]+"\t");
}
System.out.println();
}
}
}
OUTPUT
Inputted array :
12 83 27
74 23 64
1 37 8
SOURCE CODE:
import java.io.*;
class boundary_internal_sum
{
int m,n,i,j;
BufferedReader a = new BufferedReader(new
InputStreamReader(System.in));
void main()throws IOException //main fuction
{
System.out.println("Enter the numbers of Rows");
Bhargav Parashar 12 S Page | 78
m=Integer.parseInt(a.readLine());
System.out.println("Enter the numbers of Columns");
n=Integer.parseInt(a.readLine());
int arr[][]=new int [m][n]; //array initialization
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
System.out.println("Enter value for row "+(i+1)+"
and column "+(j+1));
arr[i][j]=Integer.parseInt(a.readLine()); //read
array
}
}
System.out.println("Matrix entered :"); //output
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
System.out.print(arr[i][j]+"\t");
}
System.out.println();
}
System.out.println();
int sum = 0;
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
sum = sum+arr[i][j]; //adding
}
}
int y = m-1;
int z = n-1;
int num = 0;
for(i=1;i<y;i++)
{
for(j=1;j<z;j++)
{
Bhargav Parashar 12 S Page | 79
num = num+arr[i][j];
}
}
int Bound=sum-num;
System.out.println("Sum of Outer Boundary elements
= "+Bound);
System.out.println("Sum of Inner Boundary elements =
"+num);
System.out.println("Sum of the elements of all values
in matrix= "+sum);
}
}
OUTPUT
Output:
ENTER THE TIME
4
Bhargav Parashar 12 S Page | 84
4 : 20
INPUT TIME :4,20
TIME : 4 : 20 Twenty minutes past Four
Step
10 : If count of palindromic words is not equal to zero,
then print the value stored in the variable count
Step
11 : End of algorithm for main() method
Algorithm for function boolean isPalin(String s) :
Ste 1
p : Start of algorithm for function isPalin()
Ste 2 Find the length of the String s and store it in an
p : integer variable l
Ste 3 Declare and initialize a String variable rev= for
p : storing the reverse of the String s
Ste 4 Start a reverse for loop from i = l-1 to 0 and repeat
p : step 5
SOURCE CODE:
import java.io.*;
import java.util.*;
Bhargav Parashar 12 S Page | 87
class Palin
{
boolean isPalin(String s)
{
s=s.toUpperCase();
{
word[i]=str.nextToken();
}
{
if(ob.isPalin(word[i])==true)
if(count==0)
Output:
SOURCE CODE:
import java.io.*;
public class string
{
String str;
int len, wordcount, cons;
string() //default constructor
{
str="";
len=0;
wordcount=0;
cons=0;
}
string(String ds) // parameterized constructor
Bhargav Parashar 12 S Page | 91
{
str=ds;
}
public static void main()throws IOException //
main function
{
BufferedReader a=new BufferedReader(new
InputStreamReader(System.in));
System.out.println("Please input string:");
String a1=a.readLine(); // statement for user
input of string by choice
System.out.println();
System.out.println("Original String : " +a1);
string obj=new string(a1); // object sending value to
constructor
obj.countFreq(); // method calls by object of
main function
obj.show();
}
void countFreq() //count words and consonants
{
len=str.length();
str=str.toLowerCase();
for(int i=0;i<len;i++)
{
if(str.charAt(i)==32) // checking for whitespace
wordcount++;
}
wordcount++;
for(int i=0;i<len;i++) // loop to count number of
consonants
{
if(str.charAt(i)!=32) // checking whether it is a
character
{
char d=str.charAt(i);
if(d!='a'&&d!='e'&&d!='i'&&d!='o'&&d!='u') //
statement checking if its not a vowel
cons++;
Bhargav Parashar 12 S Page | 92
}
}
}
void show()
{
System.out.println();
System.out.println("Number of words : " +wordcount);
System.out.println("Number of consonants : " +cons);
}
}
OUTPUT
Number of words : 5
Number of consonants : 16
26) Write a program to count the
frequency of words starting with a
vowel:
ALGORITHM:
1. Begin
2. Declare class
3. Declare variables (str="",frequency=0)
4. Input Sentence in str
5. Print Original sentence: str
6. Convert str to lowercase letters
7. Declare integer variables (i,l)
8. Store length of str in l
9. Declare character variables (b,d)
10. Store b=Character of str which is at(0 index)
11. If b=='a' or b=='e' or b=='i' or b=='o' or b=='u', go to next step,
else to step 13
12. frequency=frequency+1
13. Declare i=1
14. If i<l, go to next step, else go to step 20
SOURCE CODE:
import java.io.*;
public class vowelNum
{
String str;
int frequency;
BufferedReader a=new BufferedReader(new InputStreamReader(System.in));
public void main()throws IOException // main function
{
vowelNum obj=new vowelNum();
obj.read();
obj.freq();
obj.show();
}
void read()throws IOException //inputting sentence
{
System.out.println("Please Input Sentence: ");
str=a.readLine();
str=str.trim(); //removing whitespaces
System.out.println();
System.out.println("Sentence: " +str); // showing original sentence
str=str.toLowerCase(); // converting sentence to lowercase
}
vowelNum() // default constructor
{
str="";
frequency=0;
}
void freq() //count frequency of vowels at start of a word
{
int l=str.length();
char b=str.charAt(0);
if(b=='a'||b=='e'||b=='i'||b=='o'||b=='u')
frequency++;
for(int i=1;i<l;i++)
{
if(str.charAt(i)==32) // checking for whitespace
{
char d=str.charAt(i+1);
if(d=='a'||d=='e'||d=='i'||d=='o'||d=='u')
frequency++;
}
Bhargav Parashar 12 S Page | 94
}
}
void show() // showing total frequency of words starting with vowel
{
System.out.println("Frequency of Words starting with Vowel : "
+frequency);
}
}
OUTPUT
ALGORITHM:
1.Begin
2.Declare class
3.Declare variables (sent="",reverse="",size=0)
4.Input sentence in sent
5.Store length of sent in size
6.Declare sub=0 and a string variable b
7.Declare i=0 and a character variable c
8.If i<size, go to next step, else go to step 19
Bhargav Parashar 12 S Page | 95
9.Store c=sent.characterAt(i)
10. If (c='' or c='.'), go to next step, else go to step 18
11. Store sub part of string from (sub to i) of sent in b
12. If b.length()!=1, go to next step, else go to step 16
13. reverse=reverse+ Character of b which is at
(b.length()-1)
14. reverse=reverse+b.sub part of string
from(1,b.length()-1)
15. reverse=reverse+ Character of b which is at (0
index), go to step 17
16. reverse=reverse+b
17. reverse=reverse+" ", sub=i+1
18. i=i+1, go to step 8
19. Print original Sentence: sent
20. Print altered Sentence: reverse
21. End.
SOURCE CODE:
import java.io.*; //input output package
public class interchange
{
String sent, reverse;
int size;
public static void main()throws IOException // main
function
{
interchange obj=new interchange(); // object creation
obj.read();
obj.firstlast();
obj.show();
}
interchange() // default constructor
{
sent="";
reverse="";
size=0;
}
void read()throws IOException //inputting sentence
{
Bhargav Parashar 12 S Page | 96
BufferedReader f=new BufferedReader(new
InputStreamReader(System.in));
System.out.println("Enter sentence ending with full
stop:");
sent=f.readLine();
}
void firstlast() //interchange first and last
character of word
{
size=sent.length();
int sub=0;
String b;
for(int i=0;i<size;i++) // Loop to operate the whole
sentence
{
char c=sent.charAt(i); // Stores character at that
index
if(c==' '||c=='.') // checking for whitespace or
end
{
b=sent.substring(sub,i); // stores part of string
if(b.length()!=1)
{
reverse=reverse+b.charAt(b.length()-1);
reverse=reverse+b.substring(1,b.length()-1); //
stores altered sentence
reverse=reverse+b.charAt(0);
}
else
reverse=reverse+b;
reverse=reverse+" "; // adding space to
sentence
sub=i+1;
}
}
}
void show() //show initial and altered
sentence
{
Bhargav Parashar 12 S Page | 97
System.out.println();
System.out.println("Original sentence: " +sent);
System.out.println("Altered sentence: " +reverse);
}
}
OUTPUT
Enter sentence ending with full stop:
Bhargav has been working for quite some time.
int x = 0;
*Step 6: return B;
SOURCE CODE:
import java.io.*;
class Mixer
{
int arr[];
Bhargav Parashar 12 S Page | 101
int n;
static BufferedReader br = new BufferedReader(new
InputStreamReader(System.in));
Mixer(int nn)
{
n = nn;
arr = new int[n];
}
Mixer mix(Mixer A)
{
int size = this.arr.length + A.arr.length; //size of
resulting array
/* 'this' keyword refers to the current object, i.e. the
object which calls mix() function */
Mixer B = new Mixer(size); //object which will store the
result of merging
int x = 0;
return B;
}
void display()
{
for(int i=0; i<n; i++)
{
System.out.print(arr[i] + " ");
}
System.out.println();
}
DATA
STRUCTURES
Bhargav Parashar 12 S Page | 105
29)Program to push and pop
characters in an array-based queue:
ALGORITHM:
1.Begin
2.Declare class
3.Declae variables(ch[],capacity,top)
4.Assign capacity=cap, top=-1, ch.length=capacity
5.Enter Stack length to size
6.Send size value to cap in step 4
7.Declare i=0
8.If i<size, go to next step, else go to step 16
9.Enter character (i+1) in chhh
10. Assign chhh to v
11. top=top+1
12. If top=capacity, go to step 15, else go to next step
13. Assign ch[top]=v
14. i=i+1, go to step 8
15. Display " Wordpile is Full "
16. top=top+1
17. Declare i=0
Bhargav Parashar 12 S Page | 106
18. If i<capacity, go to next step, else go to step 25
19. top=top-1
20. If top=-1, go to step 24, else go to next step
21. Assign ct=ch[top]
22. Display ct
23. i=i+1, go to step 18
24. Display ch="/"
25. End.
SOURCE CODE:
import java.io.*;
public class Stack15
{
char ch[]; //variable declaration
int capacity;
int top;
public Stack15(int cap) //variable initialization
{
capacity=cap;
top=-1;
ch=new char[capacity];
}
void pushChar(char v)
{
if(top<capacity-1) //check queues status
{
top++;
ch[top]=v;
}
else
{
System.out.println("WordPile is full"); //full message
}
}
char popChar()
{
char val=' ';
if(top>=0)
{
Bhargav Parashar 12 S Page | 107
val=ch[top--];
return val;
}
Else
return '/'; //false return to indicate termination
}
void main()throws IOException //main method
{
int size;
char ct=' ';
String chhh=" ";
BufferedReader br=new BufferedReader(new
InputStreamReader(System.in));
System.out.println("Enter the size of the stack, less
than 20:");
size=Integer.parseInt(br.readLine());
Stack15 obj=new Stack15(size); //new object
for(int i=1;i<=size;i++)
{
System.out.println("Enter element to be pushed at
position:"+i);
chhh=br.readLine();
obj.pushChar(chhh.charAt(0));
chhh=" ";
}
System.out.println("Stack is now full");
for(int j=1;j<=size;j++)
{
ct=obj.popChar(); //calling method out take out
element
System.out.println("Popped element:"+ct);
}
System.out.println("Stack is now empty");
} }
OUTPUT
Enter the size of the stack, less than 20:
4
Enter element to be pushed at position:1
f
Bhargav Parashar 12 S Page | 108
Enter element to be pushed at position:2
g
Enter element to be pushed at position:3
s
Enter element to be pushed at position:4
e
Stack is now full
Popped element:e
Popped element:s
Popped element:g
Popped element:f
Stack is now empty
30) Link is an entity which can hold a maximum
of 100 integers. Link enables the user to add
elements from the rear end and remove
integers from the front end of the entity.Specify
the class Link giving details of the
constructor(int ), void addlink(int ), intdellink()
and void display():
ALGORITHM:
1.Begin
2.Declare class
3.Declae variables(str[],capacity,top)
4.Assign capacity=cap, top=-1, str.length=capacity
5.Enter Stack length in: l
6.Send l value to cap in step 4
7.Declare i=0
8.If i<l, go to next step, else go to step 16
9.Enter name (i+1) in: temp
10. Assign temp to v
11. top=top+1
12. If top=capacity, go to step 15, else go to next
step
13. Assign str[top]=v
14. i=i+1, go to step 8
15. Display " Wordpile is Full "
16. top=top+1
Bhargav Parashar 12 S Page | 109
17. Declare i=0
18. If i<capacity, go to next step, else go to step
25
19. top=top-1
20. If top=-1, go to step 24, else go to next step
21. Assign st=str[top]
22. Display st
23. i=i+1, go to step 18
24. Display st="//"
25. End.
SOURCE CODE:
import java.io.*; // Java input output package called
public class Queue // class name
{
int Q[], rear, front; // instance variables
public Queue(int n) // parameterized constructor
{
Q=new int[n];
front=-1;
rear=-1;
}
public void insert(int num)
// method for inserting elements in queue and shifting
them forward
{
int p=Q.length;
if(rear<p-1) // condition to fill
{
rear++; // Updating rear as no. of elements increase
Q[rear]=num;
}
else
// max capacity of queue reached
System.out.println("The queue is full, cannot insert
data");
}
public void display()
Bhargav Parashar 12 S Page | 110
// method to display each element of queue starting from
front
{
while(front<rear)
{
front++;
System.out.println(Q[front]);
// virtual deletion, jst display of elements in front to
rear order
}
System.out.println("The queue is empty"); // If queue is
out of elements
}
public static void main()throws IOException // main
function
{
BufferedReader x=new BufferedReader(new
InputStreamReader(System.in));
System.out.println("Enter length of Array-based
Queue:");
int l=Integer.parseInt(x.readLine()); // input queue size
Queue obj=new Queue(l); // sending length of queue
int val, ch=1;
while(ch==1) // user based input option
{
System.out.println("Enter Data : ");
val=Integer.parseInt(x.readLine());
obj.insert(val); // method call, inserting elements
System.out.println("Any more data (1 for yes/ 0 for
no)?");
ch=Integer.parseInt(x.readLine());
}
System.out.println();
System.out.println();
obj.display(); // call display method after inserting is
done
}
}
OUTPUT
Bhargav Parashar 12 S Page | 111
Enter length of Array-based Queue:
3
Enter Data :
34
Any more data (1 for yes/ 0 for no)?
1
Enter Data :
23
Any more data (1 for yes/ 0 for no)?
1
Enter Data :
76
Any more data (1 for yes/ 0 for no)?
0
34
23
76
The queue is empty
METHOD main()
1.Declare x, ret
2.Ask user to input size of array (less than 50) and store
in x
3.Send constructor Stack14 value of x and create object
obj with cap=x, top=-1, and create arrays st[cap] &
m[cap]
4.Call method input_marks(send obj)
5.Call method display(send obj)
6.Declare i=0
7.If i<x
8.Ret = the value returned by popmarks
9.If ret>0 proceed to next step, else to step 11
10. Output Element after popping: "+ret
11. Output stack underflow
12. i=i+1
13. go back to step 7
14. Call method display(send obj)
15. end
METHOD display
1.Declare i=0
2.If i<cap, go to next step, else to step 6
3.Display element stored in position st[i]
4.i=i+1
5.go back to step 2
6.go to step 6 of main method
METHOD display
1.if top>=0, proceed to next step else to step 3
2.return st[top--] to main method
3.output stack over flow and return 0 to main method
SOURCE CODE:
import java.io.*;
public class Stack14 //main class
{
int m[];
int st[];
int cap;
int top;
public Stack14(int n) //constructor overload
{
cap=n;
top=-1;
st=new int[cap];
m=new int[cap];
}
METHOD main()
1.Declare variables(v,f,max=0,chc=0)
2.Enter Size of Dequeue in: max
3.Send max to step 4 of class body
4.If chc!=6, go to next step, else go to step 27
5.Input option as per Menu given in variable: chc
6.If chc=1, go to next step, else go to step 10
7.Enter element to be inserted in front of dequeue in: v
8.Call method pushfront(send v)
9.Go to step 4
10. If chc=2, go to next step, else go to step 14
11. Enter element to be inserted in the rear of dequeue
in: v
12. Call method pushrear(send v)
13. Go to step 4
14. If chc=3, go to next step, else go to step 18
15. Call method popfront() and store popped value in: f
16. If f!=0, go to next step, else go to step 18
17. Display element popped from front: f
18. Go to step 4
19. If chc=4, go to next step, else go to step 23
20. Call method poprear() and store popped value in: f
21. If f!=0, go to next step, else go to step 23
22. Display element popped from rear: f
23. Go to step 4
24. If chc=5, go to next step, else go to step 26
Bhargav Parashar 12 S Page | 118
25. Call method display()
26. Go to step 4
27. End.
METHOD pushfront(v)
1.Receive value in v from main function
2.If front=0 and rear=0, go to next step, else go to step
5
3.ele[front]=v
4.rear=rear+1
5.If rear<=cap-1, go to next step, else go to step 12
6.Declare i=rear+1
7.If i>front, go to next step, else go to step 10
8.Put ele[i-1] in ele[i]
9.i=i-1, go to step 7
10. Put v in ele[front]
11. rear=rear+1, go to step 13
12. Display "Queue overflow"
13. Go to step 9 of method main()
METHOD pushrear(v)
1.Receive value in v from main function
2.If rear<=cap-1, go to next step, else go to step 5
3.Put v in ele[rear]
4.rear=rear+1
5.Display "Queue overflow"
6.Go to step 13 of method main()
METHOD popfront()
1.If front>=0, go to next step, else go to step 5
2.front=front+1
3.Send ele[front] value to step 15 main() method
4.Go to step 7
5.Display "Queue underflow"
6.Send 0 to step 15 main() method
7.Go to step 16 of method main()
METHOD poprear()
1.If rear!=0, go to next step, else go to step 5
Bhargav Parashar 12 S Page | 119
2.rear=rear-1
3.Send ele[rear] value to step 20 main() method
4.Go go step 7
5.Display "Queue underflow"
6.Send 0 to step 20 main() method
7.Go to step 21 of method main()
METHOD display()
1.Set i=front
2.If i<rear, go to next step, else go to step 5
3.Display Element. ele[i], with an arrow
4.i=i+1, go to step 2
5.Print line
6.Go to step 26 of method main()
SOURCE CODE:
SOURCE CODE:
import java.io.*; //importing input output package
public class Stack11
{
String st[]; //constructing variables
int size,top,ctr;
Stack11() //constructor overloaded
{
size=0;
top=0;
ctr=0;
}
public Stack11(int cap)
{
size=cap;
top=-1;
st=new String[cap];
}
void pushname(String n)
{
if (top== (size-1)) //going to next variable
System.out.println(" Stack overflow");
else
st[++top]= n;
}
String popname()
{ if (top<0)
{
System.out.println("Stack underflow");
return "invalid";
}
else
return st[top--]; //stack underflow
}
void display()
{
Bhargav Parashar 12 S Page | 127
System.out.println("The stack is:");
for(int i=0;i<=size;i++)
{
System.out.println("Element at
position(i+1)is:"+st[i]);
}
}
public void main()throws IOException //main
method
{
String names="";
String f="";
BufferedReader br=new BufferedReader(new
InputStreamReader(System.in));
System.out.println("Enter size of the array:");
int p=Integer.parseInt(br.readLine());
Stack11 obj=new Stack11(p);
for (int k = 1 ; k <=p; k++) //loop to enter names
{
System.out.println("Enter name:");
names=br.readLine();
obj.pushname(names);
names="";
}
for (int l = 1 ; l<=p; l++)
{
f = obj.popname();
System.out.println("popped value ="+f);
}
}
}
OUTPUT
CLASS Rank
1.Declare index as integer variable
2.Enter value to l and send it to lim in the parent class
through the constructor of the child class
3.Call method readvalues of the parent class from the
one in the child class
4.i=0
5.i<lim given in the parent class, go to step 6 else go to
10
6.if the rank [] from the parent class at i>index, go to
step 7
7.index=i
8.Add I to i
9.Go to step 5
10. Display the topmost rank after printing the display
method of the parent class
11. End
SOURCE CODE:
import java.io.*; //import package
class Record
{
BufferedReader br=new BufferedReader(new
InputStreamReader(System.in));
String name[];
int rnk[];
Record() //constructor
Bhargav Parashar 12 S Page | 131
{
name=new String[50];
rnk=new int[50];
}
System.out.println("Enter names");
for(int i=0;i<50;i++) //loop to enter names
{ System.out.println("Name "+(i+1)+" = ");
name[i]=br.readLine();
System.out.println("Enter his/her rank");
rnk[i]=Integer.parseInt(br.readLine());
}
}
void display() //displaying output
{
System.out.println("The names and corresponding
ranks:");
for(int i=0;i<50;i++)
{
System.out.println("Name "+name[i]+" Rank
"+rnk[i]);
}
}
}
public class Rank extends Record //sub class
{
int index;
Rank()
{
super();
index=0;
}
void display()
{
super.display();
System.out.println("The topmost rank belongs to:
"+name[index]);
}
}
class Inheritance
{
System.out.println("Output");
ob.display();
}
}
OUTPUT
NUMBER OF RATE
CALLS
100 Only rental charge
101-200 60 paisa per call +
rental charge
201-300 80 paisa per call +
rental charge
Above 300 1 rupee per call +
rental charge
ALGORITHM:
CLASS Detail
1. Begin
2. Declare variables name,address,telno,rent and
initialize through constructor
3. Print name,address,telno,rent
CLASS Bill
1.Declare variable n=no and amt
2.Send the values of na,add,tno,rnt to the parent class
through constructor
3.if(n<=100), amt=rent and go to step 7,else go to 6
4.if (n>100&& n<=200), amt=rent+(0.60*(n-100)) and
go to step 7,else go to 6
5.if(n>200&&n<=300), amt=rent+(0.60*100)+(0.80*(n-
200)) and go to step 7,else go to 6
6.amt=rent+(0.60*100)+(0.80*100)+(1*(n-300))
7.Call show option of the parent class then print the
number of calls made and the amount
8.End
SOURCE CODE:
import java.io.*;
class Detail
Bhargav Parashar 12 S Page | 135
{
protected String name; //base
variables
protected String address;
protected long telno;
protected int rent;
Detail(String na,String add, long tno, int rnt)
//constructor
{
name=na;
address=add;
telno=tno;
rent=rnt;
}
public Bill(String na,String add, long tno, int rnt, int no)
{
super(na,add,tno,rnt);
n=no;
amt=0.0;
}
void cal()
{
if(n<=100)
amt=rent;
else if (n>100&&n<=200)
amt=rent+(0.60*(n-100));
else if(n>200&&n<=300)
amt=rent+(0.60*100)+(0.80*(n-200));
else
amt=rent+(0.60*100)+(0.80*100)+(1*(n-300));
}
void show()
{
super.show();
System.out.println();
Bhargav Parashar 12 S Page | 137
System.out.println("No of phone calls: "+n);
System.out.println("Amount to be paid: "+amt);
}
}
OUTPUT
Enter name
Bhargav
Enter address
Tezpur
Enter telephone number
967878696
Enter rent:
12000
Enter number of phone calls
345
OUTPUT
Name is: Bhargav
Address is: Tezpur
Telephone number is: 967878696
Rent paid is: 12000
ALGORITHM:
SUPER CLASS:-
1.Begin
Bhargav Parashar 12 S Page | 138
2.Declare class
3.Declare variables(a,b)
4.Assign a=lenght, b=breadth
5.Calculate per=2*(a+b)
6.Display Length of Parallelogram: a
7.Display Breadth of Parallelogram: b
8.Display Perimeter of Parallelogram: per
SUBCLASS:-
1.Begin
2.Declare Class
3.Inherit super class
4.Declare variables(h,b)
5.Initialise (height, bred) for super class' step 4 after
input in sub class
6.Assign h= heigh, after input of height value in subclass
7.Call step 5 from superclass
8.area=b*h
9.Call step 6 to 8 of superclass
10. Display Height of Parallelogram: h
11. Display Area of Parallelogram: b
12. End.
SOURCE CODE:
import java.io.*;
class Peri
{
double a;
double b;
Peri(double length,double breadth)
{
a=length;
b=breadth;
}
double Calculate() //calculating
{
return (2*(a+b));
Bhargav Parashar 12 S Page | 139
}
void show() //display
{
System.out.println();
System.out.println("OUTPUT:-");
System.out.println("Length: "+a);
System.out.println("Breadth: "+b);
System.out.println("Perimeter: "+Calculate() );
}
}
public class Area extends Peri
{
double h;
double b;
void main()throws IOException //main function
{
double x=0.0;
double y=0.0;
double z=0.0;
BufferedReader br = new BufferedReader(new
InputStreamReader(System.in)); //input out buffer
System.out.println("Enter the length of the
paralellogram:");
x=Double.parseDouble(br.readLine());
System.out.println("Enter the breadth of the
paralellogram:");
y=Double.parseDouble(br.readLine());
System.out.println("Enter the height of the
paralellogram:");
z=Double.parseDouble(br.readLine());
Area obj= new Area(x,y,z);
obj.doarea();
obj.show();
}
Area(double lenght,double bred, double heigh)
{
super(lenght,bred);
h=heigh;
Bhargav Parashar 12 S Page | 140
b=0.0;
}
void doarea()
{
b=super.b*h; //calling super class
method
}
void show()
{
super.show();
System.out.println("The height of the parallelogram:
"+h);
System.out.println("The area of the parallelogram:
"+b);
}
}
OUTPUT
OUTPUT:-
Length: 2.0
Breadth: 3.0
The height of the parallelogram: 4.0
Perimeter: 10.0
ALGORITHM:
SUPER CLASS:-
1.Begin
2.Declare class
3.Declare variables(x1,y1)
4.Assign x1=x, y1=y
5.Display x-coordinate of First End Point: x1
6.Display y-coordinate of First End Point: y1
SUBCLASS:-
Bhargav Parashar 12 S Page | 142
1.Begin
2.Declare Class
3.Inherit super class
4.Declare variables(x2,y2,radius,area,pi=3.14)
5.Initialise (x,y) for super class' step 4 after input in sub
class
6.Assign x2=a, y2=b, after input of these values in
subclass
7.radius=(((x2-x1)^2)+((y2-y1)^2))^(1/2)/2
8.area=(radius^2)*pi
9.Call step 5 and 6 of Super Class
10. Display x-coordinate of Second End Point: x2
11. Display y-coordinate of Second End Point: y2
12. Display Radius of Circle: radius
13. Display Area of Circle: area
14. End.
SOURCE CODE:
OUTPUT
Output
The x-coordinate of the 1st point is: 2
The y-coordinate of the 1st point is: 2
ALGORITHM:
1.Begin
2.Declare a class SeriesSum with x and n as integer
variables and sum as a double
Variable
3.Enter the values of and integer p and q through
constructor/ object obj
4.Call functions calculate() and display() using obj
METHOD calculate()
1.Declare the variables double calculate and num
2.Integer i=2
3.If i<=n*2, go to step 4, else go to step 9
4.fact=findfact(i-1), calling function to variable fact
5.num=findpower(x,i), calling function to variable sum
6.sum=sum+(num/fact), to calculate the final output
7.Add 1 to i
8.Go to step 3
9.Exit to main
METHOD display()
1.Print sum
2.End of Algorithm.
SOURCE CODE:
OUTPUT
Enter Numerator for series sum:
2
Enter limit of series:
3
Sum of Series is:7.199999999999999
METHOD recchar(int p)
1.If p<0, call function display(), else go to step 2
2.Assign character at p to ch
3.Assign ch to m
4.If (m>=65&&m<=90), go to step 5 else go to step 8
5.Add 32 to m
6.Put character value of m to ch
7.s1=ch+s1
8.If (m>=97&&m<=122), go to step 9 else go to step
12
9.Subtract 32 from m
10. Put character value of m to ch
11. s1=ch+s1
12. call recchar(--p)
13. Exit to main
METHOD display()
1.Print original string
2.Print new string
SOURCE CODE:
Bhargav Parashar 12 S Page | 151
import java.io.*;
public class CaseRecur
{
String s; //variable declaration
String s1="";
char ch;
int n,m;
void main()throws IOException //main method
{
BufferedReader a= new BufferedReader(new
InputStreamReader(System.in));
System.out.println("Enter string:");
s=a.readLine();
n=s.length();
recchar(n-1);
}
OUTPUT
Enter string:
Go To School
The original string is:Go To School
The new string is:gOtOsCHOOL
Method sum_sq_digits(x):
1.If x>0, go to next step, else go to step (5)
2.d=x%10
3.sum=sum+(d*d)
4.x=x/10, Return to step (1)
5.x=sum, sum=0
6.if x>9, go to step (1), else go to next step
7.Send x value to step 8 method call, Exit
sum_sq_digits(x)
SOURCE CODE:
else
Bhargav Parashar 12 S Page | 155
return x; // return statement to caller function
}
}
}
OUTPUT
Enter Number:
31
31 is a Happy Number