0% found this document useful (0 votes)
95 views91 pages

Class XI Sample Practical File Computer

The document contains code and explanations for three Java programs: 1. A program to check if a number is a Smith number using prime factorization and digit sums. 2. A program to merge two integer arrays into a single array. 3. A program to perform binary search on an integer admission number array.

Uploaded by

Anish Barman
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
95 views91 pages

Class XI Sample Practical File Computer

The document contains code and explanations for three Java programs: 1. A program to check if a number is a Smith number using prime factorization and digit sums. 2. A program to merge two integer arrays into a single array. 3. A program to perform binary search on an integer admission number array.

Uploaded by

Anish Barman
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 91

NAME –SAGARIKA DEB

CLASS- XI COMPUTER SCIENCE


SESSION- 2020-21
COMPUTER PRACTICAL FILE
1.CODE

import java.util.Scanner;

class Smith {

public int sumOfDigits(int n) { // for calculation of sum of digits

int s = 0;

while (n > 0) {

int r = n % 10;

s += r;

n = n / 10;

return s;

public boolean isprime(int n) {

int c = 0;

for (int i = 1; i <= n; i++) {


if (n % i == 0)

c++;

return (c == 2);

public static void main(String args[]) {

Scanner sc = new Scanner(System.in);

System.out.println("Enter a number");

int n = sc.nextInt();

sc.close();

Smith obj = new Smith();

int sumofdig = obj.sumOfDigits(n);

if (obj.isprime(n) == false) {

int totalSum = 0;

int p = 2;

while (n > 1) {

if (n % p == 0) {

totalSum += obj.sumOfDigits(p);

n = n / p;

} else
p++;

if (totalSum == sumofdig) {

System.out.println("Smith number");

} else {

System.out.println("Not a Smith number");

else

System.out.println("Not a composite number , not a smith number");

OUTPUT:
Enter a number

666

Smith number

ALGORITHM
1. Start of algorithm.

2. Declare and store an integer entered from user to check whether it is a smith
number or not.
3. Create a new object and call the function sumOfDigits( int ) with the parameter
being the number entered to get the sum of digits of the number entered and
store it in a variable.

4. In an outer if block check whether the number is prime or not. If it is prime,


carry on from this point otherwise tell user that it was not prime and exit.

5. Inside the if block initialize a total sum to be 0 in the beginning. Also initialize p
as a counter to be 2 which holds value of next prime number for the prime
factorization of the number.

6. a) Start a while loop with condition that number entered should be greater than
1. b) If the number is divisible by the value of p then we add the sum of digits of
the value of p to the total sum by calling sumOfDigits function with the argument
p and adding the result to the total sum. Otherwise if it is not divisible, increment
p by 1. This continues till the number is greater than 1 and is kept inside while
loop.

7. Outside of the while loop after counting sum of digits of all prime factors, check
whether the total sum which holds the sum of digits of all prime factors, equals to
the sum of digits of the number calculated earlier.

8. If the condition above holds true, it is a Smith Number otherwise it is not.

9. End of algorithm.

VARIABLE DECLARATION TABLE:


sumOfDigits() -

Variable Type Purpose

n int To accept the number passed into function

s int To store the sum of digits of number

r int To store the last digit extracted from number


isPrime() –

Variable Type Purpose

n int To accept number passed into function

c int Keep track of factors

i int Loop variable, incremented to check for next

factors

2.CODE
import java.util.Scanner;

public class Mixer {

int arr[];

int n;

static Scanner sc = new Scanner(System.in);

Mixer(int nn) {

n = nn;

void accept() {

arr = new int[n];

System.out.println("Enter " + n + " no of elements in array");

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

arr[i] = sc.nextInt();
}

Mixer mix(Mixer A) {

int k = 0;

int totalLength = this.n + A.n;

Mixer temp = new Mixer(totalLength);

temp.arr = new int[totalLength];

for (int i = 0; i < this.n; i++) {

temp.arr[i] = this.arr[i];

System.out.println(temp.arr[i]);

k++;

int ind = 0;

for (int i = k; i < temp.n; i++) {

temp.arr[i] = A.arr[ind];

ind++;

return temp;

}
void display() {

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

System.out.println(arr[i]);

public static void main(String[] args) {

System.out.println("Enter the number of elements for array A");

int a = sc.nextInt();

Mixer A = new Mixer(a);

A.accept();

A.display();

System.out.println("Enter the number of elements for array B");

int b = sc.nextInt();

Mixer B = new Mixer(b);

B.accept();

B.display();

Mixer M = B.mix(A);

System.out.println("The merged array is");

M.display();

}
}

OUTPUT:
Enter the number of elements for array A

Enter 4 no of elements in array

Enter the number of elements for array B

Enter 5 no of elements in array

3
9

The merged array is

ALGORITHM:
1. Start of algorithm.

2. Input the length of first array of integers, make an object and pass that value to
the constructor which initializes the instance variable for holding the length of the
array for that object.

3. Call the accept() method with that object created in step 2, which initializes the
array for the object and enters elements into the array from the user.

4. Call the display() method with that object created in step 2 which displays the
array elements to the user by looping over the array and printing the array
elements.

5. Repeat steps 2 to 5 for another array object

. 6. Now call the mix() function with the context of one object and pass the other
object created as an argument.

7. In the mix() function, do the following a) Initialise a counter ( say k ) to 0 to keep


track of the number of elements entered into final array and also initialize a
variable to keep track of the total number of elements to be entered. b) Make a
new object with the totalLength passed to the constructor to initialize a array to
hold all elements. c) Using one loop and updating the values for k ( the counter )
appropriately to keep track of the number of elements entered, enter elements
from the array from the context of the object with which the method was called
into the newly made object’s array instance. d) Using another loop and the
previous value of k enter the elements from the array that was the part of the
object passed in as an argument, into the newly made object’s array instance.
Finally return the object holding the array with all the elements in it.

8. Store the object returned by the function mix() in a new variable.

9. Call display() with the object above to display the merged array with all the
elements.

10. End of algorithm.


VARIABLE DECLARARTION TABLE:
Instance variables -

Variable Type Purpose

arr[] int array Instance variable to hold the arrays of the

objects

n int To store the length of array.

Global variables –

Variable Type Purpose

sc Scanner Scanner object to input numbers into arrays

mix() –

Variable Type Purpose

k int To keep track of number of elements

entered in the final array

totalLength int To store the total length of the final array

temp Mixer Object to store final merged array

i int Loop variable

ind int To keep track of elements entered into

final array from the second array

accept() –

Variable Type Purpose

i int Loop variable to enter elements


Mixer() –

Variable Type Purpose

nn int Formal parameter with length of array

display() –

Variable Type Purpose

i int Loop variable

main() –

Variable Type Purpose

a int Store number of elements in first array

A Mixer Object for first array and information

b int Store number of elements in second array

B Mixer Object for second array and information

M Mixer Object to store the final merged array

Returned

3.CODE
import java.util.Scanner;

class Admission

int Adno[]=new int[100];

Admission()

{
for(int i=0;i<100;i++)

Adno[i]=0;

public void fillArray()

Scanner sc=new Scanner(System.in);

System.out.println("Enter the array elements in ascending order");

for(int i=0;i<100;i++)

Adno[i]=sc.nextInt();

public int binSearch(int l, int u, int v)

int m=(l+u)/2;

if(l<=u)

if(Adno[m]==v)

return 1;

else if(Adno[m]>v)

return binSearch(l,(m-1),v);

else

return binSearch((m+1),u,v);
}

else

return -1;

public static void main(String args[])

Admission obj=new Admission();

Scanner sc=new Scanner(System.in);

obj.fillArray();

System.out.println("Enter the admission number to be searched");

int n=sc.nextInt();

if(obj.binSearch(0,99,n)==1)

System.out.println("The admission number is present");

else

System.out.println("The admission number is not present");

ALGORITHM:
1. (a) Create the integer array of dimension 100 for storing admission numbers.

(b) Create a default constructor to assign the array elements with default initial
values.
2. In the fillArray() function, start a loop from i=0 and continue it till i<100 with
updation statement i++.

Input all the elements in ascending order from the user and store it in the array.

3. In the binSearch function() :-

(a) The formal parameters l, u, v will store lower bound of array, upper bound of
array and the particular

element to be searched using binary search respectively.

(b) The middle index will be found using the formula [m=(l+u)/2].

(c) An if-else ladder will be made which will operate until l<=u. After the condition
gets false, -1 will be

returned to the main() function and the control will be shifted to main() function.

(d) In the if block, another if-else ladder will be created. The first if block will check
whether the middle

element is equal to the element to be searched. If it matches, then 1 will be


returned to the main()

function.

(e) The second else-if block will check whether the middle element is greater than
the element to be

searched or not. If it is, then the element will be searched in the first half of the
array. The function will

be again called using recursion. The parameters passed will be l, (m-1), v.

(f) (e) The third else block will be operated when the middle element is lesser than
the element to be
searched. Then the element will be searched in the second half of the array. The
function will be again

called using recursion. The parameters passed will be (m-1), u, v.

(g) This process will continue until the element is found or the upper bound of the
array is exceeded by

the lower bound.

4. In the main() function:-

(a) An object ‘obj’ of Admission class will be created.

(b) Using the object, the fillArray() function will be called.

(c) The element to be searched from the user will be stored in the variable n.

(d) The lower bound-0, the upper bound-99 and the element to be searched- n will
be sent to the

binSearch() function.

(e) If the value returned by the function is 1, then the message that the element is
present will be

displayed.

(f) If the value returned by the function is- 1, then the message that the element is
not present will be

displayed.

VARIABLE DECLARARTION TABLE:


NAME DATA TYPE PURPOSE

Adno[] int Array for storing the admission numbers

i int For indicating index number of array


l int For storing lower bound of array

u int For storing upper bound of array

v int For storing the element to be searched

m int For storing the middle index of the array

n int Actual parameter of v

4.CODE:
import java.util.Scanner;

class Sentence

public static void main(String args[])

String s, a="",t;

int i,k=0,j=0;

String w[];

Scanner sc=new Scanner(System.in);

System.out.println("Enter a sentence");

s=sc.nextLine();

s=s+' ';

for(i=0;i<s.length();i++)

if(s.charAt(i)==' ')
k++;

w=new String[k];

for(i=0;i<s.length();i++)

if(s.charAt(i)==' ')

w[j]=a;

j++;

a="";

else

a=a+s.charAt(i);

for(i=0;i<k-1;i++)

for(j=0;j<k-i-1;j++)

if((int)w[j].charAt(0)>(int)w[j+1].charAt(0))

t=w[j];
w[j]=w[j+1];

w[j+1]=t;

System.out.println("The rearranged sentence is:-\n");

for(i=0;i<k;i++)

System.out.print(w[i]+" ");

OUTPUT:
Enter a sentence

I live in Kolkata

The rearranged sentence is:-

I Kolkata in live

ALGORITHM:
1. Input a sentence from the user. Store it in the variable s and add an extra space
at the end of

the sentence.
2. Start a loop with (i=0) and continue till (i<s.length()). Extract each character of
the array inside

the loop and check in the if-block whether each character is a blank space or not.
If the

condition is true, then keep updating a counter variable which will count the
number of blank

spaces in the sentence.

3. The number of blank spaces stored in k will indicate the number of words in the
sentence.

Create a string array w[] with dimension k which will store the words of the
sentence.

4. Again create a loop from (i=0) and continue till (i<s.length()). Extract each
character of the array

inside the loop and check in the if- block whether each character is a blank space
or not. If the

condition is false, then in the else part, keep storing each extracted character in
the variable a to

form each word until a blank space is obtained. If the extracted character is a
blank space, then

store the word a in the array. Update the index of the array and initialize the
variable a with

default initial value.

5. Now, in the Bubble Sort sorting technique:-

(a) Create an outer loop from (i=0) and continue till (i<k-1).
(b) Create an inner loop from (j=0) and continue till (j<k-1-i).

(c) In the if-block, we will extract the first character of adjacent words stored in
the array and

take their ASCII value using implicit conversion. Using their ASCII value, we will
check

whether the character of the second word is greater than the first word. If so, then
we will

swap the words inside the array. In this way, all the words will be arranged in
ascending

order on the basis of alphabetical order.

6. After the process gets completed, we will print the words of the array with
blank spaces in

between and in this way, the rearranged sentence will be printed.

VARIABLE DECLARATION TABLE:


NAME DATA TYPE PURPOSE

s String For storing the sentence.

a String For storing each word of the sentence.

t String Temporary variable for swapping words in the Bubble sort method.

i int For computing index of characters in the sentence and also in the array.

k int For storing the number of words in the sentence.

j int For computing the index of characters in the words and also in the array.

w[] String Array for storing the words of the sentence.


5.CODE:
import java.util.Scanner;

class Circular_Prime

public int prime(int n)

int i,f=0;

for(i=1;i<=n;i++)

if(n%i==0)

f++;

if(f==2)

return 1;

else

return 0;

public static void main(String args[])

int c=0,copy,d=0,n,r,f,flag=1,s;
Scanner sc=new Scanner(System.in);

Circular_Prime obj=new Circular_Prime();

System.out.println("Enter a number");

n=sc.nextInt();

for(copy=n;copy!=0;copy=copy/10)

d++;

if(obj.prime(n)==1)

while(c!=n)

r=c%(int)(Math.pow(10,(d-1)));

f=c/(int)(Math.pow(10,(d-1)));

c=(r*10)+f;

s=obj.prime(c);

if(s==0)

flag=0;

break;

}
else

flag=0

if(flag==1)

System.out.println("The number is a circular prime number");

else

System.out.println("The number is not a circular prime number");

OUTPUT:
Enter a number

131

The number is a circular prime number

ALGORITHM:
1. Start of algorithm.

2. In Prime(int) function,it checks whether the given number is prime or not.

a. If prime returns 1.

b. If not prime returns 0.

3. In main function.

a. Declare and initialize variables c (to store the no obtained after calculation) ,
copy (to calculate the
no of digits) , d (to store the no of digits) , r (to store the remainder) , f (to store
the quotient) , s (to call

the function) , flag (to be used a a count) and n (to input the number).

b. Initialize the number from the user.

c. Calculate and store the number of digits of the number.

d. Check if the number is prime or not by calling tthe function Prime(int).

e. If prime,start while loop.

i. Calculate and store the remainder.

ii. Calculate and store the quotient.

iii. Calculate and store the number obtained.

iv. Check whether the number obtained is prime or not by calling the function
Prime(int). If not prime

do flag is equal to zero.

v. If the number input is not prime do flag is equal to zero.

vi. Lastly check if flag is equal to one then number is circular prime and if not then
it is not a circular

prime number.

vii. End of algorithm.

VARIABLE DECLARATION TABLE:


Variable. Data type. Purpose.

Prime(int):-

i int To store the factors of the number.


f int To count the number of factors.

Main():-

n int To store the number.

copy int To assign itself the value of n and process with the extraction of its digits.

d int To store the number of digits.

r int To store the remainder.

f int To store the quotient.

c int To store the number obtained after the calculation.

s int To call the function and check that the number obtained is prime or not.

flag int Used as a count variable to check that the condition has been satisfied

or not.

6.CODE:
import java.util.Scanner;

class Identity

public static void min(String args[])

int a[][],m,n,i,j,flag=1;

Scanner sc=new Scanner(System.in);

System.out.println("Enter the dimensions of the matrix");

m=sc.nextInt();
n=sc.nextInt();

a=new int[m][n];

System.out.println("Enter the elements");

for(i=0;i<m;i++)

for(j=0;j<n;j++)

a[i][j]=sc.nextInt();

if(m==n)

if(m==n)

for(i=0;i<m;i++)

for(j=0;j<n;j++)

if(i==j)

if(a[i][j]!=1)

flag=0;

break;
}

else

if(a[i][j]!=0)

flag=0;

break;

else

flag=0;

if(flag==1)

System.out.println("It is an identity matrix");

else

System.out.println("It is not an identity matrix");

}
OUTPUT:
Enter the dimensions of the matrix

Enter the elements

10

It is not an identity matrix

ALGORITHM:-
1. Start of algorithm.

2. Input the number of rows and columns respectively.

3. Create an integer matrix of size m*n order.


4. Declare and initialize variables i ( index for rows) , j (index for columns) , flag (as
a count variable).

5. Start of for loop.

6. Check if i is equal to j.

a. If i==j , further check whether the element is equal to one or not and if not flag
will be 0 and then

terminate the loop using break statement.

b. If i!=j , check whether the element is equal to zero or not and if not flag will be
zero and then

terminate the loop using break statement.

7. After all the iterations are performed, outside the loop check whether flag is
equal to one or not.

a. If equal print it is an identity matrix.

b. If not equal print it is not an identity matrix.

8. End of algorithm.

VARIABLE DECLARATION TABLE:

Variable. Data type. Purpose.

a[][] int To store the matrix elements.

m int To store the no of rows.

n int To store the no of columns.

i int Index for rows.


j int Index for columns.

flag int Used as a count to check the condition is satisfied or not

7.CODE
import java.util.Scanner;

class Matrix

public static void main(String args[])

int a[][],i,j,k,t,m,n;

Scanner sc = new Scanner(System.in);

System.out.println("Enter the row and column numbers respectively which

should be more than 2 and less than 10");

m=sc.nextInt();

n=sc.nextInt();

a=new int[m][n];

System.out.println("Enter the elements");

for(i=0;i<m;i++)

for(j=0;j<n;j++)

a[i][j]=sc.nextInt();

}
System.out.println("The source array is-");

for(i=0;i<m;i++)

for(j=0;j<n;j++)

System.out.print(a[i][j]+"\t");

System.out.print("\n");

for(i=0;i<m;i++)

for(j=0;j<n-1;j++)

for(k=0;k<n-1-i;k++)

if(a[i][k]>a[i][k+1])

t=a[i][k];

a[i][k]=a[i][k+1];

a[i][k+1]=t;

}
}

System.out.println("The resultant array is;-");

for(i=0;i<m;i++)

for(j=0;j<n;j++)

System.out.print(a[i][j]+"\t");

System.out.print("\n");

OUTPUT:
Enter the row and column numbers respectively which should be more than 2 and
less than 10

Enter the elements

8
0

The source array is-

5 9 7 6

8 0 1 2

7 7 4 8

The resultant array is;-

5 6 7 9

0 1 8 2

7 7 4 8

ALGORITHM
STEP 1: Start of algorithm.

STEP 2: Input the no. of rows and columns and store it in respective integer

variables ‘m’ and ‘n’.

STEP 3: Create an integer array of size ‘m*n’.

STEP 4: Enter the integer elements of the array.


STEP 5: Print the original elements of the matrix, i.e the source array which was

input by the user.

STEP 6: Create a loop from (i=0) and continue till (i<m), to keep the value of ‘i’
fixed

as we have to sort the matrix row wise.

STEP 7: Now, to sort the matrix using the Bubble sort technique, create an outer

loop inside the previous loop starting from (j=0) and continue it till (j<n-1).

STEP 8: Create an inner loop starting from (k=0) and continue it till (k<n-1-i).

STEP 9: In the if block, check if the first element (a[i][k]) of the rows is greater than

the second element (a[i][k+1]) or not. If it is so, the elements are swapped using a

temporary variable ‘t’. This is done to sort the elements of each row in the
ascending

order.

STEP 10: Now, print the new matrix, i.e the resultant array, after sorting each row.

STEP 11: End of algorithm.

VARIABLE DECLARATION TABLE


VARIABLE NAME DATA TYPE PURPOSE

a Integer Reference variable for storing the array.

i Integer Used for iteration.

j Integer Used for iteration.

k Integer Used for iteration.

t Integer Temporary variable used for swapping the elements


of the rows.

m Integer Variable to store the no. of rows.

n Integer Variable to store the no. of columns.

column is equal to the element of the jth row and ith column.

8.CODE
import java.util.Scanner;

class Symmetric

public static void main(String args[])

int a[][],i,j,m,flag=0;

Scanner sc=new Scanner(System.in);

System.out.println("Enter the dimension of the square matrix which should be

more than 2 and less than 10");

m=sc.nextInt();

if((m>2)&&(m<10))

a=new int[m][m];

System.out.println("Enter the elements");

for(i=0;i<m;i++)

{
for(j=0;j<m;j++)

a[i][j]=sc.nextInt();

System.out.println("The original matrix is:-");

for(i=0;i<m;i++)

for(j=0;j<m;j++)

System.out.print(a[i][j]+"\t");

System.out.print("\n");

for(i=0;i<m;i++)

for(j=0;j<m;j++)

if(i!=j)

if(a[i][j]==a[j][i])

flag=1;

else

flag=0;
break;

if(flag==1)

System.out.println("The matrix is Symmetric");

else

System.out.println("The matrix is not Symmetric");

else

System.out.println("Invalid input");

OUTPUT:
Enter the dimension of the square matrix which should be more than 2 and less
than 10

Enter the elements

7
9

45

78

The original matrix is:-

3 7 9

0 3 6

45 78 1

The matrix is not Symmetric

ALGORITHM
STEP 1: Start of algorithm.

STEP 2: Input the size of the square matrix and store it in an integer variable ‘m’.

STEP 3: In an if block, check for the range of ‘m’ which is greater then 2 and less

than 10, and create an integer square array of size ‘m*m’.

STEP 4: Enter the integer elements of the array.

STEP 5: Print the original elements of the matrix, i.e the source array which was

input by the user.


STEP 6: Now, to check if the square matrix is symmetric or not, create an outer
loop

from (i=0) and continue it till (i<m).

STEP 7: Create another similar inner loop starting from (j=0) and continue it till
(j<m).

STEP 8: Next, create an outer if block and check whether the values of ‘i’ and ‘j’

equal or not, using a condition [ if(i!=j) ]. If the condition is not satisfied another

iteration of the loop takes place and this continues until this condition gets
satisfied.

STEP 9: Create an inner if block, providing a condition [ if(a[i][j]==a[j][i]) ]. If the

previous condition of the outer if block is satisfied, the control enters this block to

check whether the element of the ith row and jth column (a[i][j]) is equal to the
element

of the jth row and ith column (a[j][i]) or not.

STEP 10: If this condition is satisfied, the value of ‘flag’ is assigned to 1. If the

condition is not satisfied then the value of ‘flag’ is assigned to 0 using an else
block.

STEP 11: Now, check whether the value of ‘flag’ is equal to 1 or not, using an if
block

providing a condition [ if(flag==1) ]. If ‘flag’ is equal to 1, then the square matrix is

symmetric . Print a message saying (“The matrix is Symmetric”). If ‘flag’ is equal to


0,

the square matrix is not Symmetric. Print a message saying (“The matrix is not
Symmetric”), using an else block.

STEP 12: Lastly, for an invalid input, print a message saying (“Invalid input”).

STEP 13: End of algorithm.

VARIABLE DECLARATION TABLE


VARIABLE NAME DATA TYPE PURPOSE

a Integer Reference variable for storing the array.

i Integer Used for iteration.

j Integer Used for iteration.

m Integer To store the dimensions of the square matrix.

flag Integer To store true or false value, i.e, 1 or 0, to check if

the square matrix is symmetric or not.

9.CODE
class BSum

public static void main(String args[])

Scanner sc=new Scanner(System.in);

int i,j,m,sb=0,snb=0;

System.out.print("Enter the row & column of the

matrix");

m=sc.nextInt();
int A[][]=new int[m][m];

for(i=0;i<m;i++)

for(j=0;j<m;j++)

System.out.print("Enter the elements of array A");

A[i][j]=sc.nextInt();

System.out.print("Original Matrix");

for(i=0;i<m;i++)

for(j=0;j<m;j++)

System.out.print(A[i][j]+" ");

System.out.println();

System.out.println("Sum of boundary elements");

for(i=0;i<m;i++)

{
for(j=0;j<m;j++)

if(i==0 ||i==m-1||j==0||j==m-1)

{ System.out.print(A[i][j]+" ");

sb=sb+A[i][j];

else

System.out.print(" ");

System.out.println();

System.out.println("Sum of non-boundary elements");

for(i=0;i<m;i++)

for(j=0;j<m;j++)

if(i!=0 && i!=m-1 && j!=0 && j!=m-1)

{ System.out.print(A[i][j]+" ");

snb=snb+A[i][j];
}

else

System.out.print(" ");

System.out.println();

System.out.println("Boundary Sum"+sb);

System.out.println("Non - boundary Sum"+snb);

OUTPUT:
Enter the row & column of thE matrix3

Enter the elements of array A2

Enter the elements of array A5

Enter the elements of array A9

Enter the elements of array A0

Enter the elements of array A6

Enter the elements of array A2


Enter the elements of array A5

Enter the elements of array A6

Enter the elements of array A2

Original Matrix2 5 9

062

562

Sum of boundary elements

259

0 2

562

Sum of non-boundary elements

Boundary Sum31

Non - boundary Sum6

Algorithim:-
StepI:- Start of Algorithm

Step II:- Create an array of ‘m’ number of rows and

columns.

StepIII:- Enter the value of of ‘m’.


StepIV:- Enter the elements of array.

StepV:- Print the elements of the array that was entered.

Step VI:- For sum of the boundary elements an if block to be

created to check whether it is first row or column , or last

row and column of the inputted matrix.

StepVII:- Calculate the sum of first rows and columns and

last rows and columns(sB).

StepVIII:- For the sum of non-boundary(snb) elements an if

block to be created to check it is not the first row and

column and also not the last one.

StepIX:- Calculate the sum of non-boundary elements.

StepX:- Print the sum of boundary elements(sB) and sum of

non-boundary elements(snb ).

StepXI:-End of algorithm.

Variable Variable Type Description


i int For the iteration

of the outerloop

j int For the iteration

of inner loop

m int For storing the

value of row and


column

sB int Store the sum of

boundary

elemnts

snb int Store the sum of

non-boundary

elements

10.CODE:
import java.util.*;

class CircularMA

public static void main(String args[])

int n;

Scanner sc=new Scanner(System.in);

System.out.print("Enter the value of n");

n=sc.nextInt();

int r1=0,c1=0,r2=n-1,c2=n-1,i,j,r,c;

int a[][];

a=new int[n][n];

int k=1;
while(r1<=r2&&c1<=c2)

for(i=c1;i<=c2;i++)

a[r1][i]=k;

k++;

for(j=r1+1;j<=r2;j++)

a[j][c2]=k;

k++;

for(i=c2-1;i>=c1;i--)

a[r2][i]=k;

k++;

for(j=r2-1;j>=r1+1;j--)

a[j][c1]=k;

k++;
}

r1++;

r2--;

c1++;

c2--;

for(r=0;r<n;r++)

{for(c=0;c<n;c++)

System.out.print(a[r][c]+" ");

}System.out.println();

OUTPUT:
Enter the value of n4

1234

12 13 14 5

11 16 15 6

10 9 8 7
ALGORITHM:
Step 1 : Start of algotithm

Step 2 : Input the size of circular matrix and store it in

integer variable ‘n’

Step 3 : Create an integer square array of size ‘n*n’ which

will be the circular matrix

Step 4 : Declare and initialize variables k = 0 (for filling the

matrix), c1 = 0 (for storing index of

first column), c2 = n-1 (for storing index of last column), r1 =

0 (for storing index of

first row), r2 = n-1 (for storing index of last row)

Step 5 : Start a while loop till k<=n*n and repeat steps 6 to

10

Step 6 : (a) Start a for loop from i = c1 to c2, where ‘i’

increases by 1 every time and perform

step (b)

(b) Store the natural numbers in the first row using A[r1][i]

= k++

Step 7 : (a) Start a for loop from j = r1+1 to r2, where ‘j’

increases by 1 every time and perform

step (b)
(b) Store the natural numbers in the last column using

A[j][c2] = k++

Step 8 : (a) Start a for loop from i = c2-1 to c1, where ‘i’

decreases by 1 every time and perform

step (b)

(b) Store the natural numbers in the last row using A[r2][i] =

k++

Step 9 : (a) Start a for loop from j = r2-1 to r1+1, where ‘j’

decreases by 1 every time and

perform step (b)

(b) Store the natural numbers in the first column using

A[j][c1] = k++

Step 10 : Update the variables c1, c2, r1 and r2

Step 11 : Display the circular matrix A[ ]

Step 12 : End of Algorithm

VARIABLE DECLARATION TABLE:


n int Store the no. of

rows and column

r1 int Store the value of

first row

c1 int Store the value of


first column

r2 int Store the value of

last row

c2 int Store the value of

last column

i int For iteration of

outer loop

J int For iteration of

inner loop

r int Store the value of

rows

c int Store the value of

column

11.ALGORITHM:
Step 1: Start of algorithm.

Step 2: Accept a sentence (String s) from user. Initialise ‘n’ to 0.

Step 3: Find the length of the sentence (int l).

Step 4: Store last character of the sentence in char c.

Step 5: If c! = ‘.’ AND c! = ‘!’ AND c! = ‘?’ then display “Sentence must be
terminated with ‘.’,

‘?’ or ‘!’.” And go to Step 13.


Step 6: Start for loop from i = 0 till ‘l’, where ‘i’ increases by ‘1’ after each iteration.
Perform

steps 7 to 9.

Step 7: Extract each word.

Step 8: Find the first (char c1) and last letter (char c2) of each extracted word.

Step 9: If ‘c1’ and ‘c2’ are both vowels, then add the word to String s2 and
increment ‘n’ by 1

, else add it to String s3.

Step 10: Display “ Number of words beginning and ending with vowel :”+n.

Step 11: Display “Original sentence :”+s.

Step 12: Display “Final sentence :”+s2+s3.

Step 13: End of algorithm.

CODE:
import java.util.*;

class Vowel

public static void main(String[]args)

Scanner sc=new Scanner(System.in);

String s,s1="",s2="",s3="";

int i,l,n=0;

char c,c1,c2;
System.out.println("Enter a sentence:");

s=sc.nextLine(); //Accepting the sentence

l=s.length(); //Finding the length of sentence

c=s.charAt(l-1);

if(!(c=='.' || c=='?' || c=='!'))

System.out.println("Sentence should be terminated with '.','!','?'"); //Checking for

given condition

System.exit(0);

for(i=0;i<l;i++)

c=s.charAt(i);

if(!(c==' ' || c=='.' || c=='!' || c=='?'))

s1=s1+c; //forming word

else

c1=s1.charAt(0); //first letter

c2=s1.charAt((s1.length())-1); //last letter


if(isVowel(c1)==true && isVowel(c2)==true) //checking if they are vowels

n++;

s2=s2+s1+" "; //forming new sentence

else

s3=s3+s1+" ";

s1="";

System.out.println("Number of words beginning and ending with a vowel:"+n);

System.out.println("Original sentence : "+s);

System.out.println("Final sentence : "+(s2+s3));

static boolean isVowel(char c)

c=Character.toUpperCase(c); //converting it to upper case

if(c=='A' || c=='E' || c=='I' || c=='O' || c=='U' ) //checking for vowel

return true;
else

return false;

OUTPUT:
Enter a sentence:

What is your name?

Number of words beginning and ending with a vowel:0

Original sentence : What is your name?

Final sentence : What is your name

VARIABLE DESCRIPTION:
main() :

Name DataTypePurpose

s String For storing the original entered sentence.

s1 String For storing the words extracted.

s2 String For storing words beginning and ending with vowel.

s3 String For storing words not beginning and ending with vowel.

I int Used as a loop variable.

n int To store number of words beginning and ending with vowel.

L int To store length of entered sentence.


C char To store each extracted character.

c1 char To store first letter of each word.

c2 char To store last letter of each word.

isVowel() :-

Name Data type Purpose

c char Formal parameter for character to be checked for vowel.

12.ALGORITHM:
Step 1: Start of algorithm.

Step 2: Accept a positive number (n).

Step 3: Convert the number to binary by following steps 4 to

Step 4: Initialise s =””. Create a char array = {‘0’,’1’}.

Step 5: Start a while loop(n>0) and repeat steps 6 and 7.

Step 6: Find remainder(r) by dividing n with 2.

Step 7: Update s=dig[r] + s and n=n/2.

Step 8: Initialise c=0.

Step 9: Find length of ‘s’ and store it in ‘l’.

Step 10: Start a for loop from i=0; i<l; i++ and repeat steps 11 and 12.

Step 11: Extract each character and store in ‘ch’.

Step 12: if(ch==’1’) increment ‘c’ by 1.

Step 13: Display “Number of 1’s = “+c.

Step 14: if(c%2==0) then display “Evil number” else display “Not Evil number”.
Step 15: End of algorithm.

CODE:
import java.util.*;

class EvilNumber

String toBinary(int n) // Function to convert a number to Binary

int r;

String s=""; //variable for storing the result

char dig[]={'0','1'}; //array storing the digits (as characters) in a binary


number system

while(n>0)

r=n%2; //finding remainder by dividing the number by 2

s=dig[r]+s; //adding the remainder to the result and reversing at the same
time

n=n/2;

return s;

}
int countOne(String s) // Function to count no of 1's in binary number

int c = 0, l = s.length();

char ch;

for(int i=0; i<l; i++)

ch=s.charAt(i);

if(ch=='1')

c++;

return c;

public static void main(String args[])

EvilNumber ob = new EvilNumber();

Scanner sc = new Scanner(System.in);


System.out.print("Enter a positive number : ");

int n = sc.nextInt();

String bin = ob.toBinary(n);

System.out.println("Binary Equivalent = "+bin);

int x = ob.countOne(bin);

System.out.println("Number of Ones = "+x);

if(x%2==0)

System.out.println(n+" is an Evil Number.");

else

System.out.println(n+" is Not an Evil Number.");

OUTPUT:
Enter a positive number : 5

Binary Equivalent = 101

Number of Ones = 2

5 is an Evil Number.
VARIABLE DESCRIPTION:
toBinary() :-

Name Data Type Purpose

r int For storing remainder of n%2.

s String For storing binary equivalent.

dig[] char Stores binary values ‘0’ and ‘1’.

countOne() :-

Name Data Type Purpose

c int Stores number of 1’s in binary equivalent.

l int Stores length of binary equivalent.

ch char Stores each extracted character.

main() :-

Name Data Type Purpose

n int Stores the decimal number entered by user.

bin String Stores binary equivalent.

x int Stores number of ones in binary equivalent

13.CODE:
import java.util.Scanner;

public class Encripton13

String s;
void input()

Scanner sc=new Scanner(System.in);

char c;

do

System.out.println("Enter a sentence");

s=sc.nextLine().toUpperCase();

c=s.charAt(s.length()-1);

if(c=='?' ||c=='.' || c==',' ||c=='!')

break;

while(true);

void decode()

int ch=64,j=0;

char ab[]=new char[26];

int no[]=new int[26];


for(int i=0;i<26;i++)

ab[i]=(char)(++ch);

no[i]=++j;

s=s.substring(0,s.length()-1)+" ";

String word[]=new String[s.length()];

int value[]=new int[s.length()];

String w="";int c=-1;int sum=0;

for(int i=0;i<s.length();i++)

if(s.charAt(i)!=' ')

w=w+s.charAt(i);

else

word[++c]=w;

sum=0;

for(int k=0;k<w.length();k++)

for(int m=0;m<26;m++)

if(w.charAt(k)==ab[m])
sum+=no[m];

value[c]=sum;

w="";

for(int q=0;q<=c-1;q++)

for(int r=0;r<c-q;r++)

if(value[r]>value[r+1])

int temp=value[r];

value[r]=value[r+1];

value[r+1]=temp;

String t=word[r];

word[r]=word[r+1];

word[r+1]=t;

}
}

for(int i=0;i<=c;i++)

System.out.print(word[i]+" ");

public static void main()

Encripton13 ob=new Encripton13();

ob.input();

ob.decode();

OUTPUT:
Enter a sentence

sky is not the limit

Enter a sentence

sky is not the limit!

IS THE NOT SKY LIMIT


ALGORITHM:
STEP 1: Start of the algorithm.

STEP 2: Declare a method input() and takes a sentence as input until the sentence

is not terminated by ‘,’,’.’,’?’,’!’ using do while loop.

STEP 3: Declare a method decode_sort().

STEP 4: Declare a char array’ ab’ and int array’ no’ each of size 26 to store the

alphabets and their encrypted values respectively.Then using a for loop initialize

the values.

STEP 5: Remove the special character and add a space at the end of the sentence.

Declare a String array’ word’ and int array’ value’ each of size ‘s.length’ to store

the words and their potential values respectively.

STEP 6: Using a for loop extract the words and store them in the array ‘word’.Use

a counter variable ‘c’ for determining the index numbers.Then calculate the

potential values of the words and store it temporarily in a variable named ‘sum’

and afterwards store it in array’ value’ at position ‘c’. Empty the ‘sum’ and the

variable ‘w’ in which the particular word was stored.

STEP 7: Using bubble sort technique, sort the array ‘value’ in ascending order and

accordingly sort the array ‘word’.

STEP 8: Print the array word by giving a space.

STEP 9: Declare a main method and create an object to call the methods input()

and decode_sort()
STEP 10: End of the algorithm.

VARIABLE DECLARATION TABLE:


NAME DATA TYPE PURPOSE

s String instance variable, to store the sentence

c char local variable, to store the last character

ch int local variable,to store and increment the

value of alphabet

j int local variable,to store and increment the

encrypted value of alphabet

ab char reference variable,array to store alphabet

no int reference variable,array to store value

I int used for iteration

word String reference variable,array to store word

value int reference variable,array to store potential

w String to store each word

sum int to store each potential value

k int used for iteration

m int used for iteration

q int used for iteration

r int used for iteration

ob Encryption13 to create an object


14.CODE:
import java.util.Scanner;

public class DiagonalMatrix14

public static void main()

int c=0;

Scanner sc=new Scanner(System.in);

System.out.println("Enter size");

int n=sc.nextInt();

int a[][]=new int[n][n];

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

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

System.out.println("Enter a number");

a[i][j]=sc.nextInt();

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

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

System.out.print(a[i][j]+"\t");

System.out.println();

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

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

if(i!=j)

if(a[i][j]!=0)

++c;

if(c>0)

System.out.println("Not a diagonal matrix");

else

System.out.println("It is a diagonal matrix");


}

OUTPUT:
Enter size

Enter a number

Enter a number

Enter a number

Enter a number

Enter a number

Enter a number

Enter a number

Enter a number

0
Enter a number

1 0 0

0 2 0

0 0 0

It is a diagonal matrix

ALGORITHM:
STEP 1: Start of the algorithm

STEP 2: Input the size of the square matrix and store it in a integer variable ‘n’.

STEP 3: Create an integer square array of size ‘n*n’ .

STEP 4: Initialize the counter variable c=0.

STEP 5: Initialize and declare the variables i=0 and j=0 and start a for loop from i=0

to less than n and gets incremented by 1 with each iteration.Start a for loop from

j=0 to less than n which gets incremented by 1 with each iteration.

STEP 6: Take input from the user and fill the array a[i][j]=sc.nextInt()

STEP 7: Similarly,print the source array.

STEP 8: Initialize and declare the variables i=0 and j=0 and start a for loop from

i=0 to less than n and gets incremented by 1 with each iteration.Start a for loop

from j=0 to less than n which gets incremented by 1 with each iteration.

Within the inner loop, check if(i!=j) and if it is true, then check if(a[i][j]!=0) and if
this condition is tue then increase the value of c.

STEP 9: If c>0, print “Not a Diagonal matrix” otherwise print “It is a diagonal

matrix”.

STEP 10: End of the algorithm.

VARIABLE DECLARATION TABLE:


NAME DATA TYPE PURPOSE

c int counter variable

n int to store the size of the square matrix

a int reference variable for storing the array

i int used for iteration

j int used for iteration

15.CODE:
class FrequencyCharacter

public static void main(String[] args) {

String str = "picture perfect";

int[] freq = new int[str.length()];

int i, j;

//Converts given string into character array

char string[] = str.toCharArray();


for(i = 0; i <str.length(); i++) {

freq[i] = 1;

for(j = i+1; j <str.length(); j++) {

if(string[i] == string[j]) {

freq[i]++;

//Set string[j] to 0 to avoid printing visited character

string[j] = '0';

//Displays the each character and their corresponding frequency

System.out.println("Characters and their corresponding frequencies");

for(i = 0; i <freq.length; i++) {

if(string[i] != ' ' && string[i] != '0')

System.out.println(string[i] + "-" + freq[i]);

}
OUTPUT:
Characters and their corresponding frequencies

p-2

i-1

c-2

t-2

u-1

r-2

e-3

f-1

ALOGORITHM
STEP 1: START

STEP 2: DEFINE String str = "picture perfect"

STEP 3: INITIALIZE freq[] having same size of str.

STEP 4: DEFINE i, j

STEP 5: CONVERT str into char string[].

STEP 6: SET i=0. REPEAT STEP 7 to 11 STEP UNTIL i<str.length<

li=""></str.length<>

STEP 7: SET freq[i] =1

STEP 8: SET j = i+1. REPEAT STEP 9 to STEP 10 UNTIL j<str.length<

li=""></str.length<>
STEP 9: IF (string[i] == string[j]) then

freq[i]++

string[j]= 0

STEP 10: j = j + 1 STEP 11: i = i + 1

STEP 12: PRINT "Characters and their corresponding frequencies "

STEP 13: SET i=0. REPEAT STEP 14 to STEP 15 UNTIL i<freq.length<

li=""></freq.length<>

STEP 14: IF(string[i] != ' ' && string[i] != '0') then

PRINT string[i], freq[i]

STEP 15: i=i+1

STEP 16: END

VARIABLE DECLARATION TABLE:


name Data type Purpose

freq[] int array stores the frequency of each alphabet of string

str String stores the string

char Strting array stores the alphabets of the string

i int used for iteration in the outter loop

j int used for iteration of inner loop

16.CODE:
import java.util.Scanner;

class primeMain
{

public static void main(String[]args)

primerecurse obj=new primerecurse();

Scanner sc = new Scanner(System.in);

int n;

System.out.println("enter the number");

n= sc.nextInt();

if(obj.prime(n,2)==1)

System.out.println("prime number");

else

System.out.println(" not a prime number");

class primerecurse

int prime(int y,int i)

if(y>=2)

{
if(y%i!=0)

return prime(y,++i);

else if(y==i)

return 1;

else

return 0;

else

return 0;

OUTPUT:
enter the number

11

prime number

ALOGIRTHM
STEP 1: start

STEP 2: creating an object which contains the function that checks whether a
number is prime or
not.

STEP 3: declaring a variable to store the input from user.

STEP 4:sending the varible to function

STEP 5: The function int prime() first checks whether the number is > or = to 2 or
not

STEP 6: Then the function checks :-

1. If (y%i!=0) where y = number and I = 2.if it is true then it returns return


prime(y,++i);

2. If (y==i) , if true then it returns 1

3. If none of statements satisfies then it return 0

STEP 7: Then in the main function it is checked if(obj.prime(n,2)==1)

If true then it is a prime number or else not a prime number.

VARIABLE DECLARATION TABLE:


NAME DATATYPE PURPOSE

n int stores the inputed number from user [in main]

y int stores the copied value of n

I int stores the divisor of y

17.CODE:
import java.util.Scanner;

class fiboMain

{
public static void main(String []args)

fiborec obj =new fiborec();

System.out.println("The first n numbers of the fibonacci series");

Scanner sc=new Scanner(System.in);

int n;

n=sc.nextInt();

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

System.out.println(obj.fibo(i));

class fiborec

int fibo(int i)

if(i==0||i==1)

return i;

else
return(fibo(i-1)+fibo(i-2));

OUTPUT:
The first n numbers of the fibonacci series

VARIABLE DECLARATION TABLE:


Name Data Type Purpose

n int for storing the value of n

i int for iteration

ALGORITHM:
Step 1: Start of the algorithm.

Step 2: Initialize a variable n.

Step 3: Start of a for loop from i=0 to i<n where i increases by 1.

Step 4: The recursive function int fibo() gets called by the object obj.fibo(i).
Step 5:Now the value of i gets checked and then it returns the corresponding value
to the for loop.

Step 6:Then the loop gets executed the n number of times .

Step 7: Finally the print statement is executed.

Step 8: End of algorithm.

18.CODE:
import java.util.Scanner;

class gcdMain

public static void main(String []args)

gcdrec obj=new gcdrec();

int a,b;

System.out.println("enter the values");

Scanner sc =new Scanner(System.in);

a=sc.nextInt();

b=sc.nextInt();

System.out.println(obj.gcd(a,b));

}
class gcdrec

int gcd(int a,int b)

if(a==0)

return b;

else if(b==0)

return a;

else if(a==b)

return a;

else if(a>b)

return gcd(a-b,b);

else

return gcd(a,b-a);

OUTPUT:
enter the values

45

15

15
VARIABLE DECLARATION TABLE:
Name Data Type Purpose

a int for storing the first number

b int for storing the second number

ALGORITHM:
Step 1: Start of algorithm.

Step 2: Initialize the variables a and b.

Step 3:The recursive function int gcd() is called through the object obj.gcd(a,b).

Step 4: Now the values of a and b are checked in the corresponding if and else if
statement.

Step 5: Then the return statement gets executed according to the conditional
statements.

Step 6:Finally the print statement gets executed.

Step 7:End of algorithm.

19.CODE:
import java.util.Scanner;

class A19

void reverse(String s,int l)

{
if(l<0)

return;

else

System.out.println(s.charAt(l));

reverse(s,--l);

import java.util.Scanner;

class b

public static void main(String[]args){

a obj=new a();

String s;

Scanner sc=new Scanner(System.in);

System.out.println("Enter the string");

s=sc.nextLine();

int l=s.length();

obj.reverse(s,l-1);
}

OUTPUT:
geeks for geeks

ALGORITHM :
Step 1: Start the alg orithm.
Step 2: Use void reverse() function to accept a string variable and an integer
variable l for the length

of the string.

Step 3:Now check whether the length of the string is less than zero or not.

Step 4: Now extract character of the string

Step 5: Now create an object of the a class in the main function.

Step 6: Input a string in the main class and also find the length of the string.

VARIABLE DECLERATION TABLE :


VARIABLE NAME VARIABLE TYPE PURPOSE

s String For initializing a string variable

l int To find the length of the string.

20.CODE:
import java.util.Scanner;

class Bi

int binary_search(int a[],int l,int u,int s)

int m;

if(l<=u)

m=(l+u)/2;
if(s==a[m])

return m;

else if(s<a[m])

return binary_search(a,l,(m-1),s);

else if(s>a[m])

return binary_search(a,(m+1),u,s);

return -1;

import java.util.Scanner;

class Na

public static void main(String[]args)

Bi obj=new Bi();

int arr[],n,i,lb,ub,d,r,j,temp;

Scanner sc=new Scanner(System.in);

System.out.println("Enter the number of elements in the array");

n=sc.nextInt();

System.out.println("Enter the elements of the array");


arr=new int[n];

for(i=0;i<n;i++)

arr[i]=sc.nextInt();

System.out.println("Enter the element to be searched");

d=sc.nextInt();

lb=0;ub=n-1;

for(i=0;i<n-1;i++)

for(j=0;j<n-1-i;j++)

if(arr[j]>arr[j+1])

temp=arr[j];

arr[j]=arr[j+1];

arr[j+1]=temp;

r=obj.binary_search(arr,lb,ub,d);

if(r==-1)

System.out.println("Element not present");


else

System.out.println("Element present ");

OUTPUT:
Enter the number of elements in the array

Enter the elements of the array

67

09

10

Enter the element to be searched

10

Element present

ALGORITHM :
Step 1: Start the algorithm

Step 2: Use binary_search function to accept the following variable.

Step 3: Now use an integer value m to find the middle most value from the
variables declared

in the array.

Step 4: Now compare the middle most value with the value to be searched.
Step 5: If the number to be searched is less than the middle most value then
perform the

search in the first half of the array.

Step 6: If the number to be searched is more than the middle most value then
perform the

search in the other half of the array.

Step 7: Now create a main class.

Step 8: In the main class create an object of the bi class.

Step 9: Use a variable n to input the number of elements in the array.

Step 10: Now enter the elements in the array.

Step 11: Now start a for loop from 0 to n-1.

Step 12: Start another for loop starting from 0 to n-1-i.

Step 13:Now start comparing between two variables in the array.

VARIABLE DECLERATION TABLE


NAME DATATYPE PURPOSE

l int left most digit of the array

u int right most digit of the array

m int to find the middle element

s int number to be searched

5. n int to store the number of elements

6. d int element to be searched

7. I int to serve the purpose of outer loop


8. J int to serve the purpose of inner loop

You might also like