Krishna Singh Assignments
Krishna Singh Assignments
HAPPY NUMBER
A Happy number is a number in which the eventual sum of the square of the digits of
the number is equal to 1.
Example:
28= 2^2+8^8=4+64=68
68=6^2+8^2=36+64=100
100=1^2+0^2+0^2=1+0+0=1
Hence, 28 is a Happy Number.
Algorithm:
Step 1: Start
Step 2: Accept a number n.
Step 3: Declare and initialize a=n, s=0.
Step 4: Repeat the Steps 5 to 7 until a<=9.
Step 5: Calculate the sum of digits of number a.
Step 6: Set a=s.
Step 7: Set s=0.
Step 8: Check whether a=1 or not. If true then go to Step 9 else go to Step 10.
Step 9: Print n,” is a Happy Number” and go to Step 10.
Step 10: Print n,” is not a Happy Number”.
Step 11: Stop
Program:
import java.util.*;
class Happy_Number
{
public void isHappy()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter a Number:");
int n=sc.nextInt();
int a=n,s=0,d=0;
while (a>9)
{
while (a>0)//claculating sum of digits of a
{
d=a%10;
s=s+(d*d);//sum of square of digits
a=a/10;
}
a=s;
s=0;
}
if(a==1)
System.out.println(n+" is a Happy Number.");
else
System.out.println(n+" is not a Happy Number.");
}
}
Variable Description:
Variable Data Description
Type
N int Number input by the user
A Int Duplicate variable of a
D Int Right most digit of a
S int Sum of digits of a
Output:
Assignment 2:
Fascinating Number
Algorithm:
Step 1: Start.
Step 2: Accept a number n.
Step 3: Multiply a=n*2.
Step 4: Multiply b=n*3.
Step 5: Concatenate the number n, a and b.
Step 6: Set f=0, i=1.
Step 7: Repeat the steps 8 to 10 until i>9.
Step 8: Calculate the frequency(freq) of the digit i.
Step 9: Check whether freq>1 or freq=0 or not.
Step 10: Set f=1 and go to Step 13.
Step 11: Set freq=0.
Step 12: Set num= num1, i=i+1.
Step 13: Check whether f=0 or not. If true then go to Step 14 else go to Step 15.
Step 14: Print n,” is a Fascinating Number.”
Step 15: Print n,” is not a Fascinating Number.”
Step 16: Stop.
Program:
import java.util.*;
class fascinating
{
public void check()
{
Scanner sc= new Scanner(System.in);
System.out.println("Enter a number:");
int n= sc.nextInt();
if(n<100)
{
System.out.println("Invalid Input.");
}
else
{
int a=n*2;
int b=n*3;
//concatinating the number
String con= String.valueOf(n)+String.valueOf(a)+String.valueOf(b);
long num= Long.parseLong(con);//converting into long
System.out.println("Cocatenated Number "+num);
int f=0;
int freq=0;
long d=0,num1=num;
for(int i=0;i<=9;i++)
{
while(num>0)
{
d=num%10;
if(d==i)
freq++;
num=num/10;
}
num=num1;
freq=0;
if(freq>1||freq==0)
{
f=1;
break;
}
}
if(f==0)
System.out.println(n+" is a Fascinating Number.");
else
System.out.println(n+" is a Fascinating Number.");
}
}
}
Variable Description:
Variable Data Description
type
N Int Number input by the user
A Int Product, nx2
B Int Product, nx3
Con String Concatenated number
Num Long Concated number in long data type
num1 Long Duplicate of num
D Long Right most digit
I int Loop control variable to generate the digits 1 to 9
Freq Int Frequency of each digit
F Int To indicate the number is a Fascinating number or
not
Output:
Assignment 3
Decimal Number: 45
Number in base 16=2D
Example 4:
Enter a decimal number:
78
Enter base:
9
INVALID BASE.
Algorithm:
Step 1: Start.
Step 2: Input a number.
Step 3: Input base(b) to convert the number.
Step 4: Check whether n! = 2 and n! = 8 and n! = 16. If true then go to Step 5 else go to
Step 6.
Step 5: Print “INVALID BASE” and go to Step 15.
Step 6: Declare an array digits[] of char type and initialize it with digits 0-9, A-F.
Step 7: Declare and initialize the variable num=”” and d=0.
Step 8: Create the duplicate a=n.
Step 9: Repeat the steps 10 to 12 until a=0.
Step 10: Calculate d= a%b.
Step 11: Concatenate num= digits[d]+num.
Step 12: Update a=a/b.
Step 13: Print n.
Step 14: Print num.
Step 15: Stop.
Program:
import java.util.*;
class ConvertNum
{
public void calculate()
{
Scanner sc= new Scanner(System.in);
System.out.println("Enter a decimal number:");
int n= sc.nextInt();
System.out.println("Enter base:");
int b= sc.nextInt();
if(b!=2&&b!=8&&b!=16)
{
System.out.println("INVALID BASE");
}
else
{
char digit[]={'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};//digits
int a=n,d=0;
String num="";
while(a>0)
{
d=a%b;
num=digit[d]+num;//converting into base
a=a/b;
}
System.out.println("Decimal Number: "+n);
System.out.println("Number in base "+b+"="+num);
}
}
}
Variable Description:
Variable Data Description
Type
N int Decimal number input.
A int Base to convert the number.
B int Duplicate of n.
D int To store the digits of the number.
digits[] char Array to store digits from 0-9 and A-F.
Num String Convert number into base b.
Output:
Assignment 4
Sorting Word
Variable Description:
Output:
Assignment 5
Write a program to accept a string and convert into capital letter. The sentence should
be terminated with a full stop. Remove the double entry of the word from the string.
Example:
o Input: I AM AM PROUD PROUD OF MY MOTHERLAND.
Output: I AM PROUD OF MY MOTHERLAND
o Input: AT LAST THE TIGER WAS SAVED#
Output: INVALID INPUT
Algorithm:
Step 1: Start.
Step 2: Accept a sentence st.
Step 3: Convert st to upper case letters.
Step 4: Calculate length of the string st (len).
Step 5: Check whether the character at index number(len-1)is not equal to full stop. If
true then go to step 6 else go to Step 7.
Step 6: Print “INVALID INPUT” and go to step 17.
Step 7: Declare and initialize the variables w=0, nst=””, wrd=””, swrd =”” , i=0 and
ch=’ ‘.
Step 8: Repeat the steps 10 to 17 until i=len.
Program:
import java.util.*;
class DeleteWord
{
public void removeRepeat()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter a string:");
String st=sc.nextLine();
st=st.toUpperCase();
int len=st.length();
if(st.charAt(len-1)!='.')
{
System.out.println("INVALID INPUT");
}
else
{
String nst="",wrd="",pre="";
char ch=' ';
for(int i=0;i<len;i++)
{
ch=st.charAt(i);
if(ch!=' '&&ch!='.')
{
wrd=wrd+ch;
}
else
{
if(wrd.equals(pre)==false)
nst=nst+wrd+" ";
pre=wrd;
wrd=" ";
}
}
System.out.println("Orignal String:\n"+st);
System.out.println("Replaced String:\n"+nst);
}
}
}
Variable Description:
Variable Data type Description
st String To store the original sentence.
len Int To store the length of the sentence st.
nst String To store the new sentence.
wrd String To store each word of the sentence st.
pre String To store the previous word.
ch Char To store each character of the word wrd.
i Int Looping variable.
Output:
Assignment 6
Write a program to accept a string and replace each word by interchanging the first
alphabet with the last alphabet. The sentence should be terminated with a full stop, ! or
?. Single letter word should remain unchanged.
Example:
INPUT:
Do your best.
OUTPUT:
oD rouy tesb.
INPUT:
Action speaks louder than words#
OUTPUT:
Invalid Input.
Algorithm
Step 1: Start.
Step 2: Accept a string st.
Step 3: Find the length of the string(len).
Step 4: Check whether the last letter of the string is ? or . or !. If true then go to Step 5
else go to Step 17.
Step 5: Declare and initialize the variables nst=””,i=0.
Step 6: Repeat Steps 7 to Step 15 until i=len.
Step 7: Extract a word wrd.
Step 8: Check whether the length of the word is 1 or not. If true then go to step 9 else go
to Step 10.
Step 9: Concatenate nst=nst+wrd+” “ and go to Step 15.
Step 10: Extract the first letter of the word wrd(first).
Step 11: Extract the last letter of the wrd wrd(last).
Step 12: Extract the letter from thr index number 1 to length of the word wrd-2(mid).
Step 13: Concatenate the new word nwrd= last+mid+first.
Step 14: Concatenate nst = nst + nwrd +” ”.
Step 15: Set word =””,i=i+1.
Step 16: Print “ The Replaced sentence is :” , nst.
Step 17: Stop.
Program
import java.util.*;
class Interchange
{
public void change()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter a sentence:");
String st=sc.nextLine();
int len=st.length();
char l=st.charAt(len-1);
String wrd="";
String nst="";
String nwrd=””;
char ch=' ';
if(l=='.'||l=='?'||l=='!')
{
for(int i=0;i<len;i++)
{
ch=st.charAt(i);
if(ch!=' '&&ch!='.'&&ch!='!'&&ch!='?')
{
wrd=wrd+ch;
}
else
{
if(len==1)
nwrd=wrd;
else
{ int a=wrd.length();
char first=wrd.charAt(0);
char last=wrd.charAt(a-1);
String mid=wrd.substring(1,a-1);
nwrd=last+mid+first;
}
nst=nst+nwrd+" ";
wrd="";
}
}
System.out.println("Rearranged Sentence: "+nst);
}
else
{
System.out.println("INVALID INPUT");
}
}
}
Variable Description:
Variable Data Type Description
st String To store the original sentence
len int To store the length of the sentence
l char To store the last word of sentence
wrd String To store each wors of the sentence
nst String To store the new sentence
nwrd String To store the new word
ch char To store each character of the word
Output:
Assignment 7:
Date: 27/06/2023
Write a Program in Java to input a Date in ddmmyyyy 8-digit format (year 1900 to
2050) and print it in:
1) dd/mm/yyyy format
2) dd, month name, yyyy format
INPUT :
01011943
OUTPUT:
01/01/1943
1 January, 1943
ALGORITHM
Step 1: Start
Step 2: Accept a date in ddmmyyyy format in a string variable date
Step 3: Find the length (l) of the date
Step 4: Check l!=8 or not. If true then go to step 5 else go to step 6 to 19
Step 5: Print “Invalid Length” and goto step 20
Step 6: Extract first two digits (day) of the variable date.
Step 7: Convert day into an integer (dd)
Step 8: Extract next two digits(month) of the variable date.
Step 9: Convert month into an integer(mm)
Step 10: Extract the last four digits (year) of the variable date
Step 11: Convert year into an integer (yy)
Step 12: Create an integer array limit[12] and store the maximum days of each month
Step 13: Check whether yy is a leap year or not. If true then go to step 14 else go to step
15.
Step 14: Set limit[2]= 29 and go to step 15.
Step 15: Create a string array mnames[12] and store the names of each month
Step 16: If mm<1 OR mm>12 OR dd<1 OR dd>limit[mm] OR year<1900 OR
year>2100 then
step 17 else step 18 to 19
Step 17: Print “Invalid date” go to step 20
Step 18: Print day / month / year
Step 19: Print dd, mnames[mm], yy
Step 20: Stop
PROGRAM
import java.util.*;
class Date
{
public void display()
{
Scanner sc= new Scanner(System.in);
System.out.println("Enter a date in ddmmyyyy format:");
String date= sc.next();
if(date.length()!=8)
System.out.println("Wrong format");
else
{
//extracting and converting day, month and year
String day=date.substring(0,2);
int dd=Integer.parseInt(day);
String month=date.substring(2,4);
int mm= Integer.parseInt(month);
String year=date.substring(4);
int yy=Integer.parseInt(year);
int limit[]={0,31,28,31,30,31,30,31,31,30,31,30,31};
if((yy%400==0) || ((yy%100!=0)&&(yy%4==0)))//condition for leap year
limit[2]=29;
String mnames[]={"","January","February","March",
"April","May","June","July", "August","September","October",
"November","December"};
//Checking the limit of day, month, year
if(mm<1 ||mm>12 || dd<1 ||dd>limit[mm]||yy<1900||yy>2100)
System.out.println("Invalid date");
else
{
System.out.println("OUTPUT:");
System.out.println(day+"/"+ month+"/"+year);
System.out.println(dd+" "+ mnames[mm]+" "+yy);
}
}
}
}
Variable Description:
Output:
Assignment 8:
Date : 27/06/2023
Arranging the words according their potential in ascending order
The encryption of alphabets is to be done as follows:
A = 1 (ASCII value of A is 65, 65-64=1)
B = 2 (ASCII value of B is 66, 66-64=2)
C = 3 (ASCII value of C is 65, 67-64=1)
.
.
Z = 26(ASCII code of Z is 90, 90-64=26)
The potential of a word is found by adding the encrypted value (alphabetical position)
of the alphabets.
Example: KITE
Potential = 11 + 9 + 20 + 5 = 45
Accept a sentence which is terminated by either “ . ” , “ ? ” or “ ! ”. Each word of
sentence is separated by single space. Decode the words according to their potential
and arrange them in ascending order.
Output the result in format given below:
Example 1
INPUT :
THE SKY IS THE LIMIT.
POTENTIAL:
THE = 33
SKY = 55
IS = 28
THE = 33
LIMIT = 63
OUTPUT :
IS THE THE SKY LIMIT
Example 2
INPUT :
LOOK BEFORE YOU LEAP.
POTENTIAL :
LOOK = 53
BEFORE = 51
YOU = 61
LEAP = 34
OUTPUT :
LEAP BEFORE LOOK YOU
Example 3
INPUT :
GOOD MORNING@
OUTPUT:
INVALID INPUT
ALGORITHM
Step 1: Start
Step 2: Accept a sentence (st) in capital letter
Step 3: Check whether st is terminated with “ . ” , “ ? ” or “ ! ”. If true then go to step
4 else go to step 14.
Step 4: Split and store each word in an array words[]
Step 5: Find the length (size) of the array words[]
Step 6: Declare an array p[size] to store the potential of each word
Step 7: Repeat the steps 7 to 10 for i=0 to size-1 increase by 1
Step 8: Calculate the potential of each word(c)
Step 9: Set p[i]=c
Step 10: Print words[i], p[i].
Step 11: Sort the arrays words[] and p[] in the ascending order of potential.
Step 12: Create the new string (nst) by concatenating the sorted words in the array
words[].
Step 13: Print nst and go to step 15.
Step 14: Print “INVALID INPUT”
Step 15: Stop
PROGRAM
import java.util.*;
class Potential
{
public void encrypt()
{
Scanner sc= new Scanner(System.in);
System.out.println("Enter a string in capital letter ends with a full stop:");
String st=sc.nextLine();
char T=st.charAt(st.length()-1);
if(T=='.'||T=='?'||T=='!'||T==',')
{
String words[]= st.split("[.,?! ' ']+");//splitting at multiple delimiters and storing
each word in an array
int size=words.length;
int p[]= new int[size];//array to store the potential of each word
int c=0;
char ch=' ';
//calculating potential of each word
System.out.println("POTENTIAL:");
for(int i=0;i<words.length;i++)
{
for(int j=0;j<words[i].length();j++)
{
ch=words[i].charAt(j);
c=c+(ch-64);//adding alphabetical position of ch
}
p[i]=c;//storing the potential of each word in the array
c=0;
System.out.println(words[i]+"="+p[i]);//printing each word and its potential
}
//sorting the arrays in the ascending order of potential
String temp="";
int t=0;
for(int i=0;i<size-1;i++)
{
for(int j=0;j<size-1-i;j++)
{
if(p[j+1]<p[j])
{
//interchanging potentials
t=p[j];
p[j]=p[j+1];
p[j+1]=t;
//interchanging words at the same time
temp=words[j];
words[j]=words[j+1];
words[j+1] = temp;
}
}
}
//creating the new string
String nst="";
for(int i=0;i<size;i++)
{
nst=nst+words[i]+" "; //adding the sorted words in the new string nst
}
System.out.println("OUTPUT:"+nst);
}
else
{
System.out.println("INVALID INPUT");
}
}
}
Variable Description:
Output:
Assignment 9:
Date: 28/06/2023
A Circular Prime is a prime number that remains prime under cyclic shifts of its
digits. When the leftmost digit is removed and replaced at the end of the remaining of
digits, the generated number is still prime. The process is repeated until the original
number is reached again.
A number is said to be prime if it has only two factors 1 and itself.
Example 1:
INPUT :
131
OUTPUT:
131
311
113
Example 2:
INPUT :
29
OUTPUT:
29
92
ALGORITHM:
Step 1: Start
Step 2 : Accept a number n
Step 3: Assign n1=n
Step 4: Calculate the number of digits (d) of the number n
Step 5: Declare and initialize the variables first=0, last=0, c=0, f=0
Step 6: Print n
Step 7: Check whether n is not a Prime number. If true, then go to step 8 else go to step
9.
Step 8: Set f=1 and go to step 9.
Step 9: Extract the left most digit left=n/10(d-1)
Step 10: Extract the remaining digits right=n%10(d-1)
Step 11: Generate a new number and assign n= (rightx10)+ left
Step 12: Repeat the steps 6 to 12 until n=n1
Step 13: Check whether the value of f=1 or not. If true step 14 else step 15
Step 14: Print n, “is a Circular Prime Number” go to step 16
Step 15: Print n, “is not a Circular Prime Number”
Step 16: End
PROGRAM
import java.util.*;
class CircularPrime
{
public void check()
{
Scanner sc= new Scanner(System.in);
System.out.println("Enter a number");
int n=sc.nextInt();
int n1=n;
int d=Integer.toString(n).length();//calculating number of digits // int
d=String.valueOf(n).length();
int left=0,right=0,c=0,f=0;
System.out.println("Output:");
do
{
System.out.println(n);
c=0;
for(int i=1;i<=n;i++)//checking Prime or not
{
if(n%i==0)
c++;
}
if(c!=2)
{
f=1;
}
left=n/(int)Math.pow(10,d-1);//Extracts the left most digit
//Extracts the rest of the digits
right=n%(int)Math.pow(10,d-1);
n=(right*10)+left;
} while(n!=n1);
if(f==0)
System.out.println(n+"is Circular Prime.");
else
System.out.println(n+"is not Circular Prime.");
}
}
Variable Description:
Variable Data type Descriptions
n int The number
n1 int Duplicate of the number n
d int Number of digits
left int Left most digit
right int Rest of the digits
c int Number of factors of n
f int Variable to check the number is Prime or not in
all the shifts
Output:
Assignment 10:
Write a program to create a matrix A[][] order M . M must be greater than 2 and less
than 10.
Perform the following tasks:
1. Input the matrix elements
2. Display the original matrix
3. Rotate the matrix 90o clockwise and display the rotated matrix
4. Find the sum of the elements of the four corners.
INPUT:
1 2 3
2 4 5
3 5 6
OUTPUT:
ORIGINAL MATRIX
1 2 3
4 5 6
7 8 9
MATRIX AFTER ROTATION
7 4 1
8 5 2
9 6 3
Sum of the corner elements : 20
ALGORITHM:
Step 1: Start
Step 2: Accept order of the matrix (M).
Step 3: Check whether M<=2 OR M>10. If true, then go to step 4 else go to step 5.
Step 4: Print “OUT OF RANGE” and go to step 17
Step 5: Declare the original matrix A[][] and the rotated matrix R[][] of order M.
Step 6: Store elements in matrix A[][].
Step 7: Print the matrix A[][]
Step 8: Declare and initialize the variables i=0, j=M-1, k=0.
Step 9: Repeat the steps 10 to 13 until i=M.
Step 10: Repeat the steps 11 to 12 until j<0.
Step 11: Set R[i][k]=A[j][i]
Step 12: Update k=k+1, j=j-1
Step 13: Update i=i+1
Step 14: Print the matrix R[][]
Step 15: Calculate sum of corner elements (s)= A[0][0]+A[0][M-1]+A[M-1][0]+A[M-1]
[M-1]
Step 16: Print “Sum of corner elements:”,s
Step 17: Stop
PROGRAM:
import java.util.*;
class Rotation
{
public void rotate()
{
Scanner sc= new Scanner(System.in);
System.out.println("Enter a number:");
int M = sc.nextInt();
if(M<=2||M>10)
System.out.println("OUT OF RANGE");
else
{
int A[][]=new int[M][M];
int R[][]= new int [M][M];
//reading the array elements
System.out.println("Enter numbers:");
for(int i=0;i<M;i++)
{
for(int j=0;j<M;j++)
{
A[i][j]=sc.nextInt();
}
}
//printing the original matrix
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();
}
//rotating the matrix
for(int i=0;i<M;i++)
{
for(int j=M-1,k=0;j>=0;j--,k++)
{
R[i][k]=A[j][i];
}
}
//Printing the rotated matrix
System.out.println("ROTATED MATRIX:");
for(int i=0;i<M;i++)
{
for(int j=0;j<M;j++)
{
System.out.print(R[i][j]+"\t");
}
System.out.println();
}
Output:
Assignment 11:
Sorting Matrix(Ascending)
Write a program to declare a matrix A[][] order(MXN). Both M and N
should be greater than 2 and less than 8.
Perform the flowing task on the matrix:
i) Display the original matrix.
ii) Sort the matrix row wise in ascending order.
iii)Display the sorted matrix.
Example 1:
M=3, N=3
ORIGINAL MATRIX:
3 78 56
9 34 74
43 12 10
SORTED MATRIX:
3 56 78
9 34 74
10 12 43
Example 2: M=4,N=4
ORIGINAL MATRIX: 9
8 7 6
5 4 3 2
1 88 56 92
SORTED MATRIX:
6 7 8 9
2 3 4 5
1 56 88 92
ALGORITHM:
Step 1: Start
Step 2: Input number of rows (M).
Step 3: Input number of columns (N).
Step 4: Check whether M<=2 OR N<=2 OR M>=8 OR N>=8. If true then
go to step 5 else go to step 6.
Step 5: Print “OUT OF RANGE” and go to step 15
Step 6: Declare a matrix A[][] of order MxN
Step 7: Store elements in the matrix A[][].
Step 8: Display the Original Matrix.
Step 9: Repeat the steps 10 to 13 for r=0 to r<N, r increases by 1.
Step 10: Repeat the steps 11 to 13 for i=0 to i<N, i increases by 1
Step 11: Repeat the steps 12 to 13 for j=0 to j<N-1-i, j increases by 1.
Step 12: Check whether A[r][J+1]<A[r][j]. I true then go to step 13 else go
to step 11
Step 13: Swap the elements of A[r][J+1] and A[r]
[j]. Step 14: Print the sorted matrix
Step 15: Stop
PROGRAM:
import java.util.*;
class MatrixSort
{
public void rowSort()
{
Scanner sc= new Scanner(System.in);
System.out.println("Enter number of
rows:"); int M=sc.nextInt();
System.out.println("Enter number of columns:");
int N=sc.nextInt(); if(M<=2||N<=2||M>=8||
N>=8) System.out.println("OUT OF RANGE");
else
{
int A[][]= new int[M][N];
//Storing elements in the
array for(int i=0;i<M;i++)
{
for(int j=0;j<N;j++)
{
System.out.println("Enter a
number:"); A[i][j]=sc.nextInt();
}
}
//Printing the Original Matrix
System.out.println("ORIGINAL MATRIX:");
for(int i=0;i<M;i++)
{
for(int j=0;j<N;j++)
{
System.out.print(A[i][j]+"\t");
}
System.out.println();
}
//Sorting using bubble
sort int t=0;
for(int r=0;r<M;r++)
{
for(int i=0;i<N;i++)
{
for(int j=0;j<N-1-i;j++)
{
if(A[r][j+1]<A[r][j])
{
t=A[r][j]; A[r]
[j]=A[r][j+1];
A[r][j+1]=t;
}
}
}
}
//Printing the Sorted Matrix
System.out.println("SORTED MATRIX:");
for(int i=0;i<M;i++)
{
for(int j=0;j<N;j++)
{
System.out.print(A[i][j]+"\t");
}
System.out.println();
}
}
}
}
VARIABLE DESCRIPTION:
Variable Data Type Description
M int Number of rows
N Int Number of columns
A[][] Int To store the array
i, j Int Looping variables
t, r Int Variables used for sorting
Assignment 11:
Sorting Matrix(Descending)
1 2 3
4 5 6
7 8 9
SORTED MATRIX:
7 8 9
4 5 6
1 2 3
ALGORITHM:
Step 1: Start
Step 2: Input number of rows (M).
Step 3: Input number of columns (N).
Step 4: Check whether M<=2 OR N<=2 OR M>=8 OR N>=8. If true then
go to step 5 else go to step 6.
Step 5: Print “OUT OF RANGE” and go to step 15
Step 6: Declare a matrix A[][] of order MxN
Step 7: Store elements in the matrix A[][].
Step 8: Display the Original Matrix.
Step 9: Repeat the steps 10 to 13 for c=0 to c<M, c increases by 1.
Step 10: Repeat the steps 11 to 13 for i=0 to i<M, i increases by 1
Step 11: Repeat the steps 12 to 13 for j=0 to j<M-1-i, j increases by 1.
Step 12: Check whether A[J+1][c]>A[j][c]. I true then go to step 13 else go
to step 11
Step 13: Swap the elements of A[J+1] [c]and A[j][c].
Step 14: Print the sorted matrix
Step 15: Stop
PROGRAM:
import java.util.*;
class MatrixSort
{
public void columnSort()
{
Scanner sc= new Scanner(System.in);
System.out.println("Enter number of
rows:"); int M=sc.nextInt();
System.out.println("Enter number of columns:");
int N=sc.nextInt(); if(M<=2||N<=2||M>=8||
N>=8) System.out.println("OUT OF RANGE");
else
{
int A[][]= new int[M][N];
//Storing elements in the
array for(int i=0;i<M;i++)
{
for(int j=0;j<N;j++)
{
System.out.println("Enter a
number:"); A[i][j]=sc.nextInt();
}
}
//Printing the Original Matrix
System.out.println("ORIGINAL MATRIX:");
for(int i=0;i<M;i++)
{
for(int j=0;j<N;j++)
{
System.out.print(A[i][j]+"\t");
}
System.out.println();
}
//Sorting using bubble
sort int t=0;
for(int c=0;c<N;c++)
{
for(int i=0;i<M;i++)
{
for(int j=0;j<M-1-i;j++)
{
if(A[j+1][c]>A[j][c])
{
t=A[j][c]; A[j]
[c]=A[j+1][c];
A[j+1][c]=t;
}
}
}
}
//Printing the Sorted Matrix
System.out.println("SORTED MATRIX:");
for(int i=0;i<M;i++)
{
for(int j=0;j<N;j++)
{
System.out.print(A[i][j]+"\t");
}
System.out.println();
}
}
}
}
VARIABLE DESCRIPTION:
Variable Data Type Description
M Int To store number of rows
N Int To store number of columns
A[][] Int To store the array
T Int Temporary variable for sorting
i,j,c int Looping variable
OUTPUT:
Assignment 13:
Example 1:
M=3, N=3
ORIGINAL MATRIX:
9 8 11
12 20 4
3 2 40
SUM OF THE CORNER ELEMENTS: 63
SORTED MATRIX:
2 3 4
8 9 11
12 20 40
SUM OF THE CORNER ELEMENTS: 58
ALGORITHM:
Step 1: Start
Step 2: Input number of rows (M).
Step 3: Input number of columns (N).
Step 4: Check whether M > 2 AND M<=10 AND N>2 AND N<=10.
If true then go to step 5 else go to step 17
Step 5: Create matrix A[][] of order MxN
Step 6: Store elements in the matrix A[][].
Step 7: Calculate the sum of corner elements (s) of the matrix A[][].
Step 8: Print the matrix A[][].
Step 9: Print “SUM OF CORNER ELEMENTS”, s
Step 10: Create an array B[] size M*N.
Step 11 : Store the elements of the matrix A[][] in the array B[].
Step 12: Sort the array B[] using Bubble sort technique.
Step 13: Store the sorted elements of the array B[] into the
matrix A[][].
Step 14: Print the Sorted matrix A[][].
Step 15: Calculate the sum of corner elements (s) of the matrix A[][].
Step 16: Print “SUM OF CORNER ELEMENTS”, s.
Step 17: Print “OUT OF RANGE”
Step 18: Stop
Program
import java.util.*;
class SortMatrix
{
int A[]
[]; int
M; int
N;
static Scanner sc= new Scanner(System.in);
public SortMatrix(int MM, int NN)
{
M=MM;
N=NN;
A=new int[M][N];
}
//Reading the array
void fillMatrix()
{
for(int i=0;i<M;i++)
{
for(int j=0;j<N;j++)
{
System.out.println("Enter a
number:"); A[i][j]=sc.nextInt();
}
}
}
int sumCorner()
{
return A[0][0]+A[0][N-1]+A[M-1][0]+A[M-1][N-1];
}
void sort()
{
//storing the matrix in a single dimensional array B[]
int B[]=new
int[M*N]; int k=0;
for(int i=0;i<M;i++)
{
for(int j=0;j<N;j++)
{
B[k++]=A[i][j];
}
}
}
}
Variable Description:
Variable Data Type Description
A[][] int To store double dimensional array
M int To store number of rows
N int To store number of columns
i int Looping variable
j int Looping variable
B[] int To store double dimensional array into a single
dimensional one
k int To store index number
temp int Temporary variable for sorting
Output:
Assignment 14:
Algorithm:
Step 1: Start
Step 2: Accept a sentence(st).
Step 3: Calculate its length of st.( l).
Step 4: Check whether the string is not terminated with . and ?.
If true then go to step 5 else go to step 6.
Step 5: Print “Invalid string.” and go to step 19
Step 6: Set i=0;
Step 7: Repeat the steps 8 to 18 until
i=l. Step 8. Extract a word (wrd).
Step 9: Set j=0.
Step 10: Repeat the steps 11 to 15 until j= length of the word wrd.
Step 11: Extract the character at the index number j(ch1).
Step 12: Check whether ch1 is a vowel. If true then go to step 12 else go to
step 13.
Step 13: Increase v=v+1 and go to step 15
Step 14: Increase c=c+1
Step 15: Update j=j+1
Step 16: Print wrd, c and v
Step 17: Set wrd=””, c=0, v=0
Step 18: Update i=i+1
Step 19: Stop
Program:
import java.util.*;
class VowelCons
{
public static void main(String args[])
{
for(int j=0;j<k;j++)
{
ch1=wrd.charAt(j); if(ch1=='a'||ch1=='e'||ch1=='i'||
ch1=='o'||ch1=='u')
{
v++;
}
else
c++;
}
System.out.println(wrd+"\t"+v+"\t"+c);
wrd="";
v=0;
c=0;
}}}}}
Variable Description:
Output:
Assignment 15:
Write a program in Java to enter certain alphabets in a double dimensional
array m*n (where both m and n should be greater than 3 and less than 10).
Display the element of the matrix. Replace all the vowels of the matrix with
next character.
Example: m=4, n=5
A b t I p
d H E M Q
U n b X r
f S z M L
Output
B b t J p
d H F M Q
V n b X r
f S z M L
Algorithm:
Step 1: Start.
Step 2: Accept number rows m.
Step 3: Accept number of columns
n
Step 4: Check if n<=3 or n>9 or m<=3 or m>9. If true then go to step
5 else go to step 6.
Step 5: Print “Out of Range” and go to step 14
Step 6: Declare a double dimensional character array A[m][n].
Step 7: Store characters in the matrix.
Step 8: Print the original matrix
Step 9: Declare and initialize i=0
Step 10: Repeat the step 11 to 16 until i=
m Step 11: Declare and initialize j=0
Step 12: Repeat the step 13 to 15 until j= n
Step 13: Check A[i][j] is a vowel or not. If true then go to step 14 else
go to step 15
Step 14: Assign A[i][j] = A[i][j]+1
Step 15: Update j=j+1 and go to step 12
Step 16: Update i=i+1 and go to step 10
Step 17: Display the replaced matrix
Step 18: Stop
Program:
import java.util.*;
class Matrix
int m=sc.nextInt();
else
for(int i=0;i<m;i++)
for(int j=0;j<n;j++)
System.out.println("Enter a character:");
A[i][j]= sc.next().charAt(0);
}
//Printing the original Matrix System.out.println("Original Matrix is:");
for(int i=0;i<m;i++)
for(int j= 0;j<n;j++)
System.out.print(A[i][j]+"\t");
System.out.println();
for(int i=0;i<m;i++)
for(int j=0;j<n;j++)
if(A[i][j]=='A'||A[i][j]=='a'||A[i][j]=='E'||
A[i][j]=='e'||A[i][j]=='I'||A[i][j]=='i'||A[i][j]=='O'||A[i][j]=='i'||A[i][j]=='o'||A[i][j]=='U'||A[i][j]=='u')
A[i][j]+=1;
for(int i=0;i<m;i++)
for(int j= 0;j<n;j++)
System.out.print(A[i][j]+"\t");
System.out.println();
}
}
Variable Description
Variable Data type Description
M Int Number of columns
N Int Number of rows
A[][] Char Array to store the characters
I Int Loop control variable to generate
the row indices
J Int Loop control variable to generate the
column indices
OUTPUT:
Assignment 16:
Bouncy Number
A bouncy number is any non-negative integer that is neither increasing nor decreasing.
An increasing integer is any integer in which when going from left to right, the current
digit is greater than or equal to the previous digit.
In contrast, a decreasing integer is any integer in which when going from left to right,
the current digit is less than or equal to the previous digit.
A bouncy number’s digits neither increase nor decrease. Rather, they “bounce” between
increasing and decreasing.
Write a program to accept a positive number and check whether it is a Bouncy number
or not.
Algorithm:
Step 1: Start
Step 4: Check the digits are in increasing order or not. If false then go to step 5 else go to
step 6
Step 8: Check f=1 and f1=1. If true then go to step 9 else go to step 10
Step 11:Stop
Program:
import java.util.*;
class BouncyNumber
a=a/10; c++;
int f=0,inc=0,d;
for(int j=c;j>=1;j--)
if(d>=inc)
inc=d; }
else
f=1; break;
a=a%(int)Math.pow(10,j-1);
int dec=9;
a=n;
for(int j=c;j>=1;j--)
if(d<=dec)
dec=d;
} else {
f1=1; break;
a=a%(int)Math.pow(10,j-1);
if(f==1&&f1==1)
else
}
Assignment 17:
Calculating Denomination and number of notes
Design a program to accept the amount from the user and display the break – up in
descending order of denomination along with the total number of notes. The available
denominations are 2000, 500, 200, 100, 50, 20, 10. Also print the amount in words
according to the digit.
Example:
Enter Amount:
3450
THREE FOUR FIVE ZERO
Denominations:
2000 x 1 = 2000
500 x 2 = 1000
200 x 2 = 400
50 x 1 = 50
Total Number of Notes : 6
Algorithm:
Step 1: Start
Step 2: Accept an amount amt
Step 3: Check amt is in the given denomination. If true then go to step 4 else go to step
11
Step 4: Create an integer array and store the denominations.
Step 5: Create a string array and store the digits in words.
Step 6: Extract each digit from the left and concatenate the digits in words in a string w.
Step 7: Print the concatenated word w.
Step 8: Divide the amount with each denomination and add the quotient into the
variable total.
Step 9: Print the denominations.
Step 10: Print total number of notes, total
Step 11: End
Program:
import java.util.*;
class Money
System.out.println("Enter Amount:");
if(amt%10!=0)
System.out.println("Invalid Amount.");
else
int d[]={2000,500,200,100,50,20,10};
String
wrd[]={"ZERO","ONE","TWO","THREE","FOUR","FIVE","SIX","SEVEN","EIGHT",
"NINE"};
String w="";
int total=0,a=amt,r=0;
for(int i=l;i>0;i--)
r=a/(int)Math.pow(10,i-1);
w=w+wrd[r]+" ";
a=a%(int)Math.pow(10,i-1);
System.out.println(w);
System.out.println("Denomination:");
a=amt;
for(int i=0;i<d.length;i++)
r=a/d[i];
if(r>0)
total=total+r;
a=a%d[i];
}
}
OUTPUT:
Assignment 18:
Arranging words containing vowel in beginning
(b) Create a new string with the words that begins and end with vowels followed by
other words.
(c) Display the new sentence along with the original sentence.
Example 1:
ORIGNAL SENTENCE:
REARRANGED SENTENCE:
Example 2:
Invalid String.
Algorithm:
Step 1: Start
Step 2: Accept a sentence st.
Step 3: Convert it into uppercase.
Step 5: Check whether the string ends with ‘?’ or ‘!’ or ‘.’. If not then go to step 6 else
go to step 7
Step 6: Print “Invalid String”. Go to step 18
Step 10: Check whether the first and last character of the wrd is a vowel or
Step 11: Concatenate vow, wrd and a space and go to step 13.
Program:
import java.util.*;
class Vowels
{
System.out.println("Enter a sentence:");
int l=st.length();
System.out.println("Invalid String.");
else
int wl=0;
for(int i=0;i<l;i++)
ch=st.charAt(i);
wrd=wrd+ch;
else
wl=wrd.length();
if((ch1=='A'||ch1=='E'||ch1=='I'||ch1=='O'||ch1=='U')
&&(ch2=='A'||ch2=='E'||ch2=='I'||ch2=='O'||ch2=='U'))
vow=vow+wrd+" "; // joins the word begin and end with vowels
else
wrd="";
nst=vow+cons;
System.out.println(st);
System.out.println(nst);
}
Variable Description
Output:
Assignment 19:
Displaying Date
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. Display an error message if the
value of the day number and year are not within the limit or not according to the
condition specified.
Test your program with the following data and some random data:
INPUT:
YEAR: 2018
Algorithm:
Step 1: Start
Step 2: Accept number of days d.
Step 3: Accept year y.
Step 4: Check d>=1 and d<=366 and y>=1000 and y<= 10000. If true go to step
5 else go to step 14
Step 5: Create and initialize an array, mdays[] with number of days in each
month.
Step 6: Create and initialize an array, months [] with the month names.
Step 7: Check y is a Leap year or not. If true go to step 8.
Step 8: Increase the value of mdays[2] by 1. Go to step 9
Step 9: Declare and initialize i=1
Step 10: Repeat the steps11 to12 until d<= mdays[i]
Step 11: Reduce d=d-mdays[i]
Step 12: Update i =i+1
Step 13: Print d, mdays[i], y. Go to step 15
Step 14: Print “Invalid Date”
Step 15: Stop
Program:
import java.util.*;
System.out.println("DAY NUMBER:");
int d=sc.nextInt();
System.out.println("ENTER YEAR");
int y=sc.nextInt();
if((d>=1&&d<=366)&&(y>=1000&&y<=9999))
int mdays[]={0,31,28,31,30,31,30,31,31,30,31,30,31};
"APRIL","MAY","JUNE","JULY",
"AUGUST","SEPTEMBER",
"OCTOBER","NOVEMBER","DECEMBER"};
mdays[2]=29;
int i=1;
i++;
else
System.out.println("Invalid date");
Variable Description
Output:
Assignment 20:
Rearranging Matrix
Write a program to declare a single-dimensional array a[] and a square matrix b[][] of
size N, where N > 2 and N < 10. Allow the user to input positive integers into the single
dimensional array.
1. Sort the elements of the single-dimensional array in ascending order using any
standard sorting technique and display the sorted elements.
2. Fill the square matrix b[][] in the given format:
1 2 5 8
1 2 5 1
1 21 2
11 2 5
Algorithm:
Program:
import java.util.*;
int N=sc.nextInt();
if(N<3 || N>9)
System.out.println("Out of range");
else
for(int i=0;i<N;i++)
System.out.println("Enter a number:");
B[i]= sc.nextInt();
//sorting
int t=0;
for(int i=0;i<N;i++)
for(int j=0;j<N-1-i;j++)
if(B[j+1]<B[j])
t=B[j];
B[j]=B[j+1];
B[j+1]= t;
}
//printing the sorted array
for(int i=0;i<N;i++)
System.out.print(B[i]+" ");
for(int i=0,n=N-1;i<N;i++,n--)
A[i][j]=B[j];
A[i][k]=B[p];
for(int i=0;i<N;i++)
for(int j=0;j<N;j++)
{
System.out.print(A[i][j]+"\t");
System.out.println();
Variable Description:
Output:
Assignment 21:
Mirror Image
Sample Input:
8 15 9 18
9 10 7 6
10 8 11 13
12 16 17 19
Sample Output:
18 9 15 8
6 7 10 9
13 11 8 10
19 17 16 12
Algorithm:
Step 1: Start
Step 3: Check whether M>2, M<=10, N>2 and N<=10. If true then go to step 4 else go to
step 11.
Program:
import java.util.*;
class Mirror
int M=sc.nextInt();
int N=sc.nextInt();
if(M<=2||N<=2||M>10||N>10)
System.out.println("OUT OF RANGE");
else
for(int i=0;i<M;i++)
for(int j=0;j<N;j++)
System.out.println("Enter a number:");
A[i][j]=sc.nextInt();
System.out.println("ORIGINAL MATRIX:");
for(int i=0;i<M;i++)
for(int j=0;j<N;j++)
System.out.print(A[i][j]+"\t");
System.out.println();
int t=0;
for(int c=0;c<N;c++)
{
for(int i=0;i<M;i++)
for(int j=N-1;j>c;j--)
t=A[i][j];
A[i][j]=A[i][j-1];
A[i][j-1]=t;
for(int i=0;i<M;i++)
for(int j=0;j<N;j++)
System.out.print(A[i][j]+"\t");
System.out.println();
}
Variable Description:
Output:
Assignment 22:
Smith 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 3: Check whether it not a composite number . If true then go to step 4 else go to
step 5.
import java.util.*;
int n;
System.out.println("Enter number:");
n = sc.nextInt();
int sumDigits(int x)
int s=0;
while(x>0)
s=s+x%10;
x=x/10;
return s;
boolean isPrime(int a)
{
int c=0;
for(int i=1;i<=a;i++)
if(a%i==0)
c++;
if (c==2)
return true;
else
return false;
void check()
int f=0;
for(int i=2;i<n;i++)
if(n%i==0)
f=1;
break;
}
}
if(f==0)
else
int dsum=sumDigits(n);
int sp=0,j=2;
int a=n;
while(a>1)
if(a%j==0&&isPrime(j)==true)
System.out.print(j+" ");
sp=sp+sumDigits(j);
a=a/j;
continue;
j++;
System.out.println("\nSum of digits:"+sp);
if(dsum==sp)
obj.accept();
obj.check();
Variable Description:
Assignment 23:
Consecutive Letters
Write a program to accept a string and display the words along with the frequency of the
words which have at least a pair of consecutive letters.
Sample Input:
Sample Output:
Algorithm:
Step 1: Start
Step 7: Check whether wrd contains consecutive pair or not. If true then go to step 8 else
go
to step 10.
import java.util.*;
class Cons
System.out.println("Enter a word:");
String st=sc.nextLine();
st=st.toUpperCase();
st=st+" ";
System.out.println(st);
int l=st.length();
int wordcount=0;
String wrd="";
for(int i=0;i<l;i++)
ch=st.charAt(i);
if(ch!=' ')
wrd=wrd+ch;
}
else
ch1=wrd.charAt(j);
ch2=wrd.charAt(j+1);
if((int)ch2-(int)ch1==1)
System.out.println(wrd);
wordcount++;
break;
wrd="";
}
Variable Description:
Output:
Assignment 24:
Largest and Smallest element in each row
Write a program to create a matrix A[][] of order (MxN) and store the numbers in it.
(Both M and N should be greater than 2 and less than 10).Find the largest and smallest
element in each row.
Sample Input:
1 2 3
4 5 6
7 8 9
Sample Output:
1 3 1
2 6 4
3 9 7
Algorithm:
Step 1: Start
Step 4: Check whether M<3, M>9, N<3 or N>9. If true then go to step 5 else go to step
6.
Step 12: Check whether A[i][j]>max. If true then go to step13 else go to step 14.
Step 14: Check whether A[i][j]<min. If true then go to step15 else go to step 15.
Program:
import java.util.*;
class MinMax
if(M<3||N<3||M>9||N>9)
System.out.println("OUT OF RANGE");
else
for(int i=0;i<M;i++)
for(int j=0;j<N;j++)
A[i][j]=sc.nextInt();
System.out.println("Matrix is:");
for(int i=0;i<M;i++)
for(int j=0;j<N;j++)
System.out.print(A[i][j]+"\t");
System.out.println();
}
int min, max;
System.out.println("Row\tMaximum\tMinimum");
for(int i=0;i<M;i++)
min=A[i][0];
max=A[i][0];
for(int j=1;j<N;j++)
if(A[i][j]>max)
max=A[i][j];
if(A[i][j]<max)
min=A[i][j];
System.out.println((i+1)+"\t"+max+"\t"+min);
}
Variable Description:
OUTPUT:
Assignment 25:
Sorting the boundary elements
Write a program to create a matrix A[][] of order (MxN) and store the numbers in it.
(Both M and N should be greater than 2 and less than 10). Sort the boundary elements
of the matrix. Display the sorted matrix.
Sample Input:
11 8 34
45 21 1
9 18 74
Sample Output:
1 8 9
74 21 11
45 34 18
Algorithm:
Step 1: Start
Step 4: Check whether M<3, M>9, N<3 or N>9. If true then go to step 5 else go to step
6.
Step 12: Store the sorted border elements in the matrix A[][].
Program:
import java.util.*;
class SortOuter
int M=sc.nextInt();
int N=sc.nextInt();
if(M<=2||N<=2||M>10||N>10)
System.out.println("OUT OF RANGE");
else
{
for(int i=0;i<M;i++)
for(int j=0;j<N;j++)
System.out.println("Enter a number:");
A[i][j]=sc.nextInt();
System.out.println("ORIGINAL MATRIX:");
for(int i=0;i<M;i++)
for(int j=0;j<N;j++)
System.out.print(A[i][j]+"\t");
System.out.println();
int size=2*(M+N-2);
int k=0;
for(int i=0;i<M;i++)
for(int j=0;j<N;j++)
if(i==0||j==0||i==M-1||j==N-1)
B[k++]=A[i][j];
//sorting
int t=0;
for(int i=0;i<size;i++)
for(int j=0;j<size-1-i;j++)
if(B[j+1]<B[j])
t=B[j];
B[j]=B[j+1];
B[j+1]=t;
}
k=0;
A[0][j]=B[k++];
A[i][N-1]=B[k++];
A[M-1][j]=B[k++];
A[i][0]=B[k++];
System.out.println("SORTED MATRIX:");
for(int i=0;i<M;i++)
{
for(int j=0;j<N;j++)
System.out.print(A[i][j]+"\t");
System.out.println();
Variable Description:
Output: