Computer Project Uvi
Computer Project Uvi
COMPUTERPROJ
ECT
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.
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
31 Triangle Number
32 Evil Number
33 Smith number
34 Composite Magic Number
pg. 3
4
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]);
}
}
}
}
pg. 5
6
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
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
if(r==c)
{
s=true;
}
if(s==true)
{
}
}
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
}
}
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
Code:
import java.util.*;
public class
boundary
{
pg. 1
4
15
pg. 1
5
16
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();
}
}
}
c[i][j]=a[i][j]+b[i][j];
}
}
output(c)
;
}
pg. 1
7
18
pg. 1
8
19
Output:
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];
}
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]);
}
}
}
}
}
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
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;
}
}
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
fourbyfour ob=new
fourbyfour(); ob.input();
ob.output();
}
}
Output:
pg. 2
8
29
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);
pg. 3
1
32
else con[q+
+]=arr[i];
}
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);
}
Output:
pg. 3
2
33
pg. 3
3
34
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();}
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
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="";
}
}
}
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);
}
Output
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);
}
}
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
Output:
pg. 4
2
43
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));
}
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:
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
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
pg. 4
7
48
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:
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));
}
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
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
}
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
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
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
pg. 5
7
58
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()
{
Output:
Enter a decimal number
10
1010
pg. 5
8
59
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.
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;
}
pg. 6
0
61
Output
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]);
}
}
}
Output:
pg. 6
3
64
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
}
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]);
}
}
}
pg. 6
6
67
Output::
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
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
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:
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
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
Output
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);
}
pg. 7
5
76
char c1 = st[i].charAt(0); c1
=
Character.toUpperCase(c1);
st[i] = c1 + st[i].substring(1);
}
Output:
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
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:
Program:
import java.util.*;
class
delete_duplicate
{
pg. 8
1
82
pg. 8
2
83
pg. 8
3