0% found this document useful (0 votes)
146 views121 pages

Krishna Singh Assignments

The document describes a program to sort the words in a sentence alphabetically without using sorting techniques. It accepts a sentence as input, converts it to uppercase, and calculates the length of each word. It then extracts each word, arranges the characters of the word alphabetically, and concatenates the sorted words with spaces to form a new sentence. It prints the original uppercase sentence, number of words, and new sentence without terminating punctuation. It checks for valid sentence termination characters and returns an error message for invalid input.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
146 views121 pages

Krishna Singh Assignments

The document describes a program to sort the words in a sentence alphabetically without using sorting techniques. It accepts a sentence as input, converts it to uppercase, and calculates the length of each word. It then extracts each word, arranges the characters of the word alphabetically, and concatenates the sorted words with spaces to form a new sentence. It prints the original uppercase sentence, number of words, and new sentence without terminating punctuation. It checks for valid sentence termination characters and returns an error message for invalid input.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 121

Assignment 1:

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

Write a program in Java to input a number and check whether it is a Fascinating


Number or not.
Fascinating Number: Some numbers of 3 or more digits exhibit a very interesting
property. The property is such that, when the number is multiplied by 3 and 2, and
both these products are concatenated with the original number, all digits from 1 to 9
are present exactly once, regardless of number of zeroes.
Example:
o 192
192 x 1 = 192
192 x 2 = 384
192 x 3 = 576
Concatenating the result: 192384576
It could be observed that ‘192384576’ consists of all digits from 1 to 9 exactly once,
Hence, it could be concluded that 192 is a Fascinating Number.
Some examples of Fascinating Numbers are: 192,219,273,327,1902,1920,2019etc.

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

Converting Number into equivalent base

Write a program to convert a decimal integer into equivalent base b (8,2,16).


Example 1:
Enter a decimal number:
42
Enter base:
8
Decimal Number: 42
Number in base 8=52
Example 2:
Enter a decimal number:
84
Enter base:
2
Decimal Number: 84
Number in base 2=1010100
Example 3:
Enter a decimal number:
45
Enter base:
16

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

Write a program to accept a sentence which may terminate by either . , ? or !.


Perform the following task.
1. Convert the sentence in Upper Case.
2. Print the length of the sentence word wise.
3. Sort each word of the sentence in alphabetical order without using any sort
technique.
4. Print Upper Case sentence, length of the string and the new sentence without
terminating character.
Example:
o Input: March is the month of ISC and ICSE Examination.
Output:
MARCH IS THE MONTH OF ISC AND ICSE EXAMINATION.
Number of Words:9
ACHMR IS EHT HMNOT FO CIS ADN CEIS AAEIIMNNOTX
o Input: How are you
Output:
INVALID INPUT
o Input: How are you@
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: Extract last character of the string (t).
Step 6: Check whether t is not equal to ‘?’, ‘.’, ‘!’ and ‘,’. If true then go to step 7 else go
to Step 8.
Step 7: Print “INVALID INPUT” and go to step 21.
Step 8: Declare and initialize the variables w=0, nst=””, wrd=””, swrd =”” , i=0, a=’A’
and ch=’ ‘.
Step 9: Repeat the steps 10 to 17 until i=len.
Step 10: Extract a word(wrd).
Step 11: Increase number of word (w) by one.
Step 12: Repeat the steps 13 to 15 while a<=’Z’.
Step 13: Extract each character ch of the string wrd.
Step 14: Check if ch is equal to a. If true then go to Step 15 else go to Step 16.
Step 15: swrd= swrd + ch.
Step 16: nst = nst + swrd +” “.
Step 17: Update wrd=””, swrd=”” and i=i+1.
Step 18: Print st.
Step 19: Print number of words, w.
Step 20: Print nst.
Step 21: Stop.
Program:
import java.util.*;
class WordSort
{
public void sort()
{
Scanner sc= new Scanner(System.in);
System.out.println("Enter a String:");
String st=sc.nextLine();
st=st.toUpperCase();
int len= st.length();
char t= st.charAt(len-1);
if(t!='?'&&t!='!'&&t!='.'&&t!=',')//condition to check termination character
{
System.out.println("INVALID INPUT");
}
else
{
char ch=' ';
int w=0;
String wrd="",swrd="";
String nst="";
for(int i=0;i<len;i++)
{
ch=st.charAt(i);
if(ch!='?'&&ch!='!'&&ch!='.'&&ch!=','&&ch!=' ')//extracting each word
{
wrd=wrd+ch;
}
else
{
w++;//counting number of words
//arranging characters in the word
for(char a='A';a<='Z';a++)
{
for(int j=0;j<wrd.length();j++)
{
if(wrd.charAt(j)==a)
{
swrd=swrd+wrd.charAt(j);
}
}
}
nst=nst+swrd+" ";
wrd="";
swrd="";
}
}
System.out.println(st);
System.out.println("Number of Words:"+w);
System.out.println(nst);
}
}
}

Variable Description:

Variable Data Type Description


st String To store the original sentence.
len Int To store the length of st.
t Char To store the last character of st.
ch Char To extract each character of st.
w Int To count the number of words in st.
wrd String To extract each word in st.
swrd String To store sorted word.
nst String To store the new sentence.
i Int Looping Variable
j Int Looping Variable

Output:
Assignment 5

Removing Repeated Words

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.

Step 9: Extract each word(wrd).


Step 10: Check wrd is not equal to pre. If true then go to Step 11 else go to Step 12.
Step 11: Concatenate nst=nst+wrd+” “ and go to Step 12.
Step 12: Set pre=wrd.
Step 13: Set wrd=””.
Step 14: Increase i=i+1.
Step 15: Print, “Orignal String”,st.
Step 16: Print, “Replaced String”,nst.
Step 17: Stop.

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:

Variable Data Type Description


date String To store date in a string variable
day String To store day in a string variable
dd int To convert and store day as an integer
month String To store month in a string variable
mm int To convert and store month as an integer
year String To store year in a string variable
yy int To convert and store year as an integer
limit int To store number of days in a month

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:

Variable Data Type Description


st String String in capital letter
T char Character to store the last character of the sentence
words[] String Array to store the words
p[] int Array to store potential of each word
size int Length of the arrays
c int Potential of each word
t int Variable to swap potential
temp String Variable to swap words
nst String String to store the re arranged words
i,j int Variables to generate index no.

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

131 is a Circular Prime.

Example 2:

INPUT :

29

OUTPUT:

29

92

29 is not a Circular Prime

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();
}

//Sum of corner elements


int s=A[0][0]+A[0][M-1]+A[M-1][0]+A[M-1][M-1];
System.out.println("Sum of corner elements:"+s);
}
}
}
Variable Description:
Variable Data Type Description
M int To store the size of the matrix
A[][] int To store the original matrix
R[][] int To store the rotated matrix
i int Looping variable
j int Looping variable
s int Sum of corner elements

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)

Write a program to declare a matrix A[][] order(MXN). Both M and N


should be grater than 2 and less than 8.
Perform the flowing task on the matrix:
i) Display the original matrix.
ii) Sort the matrix column wise in descending order.
iii)Display the sorted matrix.
Example 1:
M=3, N= 3 ORIGINAL
MATRIX:

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:

Write a program to declare a matrix A[][] order(MXN). Both M and N


should be greater than 2 and less than or equal to 10
Perform the flowing task on the matrix:
1. Display the original matrix.
2. Display the sum of Corner Elements of the original matrix
3. Sort the Matrix in ascending order.
4. Display the sorted matrix.
5. Display the sum of the corner elements of the sorted matrix

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];
}
}

//sorting the array B[]


int temp=0;
for(int i=0;i<M*N;i++)
{
for(int j=0;j<M*N-1-i;j++)
{
if(B[j+1]<B[j])
{
temp=B[j];
B[j]=B[j+1];
B[j+1]=temp;
}
}
}
//storing the sorted matrix
k=0;
for(int i=0;i<M;i++)
{
for(int j=0;j<N;j++)
{
A[i][j]=B[k++];
}
}
}
void display()
{
for(int i=0;i<M;i++)
{
for(int j=0;j<N;j++)
{
System.out.print(A[i][j]+"\t");
}
System.out.println();
}
}
public static void main(String args[])
{
System.out.println("Enter number of rows:");
int r=sc.nextInt();
System.out.println("Enter number of columns:");
int c=sc.nextInt();
if(r>2&&c>2&&r<=10&&c<=10)
{
SortMatrix obj=new SortMatrix(r,c);
obj.fillMatrix();
System.out.println("ORIGINAL MATRIX:");
obj.display();
System.out.println("Sum of corner elements before
sorting:"+obj.sumCorner());
obj.sort();
System.out.println("SORTED MATRIX:");
obj.display();
System.out.println("Sum of corner elements after
sorting:"+obj.sumCorner());
}
else
System.out.println("OUT OF RANGE.");

}
}
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:

Write a program to accept a sentence which may be terminated by either ‘.’


or ‘?’ only. The words are to be separated by a single blank space. Print an
error message if the input does not terminate with ‘.’ or ‘?’. You can assume
that no word in the sentence exceeds 15 characters, so that you get a proper
formatted output.
Perform the following task:
(i) Find the number of vowels and consonants in each word and display
them with proper headings
along with the words.
Example 1
INPUT:
Intelligence plus character is education.
OUTPUT:
Intelligence plus character is education

Word Vowels Consonants


Intelligence 5 7
plus 1 3
character 3 6
is 1 1
education 5 4
Example 2:
INPUT:
All the best!
OUTPUT:
Invalid Input.

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[])
{

Scanner sc=new Scanner(System.in);


System.out.println("Enter Sentence:");
String st=sc.nextLine();
int l=st.length();
if(st.charAt(l-1) != '.' && st.charAt(l-1) != '?')
{
System.out.println("Invalid Input.");
}
else
{
String wrd="";
int v=0,c=0,k=0;
char ch=' ', ch1=' ';
System.out.println("WORD\tVOWELS\tCONSONANTS");
for(int i=0;i<l;i++)
{
ch=st.charAt(i);
if(ch!=' '&& ch!='.'&&ch!='?')
wrd=wrd+ch;
else
{
k=wrd.length();

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

public void replaceChar()

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(n<=3||n>=10||n>9||m<=3||m>=10)


System.out.println("Out of Range");

else

char A[][] = new char[m][n]; //storing characters in the aarray.

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();

//Replacing vowels with next characters

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;

//Printing the Replaced Matrix

System.out.println("Replaced 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();

}
}
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.

For example, 1234 and 14779 are both increasing integers.

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.

Some examples of decreasing integers are 6432 and 988743.

A bouncy number’s digits neither increase nor decrease. Rather, they “bounce” between
increasing and decreasing.

Example: 12435 or 1441.

Write a program to accept a positive number and check whether it is a Bouncy number
or not.

Algorithm:

Step 1: Start

Step 2: Accept a positive number n.

Step 3: Declare and initialize f=0,f1=0

Step 4: Check the digits are in increasing order or not. If false then go to step 5 else go to
step 6

Step 5: Change the value of f=1.


Step 6: Check if the digits are in decreasing order or not. If false then go to step 7 else go
to 8

Step 7: Change the value of f1=1.

Step 8: Check f=1 and f1=1. If true then go to step 9 else go to step 10

Step 9: Print n, “is a Bouncy Number”

Step 10: Print n, “is not a Bouncy Number”

Step 11:Stop

Program:

/* Program to check whether the given number is a Bouncy number or not.*/

import java.util.*;

class BouncyNumber

public void isBouncy()

Scanner sc= new Scanner(System.in); System.out.println("Enter a number:");

int n=sc.nextInt(); int c=0, a=n;

//counting number of digits while(a>0)

a=a/10; c++;

int f=0,inc=0,d;

//checking whether it is in increasing order


a=n;

for(int j=c;j>=1;j--)

d=a/(int)Math.pow(10,j-1);//Extracting the left most digit

if(d>=inc)

inc=d; }

else

f=1; break;

a=a%(int)Math.pow(10,j-1);

//checking whether the digits are in decreasing order

int dec=9;

a=n;

for(int j=c;j>=1;j--)

d=a/(int)Math.pow(10,j-1);//Extracting the left most digit

if(d<=dec)

dec=d;

} else {
f1=1; break;

a=a%(int)Math.pow(10,j-1);

if(f==1&&f1==1)

System.out.println(n+" is a Bouncy Number.");

else

System.out.println(n+" is not a Bouncy Number.");

}
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

public void deno()

Scanner sc= new Scanner(System.in);

System.out.println("Enter Amount:");

int amt= sc.nextInt();

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;

int l=Integer.toString(amt).length(); //calculating number of digits

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;

System.out.println(d[i]+ "x" + r+"="+d[i]*r);

a=a%d[i];

System.out.println("Total number of notes:"+total);

}
}

OUTPUT:
Assignment 18:
Arranging words containing vowel in beginning

Write a program to accept a paragraph of two sentence. The sentences can be


terminated with either by ‘ ?’ or ‘!’ or ‘ .’ . The words are to be separated by single blank
space and must in Upper Case.

Perform the following tasks.

(a) Check the validity for the terminated characters.

(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:

Input: An Apple a day keeps the doctor away.

ORIGNAL SENTENCE:

AN APPLE A DAY KEEPS THE DOCTOR AWAY.

REARRANGED SENTENCE:

APPLE A AN DAY KEEPS THE DOCTOR AWAY

Example 2:

Input 2: How are you

Invalid String.

Algorithm:

Step 1: Start
Step 2: Accept a sentence st.
Step 3: Convert it into uppercase.

Step 4: Calculate its length l.

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 7: Declare and initialize variables i=0, vow=”” and cons=””

Step 8: Repeat the steps 9 to 14 until i=l

Step 9: Extract a word wrd.

Step 10: Check whether the first and last character of the wrd is a vowel or

not. If yes then go to step 11 else go to step 12

Step 11: Concatenate vow, wrd and a space and go to step 13.

Step 12: Concatenate cons, wrd and a space.

Step 13: Update wrd=””,

Step 14: Update i=i+1

Step 15: Create a new string by concatenating vow and cons.

Step 16: Print st

Step 17: Print nst

Step 18: Stop

Program:

import java.util.*;

class Vowels
{

public void merge()

Scanner sc= new Scanner(System.in);

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

String st= sc.nextLine();

st=st.toUpperCase(); //coverting to Upper Case

int l=st.length();

char last=st.charAt(l-1); // extracting last character

//checking the validity of the last character

if(last!='.' && last!='?'&&last!='!' )

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

else

String nst="", wrd="",vow="",cons="";

char ch=' ', ch1=' ', ch2= ' ';

int wl=0;

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

ch=st.charAt(i);

//Extracting each word


if(ch!=' '&&ch!='?'&&ch!='!'&&ch!='.')

wrd=wrd+ch;

else

wl=wrd.length();

ch1=wrd.charAt(0); //Extracting first character of the word

ch2=wrd.charAt(wl-1); //Extracting last character of the word

//Checking whether both characters are vowel or not

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

cons=cons+wrd+" "; // joins the other words

wrd="";

nst=vow+cons;

System.out.println(st);

System.out.println(nst);
}

Variable Description

Variable Data type Description


st String The sentence
l Int Length of the sentence
Terminating character of the
last Char
string
To extract each character of the
ch Char
string
wrd String To extract each word of the string
Loop control variable to generate
i Int the index number of each
character
wl Int Length of the word
ch1 Char First character of the word
ch2 Char Last character of the word
Words that begin and end with
vow String
vowel
cons String Other words
nst String New word

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:

EXAMPLE 1: DAY NUMBER: 255

YEAR: 2018

OUTPUT: DATE: 12 SEPTEMBER, 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.*;

public class Day

public static void main(String[] args)

Scanner sc=new Scanner(System.in);

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};

String months[]= {"","JANUARY","FEBRUARY","MARCH",

"APRIL","MAY","JUNE","JULY",

"AUGUST","SEPTEMBER",

"OCTOBER","NOVEMBER","DECEMBER"};

if(y%4==0)//checking for leap year

mdays[2]=29;

int i=1;

//checking whether the number of days greater than each month


while(d>mdays[i])

d=d-mdays[i];//reducing no of days of the month

i++;

System.out.println("Date:"+ d+" "+months[i]+","+ y);

else

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

Variable Description

Variable Data type Description


d int Number of days
year Int Year
mdays[] Int Array to store total number of
days of each month.
months[] String Array to store the name of each
month.
i Int Loop control variable to count the
month.

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.

Perform the following tasks on the matrix:

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:

If the array a[] = {5, 2, 8, 1} then, after sorting a[] = {1, 2, 5, 8}

Then, the matrix b[][] would fill as below:

1 2 5 8

1 2 5 1

1 21 2

11 2 5

Algorithm:

Step 1: Accept order of the Matrix N.


Step 2: Check N<3 or N>9. If true then go to step 3 else go to 4
Step 3: Print “Out of Range”. Go to step 21
Step 4: Create a Single Dimensional array B [] of order N
Step 5: Create a double dimensional array A[][] of order N x N.
Step 6: Store elements in the array B [].
Step 7: Sort the array using Bubble sort.
Step 8: Display the sorted array.
Step 9: Declare and initialize the variables i=0, n=N-1
Step 10: Repeat the steps 11 to 19 until i=n.
Step 11: Declare and initialize a variable j=0
Step 12: Repeat the step 13 to 14 until j>n
Step 13: Assign A[i][j] = B[j]
Step 14: Increase j by 1
Step 15: Declare and initialize the variables k=n+1, p=0
Step 16: Repeat the steps 17 to 18 until k=N
Step 17: Assign A[i][k] =B[p]
Step 18: Increase the variables k and p by 1
Step 19: Increase by 1 and decrease N by 1
Step 20: Print the Matrix
Step 21:Stop

Program:

import java.util.*;

public class MatrixPattern

public static void main(String[] args)

Scanner sc=new Scanner(System.in);

System.out.println("Enter the order of the Matrix:");

int N=sc.nextInt();

if(N<3 || N>9)

System.out.println("Out of range");
else

int B[]= new int[N];

int A[][]= new int[N][N];

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

System.out.println("Sorted Array is:");

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

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

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

for(int j=0;j<=n;j++)//storing upper pattern

A[i][j]=B[j];

for(int k=n+1, p=0;k<N;k++,p++)//storing lower pattern

A[i][k]=B[p];

//Displaying the Matrix

System.out.println("\n The Matrix is:");

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

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

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

System.out.println();

Variable Description:

Variable Data Type Description


B[] Int To store the array
A[][] Int To store the matrix
M Int Number of rows
N Int Number of columns
i, j Int Looping Variables
T Int Sorting Variable
k, p Int Variables for restoring elements in thr array

Output:
Assignment 21:
Mirror Image

Write a program in Java to enter natural numbers in a double dimensional array m x n


(where m is the number of rows and n is the number of columns). Display the new
matrix in such a way that the new matrix is the mirror image of the original matrix.

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 2: Accept number of rows (M) and number of columns (N).

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.

Step 4: Create a Matrix A[][] of order MxN and store elements.

Step 5: Print the matrix A[][].


Step 6: Repeat the steps 7 to 9 for c=0 to N-1 increase by 1.

Step 7: Repeat the steps 8 to 9 for i=0 to M-1 increase by 1.

Step 8: Repeat the step 9 for j=N-1 to c+1 decrease by 1.

Step 9: Swap the elements of A[i][j] and A[i][j-1].

Step 10: Print the Mirror Image of the Matrix A[][].

Step 11: Stop

Program:

import java.util.*;

class Mirror

public void coumnSort()

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>10||N>10)

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();

//Creating Mirror Image in the same array by shifting columns

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;

//Printing the Mirror Image of the Matrix

System.out.println("MIRROR IMAGE OF THE 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


A[][] Int To store the matrix
M Int Number of rows
N Int Number of columns
i, j Int Looping Variable
t, c Int Variables to create mirror image

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.

Sample Input: 666

Sum of the digits: 6 + 6 + 6 = 18

Prime factors are: 2, 3, 3, 37

Sum of the digits of the prime factors: 2 + 3 + 3 + (3 + 7) = 18

Thus, 666 is a Smith Number.

Algorithm:

Step 1: Start

Step 2: Accept a number n.

Step 3: Check whether it not a composite number . If true then go to step 4 else go to
step 5.

Step 4: Print n, “is not a Composite Number” and go to step

Step 5: Calculate the sum of digit of digits of n(dsum).

Step 6: Calculate the sum of digits of its prime factor(sp);

Step 7: Check whether dsum=sp. If true then go to step 8 else go to step 9.

Step 8: Print n, “is a Smith Number.” and go to step 10.

Step 9: Print n, is not a Smith Number”.

Step 10: Stop


Program:

import java.util.*;

public class Smith

int n;

public void accept()

Scanner sc = new Scanner(System.in);

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()

//checking whether n is composite or not

int f=0;

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

if(n%i==0)

f=1;

break;

}
}

if(f==0)

System.out.println(n+"is not a Composite Number.");

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);

System.out.println("Sum of digits of Prime Factors:"+dsum);

if(dsum==sp)

System.out.println(n+" is a Smith Number.");


else

System.out.println(n+"is not a Smith Number.");

public static void main(String args[])

Smith obj = new Smith();

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:

Modem is an electronic device

Sample Output:

Algorithm:

Step 1: Start

Step 2: Accept a String st.

Step 3: Convert it into uppercase.

Step 4: Calculate its length (l);

Step 5: Repeat the steps 6 to 10 for i=0 to l-1 increases by 1.

Step 6: Extract each word of the sentence(wrd).

Step 7: Check whether wrd contains consecutive pair or not. If true then go to step 8 else
go

to step 10.

Step 8: Print wrd.

Step 9: Increase wordcount by 1. And go to step 10

Step 10: Set wrd=””.

Step 11: Print “Number of words contain consecutive pairs:”, wordcount.

Step 12: Stop


Program:

import java.util.*;

class Cons

public void display()

Scanner sc= new Scanner(System.in);

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;

char ch=' ', ch1=' ',ch2=' ';

String wrd="";

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

ch=st.charAt(i);

if(ch!=' ')

wrd=wrd+ch;
}

else

for(int j=0;j<wrd.length()-1; j++)

ch1=wrd.charAt(j);

ch2=wrd.charAt(j+1);

if((int)ch2-(int)ch1==1)

System.out.println(wrd);

wordcount++;

break;

wrd="";

System.out.println("Number of words contain consecutive letters:"+wordcount);

}
Variable Description:

Variable Data type Description


st Int To store the sentence
wrd Int To extract and store each word
l Int Length of the sentence (st)
ch,ch1,ch2 Int To store extracted characters
wordcount Int Number of words containing consecutive letters
i,j Int Looping Variables

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:

Row Maximum Minimum

1 3 1

2 6 4

3 9 7

Algorithm:

Step 1: Start

Step 2: Accept number of rows (M).

Step 3: Accept number of columns (N).

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 5: Print “OUT OF RANGE.” and go to step 17


Step 6: Declare a matrix A[][] of order and store elements in it.

Step 7: Print the matrix A[][].

Step 8: Repeat the steps 9 to 16 for i=0 to M-1 increase by 1.

Step 9: Set max=A[i][0]

Step 10: Set min=A[i][0]

Step 11: Repeat the steps 12 to 15 for j=1 to N-1 increase by 1

Step 12: Check whether A[i][j]>max. If true then go to step13 else go to step 14.

Step 13: Set max= A[i][j]

Step 14: Check whether A[i][j]<min. If true then go to step15 else go to step 15.

Step 15: Set min= A[i][j] and go to step 16.

Step 16: Print (i+1), max, min.

Step 17: Stop

Program:

import java.util.*;

class MinMax

public void find()

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<3||N<3||M>9||N>9)

System.out.println("OUT OF RANGE");

else

int A[][]= new int[M][N];

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

System.out.println("Enter row elements:");

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:

Variable Data Type Description


A[][] Int To store the matrix
M Int Number of rows
N Int Number of columns
i, j Int Looping Variable
Min Int Smallest element of the row
Max Int Largest element of the row

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 2: Accept number of rows (M).

Step 3: Accept number of columns (N).

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 5: Print “OUT OF RANGE.” and go to step 14

Step 6: Declare a matrix A[][] of order and store elements in it.


Step 7: Print the matrix A[][].

Step 8: Calculate size=2(M+N-2)

Step 9: Declare an array B[] of size.

Step 10: Store the boundary elements in the array B[].

Step 11: Sort the array B[] using bubble sort.

Step 12: Store the sorted border elements in the matrix A[][].

Step 13: Display the Sorted matrix A[][];

Step 14: Stop

Program:

import java.util.*;

class SortOuter

public void sort()

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>10||N>10)

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();

int size=2*(M+N-2);

int B[]=new int[size];


//storing outer elements

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;

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

A[0][j]=B[k++];

for(int i=1; i<M;i++)

A[i][N-1]=B[k++];

for(int j=N-2; j>=0;j--)

A[M-1][j]=B[k++];

for(int i=M-2; i>0;i--)

A[i][0]=B[k++];

//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


A[][] Int To store the matrix
B[] Int Array to store the elements of the matrix
M Int Number of rows
N Int Number of columns
i, j Int Looping Variable
K Int Variable to store matrix elements in the array(B[])
T Int To sort the elements in ascending order

Output:

You might also like