Comp Project
Comp Project
Write a program to accept the range of numbers M and N. Display the Narcissistic
numbers within that range. A narcissistic number is a number that is equal to the
sum of its own digits, each raised to the power of the total number of digits in the
number
ALGORITHM:
STEP 1: Start
STEP 2: Input two integers M and N
STEP 3: For each number i from M to N,do:
Set t=i , d=0;
Count Digits:
While t>0
Increment d
Divide t=t/10
Set t=i , s=0
Find sum of powers:
While t>0
Digits =t%10
s=s+(digit^ d)
s==i
Print i
STEP 4: End loop
STEP 5: Stop
SOURCE CODE:
import [Link].*;
public class Narcissistic
{
public static void main(String[] args)
{
Scanner sc = new Scanner([Link]);
// Accept range
[Link]("Enter M: ");
int M = [Link]();
[Link]("Enter N: ");
int N = [Link]();
[Link]("Narcissistic Numbers:");
for (int i= M; i <= N; i++)
{
int t = i;
int d = 0;
// Count d
while (t > 0)
{
d++;
t /= 10;
}
int s = 0;
t = i;
// Calculate s of d raised to power
while (t > 0)
{
int digit = t % 10;
s+= [Link](digit, d);
t=t/10;
}
// Check condition
if (s == i)
{
[Link](i);
}
}
}
OUTPUT:
[Link] Number
A Smith number is a composite number, whose sum of the digits is equal to the sum
of its prime factors. For example:4, 22, 27, 58, 85, 94, 121 ………. are Smith
numbers. Write a program in Java to enter a number and check whether it is a Smith
number or not.
ALGORITHM:
Step 1: Start
Step 4: Initialize c1 = 0
Step 6: If c1 ≤ 2, then
Print "Not a Smith Number" and go to Step 12
SOURCE CODE:
import [Link].*;
public class smith
{
public static void main(String args[])
{
Scanner sc = new Scanner([Link]);
int num, c1 = 0, c2 = 0, sf = 0, sd = 0, r;
[Link]("Enter a number : ");
num = [Link]();
int temp = num;
// Check composite
for(int i = 1; i <= num; i++)
{
if(num % i == 0)
c1++;
}
if(c1 > 2)
{
int n = num;
for(int i = 2; i <= n; i++)
{
while(n % i == 0)
{
c2 = 0;
for(int j = 1; j <= i; j++)
{
if(i % j == 0)
c2++;
}
if(c2 == 2) // prime factor
{
int tf = i;
while(tf > 0)
{
r = tf % 10;
sf = sf + r;
tf = tf / 10;
}
}
n = n / i;
}
}
// Sum of digits of number
int t = temp;
while(t > 0)
{
r = t % 10;
sd = sd + r;
t = t / 10;
}
if(sd == sf)
[Link](temp + " is a Smith number");
else
[Link](temp + " is not a Smith number");
}
else
{
[Link](num + " is not a Smith Number");
}
}
}
OUTPUT:
[Link] Number
A mystery number is a number that can be expressed as the sum of two numbers
and those two numbers should be the reverse of each other.
ALGORITHM:
STEP 1: Start
STEP 2: Input n
STEP 3: For i = 1 to n
STEP 4: Find reverse of i ,rev
STEP 5: If i + rev == n then print pair, stop
STEP 6: If no pair found then not a mystery number
STEP 7: End
SOURCE CODE:
import [Link].*;
public class MysteryNumber
{
public static void main(String[] args)
{
Scanner sc = new Scanner([Link]);
int n, i, t, rev, digit;
int c = 0;
[Link]("Enter number: ");
n = [Link]();
for (i = 1; i < n; i++)
{
t = i;
rev = 0;
while (t > 0)
{
digit = t % 10;
rev = rev * 10 + digit;
t = t / 10;
}
if (i + rev == n)
{
[Link]("Mystery Number");
c = 1;
break;
}
}
if (c == 0)
[Link]("Not a Mystery Number");
}
}
OUTPUT:
[Link] Number
Design a program to accept a day number (between 1 and 366), year (in 4 digits)
from the user to generate and display the corresponding date. Also, accept 'N' (1 <=
N <= 100) from the user to compute and display the future date corresponding to 'N'
days after the generated date. Display an error message if the value of the day
number, year and N are not within the limit or not according to the condition
specified.
ALGORITHM:
STEP 1: Start
month++
SOURCE CODE:
import [Link].*;
dayNo = [Link]();
year = [Link]();
[Link]("Enter N: ");
N = [Link]();
if (year < 1000 || year > 9999 || dayNo < 1 || dayNo > 366 || N < 1 || N > 100)
[Link]("Invalid Input");
return;
}
days[1] = 29;
int month = 0;
int d = dayNo;
d = d - days[month];
month++;
int date = d;
month = month + 1;
// Add N days
date = date + N;
month = 1;
year++;
days[1] = 29;
else
days[1] = 28;
OUTPUT:
[Link]
Write a program to accept a sentence which may be terminated by either ‘.’, ‘?’ or
‘!’ only. The words may be separated by a single blank space and should be case-
insensitive.
Perform the following tasks: (a) Check if the sentence is a Palindrome Sentence. A
sentence is a Palindrome Sentence if, after removing the spaces and punctuation,
the letters read the same forward and backward. Example: “Never odd or even.” (b)
Display the first-occurring most frequent word in the sentence (in lowercase). If
there is a tie, choose the word that appears first in the sentence and if no words are
repeated then print NONE.
ALGORITHM:
Step 1: Start
Step 2: Declare the required string variables, array, and integer variables
Step 5: If the last character of the sentence is not '.', '!' or '?', then display
"INVALID INPUT" and terminate the program
Step 9: If the character is not a space, add it in reverse order to the string s1
Step 10: If st and s1 are equal ignoring case, display that the sentence is a
palindrome
Step 12: For each character in the sentence, check whether it is a space
Step 14: Else store the word t into the array and reset t to empty
Step 15: For each word in the array, compare it with the remaining words in the
array
Step 16: If two words are equal ignoring case, increase the frequency count
Step 17: If the frequency count is greater than the maximum frequency, store that
word as the most frequent word
SOURCE CODE:
import [Link].*;
public class sentence
{
public static void main(String[] args)
{
// prepare variables for sentence processing
String st=""; // sentence without spaces for palindrome check
String s1=""; // reversed sentence without spaces
String t="";
String a[] = new String[100]; // array to store words
String maxword=""; // most frequent word output
int max=0,f=0,k=0; //max freq, current freq, number of words
Step 1: Start
Step 9: If ch is A or a, add 1 to s
Step 16: Continue the same process for all remaining alphabet groups
Step 17: Repeat the steps until all characters are checked
SOURCE CODE:
import [Link].*;
int s=0;
[Link]("Enter a word");
char ch = [Link](i);
if(ch=='A'||ch=='a')
s=s+1;
else if(ch=='B'||ch=='b')
s=s+2;
else
s=s+3;
if(ch=='D'||ch=='d')
s=s+1;
else if(ch=='E'||ch=='e')
s=s+2;
else
s=s+3;
}
if(ch=='G' || ch=='H' || ch=='I' || ch=='g' || ch=='h' || ch=='i')
if(ch=='G'||ch=='g')
s=s+1;
else if(ch=='H'||ch=='h')
s=s+2;
else
s=s+3;
if(ch=='J'||ch=='j')
s=s+1;
s=s+2;
else
s=s+3;
if(ch=='M'||ch=='m')
s=s+1;
else if(ch=='N'||ch=='n')
s=s+2;
else
s=s+3;
if(ch=='P'||ch=='p')
s=s+1;
s=s+2;
else if(ch=='R'||ch=='r')
s=s+3;
else
s=s+4;
if(ch=='T'||ch=='t')
s=s+1;
else if(ch=='U'||ch=='u')
s=s+2;
else
s=s+3;
if(ch=='W'||ch=='w')
s=s+1;
else if(ch=='X'||ch=='x')
s=s+2;
else if(ch=='Y'||ch=='y')
s=s+3;
else
s=s+4;
}}
OUTPUT:
[Link] IN STRING
Write a program to accept a sentence which may be terminated by either ‘.’ or ‘?’ or
‘!’ only. Any other character may be ignored. The words may be separated by more
than one blank space and are in uppercase.
Perform the following tasks: (a) Accept a sentence and remove all the extra blank
space between two words to a single blank space. (b) Accept any word from the
user along with its position and insert the word in the given position. The position is
calculated by place value of each word where first word is in
5|Page
position 1, second word in position 2 and so on. (c) Display the modified sentence.
ALGORITHM:
Step 1: Start
Step 5: If the last character of the sentence is not '.', '!' or '?', display "INVALID
INPUT" and terminate the program
Step 10: For each character in the sentence, check whether the character is a space
SOURCE CODE:
import [Link].*;
[Link]("Enter a Sentence:");
String s = [Link]();
int k = 0;
[Link]("INVALID INPUT.");
[Link](0);
// Remove extra spaces so that only single spaces remain between words
String w = [Link]();
int p = [Link]();
k++;
if(k == p-1)
{
st = [Link](0, i + 1) + w + " " + [Link](i + 1);
break;
OUTPUT:
[Link] WORDS
Write a program to accept a sentence which may be terminated by either '.', '?' or '!'
[Link] words may be separated by more than one blank space and are in UPPER
CASE.
2. Place the words which begin and end with a consonant at the beginning, followed
by the remaining words as they occur in the sentence.
ALGORITHM:
Consonants Algorithm
Step 1: Start
Step 2: Declare the required string and integer variables
Step 3: Create a Scanner object
Step 4: Accept a sentence from the user
Step 5: Remove extra spaces and convert the sentence into uppercase
Step 6: If the sentence does not end with '.', '!' or '?', display "INVALID INPUT"
and terminate the program
Step 7: For each character in the sentence, check whether it is a space
Step 8: If the character is not a space, add it to string s1
Step 9: If a space or the end of sentence is reached, process the word stored in s1
Step 10: If the word contains only one letter, check whether it is a vowel or
consonant
Step 11: If the word begins and ends with consonants, increase the count and add it
to string sc
Step 12: Else add the word to string sv
Step 13: Reset s1 to empty after processing each word
Step 14: Repeat the process until all words are checked
Step 15: Combine consonant words and remaining words into string sf
Step 16: Display the total number of consonant words
Step 17: Display the rearranged sentence
Step 18: Stop
SOURCE CODE:
import [Link].*;
String s1="",sc="",sv="",sf="";
[Link]("Enter a Sentence:");
String s = [Link]();
ss=[Link]();
int count = 0;
{
[Link]("INVALID INPUT.");
[Link](0);
if([Link](i)!=' ')
s1=s1+[Link](i);
if(i<[Link]()-2)
continue;
if([Link]()==1)
if("AEIOU".indexOf([Link](0))==-1)
count++;
sc=sc+s1+" ";
s1="";
continue;
else
sv=sv+s1+" ";
s1="";
continue;
count++;
sc=sc+s1+" ";
s1="";
else
sv=sv+s1+" ";
s1="";
}
sf=sc+sv;
OUTPUT:
[Link] TO NUMBERS
Write a program in java to accept a string s representing a Roman numeral, find it's
corresponding integer value. Roman numerals are formed using the following
symbols: I = 1, V = 5, X = 10, L = 50, C = 100, D = 500, and M = 1000. Numbers
are typically formed by combining these symbols from left to right, adding or
subtracting their values based on specific rules.
ALGORITHM:
Step 1: Start
Step 2: Declare the required integer variables
Step 3: Create a Scanner object
Step 4: Accept a Roman numeral from the user
Step 5: Initialize total to 0
Step 6: For each character in the Roman numeral, determine its integer value
Step 7: Store the value of the current Roman symbol in n1
Step 8: If the next symbol exists, determine its integer value and store it in n2
Step 9: If n1 < n2, perform subtraction and add (n2 - n1) to total
Step 10: Increase the loop counter to skip the next symbol after subtraction
Step 11: Else add n1 to total
Step 12: If no next symbol exists, add n1 to total
Step 13: Repeat the process until all symbols are processed
Step 14: Display the integer value
Step 15: Stop
SOURCE CODE:
import [Link].*;
String s = [Link]();
int total = 0, n1 = 0, n2 = 0;
char ch = [Link](i);
if(ch == 'I')
n1 = 1;
n1 = 5;
n1 = 10;
n1 = 50;
n1 = 500;
n1 = 1000;
if(ch2 == 'I')
n2 = 1;
n2 = 5;
n2 = 10;
n2 = 50;
n2 = 100;
else if(ch2 == 'D')
n2 = 500;
n2 = 1000;
// Subtraction case
i++;
else
else
}
[Link]("Integer Value = " + total);
OUTPUT:
[Link] ADDITION
Given two binary strings s1 and s2, the task is to return their sum. The input strings
may contain leading zeros but the output string should not have any leading zeros.
ALGORITHM:
Step 1: Start
Step 2: Declare the required string and integer variables
Step 3: Create a Scanner object
Step 4: Accept the first binary number from the user
Step 5: Accept the second binary number from the user
Step 6: Initialize i and j to the last index positions of both strings
Step 7: Initialize carry to 0 and ans to an empty string
Step 8: While i >= 0 or j >= 0 or carry == 1, repeat the process
Step 9: Store the value of carry in variable sum
Step 10: If i >= 0 and the current bit of first binary number is 1, add 1 to sum
Step 11: Decrease the value of i by 1
Step 12: If j >= 0 and the current bit of second binary number is 1, add 1 to sum
Step 13: Decrease the value of j by 1
Step 14: If sum % 2 == 0, add 0 at the beginning of ans
Step 15: Else add 1 at the beginning of ans
Step 16: Store sum / 2 as the new carry
Step 17: Repeat the process until all bits are added
Step 18: Remove leading zeros from the result
Step 19: Display the binary sum
Step 20: Stop
SOURCE CODE:
import [Link].*;
String s1 = [Link]();
String s2 = [Link]();
int i = [Link]() - 1;
int j = [Link]() - 1;
int carry = 0;
{
int sum = carry;
if(i >= 0)
if([Link](i) == '1')
sum = sum + 1;
i--;
if(j >= 0)
if([Link](j) == '1')
sum = sum + 1;
j--;
if(sum % 2 == 0)
else
carry = sum / 2;
}
int p = 0;
if([Link](k) == '1')
p = k;
}
OUTPUT:
[Link] TO WORD
Write a java program to accept a number and convert the given number into words.
The number should not be a negative number, if so, give an error message.
ALGORITHM:
Step 1: Start
Step 2: Declare the required arrays and integer variables
Step 3: Create a Scanner object
Step 4: Accept a number from the user
Step 5: If the number is negative, display "INVALID INPUT" and terminate the
program
Step 6: Find the highest place value of the number
Step 7: If the number is in crores, extract the digit and display its word form
Step 8: Reduce the number and update the divisor value
Step 9: If the number is in lakhs, extract the digit and display its word form
Step 10: Reduce the number and update the divisor value
Step 11: If the number is in thousands, extract the digit and display its word form
Step 12: Reduce the number and update the divisor value
Step 13: If the number is in hundreds, extract the digit and display its word form
Step 14: If the number is between 20 and 99, display the tens and units word form
Step 15: If the number is between 10 and 19, display the teen word form
Step 16: Else display the unit word form
Step 17: Stop
SOURCE CODE:
import [Link].*;
{
Scanner sc = new Scanner([Link]);
[Link]("Enter a number:");
String unit[] = {"", "One", "Two", "Three", "Four", "Five", "Six", "Seven",
"Eight", "Nine"};
int n=num,d=1,r=0;
while(n>0)
n=n/10;
d=d*10;
d=d/10;
if(num<0)
[Link]("INVALID INPUT");
[Link](0);
else
{
if(num>=100000000 && num<=999999999)
r=num/d;
num=num%d;
d=d/10;
[Link](ten[r]+" ");
r=num/d;
num=num%d;
d=d/10;
r=num/d;
num=num%d;
d=d/10;
r=num/d;
num=num%d;
d=d/10;
r=num/d;
num=num%d;
d=d/10;
[Link](ten[r]+" ");
r=num/d;
num=num%d;
d=d/10;
{
r=num/d;
num=num%d;
d=d/10;
r=num/d;
num=num%d;
d=d/10;
[Link](ten[r]+" "+unit[num%10]);
[Link](0);
r=num/d;
num=num%d;
[Link](teen[r]);
else
[Link](unit[num]);
}
OUTPUT:
[Link] ROTATION
Write a program to accept two strings s1 and s2 of equal length, determine whether
s2 is a rotation of s1.A string is said to be a rotation of another if it can be obtained
by shifting some leading characters of the original string to its end without changing
the order of characters.
ALGORITHM:
Step 1: Start
Step 2: Declare the required string variables
Step 3: Create a Scanner object
Step 4: Accept the first string from the user
Step 5: Accept the second string from the user
Step 6: If the lengths of both strings are not equal, display that they are not
rotations and terminate the program
Step 7: Initialize temporary strings for left and right rotations
Step 8: For each position in the string, perform rotation operations
Step 9: Create the right rotated string by moving the last character to the front
Step 10: Create the left rotated string by moving the first character to the end
Step 11: If the right rotated string becomes equal to the second string, display the
number of right rotations
Step 12: Terminate the program after displaying the result
Step 13: If the left rotated string becomes equal to the second string, display the
number of left rotations
Step 14: Terminate the program after displaying the result
Step 15: Update the strings for the next rotation
Step 16: Repeat the process until all rotations are checked
Step 17: If no match is found, display that the strings are not rotations of each other
Step 18: Stop
SOURCE CODE:
import [Link].*;
String s1 = [Link]();
String s2 = [Link]();
if([Link]() != [Link]())
[Link](0);
else
{
// Initialize variables for rotation
String a=s1,b=s1;
String t1 = "",t2="";
t1=[Link]([Link]()-1)+[Link](0, [Link]()-1);
t2=[Link](1, [Link]())+[Link](0);
if([Link](s2))
[Link](0);
if([Link](s2))
a=t1;
b=t2;
OUTPUT:
[Link] ROTATION
Write a program to declare a matrix A[][] of order (M × N) where ‘M’ is the
number of rows and ‘N’ is the number of columns such that both M and N must be
greater than 2 and less than 10. Allow the user to input integers into this matrix.
Display appropriate error message for an invalid input.
Perform the following tasks on the matrix: (a) Display the input matrix. (b) Shift
each row one step upwards so the first row becomes the last row, 2nd row will be
the 1st row and so on. (c) Display the rotated matrix along with the highest element
and its location in the matrix.
ALGORITHM:
STEP 1: Start
STEP 2: Declare variables M, N, i, j, m, n, max, x and y
STEP 3: Input number of rows M and columns N
STEP 4: Check whether M and N are greater than 2 and less than 10
STEP 5: If condition is false, display “INVALID INPUT” and go to Step 20
STEP 6: Create matrices a1[M][N] and a2[M][N]
STEP 7: Initialize m = 1 and n = 0
STEP 8: Display message to enter matrix elements
STEP 9: Read elements into matrix a1[][]
STEP 10: Repeat for i = 0 to M-1
STEP 11: Repeat for j = 0 to N-1
STEP 12: Copy a1[m][n] into a2[i][j]
STEP 13: Increment n
STEP 14: After inner loop set n = 0
STEP 15: Update m value for rotation
STEP 16: Display rotated matrix a2[][]
STEP 17: Find the maximum element and its position
STEP 18: Display maximum element and its position
STEP 19: End of if condition
STEP 20: Stop
SOURCE CODE:
import [Link].*;
int M = [Link]();
int N = [Link]();
int i, j;
a1[i][j] = [Link]();
[Link]();
a2[i][j]=a1[m][n];
n++;
n=0;
if(m==M-1)
m=0;
else
m++;
int max=0,x=0,y=0;
[Link](a2[i][j]+" ");
if(a2[i][j]>max)
max=a2[i][j];
x=i+1;
y=j+1;
[Link]();
else
// Invalid dimensions
[Link]("INVALID INPUT");
}
OUTPUT:
[Link]
Write a java program to accept the order of square matrix. Accept the array
elements and fill the elements in the following manner. Display the resultant matrix.
ALGORITHM:
STEP 1: Start
STEP 2: Declare variables n, i, j, k and arrays a[][] and a1[]
STEP 3: Input the size of the square matrix n
STEP 4: Create matrix a[n][n] and array a1[nn]
STEP 5: Display message to enter array elements
STEP 6: Read nn elements into array a1[]
STEP 7: Display the entered array elements
STEP 8: Initialize k = 0
STEP 9: Repeat for i = 0 to n-1
STEP 10: Check if i is even
STEP 11: If even, repeat for j = 0 to n-1
STEP 12: Store a1[k] into a[i][j] and increment k
STEP 13: Else repeat for j = n-1 to 0
STEP 14: Store a1[k] into a[i][j] and increment k
STEP 15: End of if condition
STEP 16: End of outer loop
STEP 17: Display the matrix in zigzag form
STEP 18: Stop
SOURCE CODE:
import [Link].*;
{
Scanner sc=new Scanner([Link]);
int n=[Link]();
int i,j;
for(i=0;i<n*n;i++)
a1[i]=[Link]();
for(i=0;i<n*n;i++)
[Link](a1[i]+" ");
[Link]();
int k=0;
for(i=0;i<n;i++)
if(i%2==0)
for(j=0;j<n;j++)
a[i][j]=a1[k];
k++;
else
for(j=n-1;j>=0;j--)
a[i][j]=a1[k];
k++;
for(i=0;i<n;i++)
for(j=0;j<n;j++)
{
[Link](a[i][j]+" ");
[Link]();
OUTPUT:
[Link] SQUARE
A wondrous square is an n by n grid which fulfils the following conditions: 1. It
contains integers from 1 to n2, where each integer appears only once. 2. The sum of
integers in any row or column must add up to 0.5 x n x (n2 + 1). For example, the
following grid is a wondrous square where the sum of each row or column is 65
when n=5.
17 24 1 8 15
23 5 7 14 16
4 6 13 20 22
10 12 19 21 3
11 18 25 2 9
Write a program to read n (2 <= n <= 10) and the values stored in these n by n cells
and output if the grid represents a wondrous square. Also output all the prime
numbers in the grid along with their row index and column index as shown in the
output. A natural number is said to be prime if it has exactly two divisors. For
example, 2, 3, 5, 7, 11 The first element of the given grid i.e. 17 is stored at row
index 0 and column index 0 and the next element in the row i.e. 24 is stored at row
index 0 and column index 1.
ALGORITHM:
STEP 1: Start
STEP 2: Declare variables n, i, j, rowSum, colSum and valid
STEP 3: Input the size of the square matrix n
STEP 4: Create matrix a[n][n]
STEP 5: Display message to enter matrix elements
STEP 6: Read elements into matrix a[][]
STEP 7: Calculate magicSum = n × (n² + 1) / 2
STEP 8: Initialize valid = 1
STEP 9: Find the sum of each row
STEP 10: Compare row sum with magicSum
STEP 11: If unequal, set valid = 0
STEP 12: Find the sum of each column
STEP 13: Compare column sum with magicSum
STEP 14: If unequal, set valid = 0
STEP 15: Check whether numbers from 1 to n² appear only once
STEP 16: If any number repeats or is invalid, set valid = 0
STEP 17: If valid = 1 display “Wondrous Square”
STEP 18: Else display “Not a Wondrous Square”
STEP 19: Find and display all prime numbers with row and column index
STEP 20: Stop
SOURCE CODE:
import [Link];
int i, j;
[Link]("Enter the elements of the matrix:");
// Reading matrix
for(i = 0; i < n; i++)
{
for(j = 0; j < n; j++)
{
a[i][j] = [Link]();
}
}
int valid = 1;
int rowSum = 0;
if(rowSum != magicSum)
{
valid = 0;
}
}
int colSum = 0;
if(count[value] > 1)
{
valid = 0;
}
}
}
}
// Output result
if(valid == 1)
{
[Link]("Wondrous Square");
}
else
{
[Link]("Not a Wondrous Square");
}
int factors = 0;
int k;
if(factors == 2)
{
[Link](num + " Row=" + i + " Column=" + j);
}
}
}
}
}
OUTPUT:
[Link]
Write a program to declare a matrix a[][] of order (m × n) where 'm' is the number
of rows and 'n' is the number of columns such that the values of both 'm' and 'n'
must be greater than 2 and less than 10. Allow the user to input integers into this
matrix. Perform the following tasks on the matrix: 1. Display the original matrix. 2.
Sort each column of the matrix in ascending order using Selection sort technique. 3.
Display the changed matrix after sorting each column.
ALGORITHM:
STEP 1: Start
STEP 2: Declare variables m, n, i, j, k, min and temp
STEP 3: Input number of rows m and columns n
STEP 4: Check whether m and n are greater than 2 and less than 10
STEP 5: If condition is false, display “Invalid Input” and go to Step 19
STEP 6: Create matrix a[m][n]
STEP 7: Display message to enter matrix elements
STEP 8: Read elements into matrix a[][]
STEP 9: Display the original matrix
STEP 10: Repeat for each column j = 0 to n-1
STEP 11: Repeat for each row i = 0 to m-2
STEP 12: Assume min = i
STEP 13: Repeat for k = i+1 to m-1
STEP 14: Compare a[k][j] with a[min][j]
STEP 15: Update min if smaller element is found
STEP 16: Swap a[i][j] and a[min][j]
STEP 17: Repeat until all columns are sorted
STEP 18: Display the sorted matrix
STEP 19: Stop
SOURCE CODE:
import [Link];
int m, n;
m = [Link]();
n = [Link]();
// Check validity
[Link]("Invalid Input");
}
else
a[i][j] = [Link]();
[Link]("Original Matrix:");
[Link](a[i][j] + "\t");
[Link]();
int min = i;
min = k;
// Swap elements
int temp = a[i][j];
a[i][j] = a[min][j];
a[min][j] = temp;
[Link](a[i][j] + "\t");
[Link]();
}
OUTPUT:
[Link]-CLOCKWISE INTIALIZATION
A square matrix is the matrix in which the number of rows is equal to the number of
columns. Thus, a matrix of order n* n is called as Square matrix. Write a program
in java to fill the numbers in a circular fashion (anti-clockwise) with natural
numbers from 1 to n2, taking n as an input.
14 | P a g e
21 20 19 18 17
22 7 6 5 16
23 8 1 4 15
24 9 2 3 14
25 10 11 12 13
ALGORITHM:
STEP 1: Start
STEP 2: Declare variables n, r, c, num and step
STEP 3: Input the size of the matrix n
STEP 4: Create matrix a[n][n]
STEP 5: Set r = n/2 and c = n/2
STEP 6: Store 1 at the center position a[r][c]
STEP 7: Initialize num = 2 and step = 1
STEP 8: Repeat while num ≤ n*n
STEP 9: Move downward step times and store numbers
STEP 10: Increment num after each insertion
STEP 11: Move right step times and store numbers
STEP 12: Increment step by 1
STEP 13: Move upward step times and store numbers
STEP 14: Increment num after each insertion
STEP 15: Move left step times and store numbers
STEP 16: Increment step by 1
STEP 17: Repeat until all numbers are filled
STEP 18: Display the circular matrix
STEP 19: Stop
SOURCE CODE:
import [Link];
class CircularMatrix
[Link]("Enter n: ");
int n = [Link]();
int r = n / 2;
int c = n / 2;
a[r][c] = 1;
int num = 2;
int step = 1;
while (num <= n * n)
r++;
a[r][c] = num++;
c++;
a[r][c] = num++;
r--;
a[r][c] = num++;
c--;
a[r][c] = num++;
[Link]();
OUTPUT:
[Link]-COLUMN SUM
Write a program to create a double dimensional array of size n x m. Input the
numbers in first (n-1) x (m-1) cells. Find and place the sum of each row and each
column in corresponding cells of last column and last row respectively. Finally,
display the array elements along with the sum of rows and columns.
ALGORITHM:
STEP 1: Start
STEP 2: Declare variables n, m, i, j, rowSum and colSum
STEP 3: Input number of rows n
STEP 4: Input number of columns m
STEP 5: Create matrix arr[n][m]
STEP 6: Display message to enter matrix elements
STEP 7: Read elements in first (n−1) × (m−1) positions
STEP 8: Repeat for each row i from 0 to n−2
STEP 9: Initialize rowSum = 0
STEP 10: Add all elements of the row
STEP 11: Store rowSum in last column of the row
STEP 12: Repeat for each column j from 0 to m−2
STEP 13: Initialize colSum = 0
STEP 14: Add all elements of the column
STEP 15: Store colSum in last row of the column
STEP 16: Display heading for final matrix
STEP 17: Print matrix with row sums and column sums
STEP 18: Leave bottom-right corner blank
STEP 19: End of program
STEP 20: Stop
SOURCE CODE:
import [Link];
int n = [Link]();
int m = [Link]();
arr[i][j] = [Link]();
}
int rowSum = 0;
rowSum += arr[i][j];
arr[i][m - 1] = rowSum;
int colSum = 0;
colSum += arr[i][j];
}
arr[n - 1][j] = colSum;
if (i == n - 1 && j == m - 1)
else
[Link](arr[i][j] + "\t");
[Link]();
}
OUTPUT:
[Link] FREQUENCY
Input a paragraph containing 'n' number of sentences where (1 < = n < 4). The
words are to be separated with a single blank space and are in UPPERCASE. A
sentence may be terminated either with a full stop '.' Or a question mark '?' only.
Any other character may be ignored. Perform the following operations:
Accept the number of sentences. If the number of sentences exceeds the limit, an
appropriate error message must be displayed.
Display the words in ascending order of their frequency. Words with same
frequency may appear in any order.
ALGORITHM:
STEP 1: Start
STEP 2: Declare variables n, i, j, k, c and strings p, s and word
STEP 3: Input number of sentences n
STEP 4: Check whether n is between 1 and 4
STEP 5: If invalid, display error message and stop
STEP 6: Initialize empty string p
STEP 7: Input all sentences and combine them into p
STEP 8: Remove full stop and question mark characters
STEP 9: Count total number of words manually
STEP 10: Display total number of words
STEP 11: Create string array w[] to store words
STEP 12: Extract words one by one without using split()
STEP 13: Store extracted words into array w[]
STEP 14: Create frequency array freq[]
STEP 15: Compare words and count frequency of each word
STEP 16: Mark repeated words with −1
STEP 17: Sort words according to frequency
STEP 18: Display heading “Word Frequency”
STEP 19: Print each word with its frequency
STEP 20: Stop
SOURCE CODE:
import [Link];
int n, i, j, k = 0, c = 0;
n = [Link]();
[Link]();
return;
}
String p = "", s;
[Link]("Enter sentences:");
// Input sentences
s = [Link]();
p = p + " " + s;
// Remove . and ?
char ch = [Link](i);
}
}
p = temp;
int words = 0;
words++;
words++;
char ch = [Link](i);
else
if([Link]() > 0)
w[k] = word;
k++;
word = "";
}
// Last word
if([Link]() > 0)
w[k] = word;
// Find frequency
c = 1;
if(freq[i] != -1)
if(w[i].equals(w[j]))
c++;
freq[j] = -1;
}
freq[i] = c;
int t = freq[i];
freq[i] = freq[j];
freq[j] = t;
String x = w[i];
w[i] = w[j];
w[j] = x;
}
}
// Display result
[Link]("\nWord\tFrequency");
if(freq[i] != -1)
}
OUTPUT:
[Link] MARKOV
Write a program to declare a square matrix M[][] of order ‘N’. Check if the matrix
is a Doubly Markov matrix or not. A matrix which satisfies the following
conditions is Doubly Markov Matrix: (i) All elements are >= 0 (ii) Sum of each row
= 1 (iii) Sum of each column = 1 Accept ‘N’ from the user where 3 <= N <= 9.
Display an appropriate error message if ‘N’ is not in the given range or the entered
numbers are negative. Allow the user to create a matrix and check whether the
created matrix is a Doubly Markov Matrix or [Link] your program for the
following data and some random data:
ALGORITHM:
STEP 1: Start
STEP 2: Declare variables n, i, j, sum and flag f
STEP 3: Input the size of the matrix n
STEP 4: Check whether n is between 3 and 9
STEP 5: If invalid, display “INVALID INPUT”
STEP 6: Else create matrix m[n][n]
STEP 7: Initialize flag f = 0
STEP 8: Display message to enter matrix elements
STEP 9: Read elements into matrix m[][]
STEP 10: Check whether any element is negative
STEP 11: If negative element found, set f = 1
STEP 12: Display the formed matrix
STEP 13: Find sum of each row
STEP 14: If row sum is not equal to 1, set f = 1
STEP 15: Find sum of each column
STEP 16: If column sum is not equal to 1, set f = 1
STEP 17: Check value of flag f
STEP 18: If f = 0 display “IT IS A DOUBLY MARKOV MATRIX”
STEP 19: Else display “IT IS NOT A DOUBLY MARKOV MATRIX”
STEP 20: Stop
SOURCE CODE:
import [Link].*;
class DoublyMarkov
[Link]("Enter N");
int n = [Link]();
[Link]("INVALID INPUT");
else
int f = 0;
[Link]("Enter elements in the matrix");
// Input matrix
m[i][j] = [Link]();
if(m[i][j] < 0)
f = 1;
// Display matrix
[Link]("FORMED MATRIX");
[Link](m[i][j] + "\t");
[Link]();
double sum = 0;
if(sum != 1)
f = 1;
}
// Check column sum
double sum = 0;
if(sum != 1)
f = 1;
// Final result
if(f == 0)
else
{
OUTPUT:
[Link] SUBTRACTION
A class Matrix contains a two-dimensional integer array of order [m × n]. The
maximum value possible for both ‘m’ and ‘n’ is 25.
Design a class Matrix to find the difference of the two matrices. The details of the
members of the class are given below:
Member functions: Matrix(int mm, int nn) : to initialize the size of the matrix m =
mm and n = nn void fillArray( ) : to enter the elements of the matrix Matrix
subMat(Matrix a) : subtract the current object from the matrix of
parameterized object and return the resulting object void display( ) : display the
matrix elements
Specify the class Matrix giving details of the constructor(int, int), void fillArray(),
Matrix subMat(Matrix) and void display(). Define the main() function to create
objects and call the methods accordingly to enable the task.
ALGORITHM:
STEP 1: Start
STEP 2: Declare variables r, c, i and j
STEP 3: Input number of rows r
STEP 4: Input number of columns c
STEP 5: Check whether r and c are less than or equal to 25
STEP 6: If invalid, display error message and stop
STEP 7: Create matrices A[r][c], B[r][c] and C[r][c]
STEP 8: Display message to enter first matrix
STEP 9: Read elements into matrix A[][]
STEP 10: Display message to enter second matrix
STEP 11: Read elements into matrix B[][]
STEP 12: Repeat for i = 0 to r-1
STEP 13: Repeat for j = 0 to c-1
STEP 14: Subtract corresponding elements of matrices
STEP 15: Store result in matrix C[][]
STEP 16: End inner and outer loops
STEP 17: Display the resultant matrix
STEP 18: Print all elements of matrix C[][]
STEP 19: End of program
STEP 20: Stop
SOURCE CODE:
class Matrix
int arr[][];
int m, n;
// Constructor
m = mm;
n = nn;
void fillArray()
{
arr[i][j] = [Link]();
// Subtract matrices
Matrix subMat(Matrix a)
return res;
// Display matrix
void display()
[Link](arr[i][j] + "\t");
[Link]();
}
// Main method
int r, c;
r = [Link]();
c = [Link]();
return;
[Link]();
[Link]();
Matrix C = [Link](B);
[Link]("Resultant Matrix:");
[Link]();
}
OUTPUT:
[Link] INTEGER
Given two positive numbers M and N, such that M is between 100 and 10000 and N
is less than 100. Find the smallest integer that is greater than M and whose digits
add up to N. For example, if M = 100 and N = 11, then the smallest integer greater
than 100 whose digits add up to 11 is [Link] a program to accept the numbers M
and N from the user and print the smallest required number whose sum of all its
digits is equal to N. Also, print the total number of digits present in the required
number. The program should check for the validity of the inputs and display an
appropriate message for an invalid input. For sum of digits and counting the
numbers of digits use recursive technique.
ALGORITHM:
STEP 1: Start
STEP 2: Declare variables M, N, num, sum, temp and count
STEP 3: Input value of M
STEP 4: Input value of N
STEP 5: Check whether M is between 100 and 10000
STEP 6: Check whether N is between 1 and 99
STEP 7: If invalid, display “Invalid Input” and stop
STEP 8: Initialize num = M + 1
STEP 9: Repeat indefinitely
STEP 10: Store num in temp and initialize sum = 0
STEP 11: Find sum of digits of temp
STEP 12: Add each digit to sum
STEP 13: Compare sum with N
STEP 14: If sum equals N, stop loop
STEP 15: Else increment num by 1
STEP 16: Count number of digits in num
STEP 17: Store total digits in count
STEP 18: Display the required number
STEP 19: Display total number of digits
STEP 20: Stop
SOURCE CODE:
import [Link];
class SmallestInt
int M, N;
M = [Link]();
N = [Link]();
// Checking validity
[Link]("Invalid Input");
return;
}
int num = M + 1;
while(true)
int sum = 0;
while(temp > 0)
sum = sum + d;
if(sum == N)
break;
}
num++;
int count = 0;
while(temp2 > 0)
count++;
}
OUTPUT:
[Link](QUEUE)
A linear data structure enables the user to add address from rear end and remove
address from front. Define a class Diary with the following details: Class name :
Diary Data members/instance variables: Q[ ] : array to store the addresses size :
stores the maximum capacity of the array start : to point the index of the front end
end : to point the index of the rear end Member functions: Diary(int max) :
constructor to initialize the data member size = max, start = 0 and end = 0 void
pushadd(String n) : to add address in the diary from the rear end if possible,
otherwise display the message “NO SPACE” String popadd( ) : removes and
returns the address from the front end of the diary if any, else returns “?????” void
show( ) : displays all the addresses in the diary (a) Specify the class Diary giving
details of the functions void pushadd(String) and String popadd(). Assume that the
other functions have been defined. Create a main function and call all the functions
accordingly.
ALGORITHM:
STEP 1: Start
STEP 2: Declare array Q[] and variables size, start and end
STEP 3: Input the maximum size of the diary
STEP 4: Create a Diary object with given size
STEP 5: Initialize start = 0 and end = 0
STEP 6: Display menu options for user choices
STEP 7: Input the user’s choice
STEP 8: If choice is 1, input address from user
STEP 9: Check whether end is equal to size
STEP 10: If true, display “NO SPACE”
STEP 11: Else insert address at Q[end]
STEP 12: Increment end by 1
STEP 13: If choice is 2, check whether start equals end
STEP 14: If true, display “Diary is Empty”
STEP 15: Else remove address from Q[start]
STEP 16: Increment start by 1
STEP 17: Display removed address
STEP 18: If choice is 3, display all addresses from start to end
STEP 19: Repeat steps until choice becomes 4
STEP 20: Stop
SOURCE CODE:
import [Link];
class Diary
String Q[];
int size;
int start;
int end;
// Constructor
Diary(int max)
size = max;
start = 0;
end = 0;
Q = new String[size];
}
// Add address from rear end
void pushadd(String n)
if(end == size)
[Link]("NO SPACE");
else
Q[end] = n;
end++;
String popadd()
if(start == end)
return "?????";
}
else
start++;
return val;
void show()
if(start == end)
[Link]("Diary is Empty");
else
[Link]("Addresses in Diary:");
[Link](Q[i]);
}
// Main method
int n = [Link]();
[Link]();
int ch;
String add;
do
[Link]("4. EXIT");
[Link]("Enter choice:");
ch = [Link]();
[Link]();
switch(ch)
case 1:
[Link]("Enter Address:");
add = [Link]();
[Link](add);
break;
case 2:
add = [Link]();
if([Link]("?????"))
[Link]("Diary is Empty");
}
else
[Link]("Removed Address:");
[Link](add);
break;
case 3:
[Link]();
break;
case 4:
[Link]("Program Ended");
break;
default:
[Link]("Invalid Choice");
} while(ch != 4);
}
}
OUTPUT: