Computer Project
Computer Project
2024-25
SUBMITTED BY – SUBMITTED TO –
Roll Number– 26
UID –
Name:
Class & Sec:
Number
Programs
Question 1-
Design a program to accept a positive whole no. ‘N’ where N>2 and
N<100. Find the binary equivalent of the number and count the
number of 1s in it and display whether it is an evil no. or not with an
appropriate message.
Test your program with the following data and some random data:
Example 1:
INPUT: N= 15
NUMBER OF 1’s: 4
→ALGORITHM
Step-1: Start
Step-2: Enter a decimal number.
Step-3: Convert the number into its binary equivalent.
Step-4: Calculate the number of 1 in binary equivalent.
Step-5: Using for loop, taking one digit at a time, check whether it
is equal to 1. Increment count1 variable by 1 if the digit is
equal to 1.
Step-6: Check whether variable count1 is even or not. If n is even,
then it is an evil number, otherwise not an evil number.
Step-7: Stop
→CODE
//Program to accept a positive whole number, find its binary
equivalent, count the number of 1s and display whether it is an evil
number or not
import java.util.Scanner;
class Evil
{
int n, a, count1=0;
String s = "";
void input ()
{
Scanner sc = new Scanner (System.in);
System.out.println("Enter decimal number ");
n = sc.nextInt();
System.out.println("INPUT:"+n);
}
void calculate ()
{
Input ();
while(n>0)
{
a = n%2;
If (a == 1)
{
count1++;
}
s = a+" "+s;
n = n/2;
}
System.out.println("BINARY EQUIVALENT:" +s);
System.out.println("NO. OF 1's " +count1);
}
void count ()
{
Calculate ();
If ( count 1 %2 ==0)
System.out.println("\n EVIL NUMBER");
else
System.out.println("\nNOT AN EVIL NUMBER");
}
public static void main (String args[])
{
Evil bin=new Evil();
Evil();
}
}
→VARIABLE-DESCRIPTION TABLE
Variable Description Data Type
n to store decimal number int
a to store modulus value int
count1 to count no. of 1’s int
s to store binary equivalent String
→OUTPUT
Enter decimal number
24
BINARY EQUIVALENT:1 1 0 0 0
NO. OF 1’s 2
EVIL NUMBER
Question 2-
→ALGORITHM
Step-1: Start
Step-2: Initialize rev =0 and divisor f=2
Step-3: Check the given number (n) is prime or not
Step-4: Check by using recursive technique and return 1 if prime
otherwise return 0
Step-5: If prime, find the reverse of the given number (n).
Step-6: Check if the reverse of the number is prime or not
Step-7: Create main and call the functions
Step-8: Stop
→CODE
import java.util.*;
public class Emirp
{
int n ,rev, f;
Emirp(int nn)
{
n=nn;
rev=0;
f=2;
}
int isprime(int x)
{
if(n==x)
{
return 1;
}
else if(n % x==0 || n==1)
{
return 0;
}
else
{
return isprime(x+1);
}
}
void isEmirp()
{
int x=n;
while(x!=0)
{
rev=(rev*10)+x%10;
x=x/10;
}
int a=isprime(f);
x=n;
n=rev;
f=2;
int b=isprime(f);
if(a==1 && b==1)
{
System.out.println(x+” is an Emirp number”);
}
else
{
System.out.println(x+” is not an Emirp number”);
}
}
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println(“Enter the number:”);
int x=sc.nextInt();
Emirp obj=new Emirp(x);
obj.isEmirp();
}
}
→VARIABLE-DESCRIPTION TABLE
Variable Description Data Type
N to store number to be int
checked
F to store the divisor int
nn to assign n int
X to pass value to n int
rev to store reverse int
A to store 1 if number is prime int
B to store 1 if reverse of int
number is prime
→OUTPUT
Enter the number:
13
13 is an Emirp number
Enter the number:
51
51 is not an Emirp number
Question 3-
Example-: 135=1
Member Methods/Function
int sumofDigits : to return the sum of the digits of the number (n) to
the power of their respective positions.
Specify the class Disarium giving the details of the constructor (),void
countDigit(), int sumofDigits() and void check(). Define the main()
function to create an object and call the functions accordingly to
enable the task.
→ALGORITHM
Step-1: START
int num=n;
while(num>0)
int d=num%10;;
size++;
num=num/10;
Step-10: Stop
→CODE
import java.util.*;
int n,size;
Disarium(int nn)
n=nn;
size=0;
void countDigits()
int num=n;
while(num>0)
int d=num%10;;
size++;
num=num/10;
int sumofDigits()
{
int num=n;
while(num>0)
int d=num%10;
sum= sum+(int)Math.pow(d,size);
num=num/10;
size--;
return sum;
void check()
if(sumofDigits()==n)
else
ob.check();
→VARIABLE-DESCRIPTION TABLE
→OUTPUT
89 is a Disarium number
→ALGORITHM
Step-1: Start
Step-2: Multiply the given number by 2 and 3 separately
Step-3: Concatenate the results from step-2 with the given
number by converting to string
Step-4: Run for loop from 49-57(ASCII code of 1-9)
Step-5: Run nested loop till length of string
Step-6: Check whether each digit is present in the string by
extracting characters from the string
Step-7: If a digit does not appear in the string or is repeated,
terminate loop and set flag variable to false
Step-8: If flag variable is true, display “It is a fascinating number”,
else, display “It is not a fascinating number”
Step-9: Stop
→CODE
import java.util.*;
class fascinating
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
int n,n2,n3,i,j,c;
char ch;
boolean b=true;
System.out.println("Enter a number");
n=sc.nextInt();
n2=n*2;
n3=n*3;
String str=""+n+n2+n3;
for(i=49;i<=57;i++)
{
c=0;
for(j=0;j<str.length();j++)
{
ch=str.charAt(j);
if(i==ch)
c++;
}
if(c!=1)
{
b=false;
break;
}
}
if(b)
System.out.println("It is a fascinating number");
else
System.out.println("It is not a fascinating number");
}
}
→VARIABLE-DESCRIPTION TABLE
Variable Description Data Type
N To store inputted number int
n2 Number x 2 int
n3 Number x 3 int
I Looping variable int
J Looping variable int
C Counter variable int
ch To extract characters char
B Flag variable boolean
str Concatenated string String
→OUTPUT
Enter a number
192
It is a fascinating number
Enter a number
193
It is not a fascinating number
Question 5-
Prime number: A number which has only two factors, i.e. 1 and the
number itself.
Example: 2, 3, 5, 7 … etc.
Adam number: The square of a number and the square of its reverse
are reverse to each other.
(13)2 = 169
Test your program with the following data and some random data:
Example 1
INPUT:
m=5
n = 100
OUTPUT:
THE PRIME-ADAM INTEGERS ARE:
11 13 31
FREQUENCY OF PRIME-ADAM INTEGERS IS: 3
→ALGORITHM
Step-1: Start
Step-8: Stop
→CODE
import java.util.Scanner ;
int rev = 0;
while (num != 0)
{
rev = rev * 10 + d;
num /= 10;
return rev;
int c = 0;
if (num % i == 0)
{
c++;
return c == 2;
int m = in.nextInt();
int n = in.nextInt();
int count = 0;
if (m >= n)
System.out.println("INVALID INPUT");
return;
if (adam) {
if (prime) {
count++;
if (count == 0) {
System.out.print("NIL");
System.out.println();
→VARIABLE-DESCRIPTION TABLE
→OUTPUT
Question 6-
Member functions/methods:
void display(): displays the original numbe along with its reverse b
invoking the method recursive
→ALGORITHM
Step-1: Start
Step-2: Create default constructor
Step-3: Create function inputnum() to accept number
Step-4: Create function reverse(int nn) with return type ‘int’ to
reverse the original number
Step-5: Display the original and reversed number
Step-6: Stop
→CODE
import java.util.*;
class Revno
{
int num;
Revno()
{
num=0;
}
void inputnum()
{
Scanner sc= new Scanner (System.in);
System.out.println("Enter the number:");
num=sc.nextInt();
}
void Reverse(int num)
{
if(num<10)
{
System.out.println(num);
return;
}
else
{
System.out.print(num%10);
Reverse(num/10);
}
}
void display()
{
System.out.println("Original Number:\n"+num);
System.out.println("Reversed Number: ");
Reverse(num);
}
public static void main()
{
Revno ob= new Revno();
ob.inputnum();
ob.display();
}
}
→VARIABLE-DESCRIPTION TABLE
Variable Description Data Type
num to input number int
→OUTPUT
Enter the number:
123456789
Original Number:
123456789
Reversed Number:
987654321
Question 7-
Example
372 = 33+73+13
1634 = 14+64+34+44
54748 = 55+45+75+45+85
Thus, 371, 1634 and 54748 are all examples of Armen or numbers
Data members
ArmNum
int sum pow(int i): returns the sum of each digit raised to the power
of the length of the number using recursive technique e.g., 34 will
return 3 2 + 42 (as the length of the number is 2)
void is Armstrong(): checks whether the given number is an
Armstrong number by invoking the function sum_pow () and displays
the result with an appropriate message
→ALGORITHM
Step-1: Start
Step-2: Accept the input number
Step-3: Initialize variables: n(input number), I(length of the
number), sum (to store the sum of digits raised to the
power of length)
Step-4: Calculate the length of the number using a function
'getLength(num)'
Step-5: Initialize sum as 0
Step-6: Iterate through each digit of the number:
- Extract the rightmost digit using modulo operator.
- Raise the digit to the power of the length of the
number.
- Add the result to the sum.
- Remove the rightmost digit by dividing the number
by 1 0. Repeat until all digits are processed.
Step-7: Check if the calculated sum is equal to the original
number:
- If yes, then the number is an Armstrong number.
Print a message indicating that.
- If no, then the number is not an Armstrong number.
Print a message indicating that.
Step-8: Stop
→CODE
Import java.util.*;
{
Public class ArmNum
{
int n; int l;
// Parameterized constructor to initialize data members
ArmNum(int nn)
{
n = nn;
I = getLength(n);
}
// Method to calculate length of the number int getLength(int num)
{
int length = 0; while (num !=0)
length++; num 10;
return length;
}
// Recursive function to calculate sum of digits raised to the power of
length int sum_pow(int i)
{
if (i<0){
return 0;
int digit =
return (int) Math.pow(digit,l)+ sum_pow(i/ 1 0);
// Method to check if the number is Armstrong or not
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.");
}
public static void main(String[] args)
{
ArmNum armNum = new ArmNum(num); armNum.isArmstrong();
}
}
}
→VARIABLE-DESCRIPTION TABLE
Variable Description Data Type
n to input number Int
l length of inputted number Int
sum the sum of digits raised to Int
the power of the length
num Input number provided via Int
command-line argument
armnum An instance of the ArmNum Int
class
→OUTPUT
Input : 1634
Input : 216
→ALGORITHM
Step-1: Start
Step-2: Take the input of the number and store in 'n'
Step-3: Verify whether the number is greater than 4 If not then
show invalid input
Step-4: Check whether the n in prime or not
Step-5: Make a function to get the prime pair and odd prime pair
Step-6: Print the pairs of the inputted gold batch number
Step-7: Stop
→CODE
import java.util.*;
public class Goldbach_PrimePairs
{
public static void main(String[] args)
{
//TAKE INPUT USING SCANNER METHOD
Scanner sc = new Scanner(System.in); System.out.print("N = ");
int n=sc.nextInt(); if(n>4)
//VALIDATE THE NUMBER TO BE A GOLD BACH NUMBER
{
if(n>9&&n<50)
{ if(n%2==0)
{
System.out.println ("PRIME PAIRS ARE :"); goldbach(n);
}
else
{
System.out.println ("INVALID INPUT.NUMBER IS ODD");
}
}
else
{
System.out.println ("INVALID INPUT.NUMBER OUT OF RANGE");
}
}
else
{
System.out.println ("NOT A GOLDBACH NUMBER");
}
}public static void goldbach( int num )
//FUNCTION TO GET THE PRIME PAIRS
{
for(int i=1;i<num;i++)
{
if(oddprime(i))
{
int op = num-i; if(oddprime(op)&&i<=op)
{
System.out.println (i+" , "+op);
}
}
}
}
public static boolean oddprime(int u)
//FUNCTION TO GET THE ODDPRIMES
{
if(u%2!=0&&prime(u)) return true;
else
return false;
}
public static boolean prime(int p)
//FUNCTION TO GET THE PRIMES
{
int c=0;
for(int i=1;i<=p;i++)
{
if(p%i==0) c++;
}
if(c==2) return true; else
return false;
}
}
→VARIABLE-DESCRIPTION TABLE
Variable Description Data Type
num to input number int
pair[] Used for an array int
i Used for primary loop int
j Used for secondary loop int
sum Used to calculate sum int
c Used as a counter int
k Used as a counter int
flag Used as a flag int
→OUTPUT
N = 46
5, 41
17, 29
23, 23
Question 9-
Some of the members of the class are given below: Class name:
Perfect Data members/instance variables: num: to store the number
Methods/Member functions: Perfect
→ALGORITHM
Step-1: Start
Step-2: Class Definition:
- Define a class named `Perfect`.
Step-3: Data Members:
- Declare an integer data member `num` to store the
number.
Step-4: Constructor:
- Define a parameterized constructor `Perfect(int nn)`:
- Initialize the data member `num` with the value of `nn`.
Step-5: Method: sum_of_factors:
- Define a private method `int sum_of_factors(int i)`:
- If `i` is 1, return 0 (base case for recursion).
- If `num` is divisible by `i` (i.e., `num % i == 0`),
add `i` to the result of `sum_of_factors(i - 1)`.
- Otherwise, return the result of `sum_of_factors(i - 1)`
without adding `i`.
Step-6: Method: check:
- Define a public method `void check()`:
- Call `sum_of_factors(num - 1)` to get the sum of the
factors of `num` excluding itself.
- If the sum is equal to `num`, print that `num` is a perfect
number.
- Otherwise, print that `num` is not a perfect number.
Step-7: Main Method:
- Define the `main` method:
- Create an object of the `Perfect` class with a specific
number (e.g., `Perfect p = new Perfect(6)`).
- Call the `check()` method on the object to check if the
number is perfect.
Step-8: Stop
→CODE
class Perfect {
private int num;
→OUTPUT
Input: 6
6 is a perfect number
Input: 28
28 is a perfect number
Question 10-
Example: If the first number is 23 and the second is 764, then the
concatenated then the concatenated number will be 23764.
DATA MEBERS:
MEMBER FUNCTION:
void show()- to display the original numbers and the merged number
with appropiate messages
Specify the class Merger , giving the details of the constructor(), void
readNum(), void JoinNum() and voidshow(). Define the main()
function to create an object and call the functions accordingly to
enable the task.
→ALGORITHM
Step-1: Start
Step-2: DECLARE THE DATA VARIABLES
Step-3: CREATE THE MEMBER METHODS TO ACCEPT THE VALUES
OF N1,N2
Step-4: TO CONCATENATE THE NUMBERS N1 AND N2 AND STORE
IT IN mergNum
Step-5: NOW DISPLAY ORIGINAL NO AND MERGED NO.
Step-6 Stop
→CODE
import java util.;
class Merger
{
long n1, n2;
long mergNum;
public Merger()
{
n1=1;
n2=1;
mergNum=11;
}
public void readNum()
{
Scanner sc=new Scanner(System.in);
System.out.print("Enter first number”);
n1=(int)Math.abs(sc.nextLong());
System.out.print("Enter second number:");
n2=(int)Math.abs(sc.nextLong());
if(n1==0) n1=1;
if(n2==0) n2=1;
}
public void joinNum()
{
String num1 = Long.toString(n1);
String num2 = Long.toString(n2);
String merged=num1 + num2;
mergNum=Long.parseLong(merged);
}
public void show()
{
System.out.println("First Number: "+n1);
System.out.println("Second Number: "+n2);
System.out.println("Merged Number: "+mergNum);
}
public static void main(String args[])
{
Merger obj=new Merger));
obj.readNum();
obj.joinNum();
obj.show();
}
}
→VARIABLE-DESCRIPTION TABLE
Variable Description Data Type
n1 to store first number long
n2 To store second number Long
mergNum To store merged number Long
→OUTPUT
Enter first number:
34567
873490
First number: 34567
Test your program for the following data and some random data:
Example 1
INPUT:
M=4
N=3
11 -2 3
5 16 7
9 0 4
3 1 8
OUTPUT:
ORIGINAL MATRIX
11 -2 3
5 16 7
9 0 4
3 1 8
-2 3 11
5 7 16
0 4 9
1 3 8
→ALGORITHM
Step-1: Start
Step-4: If(m<=2||m>=10||n<=2||n>=10)
Step-11: If the next element is greater than the previous one, then
swap their positions.
Step-13: Stop
→CODE
import java.util.Scanner;
int m = in.nextInt();
int n = in.nextInt();
A[i][j] = in.nextInt();
System.out.println(“ORIGINAL MATRIX”);
System.out.print(A[i][j] + “ “);
System.out.println();
}
for (int i = 0; i < m; i++)
int t = A[i][k];
A[i][k] = A[i][k+1];
A[i][k+1] = t;
System.out.print(A[i][j] + “ “);
}
System.out.println();
→VARIABLE-DESCRIPTION TABLE
→OUTPUT
ENTER THE VALUE OF M: 3
22 7 9
5 36 13
19 12 6
ORIGINAL MATRIX
22 5 19
7 36 12
9 13 6
5 19 22
7 12 36
6 9 13
Question 12-
→ALGORITHM
Step-1: Start
Step-9: Create the static function and pass the values into the
constructor
Step-11: Stop
→CODE
import java.util.*;
class MatRev
int arr[][];
int m;
int n;
MatRev(int mm,int nn)
m=mm;
n=nn;
arr=new int[m][n];
void fillarray()
int i,j;
for(i=0;i<m;i++)
for(j=0;j<n;j++)
arr[i][j]=in.nextInt();
int d,r=0;
d=x%10;
r=r*10+d;
x=x/10;
return r;
void revMat(MatRev P)
int i,j;
for(i=0;i<m;i++)
for(j=0;j<n;j++)
}
}
int i,j;
for(i=0;i<m;i++)
for(j=0;j<n;j++)
System.out.print(arr[i][j]+"\t");
System.out.println();
ob.revMat(ob);
ob.show();
}
}
→VARIABLE-DESCRIPTION TABLE
→OUTPUT
11 12 13 14 15 16 17 18 19
11 21 31
41 51 61
71 81 91
Question 13-
2. Fill the square matrix b[][] in the following format . If the array a[]
= { 5,2,1,8} then the matrix b[][] would be fill as below:
1 2 5 8
1 2 5 1
1 2 1 2
1 1 2 5
→ALGORITHM
Step-1: Start
Step-3: If the value of N if N< 2 or N>10 the print - matrix size out
of range
Step-8: Check whether i+j <= N or not if b[i][j] = a[j] else b[i][j] =
a[i+j-(N-1)-1)
Step-10: Stop
→CODE
import java.util.*;
class array
int i,j,t,N;
N = in.nextInt();
if (N<2 || N>10)
{
else
a[i] = in.nextInt();
for(j=0;j<((N-1)-i);j++)
if(a[j]>a[j+1])
t= a[j];
a[j] = a[j+1];
a[j+1] = t;
}
System.out.println(" sorted array in ascending order;");
System.out.println(a[i]);
// square matrix
for( i=0;i<N;i++)
for(j =0;j<N;j++)
b[i][j] = a[j];
System.out.print(b[i][j]+" ");
else
int k = i+j;
b[i][j] = a[(k-(N-1))-1];
}
}
System.out.println();
→VARIABLE-DESCRIPTION TABLE
→OUTPUT
4
enter the elements:
5 2 1 8
1 2 5 8
filled matrix:
1 2 5 8
1 2 5 1
1 2 1 2
1 1 2 5
Question 14-
Member functions/methods:
Specify the class Sum_rowcol giving the details of the functions void
row_col (int mx, int nx), void read row_col(), void display_mat() and
void sum_mat().
→ALGORITHM
Step-1: Start
Step-2: Creating a new matrix mat and defining member
variables.
Step-14: Stop
→CODE
import java.util.*;
int m, n, i, j;
Sum_rowcol() {
mat[i][j] = 0;
m = mx;
n = nx;
void read_row_col() {
void display_mat() {
System.out.println();
void sum_mat() {
int c, r;
r = 0;
r = r + mat[i][j];
}
mat[i][n-1] = r;
c = 0;
c = c + mat[j][i];
mat[m-1][i] = c;
obj.row_col(5, 5);
obj.read_row_col();
obj.sum_mat();
obj.display_mat();
}
→VARIABLE-DESCRIPTION TABLE
→OUTPUT
1257377259756234
1 2 5 7 15
3 7 7 2 19
5 9 7 5 26
6 2 3 4 15
15 20 22 18 0
Question 15-
(i.e., the first row becomes the last, the second row becomes the
first and so on).
MEMBER FUNCTIONS:
Shift(int mm, int nn): to initialize the size of the matrix m=mm and
n=nn
void cyclic(Shift P):enables the matrix of the object (P) to shift each
row upwards in a cyclic manner and store the resultant matrix in the
current object
→ALGORITHM
Step-1: Start
Step-3: Create a function void input() to ask the user to enter the
elements of the array.
Step-8: Stop
→CODE
import java.util.*;
class Shift
int mat[][],n,m;
Shift(int mm ,int nn)
m=mm;
n=nn;
void input()
for(int i=0;i<m;i++)
for(int j=0;j<n;j++)
mat[i][j]=in.nextInt();
void cyclic(Shift p)
if(i==0)
this.mat[m-1][j]=p.mat[i][j];
else
this.mat[i-1][j]=p.mat[i][j];
void display()
System.out.print(mat[i][j]+"\t");
}
System.out.println();
ob.input();
System.out.println("Original Matrix");
ob.display();
ob1.cyclic(ob);
System.out.println("Changed Matrix");
ob1.display();
→VARIABLE-DESCRIPTION TABLE
Variable Description Data Type
Mat[][] To store double dimensional int
array
m To store number of rows int
n To store number of columns int
i Control variable in for loop int
j Control variable in for loop int
→OUTPUT
11 22 33 44 55 66 77 88 99
Original Matrix
11 22 33
44 55 66
77 88 99
Changed Matrix
44 55 66
77 88 99
11 22 33
Question 16-
ORIGINAL
11 5 7
8 13 9
1 6 20
TRANSPOSE
11 8 1
5 13 6
7 9 20
Methods/Member functions:
void display( ): displays the original matrix and the transposed matrix
by invoking the method transpose( )
→ALGORITHM
Step-1: Start
Step-8: Create main function and call all the required functions.
Step-9: Stop
→CODE
import java.util.Scanner;
int m;
Trans(int mm)
m=mm;
void fillarray( )
int i,j;
for( i = 0;i<m;i++)
arr[i][j]=sc.nextInt();
System.out.print(“ “);
void transpose( )
temp[i][j]=arr[j][i];
arr =temp ;
void display( )
{
for(int j=0;j<m;j++)
System.out.println(“ “);
transpose( )
for(int j=0;j<m;j++)
System.out.println(“ “);
{
Scanner sc = new Scanner (System.in);
ob.fillarray();
ob.display();
→VARIABLE-DESCRIPTION TABLE
→OUTPUT
11 5 7
8 13 9
1 6 20
5 13 6
7 9 20
Question 17-
Test your program for the following data and some random data:
Example 1
INPUT:
M=4
9 2 1 5
8 13 8 4
15 6 3 11
7 12 23 8
OUTPUT:
ORIGINAL MATRIX
9 2 1 5
8 13 8 4
15 6 3 11
7 12 23 8
REARRANGED MATRIX
1 2 4 5
23 3 6 7
15 8 13 8
12 11 9 8
DIAGONAL ELEMENTS
1 5
3 6
8 13
12 8
→ALGORITHM
Step 1: Start
Step 8: Stop
→CODE
import java.util.*;
class Matrix
int M=in.nextInt();
int k= 0;
int p= 1;
A[i][j] = in.nextInt();
if(i == 0 || j == 0 || i == M- 1 || j == M - 1)
b[k++] = A[i][j];
else
{ p*= A[i][j];
System.out.println("ORIGINAL MATRIX");
for(int i=0;i<M;i++)
{
for(int j=0;j<M;j++)
System.out.print(A[i][j]+"\t");
System.out.println();
b[j + 1] = temp;
k = 0;
A[0][j] = b[k++];
A[i][0] = b[k++];
System.out.println("REARRANGED MATRIX");
for(int i=0;i<M;i++)
System.out.print(A[i][j]+"\t");
System.out.println();
int sd=0;
System.out.println("Diagonal Elements:");
for(int i=0;i<M;i++)
for(int j=0;j<M;j++)
if((i==j)||(i+j)==(M-1))
{
sd=sd+A[i][j];
System.out.print(A[i][j]+"\t");
else
System.out.print("\t");
System.out.println();
→VARIABLE-DESCRIPTION TABLE
→OUTPUT
Enter value of M
4
ENTER MATRIX ELEMENTS:23 3 13 4 6 12 17 14 18 29 7 8 5 19 20 31
ORIGINAL MATRIX
23 3 13 4
6 12 17 14
18 29 7 8
5 19 20 31
REARRANGED MATRIX
3 4 5 6
31 12 17 8
23 29 7 13
20 19 18 14
Diagonal Elements:
3 6
12 17
29 7
20 14
Sum of the diagonal elements=108
Question 18-
Member functions/methods:
→ALGORITHM
Step-1: Start
Step-8: Stop
→CODE
import java.util.*;
class ArrayPrime
int arr[][], m;
ArrayPrime(int mm)
m=mm;
a=new int[m][m];
void readarray()
{
Scanner sc=new Scanner(System.in);
for(int i=0;i<m;i++)
for(int j=0;j<m;j++)
arr[i][j]=sc.nextInt();
int isPrime(int x)
int i,f=0;
for(i=2;i<x/2;i++)
if(x%i==0)
f++;
if(f==0)
return 1;
else
return -1;
void display()
for(int i=0;i<m;i++)
{
for(int j=0;j<m;j++)
System.out.println(“Row”+i);
if(isPrime(arr[i][j])==1)
System.out.print(arr[i][j]);
else
System.out.print(\t);
System.out.println();
ob.readarray();
ob.display();
}
→VARIABLE-DESCRIPTION TABLE
→OUTPUT
3 4 3
2 6 7
Row1
Row2
3 3
Row3
2 7
Question 19-
b) Calculate the decimal equivalent for each row and display as per
the format given below
→ALGORITHM
Step-1: Start
Step-2: Taking input for the number of rows (M) and columns (N)
of the matrix and check if the input values are within a
specific range (m <= 0 || m >= 10 || n <= 2 || n >= 6)
Step-4: Using nested for loops, input elements for each row of
the matrix from the user.
Step-7: Stop
→CODE
import java.util.*;
int n = in.nextInt();
}
int a[][] = new int[m][n]; for (int i = 0; i < m; i++)
a[i][j] = in.nextInt();
int decNum = 0;
{
decNum += a[i][j] * Math.pow(8, n - j - 1 );
→VARIABLE-DESCRIPTION TABLE
→OUTPUT
4 2 3
7 5 3
FILLED MATRIX
1 5 6
4 2 3
7 5 3
DECIMAL EQUIVALENT
1 5 6 110
4 2 3 275
753 491
Question 20-
(c) Display the original matrix and the sorted matrix along with the
sum of their boundary elements respectively
→ALGORITHM
Step-1: Start
Step-2: Take input for the number of rows (M) and columns (N) of
the matrix from user.
Step-9: Stop
→CODE
import java.util.*;
for(int i=0;i<r;i++)
for(int j=0;j<c;j++)
mat[i][j]=sc.nextInt();
System.out.println ();
//ORIGINAL MATRIX
for(int j=0;j<c;j++)
System.out.print( mat[i][j] );
System.out.println ( );
}
int sum = 0;
if( i==0||i==r-1 )
sum+=mat[i][j];
else if (j==0||j==c-1)
sum+=mat[i][j];
//SORTED MATRIX
{
for(int j=0;j<c;j++)
System.out.print(mat[i][j]);
System.out.println ();
sum=0;
for(int i=0;i<r;i++)
for(int j=0;j<c;j++)
if(i==0||i==r-1)
sum+=mat[i][j];
sum+=mat[i][j];
}
}
for(int i=0;i<a;i++)
for(int j=0;j<b;j++)
arr[k]=mat[i][j]; k++;
//SORTING
if(arr[j]<arr[j+1])
{
int copy = arr[j]; arr[j]=arr[j+1]; arr[j+1] = copy;
for(int i=0;i<a;i++)
for(int j=0;j<b;j++)
mat[i][j]=arr[k]; k++;
→VARIABLE-DESCRIPTION TABLE
Variable Description Data Type
mat[][] To store matrix int
m To store number of columns int
n To store number of rows int
i Control variable in for loop int
j Control variable in for loop int
sum to sum up the values int
c to count int
k To count int
copy To copy int
→OUTPUT
M: 3
N: 3
123456789
ORIGINAL MATRIX
1 2 3
4 5 6
7 8 9
SORTED MATRIX
9 8 7
6 5 4
3 2 1
A class Mystring has been defined to print the code of each word.
Code will be calculated by adding the ASCII code of each character.
Ex- the code for ACE will be 201(65+67+69). Define the class Mystring
using following methods
Data members
Member functions
Mystring(): Constructor
Specify the class Mystring giving details of the constructor and void
readstring(), int code(String w), void word() only. Write the main
function to create objects and call the functions accordingly.
→ALGORITHM
Step-1: Start
Step-6: Create function void display() and display each word along
with its code in different lines by extracting words from
String str and invoking function int code(String w)
Step-8: Stop
→CODE
import java.util.*;
class Mystring
String str;
int c;
Mystring()
str=" ";
c=0;
void readstring()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter a sentence");
str=sc.nextLine();
int code(String w)
int i;
for(i=0;i<w.length();i++)
c=c+w.charAt(i);
return c;
void display()
int i;
String s="";
for(i=0; i<str.length();i++)
if(str.charAt(i)==' ')
{
System.out.println(s+":"+code(s));
s="";
else
s=s+str.charAt(i);
System.out.println(s+":"+code(s));
ob.readstring();
ob.display();
→VARIABLE-DESCRIPTION TABLE
→OUTPUT
Enter a sentence
Invisible String
Invisible:933
String:631
Question 22-
→ALGORITHM
Step-1: Start
Step-2: Create a class with name WordWise
Step-3: Declare instance variable
Step-4: Function int freq_vowel(string w) is a parameterized
function to find the frequency of vowel
Step-5: Create void arrange to print the words with their
frequency
Step-6: Create an object of a class
Step-7: Call the function
Step-8: Stop
→CODE
import java.util.*;
class WordWise
{
String str;
WordWise()
{
str=" ";
}
void readsent()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter a sentence in upper case");
str=sc.nextLine();
str=str.toUpperCase();
}
int freq_vowel(String w)
{
int i,v=0;
char c;
for(i=0;i<w.length();i++)
{
c=w.charAt(i);
if(c=='A'||c=='E'||c=='I'||c=='O'||c=='U')
v++;
}
return v;
}
void arrange()
{
int i;
String s=" ";
for(i=0; i<str.length();i++)
{
if(str.charAt(i)==' ')
{
System.out.println(s+":"+freq_vowel(s));
s=" ";
}
else
s=s+str.charAt(i);
}
System.out.println(s+":"+freq_vowel(s));
}
public static void main()
{
WordWise ob=new WordWise();
ob.readsent();
ob.arrange();
}
}
→VARIABLE-DESCRIPTION TABLE
Variable Description Data Type
str to store string String
i looping variable int
v to count variables int
c to extract character char
s to extract word String
→OUTPUT
Enter a sentence in upper case
THE TORTURED POETS DEPARTMENT
THE:1
TORTURED:3
POETS:2
DEPARTMENT:3
Question 23-
Write a Program to accept a sentence that may be terminated by
either “.” , “?” or “!” only. The words of sentences are separated by a
single space and are in UPPERCASE. Decode the words according to
their Potential and arrange them in ascending order of their
potential strength. Test your program with the following data and
some random data.
Example 1:
DO DO HOW YOU
→ALGORITHM
Step-1: Start
Step-8: Stop
→CODE
import java.util.*;
System.out.println("INVALID INPUT");
return;
strArr[i] = st.nextToken();
potArr[i] = computePotential(strArr[i]);
System.out.println("Potential:");
/*
*/
{
int t = potArr[j];
potArr[j] = potArr[j+1];
potArr[j+1] = t;
strArr[j] = strArr[j+1];
strArr[j+1] = temp;
System.out.println("Sorted Sentence");
int p = 0;
int l = str.length();
/*
* Substracting 64 from ASCII Value of
* A => 65 - 64 = 1
* B => 66 - 64 = 2
* ..
* ..
*/
p += str.charAt(i) - 64;
return p;
→VARIABLE-DESCRIPTION TABLE
→OUTPUT
POTENTIAL:
THE=33
SKY=55
IS=28
THE=33
LIMIT=63
Sorted sentence
→ALGORITHM
Step-1: Start
Step-2: Create class ConsChange and declare data members
Step-3: Create default constructor to initialize values to data
members
Step-4: Create function readword() to input a word from user and
store in wrd
Step-5: Create function shiftcons() to shift the consonants in front
followed by the variables and store in shiftwrd
Step-6: Create function show() for displaying original word and
shifted word
Step-7: Create main to call the functions to enable the task by
object creation
Step-8: Stop
→CODE
import java.util.*;
class ConsChange
{
String wrd, shiftwrd; int len;
ConsChange()
{
wrd="";
shiftwrd="";
len=0;
}
void readword()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter a word in lower case");
wrd=sc.next().toLowerCase();
len=wrd.length();
}
void shiftcons()
{
int i;
char ch;
String c="",v="";
for(i=0;i<len;i++)
{
ch=wrd.charAt(i);
if("aeiou".indexOf(ch)==-1)
c=c+ch;
else
v=v+ch;
}
shiftwrd=c+v;
}
void show()
{
System.out.println("Original word:"+wrd);
System.out.println("Shifted word:"+shiftwrd);
}
public static void main()
{
ConsChange ob=new ConsChange();
ob.readword();
ob.shiftcons();
ob.show();
}
}
→VARIABLE-DESCRIPTION TABLE
Variable Description Data Type
wrd To store inputted word String
shiftwrd To store shifted word String
len To store length of word int
i Looping variable int
ch To extract character char
c To store all consonants String
v To store all vowels String
→OUTPUT
Believe
Original word: believe
Shifted word: blveiee
Question 25-
The reverse of the word HELP would be LEH (omitting the last
alphabet) and by concatenating both, the new palindrome word is
HELPLEH. Thus, the word HELP becomes HELPLEH. Write a program
to input a sentence and perform the task with each word of the
sentence, then display the new sentence.
Example-
Input: THE BIRD IS FLYING
Output: THEHT BIRDRIB ISI FLYINGNIYLF
→ALGORITHM
Step-1: Start
Step-2: Input a sentence and convert it into upper case
Step-3: Run a loop from i=0 to i<s.length
Step-4: Extract each word from the sentence
Step-5: Run a nested loop from j=0 to j<w.length
Step-6: Store the palindromic word of the original word in
another string
Step-7: Print the sentence
Step-8: Stop
→CODE
import java.util.*;
class reverse
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
String s,w="",w1="";
int i,j;
char ch,ch1;
System.out.println("Enter a sentence in upper case");
s=sc.nextLine().toUpperCase();
for(i=0;i<s.length();i++)
{
ch=s.charAt(i);
if(ch==' ')
{
for(j=0;j<w.length()-1;j++)
{
ch1=w.charAt(j);
w1=ch1+w1;
}
w1=w+w1;
System.out.print(w1+" ");
w="";
w1="";
}
else
w=w+ch;
}
for(j=0;j<w.length()-1;j++)
{
ch1=w.charAt(j);
w1=ch1+w1;
}
w1=w+w1;
System.out.print(w1+" ");
}
}
→VARIABLE-DESCRIPTION TABLE
Variable Description Data Type
s Inputs the string from user String
i Outer loop variable Int
j Inner loop variable int
ch For extracting characters char
w For extracting words String
ch1 For extracting characters char
from extracted words
w1 For changing word into String
palindrome
→OUTPUT
Enter a sentence
THE SKY IS BLUE
THEHT SKYKS ISI BLUEULB
Question 26-
→ALGORITHM
Step-1: Start
Step-2: In the function void input(),
Step-3: Enter a sentence.
Step-4: In the function void process(),
Step-5: Count the number of letters in the sentence i.e. it’s length.
Step-6: Run a loop from length -1 to 0.
Step-7: Extract the letters and add it in the new string rev.
Step-8: Print the reversed string.
Step-9: In the function void find(),
Step-10: Count the number of words using string tokenizer.
Step-11: Display the no. of words and spaces.
Step-12: Make a main function and call all the functions.
Step-13: Stop
→CODE
// A program to display String Operations
Import java.util.*;
Public class StringOP
{
String str, rev;
StringOP()
{
Str =”” ;
Rev =”” ;
}
Void input()
{
Scanner in=new Scanner(System.in);
System.out.println(“Enter a sentence”);
Str =in.nextLine();
}
Void process()
{
Char b;
Int I, p;
P = str .length();
For( I = p – 1; I >= 0 ; i--)
{
B= str .charAt(i);
Rev = rev + b;
}
System.out.println(“The reverse sentence:” + rev);
}
Void find()
{
Int w;
StringTokenizer st=new StringTokenizer(str);
W = st.countTokens();
System.out.println(“Number of words in sentence:” + w);
System.out.println(“Number of spaces in sentence:” + (w-1)); }
}
Public static void main(String args[])
{
StringOP ob=new StringOP();
Ob.input();
Ob.process();
Ob.find();
}
}
→VARIABLE-DESCRIPTION TABLE
Variable Description Data Type
str To store sentence String
rev To store reversed sentence String
b For extracting characters Char
i Looping variable Int
p To store length of sentence int
w To count number of words int
→OUTPUT
Enter a sentence
Design a class Unique, which checks whether a word begins and ends
with a vowel.
Example: APPLE, ARORA etc.
The details of the members of the class are given below.
Class name: Unique
Data members/instance variables:
Word : stores a word
Len: To store the length of the word
Methods/Member functions:
Unique() : Default constructor
Void aceptword(): To accept the word in UPPER CASE
Boolean checkUnique(): Checks and returns ‘true’ if the word starts
and ends with a vowel otherwise returns ‘false’
Void display() : displays the word along with an appropriate message
Specify the class Unique, giving details of the constructor, void
acceptword(), boolean checkUnique() and void display(). Define the
main() function to create an object and call the functions accordingly
to enable the task.
→ALGORITHM
Step-1: Start
Step-2: Declare the global variables.
Step-3: Construct the default constructor and initialize global
variables.
Step-4: Construct a function to input the word and find its length.
Step-5: Construct another function to find first and last character
and checks whether both characters are vowel.
Step-6: Construct the last function which calls the previous
function and displays appropriate message
Step-7: Write main program and call the functions required.
Step-8: Stop
→CODE
import java.util.*;
Class Unique
{
String word;
int Len;
// Default constructor
public Unique()
{
Word = “”;
Len=0;
}
→VARIABLE-DESCRIPTION TABLE
Variable Description Data Type
word To store word String
len Length of word Int
ch1 First character char
ch2 Last character char
→OUTPUT
Enter a word in Upper Case
ARORA
→ALGORITHM
Step-1: Start
Step-2: Design a class name exchange.
Step-3: Declare the instant variable.
Step-4: Create function void readsentence() to accept the
sentence.
Step-5: Create function void exfirstlast() to extract each word and
interchange first and last alphabet of the word and form a
new sentence rev words
Step-6: Create void display() to display reversed string
Step-7: Stop
→CODE
import java.util.Scanner;
public class Exchange
{
String sent,rev;
int size;
Exchange()
{
sent="";
rev="";
size=0;
}
void readsentence()
{
Scanner sc=new Scanner(System.in);
System.out.print("Enter a sentence");
sent=sc.nextLine();
size=sent.length();
}
void exfirstlast()
{
char ch=' ';
String wd="";
for(int i=0;i< size;i++)
{
ch=sent.charAt(i);
if(ch==' '||ch=='.')
{
if(wd.length()>1)
{
/*last character*/
rev=rev + wd.charAt(wd.length()-1);
/*middle characters*/
rev=rev + wd.substring(1,wd.length()-1);
/*first character*/
rev=rev + wd.charAt(0);
/*space or full stop*/
rev=rev+ch;
}
else
{
rev=rev+wd+ch;
}
wd="";
}
else
{
wd=wd+ch;
}
}
}
void display()
{
System.out.println("Input:"+sent);
System.out.println("Output: "+rev);
}
public static void main()
{
Exchange obj1=new Exchange();
obj1.readsentence();
obj1.exfirstlast();
obj1.display();
}
}
→VARIABLE-DESCRIPTION TABLE
Variable Description Data Type
sent Inputs the string from user String
rev To store new string String
size Length of sentence int
i loop variable int
ch For extracting characters char
→OUTPUT
Enter a sentence
THE SKY IS BLUE
THEHT SKYKS ISI BLUEULB
Question 29-
→ALGORITHM
Step-1: Start
Step-2: Define the class TheString.
Step-3: Store the variables for their respective functions.
Step-4: Assign the variables in default constructor.
Step-5: Accept a string.
Step-6: Count the number of words and number of consonants
and store them.
Step-7: Display the original string and the number of consonants.
Step-8: Define the main function for the same.
Step-9: Stop
→CODE
import java.util.*;
TheString
{
String str;
int len, wordcount, cons;
TheString()
{
str=””;
len=0;
wordcount=0;
cons=0;
}
TheString(String ds)
{
str=ds;
}
void count freq()
{
len=str.length();
String Tokenizer st=new StringTokenizer(str);
wordcount= st.countTokens();
for(i=0;i<len;i++)
{
char ch=str.charAt(i);
if(ch!=’A’ && ch!=’E’ && ch!=’I’ && ch!=’O’ && ch!=’U’ && ch!=’a’
&& ch!=’e’ && ch!=’i’ && ch!=’o’ && ch!=’u’)
cons= cons+1;
}
}
void Display()
{
System.out.println(“Original String: “+str);
System.out.println(“Number of consonants: “+cons);
}
public static void main(String s)
{
TheString ob=new TheString();
ob.count freq();
ob.Display();
}
}
→VARIABLE-DESCRIPTION TABLE
Variable Description Data Type
Str To store original string String
Len To find length of string Int
countwords To store number of words int
Cons To count number of int
consonants
Ds To store original string String
i looping variable int
→OUTPUT
“God is great”
Number of consonants: 6
Question 30-
→ALGORITHM
Step-1: Start
Step-2: Create a class by name of Rearrange
Step-3: Input the word in the readword()
Step-4: Find frequency of vowels and consonants in
freq_vow_con()
Step-5: Form the rearranged word in arrange()
Step-6: Display the original and rearranged word in display ()
Step-7: Stop
→CODE
import java.util.*;
public class Rearrange
{
String newwrd,wrd;
Rearrange()
{
wrd="";
newwrd="";
}
void readword()
{
Scanner sc=new Scanner(System.in);
System.out.println("enter a word in uppercase");
wrd=sc.nextLine();
wrd=wrd.toUpperCase();
}
void freq_vow_con()
{
int i,j,v=0;
for( i='A';i<='Z';i++)
{
v=0;
for(j=0;j<wrd.length();j++)
{
if(wrd.charAt(j)==(char)i)
{
v++;
}
}
if(v!=0)
System.out.println("frequency of "+(char)i+" is "+v);
else
continue;
}
}
void arrange()
{
String st="",str="";int i;
for( i=0;i<wrd.length();i++)
{
char ch=wrd.charAt(i);
if(ch=='A'||ch=='E'||ch=='I'||ch=='O'||ch=='U')
st=st+ch;
else
str=str+ch;
}
newwrd=st+str;
}
void display()
{
System.out.println("original word is "+wrd);
System.out.println("word formed is "+newwrd);
}
public static void main(String args[])
{
Rearrange ob=new Rearrange ();
ob.readword();
ob.freq_vow_con();
ob.arrange();
ob.display();
}
}
→VARIABLE-DESCRIPTION TABLE
Variable Description Data Type
Wrd To store word String
Ch To store extracted character char
V To store frequency of each int
vowel and consonant
St To form a string with vowels char
Str To form a string with String
consonants
newwrd To store rearranged word by char
concatenating st and str
→OUTPUT
enter a word in uppercase
ORIGINAL
frequency of A is 1
frequency of G is 1
frequency of I is 2
frequency of L is 1
frequency of N is 1
frequency of O is 1
frequency of R is 1
original word is ORIGINAL
word formed is OIIARGNL