0% found this document useful (0 votes)
7 views

Annual Computer Project 11

Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Annual Computer Project 11

Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 46

ANNUAL COMPUTER PROJECT

Shashank Mishra

11A

21/02/2024

11th Grade Computer Science


Program 1: Arrange the word according to the strength in
descending order.
Program:
import java.util.Scanner;
import java.util.StringTokenizer;
class WordStrength
{
void main()
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter a word : ");
String s = sc.nextLine();
StringTokenizer str= new StringTokenizer(s);
int c = str.countTokens();
String n[]= new String[c];
int p=0;
while(str.hasMoreTokens())
{
n[p]= str.nextToken();
p++;
}
int su=0;
int max=0;
String hs= "";
for(int i=0;i<c;i++){
su=0;max=0;
for(int j=0;j<n[i].length();j++)
{
char ch = Character.toUpperCase(n[i].charAt(j));
su = su+(ch-64);
}
if(max<su){
max = su;
hs = n[i];
}
}
System.out.println("Maximum Strength : " + hs + " , Value : " + max);
}
}

Output:
Enter a word :
We are doing recursion in java
Maximum Strength : recursion , Value : 122

Algorithm:
Start
Step 1: Input a word in variable s
Step 2: declare a variable c to count the number of tokens
Step 3: declare an array n in of string; String n[]= new String[c]; and
initialise p to 0
Step 4: store different words of the string s in array n
Step 5: initialise su and max to 0, String hs =””;
Step 6: write a prototype to find out the word having maximum strength and
then print the word having maximum strength
Stop

Variables list:
Data name Data type Description
s String stores the sentence
str String breaks the sentence
into different words
c Integer count the number of
words in a sentence
n[] String array to store all the
words of a sentence
p Integer used to input words in
array
su Integer stores the value of word
max Integer stores maximum value
of a word
hs String stores the word of
maximum strength
i Integer Loop variable
j Integer Loop variable
ch Character Check variable
Program 2: Insertion sort
Program:
import java.util.Scanner;
class insertion
{
Scanner sc = new Scanner(System.in);
void display(){
System.out.println("Enter number of elements of array: ");
int n = sc.nextInt();
int a[]= new int[n];
System.out.print("Enter an array: ");
for(int i =0;i<n;i++){
a[i]= sc.nextInt();
}
int t;
for(int i=1;i<10;i++)
{
int c= i-1;
t = a[i];
while(c>=0 && t<=a[c])
{
a[c+1]= a[c];
c--;
}
a[c+1]=t;
}
System.out.println("The inserted array is: ");
for(int i =0;i<n;i++){
a[i]= sc.nextInt();
}
}
}

Output:
Enter number of elements of array:
5
Enter an array:
58672
The inserted array is:
25678

Algorithm:
Start
Step 1: input the number of elements in variable n
Step 2: declare an array a and input the elements in the array
Step 3: write a prototype for insertion in an array
Step 4: print the inserted array
Stop

Variables list:
Data name Data type Description
n Integer number of
elements in an
array
a[] Integer stores all the
elements
i Integer loop variable
t Integer variable to
change the stored
number
c Integer were you able to
change the
position
Program 3: To add any two accepted time.
Program:
import java.util.Scanner;
class Adder
{
int a []= new int[2];
Adder()
{
for(int i=0;i<2;i++)
{
a[i]=0;
}
}
void readtime()
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter hour and minute: ");
for(int i=0; i<2;i++)
{
a[i]= sc.nextInt();
}
}
void addtime(Adder X,Adder Y)
{
for(int i =1;i>=0;i--){
a[i]= X.a[i]+Y.a[i];
}
a[0]= a[0]+a[1]/60;
a[1]= a[1]%60;
}
void distime(){
System.out.println("Hour is: "+ a[0]);
System.out.println("Minutes is"+a[1]);
}
void main()
{
Adder a1= new Adder();
Adder a2 = new Adder();
Adder a3 = new Adder();
a1.readtime();
a2.readtime();
a3.addtime(a1,a2);
System.out.println("1st time is: ");
a1.distime();
System.out.println("2nd time is: ");
a2.distime();
System.out.println("Added time is: ");
a3.distime();
}
}
Output:
Enter hour and minute:
2
45
Enter hour and minute:
3
25
1st time is:
Hour is: 2
Minutes is45
2nd time is:
Hour is: 3
Minutes is25
Added time is:
Hour is: 6
Minutes is10

Algorithm:
Start
Step 1: create an array of size 2 and in the constructor initialise both the
elements of the array to zero
Step 2: create a function readtime 2 input the data of hour and minute for
both the two times
Step 3: create a function void addtime with Adder X and Adder Y as formal
arguments to add both the times and convert the minute into its proper
value if it is exceeding above 60 or equal to 60
Step 4: create a function while distime to display the final added hours and
minutes
Step 5: create a function void main and call all the functions in it except the
both the times and add 4 the times to get the new resultant added time
display both the original times and the new added time
Stop

Variables list:
Data name Data type Description
a[] Integer stores the value
of both the two
times
i Integer loop variable

Program 4: Hamming Number


Program:
import java.util.Scanner;
class hammimg{
Scanner sc = new Scanner(System.in);
boolean hamming(int n)
{

while(n%2==0)
n= n/2;
while(n%3==0)
n= n/3;
while(n%5==0)
n= n/5;
return (n==1);
}
void main(){
Scanner sc = new Scanner(System.in);
System.out.println("enter a number: ");
int n = sc.nextInt();
if(hamming(n)==true)
System.out.println("Hamming no.");
else
System.out.println("Not a hamming number");
}
}
Output:
enter a number:
20
Hamming no.
Or,
enter a number:
14
Not a hamming number
Algorithm:
Start
Step 1: create a function boolean hamming with n as a formal argument and
check that if the integer north is divisible by 2 or 3 or 5 then the function
returns the value as one that is the function returns the true value
Step 2: create a void main function to except the value of the number and
then call the previous function to check whether the number is a hamming
number or not, if the function returns the true value then display that the
number is a hamming number otherwise it is not a hamming number
Stop
Variables list:
Data name Data type Description
n Integer stores the number to be
checked

Program 5: Accept a sentence and count the number of


words beginning with a vowel
Program:
import java.util.*;
class VowelWord
{
String str;
int freq;
Scanner sc = new Scanner(System.in);
VowelWord(){
str= "";
freq =0;
}
void readstr(){
System.out.println("Enter a sentence: ");
str = sc.nextLine();
}
void freq_vowel(){
StringTokenizer st = new StringTokenizer(str);
int c = st.countTokens();
for(int i=0;i<c;i++){
String s = st.nextToken();
char ch = s.charAt(0);
if(ch=='A'||ch=='E'||ch=='I'||ch=='O'||ch=='U'||ch=='a'||ch=='e'||
ch=='i'||ch=='o'||ch=='u')
freq++;
}
}
void display(){
System.out.println("ORIGINAL SENTENCE is: "+str);
System.out.println("THE FREQUENCY OF WORDS BEGINNING WITHA
VOWEL is: "+freq);
}
void main(String args[]){
VowelWord obj = new VowelWord();
obj.readstr();
obj.freq_vowel();
obj.display();
}
}

Output:
Enter a sentence:
We only love to eat icecream
ORIGINAL SENTENCE is: We only love to eat icecream
THE FREQUENCY OF WORDS BEGINNING WITHA VOWEL is: 3

Algorithm:
Start
Step 1: intialise String str to “” and freq to 0 in the constructor
Step 2: create a readstr function to input the string
Step 3: create a freq_vowel function to breakdown the string into different
different words and then count the number of tokens present in the string
Step 4: design a prototype to find the frequency of vowels acquiring in each
of the words
Step 5: in the function display display the original sentence and the
frequency of the words beginning with a vowel and create a main function
Stop

Variables list:
Data name Data type Description
str String stores the
sentence
freq Integer stores the number of
words beginning with a
vowel
c Integer counts the
number of words
in a sentence
i Integer loop variable
s String call the next word of a
sentence
ch Character check variable
st String breaks the
different words of
the sentence
Program 6: Frequency of words “an” and “and” in a
sentence
Program:
import java.util.StringTokenizer;
import java.util.Scanner;
class Frequency
{
String text;
int countAnd;
int countAn;
int len;
Frequency(){
text = "";
countAnd =0;
countAn = 0;
len=0;
}
void accept(String n){
text = n.toLowerCase();
countAnd =0;
countAn = 0;
len = text.length();
}
void checkAndFreq(){
StringTokenizer st= new StringTokenizer(text);
int count = st.countTokens();
for(int i =1;i<=count;i++){
String word = st.nextToken();
if(word.equals("and"))
countAnd++;
}
}
void checkAnFreq(){
StringTokenizer st= new StringTokenizer(text);
int count = st.countTokens();
for(int i =1;i<=count;i++){
String word = st.nextToken();
if(word.equals("an"))
countAn++;
}
}
void display(){
System.out.println("Number of ANDs "+countAnd);
System.out.println("Number of ANs "+countAn);
}
void main(){
Scanner sc = new Scanner(System.in);
System.out.println("Enter a sentence");
String s = sc.nextLine();
Frequency obj = new Frequency();
obj.accept(s);
obj.checkAndFreq();
obj.checkAnFreq();
obj.display();
}
}
Output:
Enter a sentence
There once live an elephant and an owl and a lion
Number of ANDs 2
Number of ANs 2

Algorithm:
Start
Step 1: create String text; int countAnd ,countAn, len and initialise them to
“”, 0,0,0 respectively in the constructor
Step 2: create function accept function convert the text to the lower cases
assign countAnd and countAn to zero and find out the length of the text
Step 3: create 2 functions checkAndFreq and checkAnFreq to count the
number of times ‘and’ and ‘an’ has occurred in the string
Step 4: in the function display ,display the number of times ‘and’ and
number of times ‘ an’ has occurred
Step 5: in thefunction main call all the previous functions and accept the
string and display the frequency of ‘and’ and ‘an’
Stop

Variables list:
Data name Data type Description
text String stores the
sentence in lower
case
countAnd Integer stores the
frequency of and
countAn Integer stores the
frequency of an
len Integer stores the
number of letters
in the sentence
n String stores the
sentence
i Integer loop variable
s String stores the original
sentence
Program 7: Frequency of vowels and consonants of each
word in a sentence
Program:
import java.util.*;
class str{
void main(){
String s;
Scanner sc = new Scanner(System.in);
System.out.println("Enter a string: ");
s = sc.nextLine();
StringTokenizer st = new StringTokenizer(s);
int l = st.countTokens();
int v=0,c=0;
String word = st.nextToken();
for(int i=0;i<l;i++)
{
for(int j =0;j<word.length();j++){
char ch = word.charAt(j);
if(ch=='A'||ch=='E'||ch=='I'||ch=='O'||ch=='U'||ch=='a'||
ch=='e'||ch=='i'||ch=='o'||ch=='u')
v++;
else
c++;
}
}
System.out.println(word+" No of Vowels"+v+" No of consonants "+c);
}
}

Output:
Enter a string:
The king is always elegant
elegant No of Vowels 8 No of consonants 14
Algorithm:
Start
Step 1: input a string in variable s
Step 2: declare a variable l to count the number of tokens present in the
string s
Step 3: initialise int v and c to zero
Step 4:String word = st.nextToken();
Step 5: create a prototype to count the number of vowels and consonants
and then display them
Stop

Variables list:
Data name Data type Description
s String storage the
sentence
st String breaks the
sentence into
different words
l Integer counts the
number of words
in a sentence
v Integer frequency of
vowels
c Integer frequency of
consonants
word String calls the next
word
i Integer loop variable
j Integer loop variable
ch Character check variable
Program 8: Write a program to declare a square matrix M
[ ] [ ] of order ‘N’ where ‘N’ must be greater than 3 and
less than 10.
Allow the user to accept three different characters from
the keyboard and fill the array according to the
instruction given below:
(i) Fill the four corners of the square matrix by character 1.
(ii) Fill the boundary elements of the matrix (except the four corners)
by character 2.
(iii) Fill the non-boundary elements of the matrix by character 3.
Program:
import java.util.*;
class mat
{
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter no.of rows M of a square matrix:");
int m=sc.nextInt();
if(m<3 || m>10)
{
System.out.println("Wrong Data ");
}
else
{
char mat[][] = new char[m][m];
System.out.println("Enter corner elements - 1st character:");
char ch1=sc.next().charAt(0);
System.out.println("Enter boundary except corner elements -
2ndcharacter:");
char ch2=sc.next().charAt(0);
System.out.println("Enter non boundary - 3rdcharacter:");
char ch3=sc.next().charAt(0);
for(int i=0; i<m; i++)
{
for(int j=0; j<m;j++)
{
//fill corners with char 1
if( (i==0||i==m-1) && (j==0 || j==m-1))
mat[i][j]=ch1;
//fill non-corner boundary elememts with char 2
else if(i==0 || j==0 || i==m-1 || j==m-1)
mat[i][j]=ch2;
//fill rest with char 3
else
mat[i][j]=ch3;
}
}
//print the array
System.out.println("The array is");
for(int i=0; i<m; i++)
{
for(int j=0; j<m; j++)
{
System.out.print(mat[i][j] + "\t");
}
System.out.println();
}
}
}
}

Output:
Enter no.of rows M of a square matrix:
4
Enter corner elements - 1st character:
@
Enter boundary except corner elements - 2ndcharacter:
%
Enter non boundary - 3rdcharacter:
&
The array is
@ % % @
% & & %
% & & %
@ % % @

Algorithm:
Start
Step 1: input the size of the square matrix; show that if the size enter is
lesser than 3 or greater than 10 then it is wrong data
Step 2: input all those 3 types of characters which are required to be entered
as according to the question that is for the corner elements for boundary
elements except the corner elements and the non boundary elements
Step 3: create a prototype to fill all these 3 different types of characters in in
3 specific positions as asked in the question
Step 4: print the final matrix
Stop
Variables list:
Data name Data type Description
m Integer stores the size of
square matrix
mat[][] Character matrix that stores all
the characters
ch1 Character stores character 1
ch2 Character stores character 2
ch3 Character stores character 3
i Integer loop variable
j Integer loop variable
Program 9: Spiral Matrix
Program:
class SpiralPattern
{

void printSpiralPattern(int size)


{

int row = 0, col = 0;


int boundary = size - 1;
int sizeLeft = size - 1;
int flag = 1;

char move = 'r';

int[][] matrix =new int [size][size];


for (int i = 1; i < size * size + 1; i++)
{

matrix[row][col] = i;

switch (move)
{

case 'r':
col += 1;
break;

case 'l':
col -= 1;
break;

case 'u':
row -= 1;
break;

case 'd':
row += 1;
break;
}

if (i == boundary)
{

boundary = boundary + sizeLeft;

if (flag != 2)
{
flag = 2;
}
else
{
flag = 1;
sizeLeft -= 1;
}

switch (move)
{

case 'r':
move = 'd';
break;

case 'd':
move = 'l';
break;

case 'l':
move = 'u';
break;

case 'u':
move = 'r';
break;
}
}
}

for (row = 0; row < size; row++)


{

for (col = 0; col < size; col++)


{
int n = matrix[row][col];
if(n < 10)
System.out.print(n +" ");
else
System.out.print(n +" ");
}
System.out.println();
}
}

void main(String args[])


{
int size = 5;
System.out.println("Spiral Matrix or Pattern is: \n");
printSpiralPattern(size);
}
}

Output:

1 2 3 4
12 13 14 5
11 16 15 6
10 9 8 7

Algorithm:
Start
Step 1: create a function printSpiralPattern with formal argument as int
size ;assign int row and int col to 0
Step 2: int boundary = size-1;
Step 3: int sizeLeft = size-1; int flag =1;
Step 4:char move =’r’;
Step 5: create a 2D array of size as taken in the input and then create a
prototype to make a spiral matrix
Step 6: in the main function display the spiral matrix
Stop

Variables list:
Data name Data type Description
row Integer stores value for
row
col Integer stores value for
column
size Integer store the size of the
matrix
boundary Integer storage limiting
size of the matrix
sizeLeft Integer stores the value
of size left
flag Integer check variable
move Character check variable
i Integer loop variable

Program 10: Accepting two moneys and adding them


Program:
import java.util.Scanner;
class Money
{
int rs,ps;
void acceptmoney(){
Scanner sc = new Scanner(System.in);
System.out.println("Enter rupess: ");
rs = sc.nextInt();
System.out.println("Enter paise: ");
ps = sc.nextInt();
}
void fnShow(){
System.out.println("Money is: "+rs+"."+ps);
}
void fnAdd(Money m1,Money m2){
ps = m1.ps+m2.ps;
rs= m1.rs+m2.rs+ ps/100;
ps = ps%100;
}
void main(){
Money a1 = new Money();
Money a2 = new Money();
Money a3 = new Money();
a1.acceptmoney();
a2.acceptmoney();
a3.fnAdd(a1,a2);
System.out.println("First money is: ");
a1.fnShow();
System.out.println("Second money is: ");
a2.fnShow();
System.out.println("Added money is: ");
a3.fnShow();
}
}
Output:
Enter rupess:
5
Enter paise:
50
Enter rupess:
2
Enter paise:
35
First money is:
Money is: 5.50
Second money is:
Money is: 2.35
Added money is:
Money is: 7.85

Algorithm:
Start
Step 1: declare variables rs and ps
Step 2: in the function accept money enter the value of rupees and enter the
value of paise
Step 3: in the function fnShow ,show the submission of total money that is
the summation of rupees and paise
Step 4: in the function fnAdd, add both the moneys m1 and m2 and find the
resultant money if the paise exceeds above 100 then adjust it
Step 5: when the function void main call all the previous functions and then
accept money m1 and money m2 add the money m1 and m2 and then
display money m1 and m2 and finally display the resultant money after
addition
Stop

Variables list:
Data name Data type Description
rs Integer stores rupees
ps Integer stores paise
Program 11: To find longest and smallest word in a
sentence
Program:
import java.util.Scanner;
import java.util.StringTokenizer;
class strProg
{
String t;
StringTokenizer st;
String ar[];
void input(){
Scanner sc = new Scanner(System.in);
System.out.println("Enter a sentence: ");
t = sc.nextLine();
st = new StringTokenizer(t);
ar = new String[st.countTokens()];
int i =0;
while(st.hasMoreTokens()){
ar[i]= st.nextToken();
i++;
}
findBigandSmall();
}
void findBigandSmall(){
int max,min,i;
String maxw= "", minw= "";
max = min = ar[0].length();
for( i =0;i<ar.length;i++){
if(max<ar[i].length()){
max = ar[i].length();
maxw = ar[i];
}
if(min>=ar[i].length()){
min = ar[i].length();
minw = ar[i];
}
}
System.out.println("Longest word is: "+maxw+" size: "+ max);
System.out.println("Smallest word is: "+minw+" size: "+ min);
}
}
Output:
Enter a sentence:
We love java
Longest word is: love size: 4
Smallest word is: We size: 2

Algorithm:
Start
Step 1: .Scanner class called
Step 2: String Tokenizer class called
Step 3.A class strProg is made
Step 4.String t is taken
Step 5. String ar[] is taken
Step 6. A function void input is made
Step 7. Inside void input, Scanner is taken and t is taken as input from user
Step 8. st = new Stringtokenizer(t)
Step 9. ar = new String[st.countTokens()];
Step 10. A while loop is run with i variable where ar[i] = st.nextToken() is
used inside the loop.
Step 11. A function void findBigandSmall is made to find longest and shortest
word
Step 12. int max and int min is taken, to store length of the words, and two
strings maxw and minw is taken to store the longest and shortest words
respectively.
Step 13. A for loop is run to determine the longest and shortest word from
the sentence by using .length .
Step 14. Longest word length stored in max and the word is stored in maxw
Step 15. Shortest word length stored in min and the word is stored in minw
Step 16. max , min, maxw and minw is printed
Stop

Variables list:
Data name Data type Description
t String stores sentence
st String breaks different
words of a
sentence
ar[] String stores words of a
sentence
i Integer loop variable
max Integer stores maximum
value
min Integer stores minimum
value
maxw String stores word
having maximum
value
minw String stores word
having minimum
value
Program 12: Combination of given variables
Program:
import java.util.Scanner;
class Combination{
int n,k,com;
Combination(){
n=0;
k=0;

}
void read(){
Scanner sc = new Scanner(System.in);
System.out.println("Enter value of n: ");
n = sc.nextInt();
System.out.println("Enter value of k: ");
k = sc.nextInt();

}
int fact(int f){
if(f==0||f==1)
return f;
else
return (f*fact(f-1));
}
void compute(){
com = (fact(n)/(fact(k)*fact(n-k)));
}
void display(){
System.out.println("The combination is: "+com);
}
void main(){
Combination ob = new Combination();
ob.read();
ob.compute();
ob.display();
}
}

Output:
Enter value of n
10
Enter value of k
4
The combination is: 210

Algorithm:
Start
Step 1.Scanner class is called
Step 2. Class named Combination is made
Step Three integer variables n, k and com is taken
Step 4. Constructor is made to initialise n and k to 0
Step 5. A function void read is made to enter values of n and k
Step 6. A function int fact(int f) is made to carry out factorials. If f=0 or 1,
then it would return f else it would return f*fact(f-1).
Step 7. A function void compute is made to carry out the combination.
Step 8. Inside compute function - com = (fact(n)/(fact(k)*fact(n-k))).
Step 9. A function void display is made to print the result of the combination.
Step 10. void main is made.
Stop

Variables list:
Data name Data type Description
n Integer stones the value
of n
k Integer stores the value
of k
com Integer stores the value
of combination
f Integer stores the
factorial of a
number
Program 13: Mirror Matrix
Program:
import java.util.*;
class MirrorMatrix{
void main(){
Scanner sc = new Scanner(System.in);
System.out.println("Enter rows and column for matrix");
int m = sc.nextInt();
int n = sc.nextInt();
int A1 [][]= new int[m][n];
int A2 [][]= new int [m][n];
System.out.println("Enter numbers in the matrix");
int i,j;
for(i=0;i<m;i++){
for(j=0;j<n;j++){
A1[i][j]= sc.nextInt();
}
}
for(i=0;i<m;i++){
for(j=0;j<m;j++){
A2[i][n-1-j]= A1[i][j];
}
}
System.out.println("The mirrored matrix is: ");
for(i=0;i<m;i++){
for(j=0;j<n;j++){
System.out.print(A2[i][j]+"\t");
}
System.out.println();
}
}
}

Output:
The original matrix is:
1 23
4 5 6
7 8 9
The mirrored matrix is:
3 2 1
6 5 4
9 8 7

Algorithm:
Start
Step 1: input the number of rows of matrix in m and number of rows of
column in n
Step 2: input the numbers in the matrix
Step 3: create a prototype to make a mirror image of the original matrix
Step 4: print the mirror matrix
Stop

Variables list:
Data name Data type Description
m Integer stores number of
rows
n Integer stores number of
columns
A1 Integer stores original
matrix
A2 Integer stores mirror
matrix
i Integer loop variable
j Integer loop variable
Program 14: To check whether two words are palindrome
or not
Program:
import java.util.Scanner;
import java.util.StringTokenizer;
class prg{
String t;
StringTokenizer st;
String ar[];
void input(){
Scanner sc = new Scanner(System.in);
System.out.println("Enter a sentence");
t = sc.nextLine();
st = new StringTokenizer(t);
ar = new String[st.countTokens()];
int i =0;
while(st.hasMoreTokens()){
ar[i]= st.nextToken();
i++;
}
}
void palindrome(){
int i,j;
for(i=0;i<ar.length;i++){
String w = "";
for( j =0;j<ar[i].length();j++)
w = ar[i].charAt(j)+w;
if(ar[i].equals(w))
System.out.println(w);

}
}
}

Output:
Enter a sentence
madam is teaching computer
Madam is palindrome

Algorithm:
Start
Step 1.Scanner class is called
Step 2. String Tokenizer class is called
Step 3. Class prg is made
Step 4. A string t is taken, along with a String array ar[]
Step 5.A function void input is made to input a sentence t by using Scanner.
Step 6. st = new StringTokenizer(t)
Step 7. ar = new String[st.countTokens()];
Step 8. A while loop is run with ar[i] = st.nextToken()
Step 9. A function void palindrome is made which would check if the word is
palindrome or not.
Step 10. Inside void palindrome, a for loop is with i variable run from 0 till
ar.length, inside which another for loop is with j variable run from 0 to
ar.length. A string w is made which contains ar[i].charAt[j] + w which would
check if it is palindrome or not and i would print the palindrome number
Stop

Variables list:
Data name Data type Description
t String stores the word
st String breaks the world
ar[] String store each letter of a
word
i Integer loop variable
j Integer loop variable
w String creates the
palindrome of the
original word
Program 15: To concatenate two prime numbers
Program:
import java.util.Scanner;
class number{
int n;
number(){
n=0;
}
void input(){
Scanner sc = new Scanner(System.in);
System.out.println("Enter a number");
n = sc.nextInt();
}
boolean isPrime(int d){
for(int i =2;i<=d/2;i++){
if(d%i==0)
return false;
}
return true;
}
void concat_prime(number n1){
String t ="";
while(n>0){
int d1 = n%10;
n = n/10;
if(isPrime(d1))
t = d1+t;
}
int num =n1.n;
while(num>0){
int d2= num%10;
num = num/10;
if(isPrime(d2))
t =d2+t;
}
System.out.println("The concatenated number is "+t);

}
void main(){
number ob1 = new number();
number ob2 = new number();
ob1.input();
ob2.input();
ob1.concat_prime(ob2);
}
}

Output:
Enter a number
23
Enter a number
31
The concatenated number is 3123
Algorithm:
Start
Step 1.Scanner class is called
Step 2. A class Number is made
Step An integer variable n is taken
Step 4. A constructor is made to initialise n to 0
Step A function void input is made to input the number
Step 6. A boolean function isPrime with int d is made to check if the number
is prime or not
Step 7. Inside boolean isPrime - a for loop with variable i is run from 2 till
d/2. If d%i is zero then false is returned, else true.
Step 8. A function void concat prime is made which concatenates the two
numbers if they are proved to be prime. It is done by calling isPrime(d1) and
isPrime(d2) and running a while loop and storing the number in a variable t.
Step 9. Void main is made
Stop

Variables list:
Data name Data type Description
n Integer stores one
number
d Integer stores digits of
number
i Integer loop variable
t String stores
concatenated
number
d1 Integer stores digit for
first number
d2 Integer stores digit for
second number
Program 16: Combining array of integers
Program:
import java.util.Scanner;
class Combine{
Scanner sc =new Scanner(System.in);
int com[]= new int[10];
void inputarry(){
for(int i =0;i<10;i++){
com[i]= sc.nextInt();
}
}
void sort(int v[]){
for(int i =0;i<v.length-1;i++){
int min = v[i];
int pos =i;
for(int j = i+1;j<v.length;j++){
if(min>v[j]){
min = v[j];
pos =j;
}
}
if(pos!=1){
int temp = v[i];
v[i]= v[pos];
v[pos]= temp;
}
}
}
void display(){
for(int i=0;i<com.length;i++){
System.out.println(com[i]+" ");
}
}
void mix(CombineA,CombineB){
int a[]= new it[A.com.length+B.com.length]
for(i=0;i<A.com.length;i++){
a[i]= A.com[i];
}
for(i=0;i<B.com.length;i++){
a[i]= B.com[j];
}
sort(a);
}
void main(){
Combine c1= new Combine();
Combine c2= new Combine();
Combine c1= new Combine();
c1.inputarray();
c2.inputarray();
c3.mix(c1,c2);
System.out.println("The first array is");
c1.display();
System.out.println("The second array is");
c2.display();
System.out.println("Mixed sorted array is");
c3.display();
}
}

Output:
Enter array elements for first array
1
2
3
4
5
6
7
8
9
10
Enter array elements for second array
10
9
8
7
6
5
4
3
2
1
The combined array is
1234567891010987654321

Algorithm:
Start
Step 1.Scanner called
Step 2. Combine class made
Step 3. Array is inputted by calling Scanner and running a for loop for
inputting each element
Step 4. The array is sorted by making void sort and taking int v[].
Step 5. The sorted array is displayed by void display
Step 6. Void mix is made which combines the two arrays inputted by
running to for loops for each array.
Step 7. Void main is made
Stop

Variables list:
Data name Data type Description
com[] Integer stores and array of
10 integers
i Integer loop variable
min Integer store the
minimum value
pos Integer shows the
position
j Integer loop variable
a[] Integer stores the sorted
combined array
temp Integer used to interchange
the position of
elements
v[][] Integer sorting of an array
Program 17: Unique number
Program:
import java.util.Scanner;
class unique{
int n;
void input(){
Scanner sc = new Scanner(System.in);
System.out.println("Enter a number");
n =sc.nextInt();
}
void check(){
int cnt =0,d,copy,flag=1;
for(int i =0;i<=9;i++){
cnt =0;
copy =n;
while(n>0){
d= n%10;
n=n/10;
if(d==i)
cnt++;
}
if(cnt>1){
flag=0;
break;
}
}
}
void main(){
unique ob = new unique();
ob.input();
ob.check();
}
}

Enter a number
3456
Unique number

Algorithm:
Start
Step 1.Scanner called
Step 2. A variable int n is taken
Step 3. A number is inputted by user in n by using void input
Step 4. Void check is made which checks if a number is unique or not by
running a while loop of digit extraction.
Step 5. A variable flag is taken which returns 0 if count of digits is greater
than 1.
Step 6. By this we can check if all the digits are different or not. If they are
different then the number is a unique number.
Step 7. Void main is made
Stop

Variables list:
Data name Data type Description
n Integer stores the
number
cnt Integer check variable
d Integer store 3 digit of a
number
copy Integer store copy of a
number
flag Integer check variable
i Integer loop variable
Program 18: Spy Number
Program:
import java.util.Scanner;
class spy{
int n,p,s;
void input(){
Scanner sc = new Scanner(System.in);
System.out.println("Enter a number");
n =sc.nextInt();
int copy =n;
}
void check(){
int d;
while(n>0){
d= n%10;
p=p*d;
s=s+d;
n=n/10;
}
if(p==s)
System.out.println("Its is a spy number");
else
System.out.println("Its is not a spy number");
}
void main(){
spy ob = new spy();
ob.input();
ob.check();
}
}

Output:
Enter a number
132
Its is a spy number

Algorithm:
Start
Step 1.Scanner class called
Step 2. Class spy is made
Step 3. Three variables n,p,s are made.
Step 4. Void input is used to input a number in n.
Step 5. Void check is made to check whether the number is a spy number or
not.
Step 6. Inside void check - a while loop is run which extracts the digits and
multiplies the digits and adds them. If they are equal then it is a spy
number.
Step 7. Void main is made
Stop

Variables list:
Data name Data type Description
n Integer stores the
number
p Integer stores product of
digits
s Integer stores sum of
digits
copy Integer stores duplicate
of original
number
d Integer store digits of
number
Program 19: Kaprekar Number
Program:
import java.util.Scanner;
class kaprekar{
Scanner sc = new Scanner(System.in);
int n,sum,sq;
void input(){
System.out.println("Enter a number");
n = sc.nextInt();
sq=n*n;
}
void check(){
String t =""+sq;
int len = t.length();
String f = t.substring(0,len/2);
String l = t.substring(len/2);
int n1= Integer.parseInt(f);
int n2= Integer.parseInt(l);
if((n1+n2)==n)
System.out.println("Kaprekar no.");
else if(len%2==1){
f= t.substring(0,len/2+1);
l = t.substring(len/2+1);
n1= Integer.parseInt(f);
n2= Integer.parseInt(l);
if((n1+n2)==n)
System.out.println("Kaprekar no.");
else
System.out.println("not a kaprekar number");
}
else
System.out.println("Not a kaprekar number");
}
void main(){
kaprekar ob = new kaprekar();
ob.input();
ob.check();
}
}

Output:
Enter a number
45
Kaprekar no.

Algorithm:
Start
Step 1.Scanner class called
Step 2. Kaprekar class is made
Step 3. A number is inputted in variable n and it is squared in void input
Step 4. Void check is made to check whether it is kaprekar or not
Step 5. Inside void check-
String t stores the square of the number in string form
Int len stores the length of the string.
String f and l are made which are substrings of t.
Int n1 and n2 is made by parsing f and l
If n1 and n2 equal to n then it is kaprekar number else it is not a kaprekar
number
Step 6.void main is made
Stop

Variables list:
Data name Data type Description
n Integer stores the
number
sq Integer stores the square
of a number
t String stores square of
digit of the
number
len Integer stores the length of
the number found
by squaring the
digits of the original
number
f String stores the first
part of the new
no
l String stores the last
part of the new
number
n1 Integer stores the
integer value of
first part of new
number
n2 Integer store the integer
value of second
part of new
number
Program 20: Magic number
Program:
import java.util.Scanner;
class magic{
Scanner sc = new Scanner(System.in);
int n,sum;
void input(){
System.out.println("Enter a number");
n = sc.nextInt();
}
boolean isMagic(int a ){
while(a>0){
int d = a%10;
a=a/10;
sum = sum+d;
if(a==0&&sum>9){
a=sum;
sum=0;
}
}
if(sum==1)
return true;
else
return false;
}
void display(){
System.out.println("Original number is "+n);
if(isMagic(n))
System.out.println("It is a magic number");
else
System.out.println("It is not a magic number");
}
void main(){
magic ob = new magic();
ob.input();
ob.display();
}
}

Output:
Enter a number
127
It is a magic number

Algorithm:
Start
Step 1.Scanner class is called
Step 2. Magic class is made
Step Two variables int and sum are taken
Step A number inputted by user is stored in n.
Step 5. Boolean isMagic is made to check if the number is a magic number or
not.
Step 6.Inside boolean isMagic -
while(a>0)
int d = a%10
a=a/10
sum = sum + d
if(a==0&&sum>9)
a=sum
sum=0
Step 7. If sum is returned true then magic number else not a magic number
Step 8. Void display is used to print the number and its output
Step 9. Void main is made
Stop

Variables list:
Data name Data type Description
n Integer stores the
number
a Integer stores the value
after dividing the
number by 10
d Integer stores the digit of
the number
sum Integer stores the sum
Program 21: Happy number
Program:
import java.util.Scanner;
class happy{
Scanner sc = new Scanner(System.in);
int n,sum;
void input(){
System.out.println("Enter a number");
n = sc.nextInt();
}
boolean isHappy(int a ){
while(a>0){
int d = a%10;
a=a/10;
sum = sum+(d*d);
if(a==0 && sum>9){
a=sum;
sum=0;
}
}
if(sum==1)
return true;
else
return false;
}
void display(){
System.out.println("Original number is "+n);
if(isHappy(n))
System.out.println("It is a happy number");
else
System.out.println("It is not a happy number");
}
void main(){
happy ob = new happy();
ob.input();
ob.display();
}
}

Output:
Enter a number
49
Original number is 49
It is a happy number

Algorithm:
Start
Step 1.Scanner class is called
Step 2. Class Happy is made
Step 3. Two variables n and sum are taken.
Step 4. Void input is made in which a number is inputted by the user in n
Step 5. Boolean isHappy is made which checks if a number is happy
number or not.
Step 6. Inside boolean isHappy(int a)
while(a>0)
int d = a%10
a=a/10
sum=sum+(d*d)
if(a==0&&sum>9)
a=sum
sum=0
Step 7. If sum is returned equal to one then happy number else not happy
number
Step 8. Void display is to print number output
Step 9. Void main is made
Stop

Variables list:
Data name Data type Description
n Integer stores the
number
sum Integer stores the sum of
square of the
digits
a Integer formal argument
of the number
d Integer stores digit of a
number

THANKYOU…

You might also like