0% found this document useful (0 votes)
2 views83 pages

Computer Project Uvi

The document outlines a computer project submitted by Arihan Khatri, which includes various programming tasks primarily focused on matrix operations, string manipulations, and sorting algorithms. Each task is accompanied by an algorithm and sample code in Java, covering functionalities such as matrix transposition, diagonal sums, boundary element extraction, and sorting methods. The document serves as a comprehensive guide for implementing these programming exercises.

Uploaded by

shouryay2508
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)
2 views83 pages

Computer Project Uvi

The document outlines a computer project submitted by Arihan Khatri, which includes various programming tasks primarily focused on matrix operations, string manipulations, and sorting algorithms. Each task is accompanied by an algorithm and sample code in Java, covering functionalities such as matrix transposition, diagonal sums, boundary element extraction, and sorting methods. The document serves as a comprehensive guide for implementing these programming exercises.

Uploaded by

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

1

COMPUTERPROJ
ECT

Submitted to:- Submitted


by:-
Mrs.Anjna Mohindru Arihan Khatri,UVI

pg. 1
2

662

Index
1 Transpose of matrix
2 Write a program to convert a string into piglatin form 3 Input
a 2D array and print the sum of the diagonals of the array.
4 Take input for a 2D array and check whether it is a square
matrix.
5 Write a program to take input for a 2D array and print the
boundary elements.
6 Write a program using recursion to print a number entered by
the user to a power entered by the user.

7 Take input of 2 matrices and display the sum of those 2


matrices in another matrix.
8 Write a program to take input for a 2D array and print the
sum of the columns.
9 Write a program to take input for a 2D array and print the
sum of columns and rows
10 Write a program to display the vowels and the most
prominent vowel from a string entered by the user.
11 Write a program to display any words that start and end with
a vowel and display the same string but now with the words
starting and ending with vowels at the beginning.
12 Write a program to accept strings in a 2D array and arrange
the words in increasing order of size and display them in a
new array.
13 Take input in a 2D array and print the even element of that
array.
14 Replace all vowels of a sentence with succeeding letter
15 Program to delete extra spaces

pg. 2
3

16 Intercepted message
17 Anagram
18 Write a program using recursion to print the reverse of a
number entered by the user.
19 Insertion Sorting And Selection Sorting

20 Write a program using recursion to print the factorial of a


number entered by the user.
21 Write a program using recursion to convert a number entered
by the user from binary to decimal or from decimal to binary.
22 Write a program using recursion to print the reverse of a
string entered by the user.
23 Write a program using recursion to search for the position of
an element in an array using binary search.
24 Magic number using recursion
25 Finding HCF using recursion
26 Armstrong number using recursion
27 Decimal to binary conversion using recursion
28 Interface program
29 Write a program in Java that implements a stack using an
array.

30 Write a program in Java that implements a basic queue data


structure.

31 Triangle Number
32 Evil Number
33 Smith number
34 Composite Magic Number

pg. 3
4

35 Number of words that start and end with a vowel.


36 Printing no. of vowels and consonants of each word in a
sentence
37 Fascinating number
38 Bouncy Number
39 Program to find whether a number is Keith or not
40 Delete duplicate elements in an array

pg. 4
5

1) Take input in a 2D array and print the even element of that array.

Algorithm:
1. Start
2. Define a method "calculate" that takes in a 2D array of integers as its
parameter
3. In the calculate method print the message "The even elements of the
array are:
"
4. Using two for loops, iterate through the 2D array
5. Within the nested for loops, check if the current element is even
6. If the element is even, print the element
7. Stop

Code:
import java.util.*;
public class even2d
{
public void calculate(int a[][])
{
System.out.println("The even elements of the array are: ");
for(int i=0;i<3;i++)
{ for(int j=0;j<3;j+
+)
{ if(a[i][j]
%2==0)
{
System.out.println(a[i][j]);
}
}
}
}

public static void main()


{
Scanner sc=new
Scanner(System.in); int a[]
[]=new int[3][3]; for(int
i=0;i<3;i++)
{ for(int j=0;j<3;j+
+)
{
System.out.println("Enter the elements"); a[i]
[j]=sc.nextInt();
}
}

pg. 5
6

even2d ob=new even2d();


ob.calculate(a);
}
}

Output:

pg. 6
7

2) Input a 2D array and print the sum of the diagonals of the array.

pg. 7
8

Algorithm:
1. Start
2. Initialize two variables, "b" and "c", to store the sum of the diagonal
elements.
3. Check if "i" is equal to "j", if so, add the element of the array located at (i,j)
to
"b".
4. In another loop, add the element of the array located at (i,2-i) to "c".
5. Print the sum of the left to right diagonal elements, "b", and the sum of the
right to left diagonal elements, "c".
6. Stop

Code:
import java.util.*;
public class diagonal
{
public void calculate(int a[][])
{
int b=0; int
c=0; for(int
i=0;i<3;i++)
{ for(int j=0;j<3;j+
+)
{ if(i==j)
{ b=b+a[i]
[j];}
}
} for(int
i=0;i<3;i++)
{
int j=2;

c=c+a[i][j];
j--;
}
System.out.println("sum of left to right diagonal "+b);
System.out.println("sum of right to left diagonal "+c);

}
public static void main()
{
Scanner sc=new Scanner(System.in);
int a[][]=new int[3][3];
System.out.println("Enter the
elements"); for(int i=0;i<3;i++)

pg. 8
9

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

a[i][j]=sc.nextInt();
}
}
System.out.println ("The numbers
are"); for (int i=0;i<3;i++)
{
for (int j=0;j<3;j++)
{
System.out.print(a[i][j]+" ");
}
System.out.println();

}
diagonal ob=new diagonal();
ob.calculate(a);
}
}

Output:

pg. 9
10

4) Take input for a 2D array and check whether it is a square matrix.

Algorithm:
1. Start
2. Create a method "input" to take the size of the array and input elements of
the square matrix.
3. Create a method "check" to determine whether the matrix is a square
matrix or not.
4. If it is a square matrix, print the matrix and "The matrix is a square matrix
and the numbers are".
5. If it is not a square matrix, print "The matrix is not a square matrix".
6. Stop

Code:
import java.util.*;
public class square
{
public void input(int x[][], int r, int c)
{

pg. 1
0
11

Scanner sc=new Scanner(System.in);


System.out.println("Enter the numbers");
for(int i=0;i<r;i++)
{ for(int j=0;j<c;j+
+)
{ x[i]
[j]=sc.nextInt();
}
}
}

void check(int a[][],int r, int c, boolean s)


{

if(r==c)
{
s=true;
}

if(s==true)
{

System.out.println ("The matrix is a square matrix and the numbers


are"); for (int i=0;i<r;i++)
{
for (int j=0;j<c;j++)
{
System.out.print(a[i][j]+" ");
}
System.out.println();

}
}
else
{
System.out.println("The matrix is not a square matrix and the numbers
are:"); for (int i=0;i<r;i++)
{
for (int j=0;j<c;j++)
{
System.out.print(a[i][j]+" ");
}
System.out.println();

}
}

pg. 1
1
12

public static void main()


{

square ob=new square();


Scanner sc=new Scanner(System.in);
System.out.println("Enter the number of
columns"); int c=sc.nextInt();
System.out.println("Enter the number of
rows"); int r=sc.nextInt(); int a[][]=new
int[r][c]; boolean s=false; ob.input(a,r,c);
ob.check(a,r,c,s);

}
}
Output:

pg. 1
2
13

5) Write a program to take input for a 2D array and print the boundary
elements.

Algorithm:
1. Start
2. Take the number of rows and columns of the matrix as input.
3. Read the elements of the matrix.
4. Initialize variables x and y as r-1 and c-1 respectively.
5. Use a nested for loop to iterate over the matrix elements.
6. In the inner for loop, check if the current row i is not equal to 0 or x and if
the current column j is not equal to 0 or y.

pg. 1
3
14

7. If both conditions are true, print a blank space.


8. If either of the conditions is false, print the value of the matrix element a[i]
[j].
9. Stop

Code:
import java.util.*;
public class
boundary
{

void output(int a[][],int r,int c)


{
Scanner sc=new
Scanner(System.in); int x=r-1; int
y=c-1; for(int i = 0; i < r; i++)
{
for(int j = 0; j < c; j++)
{ if(i!=0&&i!
=x)
{ if(j!=0&&j!
=y)
{
System.out.print(" ");
}
else
{
System.out.print(a[i][j]);
}
}
else{
System.out.print(a[i][j]);
}
}
System.out.println();
}

public static void main()


{
Scanner sc=new Scanner(System.in);
System.out.println("Enter the number of
rows"); int r=sc.nextInt();

pg. 1
4
15

System.out.println("Enter the number of


columns"); int c=sc.nextInt(); int a[][]=new
int[r][c]; System.out.println("Enter matrix:");
for(int i = 0; i < r; i++)
{
for(int j = 0; j < c; j++)
{
a[i][j] = sc.nextInt();
}
}
boundary ob=new boundary();
ob.output(a,r,c);
}
}
Output:

pg. 1
5
16

1) Take input of 2 matrices and display the sum of those 2 matrices in


another matrix.

Algorithm:
1. Start
2. Initialize two 3x3 matrices, a and b.
3. Call the input method and pass matrix a to it to take input from the user
and store it in matrix a.
4. Call the input method again and pass matrix b to it to take input from the
user and store it in matrix b.
5. Call the output method and pass matrix a to it to print the elements of
matrix a.
6. Call the output method again and pass matrix b to it to print the elements
of matrix b.
7. Call the sum method and pass matrices a and b to it to calculate the sum
of matrices a and b and store it in matrix c.

pg. 1
6
17

8. Call the output method and pass matrix c to it to print the elements of the
result matrix c.
9. Stop

Code:
import java.util.*;
class matrixadd
{
public void input(int x[][])
{
Scanner sc=new Scanner(System.in);
for(int i=0;i<3;i++)
{ for(int j=0;j<3;j+
+)
{ x[i]
[j]=sc.nextInt();
}
}
}

void sum(int a[][],int b[][])


{
int c[][]=new int[3][3];
for(int i=0;i<3;i++)
{ for(int j=0;j<3;j+
+)
{

c[i][j]=a[i][j]+b[i][j];
}
}
output(c)
;
}

public void output(int a[][])


{ for(int
i=0;i<3;i++)
{ for(int j=0;j<3;j+
+)
{
System.out.print(a[i][j]);
}
System.out.println();
}
}

pg. 1
7
18

public static void main()


{
Scanner sc=new
Scanner(System.in); int a[]
[]=new int[3][3]; int b[][]=new
int[3][3]; matrixadd ob=new
matrixadd();
System.out.println("Enter elements of 1st matrix");
ob.input(a);
System.out.println("Enter elements of 2st matrix");
ob.input(b);
System.out.println("Matrix 1");
ob.output(a);
System.out.println("Matrix 2");
ob.output(b);
System.out.println("Result");
ob.sum(a,b);
}
}

pg. 1
8
19

Output:

6) Insertion Sorting And Selection Sorting

Code:
import java.util.*;
class sort
{ int a[];
int n;
int

pg. 1
9
20

min;
int p;
int t;

sort(int nn)
{
n=nn;
a=new
int[nn];
}

public void input()


{
Scanner sc=new Scanner(System.in);
System.out.println("Enter the elements");
for(int i=0;i<n;i++)
{ a[i]=sc.nextInt(
);
}
}

public void selectionsort()


{

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


{ min=a[i]; for(int
j=i+1; j < n; j++){
if(min > a[j]){
min=a[
j]; p=j; }
t=a[i];
a[i]=a[p];
a[p]=t;
}
}

System.out.println("Sorted element");
for(int i=0;i<n;i++)
{
System.out.println(a[i]);
}
}

void insertionsort()
{ for (int i=1;i<n;
i++)

pg. 2
0
21

{
int temp=a[i]; int j=i-
1; while
(j>=0&&a[j]>temp)
{ a[j+1]=a[
j]; j--;
}
a[j+1]=temp;
}
System.out.println("Sorted element");
for(int i=0;i<n;i++)
{
System.out.println(a[i]);
}
}

public static void main()


{
Scanner sc=new Scanner(System.in); System.out.println("Enter the size");
int n=sc.nextInt();
System.out.println("Enter 1 for selection sort or 2 for insertion
sort"); char s=sc.next().charAt(0); sort ob=new sort(n);
ob.input(); switch(s)
{
case '1':
{ ob.selectionsort(
); break;
}
case '2':
{ ob.insertionsort(
); break;
}

}
}
}

Output:

pg. 2
1
22

7) Transpose of a matrix

Algorithm:
1. Start
2. Initialize a 2-D array of size 3x3

pg. 2
2
23

3. Take input from the user and store it in the array


4. Display the original matrix
5. Transpose the matrix and display the transposed matrix
6. Stop

Code:
import java.util.Scanner;
public class Transpose
{
public static void main()
{ int i, j;
Scanner s = new
Scanner(System.in); int array[][] =
new int[3][3];
System.out.println("Enter matrix:");
for(i = 0; i < 3; i++)
{
for(j = 0; j < 3; j++)
{
array[i][j] = s.nextInt();
System.out.print(" ");
}
}
System.out.println("The above matrix before Transpose is ");
for(i = 0; i < 3; i++)
{
for(j = 0; j < 3; j++)
{
System.out.print(array[i][j]+" ");
}
System.out.println(" ");
}
System.out.println("The above matrix after Transpose is ");
for(i = 0; i < 3; i++)
{
for(j = 0; j < 3; j++)
{
System.out.print(array[j][i]+" ");
}
System.out.println(" ");
}
}
}

pg. 2
3
24

Output:

8) Write a program to take input for a 2D array and print the sum of
the columns.

Algorithm
1. Start
2. Create a 3x3 array and read the elements from the user.
3. In the class, create a method called "calculate" that takes an array of
integers as an input.
4. In the "calculate" method, initialize a variable "c" to zero.
5. Use two for loops to iterate over the rows and columns of the array.
6. In the inner loop, add the current element to the variable "c".
7. After the inner loop, print the sum of the current column using the value of
"c".
8. Reset the value of "c" to zero before the next iteration of the outer loop.
9. Print the elements of the array.
10.Stop

Code import
java.util.Scanner;

pg. 2
4
25

import java.util.*;
public class column
{
public void calculate(int a[][])
{
int c=0; for(int
i=0;i<3;i++)
{ for(int j=0;j<3;j+
+)
{ c=c+a[j]
[i];
}
System.out.println("sum of column"+
(i+1)+":"+c); c=0;
}
}

public static void main()


{
Scanner sc=new
Scanner(System.in); int a[][]=new
int[3][3];
System.out.println("Enter the elements");
for(int i=0;i<3;i++)
{ for(int j=0;j<3;j+
+)
{

a[i][j]=sc.nextInt();
}
}
System.out.println ("The numbers
are"); for (int i=0;i<3;i++)
{
for (int j=0;j<3;j++)
{
System.out.print(a[i][j]+" ");
}
System.out.println();

}
column ob=new column();
ob.calculate(a);
}
}

pg. 2
5
26

Output:

9) Write a program to take input for a 2D array and print the sum of
columns and rows

Algorithm:
1. Start
2. Initialize the class fourbyfour with a 4x4 array
3. Call the input method to take input from the user
4. Call the output method to display the input and the sum of the rows and
columns
5. Stop

Code:
import java.util.*;
public class
fourbyfour
{ int a[][];
fourbyfour(
)
{
a=new int[4][4];

pg. 2
6
27

void input(){
Scanner sc=new Scanner(System.in);
System.out.println ("Enter the
numbers"); for (int i=0;i<4;i++)
{
for (int j=0;j<4;j++)
{ a[i]
[j]=sc.nextInt();
}

}
}

void output()
{ int
sumr=0; int
sumc=0;
System.out.println ("The numbers
are"); for (int i=0;i<4;i++)
{
for (int j=0;j<4;j++)
{
System.out.print(a[i][j]+" ");
}
System.out.println();

} for (int
i=0;i<4;i++)
{
for (int j=0;j<4;j++)
{sumc=sumc+a[j][i];
}
System.out.println("the sum of columns "+sumc);
sumc=0;
} for (int
i=0;i<4;i++)
{
for (int j=0;j<4;j++)
{sumr=sumr+a[i][j];
}
System.out.println("the sum of rows "+sumr);
sumr=0;
}
}

pg. 2
7
28

public static void main()


{

fourbyfour ob=new
fourbyfour(); ob.input();
ob.output();
}
}
Output:

pg. 2
8
29

10)Write a program to display the vowels and the most prominent


vowel from a string entered by the user.

Algorithm:
1. Start
2. Create an array "ar" to store the vowels present in the input string.
3. Loop through the input string, checking if each character is a vowel (i.e. a,
e, i, o,
u). If it is, add it to the "ar" array.
4. Create an array "v" containing the vowels a, e, i, o, u.
5. Loop through the "v" array and count the number of occurrences of each
vowel in the "ar" array.
6. Keep track of the most frequent vowel and its count, updating it if a more
frequent vowel is found.
7. Print the vowels in the "ar" array and the most frequent vowel with its
count.
8. Stop

Code:
import java.util.*;
class vowels
{
void check(String s)
{ int l=s.length(); char c=' ';
int i,g=0,max=0,cnt=0;
char ar[]=new
char[s.length()]; char
v[]={'a','e','i','o','u'};
for(i=0;i<l;i++)
{ c=s.charAt(i); if(c=='a' ||c=='e' ||c=='i'
||c=='o' ||c=='u')
{ ar[g]=s.charAt(i
); g++;
}
} for(int
j=0;j<5;j++)
{
cnt=0; for(int
k=0;k<g;k++)
{ if(v[j]==ar[k
])
{ cnt+
+;
}
}
if(cnt>=max)
{

pg. 2
9
30

max=cnt;
c=v[j];
}
}
for(int k=0;k<g;k++)
System.out.print(ar[k]+", ");
System.out.println("The prominent vowel is "+ c + "- "+ max);
}
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter the
string"); String s=sc.nextLine();
vowels ob=new vowels();
ob.check(s);
}
}

Output:
11)
12)Write a program to display any words that start and end with a
vowel and display the same string but now with the words
starting and ending with vowels at the beginning.

Algorithm:
1. Start
2. Read the input string from the user.
3. Create an object of the class stringvowel and pass the string to the
constructor.
4. Call the tokenizer() method.
5. Convert the string into an array of words using the StringTokenizer class.
6. Count the number of words that start and end with a vowel.
7. Store the words starting and ending with vowels in one array and the rest
of the words in another array.
8. Concatenate both the arrays to form a new string.
9. Print the number of words starting and ending with vowels and the final
string.
10.Stop

Code:
import java.util.*;
public class
stringvowel

pg. 3
0
31

{
String a;
stringvowel(String s)
{ a=s
;
}

void tokenizer()
{
StringTokenizer st=new
StringTokenizer(a); String arr[]=new
String[st.countTokens()]; int c=0;
while(st.hasMoreTokens())
{
arr[c]=st.nextToken();
c++;
}
int count=0; for(int
i=0;i<arr.length;i++)
{
char a=arr[i].charAt(0); char
b=arr[i].charAt(arr[i].length()-1);
if(a=='a'||a=='e'||a=='i'||a=='o'||
a=='u')
{ if(b=='a'||b=='e'||b=='i'||b=='o'||
b=='u')
{
count++;
}
}
}
System.out.println("Number of words that start and end with an vowel:
"+count);

String v[]=new String[count];


String con[]=new String[arr.length-
count]; int j=0; int q=0; for(int
i=0;i<arr.length;i++)
{
char a=arr[i].charAt(0); char
b=arr[i].charAt(arr[i].length()-1);
if(a=='a'||a=='e'||a=='i'||a=='o'||
a=='u')
{ if(b=='a'||b=='e'||b=='i'||b=='o'||
b=='u')
{ v[j+
+]=arr[i];

pg. 3
1
32

else con[q+
+]=arr[i];
}

String str=""; for(int

i=0;i<v.length;i++)

{ str=str+v[i]+" ";

for(int i=0;i<con.length;i++)
{
str=str+con[i]+" ";
}
System.out.print(str);
}

public static void main()


{
Scanner sc=new Scanner(System.in);
System.out.println("Enter the string");
String s=sc.nextLine();
StringTokenizer st=new
StringTokenizer(s," .!?,"); stringvowel ob=new
stringvowel(s); ob.tokenizer();
}
}

Output:

pg. 3
2
33

pg. 3
3
34

12) Write a program to accept strings in a 2D array and arrange the


words in increasing order of size and display them in a new array.

Algorithm:
1. Start
2. Accept the number of rows and columns from the user and store it in
variables m and n.
3. Create a 2D array of strings a[][] with size m x n and initialize it with user
input strings.
4. Call the check() method to determine the longest and shortest word in the
array and print it.
5. Call the print() method to print the original matrix and a new matrix where
the elements above the diagonal are printed.
6. Call the rotate() method to rotate the matrix and print the rotated matrix.
7. Stop

Code:
import java.util.*;
public class
large_min
{
String a[][]; int m,n;
large_min(int mm,int
nn)
{
m=mm;
n=nn; a=new
String[m][n];

void check()
{
String max=""; String
min=a[0][0]; for(int
i=0;i<m;i++){ for(int
j=0;j<n;j++){ int
f=a[i][j].length();
if(f>max.length())
max=a[i][j];
if(f<max.length())
min=a[i][j];}
}
System.out.println("longest word="+max+"\n shortest
word="+min); print();
}

pg. 3
4
35

void print(){

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]);
}
System.out.println();}
System.out.println("new matrix:");

for(int i=0;i<m;i++){
for(int j=0;j<m;j++)
{ if(i<j)
System.out.print(a[i][j]);
else
System.out.print(" ");

}
System.out.println();}}

void rotate(){
String b[][]=new String[m]
[n]; for(int i=0;i<m-1;i++)
{ for(int j=0;j<n;j++)
{ b[i+1][j]=a[i][j];}
} for(int
i=0;i<n;i++)
{ b[0][i]=a[n-1]
[i];}

System.out.println("new matrix:");
for(int i=0;i<m;i++){
for(int j=0;j<m;j++){
System.out.print(b[i]
[j]); }
System.out.println();}

public static void main()


{
Scanner sc=new
Scanner(System.in);

pg. 3
5
36

System.out.println("Enter
dimensions"); int m=sc.nextInt(); int
n=sc.nextInt(); large_min ob=new
large_min(m,n); String s[][]=new
String[m][n];
System.out.println("Enter the
strings"); for(int i=0;i<m;i++){
for(int j=0;j<n;j++){
ob.a[i][j]=sc.next();}}

ob.rotate();
}
}

Output:

pg. 3
6
37

13) Write a program to convert a string into piglatin form,

1. Start
2. Store the input sentence, convert it to lowercase, and append a space at
the end.
3. Initialize an index variable f to track the start of each word.
4. Iterate through each character in the sentence.
5. When a space is encountered, extract the word, convert it to Pig Latin
using the convert method, and append it to the result string s
6. Display the converted sentence stored in the s variable.
7. Stop

Code:
import java.util.*;
class
piglatin{ String s;
piglatin()
{ s="";
}

void extract(String a){ int


f=0; for(int
i=0;i<a.length();i++)
{ if(a.charAt(i)==' ')
{ String c=a.substring(f,i);
f=i+1; s=s+(convert(c)+"
");
}

}
}

String convert(String b)
{ for(int
i=0;i<b.length();i++)
{ char c=b.charAt(i);
if(c=='a'||c=='e'||c=='i'||c=='o'||c=='u'){
String d=b.substring(i,b.length())+b.substring(0,i)+"ay";
return d;
}
}
return b+"ay";
}

void print(){

pg. 3
7
38

System.out.println(s);
}

public static void main(){


Scanner sc=new Scanner(System.in);
System.out.println("enter a
sentence"); String a=sc.nextLine();
a=a.toLowerCase(); a=a+" ";
piglatin ob=new piglatin();
ob.extract(a); ob.print();
}
}

Output

14) Replace all vowels of a sentence with succeeding letter

Algorithm:
STEP 1: START
STEP 2: Find length of the entered word and store in variable I
STEP 3: for i=0 to l-1, repeat STEP 4,5,6
STEP 4: Extract characters of the word and store in variable ch
STEP 5: if ch is a vowel, ch=ch+1
STEP 6: str=str+ch
STEP 7: Display the original word and the new word
STEP 8: END

Program: class
character_swaping
{
static void main(String s)
{
int l = s.length();
String str = "";
for(int i = 0; i<l; i+
+)
{

pg. 3
8
39

char
ch=s.charAt(i); if
(ch=='a'||ch=='e'||ch=='i'||ch=='o'||ch=='u'||ch=='A'||ch=='E'||ch=='I'||
ch=='
O'||ch=='U')
ch++;
str+=ch;
}
System.out.println("Original - "+s+"\nNew word - "+str);
}
}

Output:
Original – Computer
New word – CPMVTFR
15) Program to delete extra spaces

Program:
import java.util.*; class
extra_space_delete
{
void getStr()
{
Scanner in = new Scanner(System.in);
System.out.println("Enter the sentence in capitals including extra
spaces in words and terminated by '.', '!', '?'. "); String str = in.nextLine();
int l = str.length(); char c=str.charAt(l-1); if(c=='!'||c=='.'||c=='?')
{}
else
{
System.out.println("Invalid Input");
System.exit(0);
}
System.out.println("Enter the word to be deleted: ");
String wd = in.next();
System.out.println("Enter the position number of the word to be
removed:
");
int pos = in.nextInt();
String s = "";

pg. 3
9
40

System.out.println(str);
String st[] =
str.split("//s"); l =
st.length; for (int i = 0;
i<l; i++)
{
if (st[i].equals(wd)&&pos==i+1)
{}
else
s = s + ' '+ st[i];
}
System.out.println("Sentence after deleting a word: "+s);
}
}

16) Intercepted message

Program:

import java.util.Scanner;
class InterceptedMessage
{
public static void main()
{
Scanner in = new Scanner(System.in);
System.out.println("Enter the encoded message");
String m = in.nextLine();
StringBuffer em = new
StringBuffer(m); em.reverse(); m =
em.toString(); int l = m.length();
String de =
""; String d;
int i = 0;
char c; while
(i < l)
{
c = m.charAt(i);
if (c != '1')
{

pg. 4
0
41

d =
m.substring(i, i +
2); i = i + 2;
}
else
{
d = m.substring(i, i +
3); i = i + 3;
}
int x =
Integer.parseInt(d); char
y = (char) x; de += y;
}
System.out.println("The decoded message is: " +
de);
}
}

17) Anagram

Program:
import java.util.*;
class anagram
{
static String sort(String a)
{
String d =""; for (int i
= 65; i<=90; i++)
{
for(int j = 0; j<a.length(); j++)
{
char z = a.charAt(j);
if((int)z==i)
{
d = d + z;
}
}
}
return d;

pg. 4
1
42

public static void main()


{
Scanner in = new Scanner(System.in);
System.out.println("Enter the first word");
System.out.println("Enter the word that is to be
checked"); String a = in.next(); a = a.toUpperCase();
int l = a.length(); String b = in.next(); b =
b.toUpperCase(); int len = b.length(); if (l!=len)
{
System.out.println("Not Anagram");
System.exit(0);
}
else
{
String x =
anagram.sort(a); String y
= anagram.sort(b); if
(x.equals(y))
System.out.println("Anagram");
else
System.out.println("Not Anagram");
}
}
}

Output:

pg. 4
2
43

18) Write a program using recursion to print the reverse of a number


entered by the user.
Algorithm:
1. Start
2. Create a class named revnum.
3. Declare an instance variable r of type integer.
4. Define a method named input within the revnum class:
5. Define a method named reverse within the revnum class
6. Check if m is equal to 0:
7. If true, return the value of r.
8. If false, proceed to the next step.
9. Calculate the remainder of m when divided by 10 and add it to the current
value of r after multiplying it by 10.
10.Recursively call the reverse method with the updated values of m and r.
11.Stop

Code:
import java.util.*;
class revnum
{
int r=0; void
input()
{
Scanner sc=new
Scanner(System.in);
System.out.println("Enter the
number"); int m=sc.nextInt(); int
l=0;
System.out.println("The reversed number is "+reverse(m,l));
}

int reverse(int m, int l)


{
if (m==0)
return r;

else r=(r*10)+(m%10);
return
reverse(m/10,r);
}
public static void main()
{
Scanner sc=new Scanner(System.in);
revnum ob=new revnum();
ob.input();
}

pg. 4
3
44

Output:

19) Write a program using recursion to print a number entered by the


user to a power entered by the user.

Algorithm:
1. Start
2. Input m and n from the user.
3. Calculate the exponentiation using the exponent method with m and n and
display the result.
4. Define an exponent method with parameters m and n:
5. If n is 1, return m.
6. If n is 0, return 1.
7. If n is less than 0, return (1.0 / m) * exponent(m, n + 1).
8. Otherwise, return m * exponent(m, n - 1).
9. Recursively call the exponent method with updated values of m and n.
10.Stop

pg. 4
4
45

Code:
import java.util.*;
class exprecur
{
void input()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter the
number"); int m=sc.nextInt();
System.out.println("Enter the
power"); int n=sc.nextInt(); double
x=exponent(m,n);
System.out.println("the number is " + x);
}
double exponent(int m, int n)
{ if(n==1)
return m;
else if
(n==0)
return 1;
else if
(n<0)
return
(1.0/m)*exp
onent(m,+
+n); else
return
m*exponen
t(m,--n);

}
public static void main()
{
Scanner sc=new
Scanner(System.in); exprecur
ob=new exprecur(); ob.input();
}
}

Output:

pg. 4
5
46

20) Write a program using recursion to print the factorial of a


number entered by the user.

Algorithm:
1. Start
2. Accept an integer a as an argument in the fact method
3. Check if a is equal to 1:
4. If true, return 1 as the base case for factorial.
5. If false, proceed to the next step.
6. Otherwise, return a multiplied by the result of the fact method called
recursively with a - 1.
7. Stop

Code:
import java.util.*;
class factrecur
{
int fact(int a)
{ if(a==1) return 1; else
return(a*fact(a-1));
}
public static void main()
{

pg. 4
6
47

Scanner sc= new


Scanner(System.in);
System.out.println("Enter the
number"); int n=sc.nextInt();
factrecur ob=new factrecur();
System.out.println(ob.fact(n));
}
}
Output:

pg. 4
7
48

21) Write a program using recursion to convert a number entered by


the user from binary to decimal or from decimal to binary.

Algorithm:
1. Start
2. Define a dectobin method to convert decimal to binary:
3. Accept an integer a.
4. Calculate binary digits using recursion.
5. Define a bintodec method to convert binary to decimal:
6. Accept a binary string a.
7. Convert binary digits to decimal using recursion.
8. Use a switch statement to call the appropriate conversion method based
on c in the main method.
9. Stop

Code:
import java.util.*;
class decbin
{
int dectobin(int a)
{
if (a == 0)
return 0;
else return(a%2 +
10*(dectobin(a/2)));
}

int bintodec(int a)
{ if(a==
0)
{
return 0;
}
else
{
return(a%10+2*bintodec(a/10));
}
}
public static void main()
{
Scanner sc= new Scanner(System.in);
System.out.println("Enter the
number"); int n=sc.nextInt();
System.out.println("enter 1 to convert decimal to binary or 2 for
binary to decimal"); int c=sc.nextInt(); decbin ob=new decbin();
switch(c)

pg. 4
8
49

{
case 1:
System.out.println(ob.dectobin(n));
break; case 2:
System.out.println(ob.bintodec(n));
break;
}

}
}

Output:

22) Write a program using recursion to print the reverse of a string


entered by the user.

Algorithm:
1. Start
2. Read a string s from the user.
3. Initialize an integer l to 0.
4. Call the reverse method with s and l as arguments and display the
reversed string.
5. Use recursion to reverse the string by appending characters one by one.
6. Stop

Code:
import java.util.*;
class strrecur
{

pg. 4
9
50

String b= "";
void input()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter the
string"); String s=sc.nextLine(); int
l=0;
System.out.println("The reversed string is "+reverse(s,l));
}

String reverse(String a, int l)


{

if(l<a.length())
{
b=a.charAt(l)+b;
return
reverse(a,l+1);
}
else
return b;
}
public static void main()
{
Scanner sc=new
Scanner(System.in); strrecur
ob=new strrecur(); ob.input();
}
}

Output:

pg. 5
0
51

23) Write a program using recursion to search for the position of an


element in an array using binary search. Algorithm:
1. Start
2. Read the size of the array (m) and initialize lower (lb) and upper (ub)
bounds.
3. Input elements into the arr array.
4. Input the element to be searched (se).
5. Perform binary search by calling the binSearch method with se, lb, and ub.
6. Display the position of the element if found or "Invalid" if not.
7. Calculate the middle index (mid) and compare the element at mid with se
in binSearch method
8. Adjust lb and ub based on the search condition.
9. Recursively call binSearch until the element is found or the search range is
exhausted.
10.Stop

Code:
import java.util.*;
class bsrecur
{ int arr[]; void
input(int m)
{
Scanner sc=new
Scanner(System.in); int ub=0; int
lb=0; arr= new int[m];

pg. 5
1
52

System.out.println("Enter the elements");


for(int i=0;i<m;i++)
{ arr[i]=sc.nextInt()
;
}
System.out.println("Enter the element to be
searched"); int se=sc.nextInt(); lb=0; ub=m-1; int
x=binSearch(se,lb,ub); if(x==-1)
System.out.println("Invalid" );
else
System.out.println("the position of the element is " +(x+1));
}
int binSearch(int se,int lb, int ub)
{
int mid=(lb+ub)/2;
if(arr[mid]==se) return
mid; else
if(se<arr[mid]&&(lb<=ub))
{
ub=mid-1; return
binSearch(se,lb,ub);
}
else if(se>arr[mid]&&(lb<=ub))
{
lb=mid+1; return
binSearch(se,lb,ub);
}
else
return -1;

}
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter the size of the
array"); int m=sc.nextInt(); bsrecur ob=new
bsrecur(); ob.input(m);
}
}
Output:

pg. 5
2
53

pg. 5
3
54

24) Magic number using


recursion Algorithm:
main()→
STEP 1: Accept number and store in variable a
STEP 2: Repeat STEP 3 until a>9
STEP 3: Calculate magic number
STEP 4: Check for magic number
STEP 5: If STEP 4 is true, display Magic number OTHERWISE display not a
magic number
Sum digit(int n)→
STEP 1: Extract digits from the number and store in variable r
STEP 2: if (n=0) is true then return 0 STEP
3: Otherwise return (r+sum_digit(n/10))

Program:
import java.util.*;
class magic
{
void main()
{
System.out.println("Enter number");
Scanner in = new Scanner(System.in);
int a = in.nextInt();
while(a<9)
{
a = sum_digit(a);
}
if (a==1)
System.out.println("Magic Number");
else
System.out.println("Not a magic number");
}
int sum_digit(int n)
{
int r = n%10;
if(n==0)
return 0;
else
return(r+sum_digit(n/10));
}
}
Output:

pg. 5
4
55

Enter number
19
Magic number

Enter number
20
Not a magic number

pg. 5
5
56

25) Finding HCF using recursion

Algorithm:
main() →
STEP 1: Accept first number
STEP 2: Accept second number
STEP 3: Calculate HCF
STEP 4: Display HCF
hcf(int a, int b)→
STEP 1: If (a>b)is true then return (hcf(a-b,b))
STEP 2: Otherwise if (b>a) is true then return (hcf(a,b-a))
STEP 3: Otherwise return a

Program:
import java.util.*;
class HCF
{
void main()
{
Scanner in = new Scanner (System.in);
System.out.println("Enter numbers to find their
HCF "); int a = in.nextInt(); int b=in.nextInt(); int c
= hcf(a,b);
System.out.println("HCF:"+c);
}
int hcf(int a, int b)
{ if(a>b) return
(hcf(a-b,b)); else
if(b>a) return
(hcd(a,b-a)); else
return a;
}
}
Output:
Enter numbers to find their HCF
6
36
HCF: 6

pg. 5
6
57

26) Armstrong number using


recursion Algorithm: main()→
STEP 1: Start
STEP 2: Accept number and store in variable s
STEP 3: Repeat STEP 3 until a
STEP 4: Calculate magic number
STEP 5: Check for magic number
STEP 5: If STEP 4 is true, display Magic number OTHERWISE display
not a magic number checknum(int n)→
STEP 1: Receive actual parametric value in variable n
STEP 2: if (n=0) is true then return 0
STEP 3: Otherwise (int)Math.pow(n%10,3) checknum(n/10)
STEP 4: Stop
Program:
import java.util.*;
class armstrong
{
public static void main()
{
armstrong ob = new
armstrong(); int i, n, sum, m;
Scanner in = new
Scanner(System.in);
System.out.println("Enter a
number"); n = in.nextInt();
m=ob.checknum(n); if(n==m)
System.out.println("It is an armstrong number");
else if(m==0)
System.out.println("Not an armstrong
number"); else
System.out.println("Not an armstrong number");
}
int checknum(int n)
{
if(n==0) return 0; else
return(int)Math.pow(n
%10,3)+checknum(n/10);
}
}
Output:
Enter a number: 153
It is an armstrong number

pg. 5
7
58

27) Decimal to binary conversion using recursion

Algorithm: main()→
STEP 1: Accept
number task(int n)→
STEP 1: Proceed if n>2
STEP 2: store remainder in variable d when n is divided by 2
STEP 3: task(n/2) STEP 4:
Display d backwards

Program:
import java.util.*;
class D2B
{
void main()
{

Scanner in = new Scanner (System.in);


System.out.println("Enter a decimal
number"); int a = in.nextInt(); task(a);
}
void task(int n)
{ if(n>0
)
{
int d = n%2;
task(n/2);
System.out.print(d);
}
}
}

Output:
Enter a decimal number
10
1010

pg. 5
8
59

28) Interface program

Algorithm
Interface: Data
Create an interface named Data.
Declare a constant pi with a value of 3.142.
Declare an abstract method volume()

Class: Base
Declare an instance variable rad of type double to store the radius.
Create a parameterized constructor that takes a double r and initializes the rad
variable.
Display "The radius is " followed by the value of rad.

Class: CalVol (Inherits from Base and Implements Data)


Declare an additional instance variable ht of type double to store the height.
Calculate the volume (v) using the formula pi * rad * rad * ht.
Return the calculated volume.
Display "The volume is " followed by the result of the volume method.
Prompt the user to enter the radius (r) and height (h).
Code public
interface Data
{
public final double pi = 3.142;
public double volume();
}
.
import java.util.*;
public class Base
{
double rad;
Base(double r)
{
rad=r;
}

void show()
{
System.out.println("The radius is "+rad);
}
}
.
import java.util.*; public class CalVol extends
Base implements Data
{
double ht;

pg. 5
9
60

CalVol(double h, double r)
{ super(r)
; ht=h;
}
public double volume()
{
double v=0;
v=pi*rad*rad*ht;
return v;
}

public void show()


{
super.show();
System.out.println("The volume is " +
volume());
}
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter the
radius"); int r=sc.nextInt();
System.out.println("Enter the
height"); int h=sc.nextInt(); CalVol
ob=new CalVol(h,r); ob.show();
}
}

pg. 6
0
61

Output

29) Write a program in Java that implements a stack using an array.


Algorithm:
PUSH
if(TOP=SIZE) display (“STACK
OVERFLOWS”) return
TOP=TOP+1
STACK[TOP]=ITEM
return

POP if(TOP=NULL)
display(“STACK
UNDERFLOWS”) return
Val=STACK[TOP]
display(“Popped out element
is”+Val) TOP=TOP-1 return

Code:

pg. 6
1
62

import java.util.*;
class stackprog
{ int a[]; int size,
top;
stackprog(int
m)
{
size=m;
a=new
int[m];
top=0;
}

void push()
{
Scanner sc=new
Scanner(System.in);
System.out.println("enter the
element"); int i=sc.nextInt();
if(top==size)
System.out.println("Stack overflow");
else
{ a[top]=i;
top=top
+1;
}
}

void pop()
{
if(top==0)
System.out.println("Stack underflow");
else
{
System.out.println("Popped out element is: "+a[top-1]);
top=top-1;
}
}

void display()
{
System.out.println("Stacked elements are:");
if(top==0)
System.out.println("Stack underflow");
else
{ for(int
i=1;i<=top;i++)

pg. 6
2
63

{
System.out.println(+a[i]);
}
}
}

public static void main()


{
Scanner sc=new
Scanner(System.in);
System.out.println("enter the
size"); int i=sc.nextInt();
stackprog ob=new stackprog(i);
int ch=0;
for(int j=0;;i++)
{
System.out.println("enter 1 for push, 2 for pop, 3 for display and 4 for
exit"); ch=sc.nextInt(); switch(ch)
{
case 1:ob.push(); break; case
2:ob.pop(); break; case 3:ob.display();
break; case 4:System.exit(0); break;
default: System.out.println("Invalid
input");
}
}
}
}

Output:

pg. 6
3
64

30) Write a program in Java that implements a basic queue data


structure.

Algorithm:
INSERT
if(R=SIZE)
Display(“QUEUE
OVERFLOWS”) return if(F=0
and R=0) F=1 and R=1
else
R=R+1
Q[R]=ITEM
return

DELETE
if(F=0 and
R=0)
Display(“QUEUE UNDERFLOWS”)
return
VAL=Q[F]
Display (“Element Deleted”,VAL)
if(F=R)

pg. 6
4
65

F=0 and R=0


else
F=F
+1 return
Code:
import java.util.*;
class queue
{ int a[]; int
size, f,r;
queue(int
m)
{
size=m+1;
a=new
int[size]; f=0;
r=0;
}
void insert()
{
Scanner sc=new
Scanner(System.in);
System.out.println("enter the
element"); int i=sc.nextInt();
if(r==size-1)
{
System.out.println("queue overflow");

}
else if(r==0 && f==0)
{ f=1
;
r=
1;
a[r]=i;
}
else
{ a[+
+r]=i;
}

int delete()
{
if((f==0 && r==0)||(f==size))
{

pg. 6
5
66

return -99999;
} int
val=a[f];
if(f==r)
{
f=0;
r=0;
}
else
f=f+1;
return val; }

void display()
{
System.out.println("queued elements are:");
if(r==0)
System.out.println("queue underflow");
else
{ for(int
i=f;i<=r;i++)
{
System.out.println(a[i]);
}
}
}

public static void main()


{
Scanner sc=new
Scanner(System.in);
System.out.println("enter the
size"); int i=sc.nextInt(); queue
ob=new queue(i); int ch=0;
for(int j=0;;i++)
{
System.out.println("enter 1 for insert, 2 for delete, 3 for display and 4 for
exit");
ch=sc.nextInt(
); switch(ch)
{
case 1:ob.insert(); break;
case 2:int v=ob.delete();
if(v==-9999)
{ System.out.println("queue
underflow"); } else
System.out.println("deleted element: "+v);
break; case 3:ob.display(); break; case

pg. 6
6
67

4:System.exit(0); break; default:


System.out.println("Invalid input");
}
}
}
}

Output::

31) Triangle Number

Program:

pg. 6
7
68

import java.util.Scanner;
class triangle_num
{
static void main()
{
Scanner in = new Scanner(System.in);
System.out.println("Enter a value for
'n'"); int n = in.nextInt(); int a[] = new
int[n]; int i,j,k = 1; for(i=1;i<n+1;i++)
{
k = 1;
for(j=0;j<=i;j++)
{
a[i-1] = a[i-1] + k;
k++;
}
}
System.out.println("The trinagle numbers are:");
for(i=2;i<n+2;i++)
{
k = 1;
for(j=0;j<i;j++)
{
System.out.print(k++);
if(j<i-1)
{
System.out.print(" + ");
}
}
System.out.println(" = "+a[i-2]);
}
}
}

Output:

pg. 6
8
69

32) Evil Number

Program:
import java.util.*;
class evil_number
{
public static void main()
{
Scanner in = new Scanner (System.in);
System.out.println("Enter a positive whole number");
int n = in.nextInt();
int remainder, i = 1, b = 0;
while (n!=0)
{
remainder = n % 2;
n /= 2;
b = b+ remainder * i;
i *= 10;
}
System.out.println("Binary equivalent: "+b);
int k=0; int a = b; int r;
while(b!=0)
{
r = b%10;
if (r==1)
{
k++;
}
b = b/10;
}
System.out.println("No. of 1's: "+k);
if(k%2==0)
System.out.println("Evil number");
else
System.out.println("Not an evil number");

pg. 6
9
70

}
}

Output:

pg. 7
0
71

33) Smith Number

Program:
import java.util.*;
class Smith
{
static void main()
{
Scanner in = new Scanner(System.in);
System.out.println("Enter a number");
int x = in.nextInt();
int y = x;
int sum_dig = 0;
while(y!=0)
{
sum_dig = sum_dig + y%10;
y = y/10;
}
int i,j = 0;
y = x;
int check = 0;
for(i=2;i<x;i++)
{
if(x%i==0)
{
check++;
}
}
if(check==0)
{
System.out.println("The number is prime");
System.exit(0);
}
int n = 0;
for(i=2;i<x;i++)
{
while((y%i)==0)
{
n++;
y = y/i;
}
}
y = x;
int arr[] = new int[n];
int k = 0;
int sum = 0;

pg. 7
1
72

int de = 2;
for(i=0;i<x;i++)
{
while((y%de)==0)
{
arr[k] = de;
k++;
y = y/de;
}
de++;
}
for(i=0;i<n;i++)
{
int num = arr[i];
while(num!=0)
{
sum = sum + num%10;
num = num/10;
}
}
if(sum_dig==sum)
System.out.println("The number is a smith number");
else
System.out.println("The number is not a smith number");
}
}

Output:

34) Composite Magic Number

Program:

pg. 7
2
73

import java.util.*;
class comp_mag
{
static void main()
{
Scanner in = new Scanner(System.in);
System.out.println("Enter your range from low to high");
int m = in.nextInt();
int n = in.nextInt();
if(m>1000 || n>1000 || m>n)
{
System.out.println("Wrong Range");
System.exit(0);
}
int i,j,k = 0;
System.out.println("All Composite Magic numbers in the range are :");
for(i=m;i<=n;i++)
{
int check = i;
k = 0;
for(j=2;j<check;j++)
{
if(check%j==0)
k++;
}
int p = i;
int sum = 0;
while(p>9)
{
sum = 0;
while(p!=0)
{
sum = sum + p%10;
p = p/10;
}
p = sum;
}
if(k>0 && p==1)
System.out.println(i);
}
}
}

pg. 7
3
74

35) Number of words that start and end with a vowel.

Program:

import java.util.*;
class pg_13
{
public static void main()
{
Scanner in = new Scanner(System.in);
System.out.println("Enter a
sentence:"); String str =
in.nextLine(); str =
str.toUpperCase(); str = str.trim();
String st[] = str.split("\\s"); int l =
str.length(); int l1 = st.length; char c
= str.charAt(l - 1); if (c == '.' || c ==
'?'||c == '!')
{}
else
{
System.out.println("Invalid Input");
System.exit(0);
}
String a="",b ="";int count=0;
for(int i = 0; i<l1; i++)
{
String x = st[i]; char
c1=x.charAt(0); char
c2=x.charAt(x.length()-1);

if((c1=='A'||c1=='E'||c1=='I'||c1=='O'||c1=='U')&&(c2=='A'||c2=='E'||
c2=='I'|| c2=='O'||c2=='U'))
{
count++;
a=a+" "+x;
}
else
b=b+"
"+x;
}
System.out.println("Total number of words starting and ending with a
vowel are:"+count);

pg. 7
4
75

System.out.println("The modified string is:"+a+" "+b);


}
}

Output

36) Printing no. of vowels and consonants of each word in a sentence

Program:
import java.util.*;

class pg_12
{
public static void main()
{
Scanner in = new Scanner(System.in);
System.out.println("Enter a
sentence:"); String str =
in.nextLine(); str = str.trim();
String st[] = str.split("\\
s+"); int l = str.length();
int l1 = st.length; char c =
str.charAt(l - 1);

if (c == '.' || c == '?')
{

}
else
{
System.out.println("Invalid Input");
System.exit(0);
}

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


{

pg. 7
5
76

char c1 = st[i].charAt(0); c1
=
Character.toUpperCase(c1);
st[i] = c1 + st[i].substring(1);
}

System.out.println("WORD" + "\t" + "VOWELS" + "\t" +


"CONSONANTS");
for (int i = 0; i < l1;
i++) { String x =
st[i]; int vowels =
0; int consonants =
0;

for (int j = 0; j < x.length(); j++)


{
char ch = Character.toLowerCase(x.charAt(j)); if (ch
== 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u')
{
vowels++;
}
else if (Character.isLetter(ch))
{
consonants++;
}
}
System.out.println(x + "\t" + vowels + "\t" + consonants);
}
}
}

Output:

37) Bouncy Number

pg. 7
6
77

Program:
import java.util.*;
class bouncy
{
public static void main()
{
Scanner in=new Scanner(System.in);
System.out.println("Enter a number");
int n = in.nextInt();
int t = n;
int prev = t%10;
boolean isIncreasing = true, isDecreasing = true;
if (n<100)
{
System.out.println(n + " is not a Bouncy Number.");
System.exit(0);
}
while(t!=0)
{
int d = t%10;
if(d>prev)
{
isIncreasing = false;
break;
}
prev = d;
t = t/10;
}
t = n;
prev = t%10;
while(t!=0)
{
int d = t%10;
if(d<prev)
{
isDecreasing = false;
break;
}
prev = d;
t = t/10;
}
if (!isIncreasing && !isDecreasing) System.out.println(n + " is a
Bouncy Number.");
else
System.out.println(n + " is not a Bouncy Number.");
}

pg. 7
7
78

pg. 7
8
79

Output:

pg. 7
9
80

39) Program to find whether a number is Keith or not

Program:

import java.util.*;
class keith
{
boolean tribonacci(int x)
{
int arr[] = new int[3];
int y = x;
int i,j,k = 0;
for(i=2;i>=0;i--)
{
arr[i] = y%10;
y = y/10;
}
while(k<x || k==x)
{
if(k==x)
return true;
k = arr[0] + arr[1] + arr[2];
arr[0] = arr[1];
arr[1] = arr[2];
arr[2] = k;
}
return false;
}
boolean fibonacci(int x)
{
int arr[] = new int[2];
int y = x;
int i,j,k = 0;
for(i=1;i>=0;i--)
{
arr[i] = y%10;
y = y/10;
}
while(k<x || k==x)
{
if(k==x)
return true;
k = arr[0] + arr[1];
arr[0] = arr[1];
arr[1] = k;
}

pg. 8
0
81

return false;
}
static void main()
{
keith ob = new keith();
Scanner in = new Scanner(System.in);
System.out.println("Enter a number");
int x = in.nextInt();
int y = x;
int n = 0;
while(y!=0)
{
y = y/10;
n++;
}
boolean check = true;
if(n==2)
check = ob.fibonacci(x);
else if(n==3)
check = ob.tribonacci(x);
else
{
System.out.println("Invalid number");
System.exit(0);
}
if(check==true)
System.out.println("The number is a Keith number");
else
System.out.println("The number is not a Keith number");
}
}

Output:

40) Delete duplicate elements in an array

Program:
import java.util.*;
class
delete_duplicate
{

pg. 8
1
82

public static void main()


{
Scanner in = new
Scanner(System.in); int a[] = new
int[10];
System.out.println("Enter the elements of the array");
for(int i = 0; i<10; i++)
{
a[i] = in.nextInt();
}
for(int i = 0; i<10; i++)
{
for (int j=i+1; j<10; j++)
{ if(a[i]==a[j
])
{
a[j]=-999;
}
}
}
for(int i = 0; i<10; i++)
{ if(a[i]!=-
999)
{
System.out.println(a[i]);
}
}
}
}
Output:

pg. 8
2
83

pg. 8
3

You might also like