0% found this document useful (0 votes)
81 views65 pages

Computer Project 2020

The document describes a program to sort an array using selection sort. It includes steps to take input of array size and elements, implement a selection sort algorithm to find the minimum element and swap it with the first element, and print the original and sorted arrays. The code defines classes and methods to take input, implement selection sort logic, and print output. It takes input of an integer array, sorts it using selection sort, and prints the original and sorted arrays.
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)
81 views65 pages

Computer Project 2020

The document describes a program to sort an array using selection sort. It includes steps to take input of array size and elements, implement a selection sort algorithm to find the minimum element and swap it with the first element, and print the original and sorted arrays. The code defines classes and methods to take input, implement selection sort logic, and print output. It takes input of an integer array, sorts it using selection sort, and prints the original and sorted arrays.
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/ 65

Write A program to enter name & basic salary of an employee.

Calculate his annual salary & the tax payable by him/her


according to the given table:
Annual Income Tax Payable
Below 50,000 NILL
50,000-1,00,000 1000+10% of annual income exceeding
50,000
1,00,000-2,50,000 2000+20% of annual income exceeding
1,00,000
Above 2,50,000 5000+30% of annual income exceeding
2,50,000
Print the Name, Annual Income, & Tax Payable.
Algorithm
Step 1: START
Step 2: Importing java.util package
Step 3: Declaring class Exceeding
Step 4: Declaring Instance Variable
Step 5: Declaring the function input()
Step 6: Creating object of scanner class
Step 7: Taking input of details
Step 8: Declaring the function calc()
Step 9: Calculating the annual salary
Step 10: Checking range below 50,000
Step 11: Checking range between 50,000-1,00,000
Step 12: Checking range between 1,00,000-2,50,000
Step 13: Checking above 2,50,000
Step 14: Calculating tax payable
Step 15: Declaring the function print()
1
Step 16: Printing Employees Name
Step 17: Printing the monthly salary
Step 18: Printing the tax payable
Step 19: Declaring the function main()
Step 20: Creating an object of class Exceeding
Step 21: Calling the member the functions [input(), calc(),
print()]
Step 22: STOP

2
Code
import java.util.*;
class Exceeding
{
private int bp;
private String name;
private double tax;
public void input()
{
Scanner sc=new Scanner(System.in);
System.out.print("Name:");
name=sc.nextLine();
System.out.print("Basic Pay:");
bp=sc.nextInt();
}
public void calc()
{
int ap=12*bp;
if(ap<50000)
{
tax=0;
}
else if(ap>=50000&&ap<100000)
{
tax=1000+(0.1*(ap-50000));
}
else if(ap>=100000&&ap<250000)
{
tax=2000+(0.2*(ap-100000));
}
else if(ap>250000)
{
tax=5000+(0.3*(ap-250000));
}
} 3
public void print()
{
System.out.println("Name:"+name);
System.out.println("Taxable Income:"+(12*bp));
System.out.println("Tax:"+tax);
}
public static void main(String args[])
{
Exceeding ex=new Exceeding();
ex.input();
ex.calc();
ex.print();
}
}

Output

Input Output
Name: Sankalp Shrivastava Name: Sankalp Shrivastava
Basic Pay:25000 Taxable Income:300000
Tax:20000.0

Input Output
Name: Sameer Gupta Name: Sameer Gupta
Basic Pay:120000 Taxable Income:1440000
Tax:362000.0

4
A taxi meter charges fare according to the kilometre or distance
travelled. Write a program to enter name, age, address & distance
travelled (in km) by the customer & calculate the fare according to
the given slab:
Distance (in km) Tax Payable
First 15km Rs 20/km
Next 15km Rs 25/km
Next 20km Rs 30/km
More Than 50km Rs 35/km
Print the Name, Age, Address & Fare.
Algorithm
Step 1: START
Step 2: Importing java.util package
Step 3: Declaring class First_Next
Step 4: Declaring Instance Variable
Step 5: Declaring the function input()
Step 6: Creating object of scanner class
Step 7: Taking input of details
Step 8: Declaring the function calc()
Step 9: Checking range below 15km
Step 10: Checking range between 15km-30km
Step 11: Checking range between 30km-50km
Step 12: Checking above 50km
Step 13: Calculating fare
Step 14: Declaring the function print()
Step 15: Printing Customer’s Name
Step 16: Printing Customer’s Address 5
Step 17: Printing Employee’s Age
Step 18: Printing the distance travelled
Step 19: Printing Fare
Step 20: Declaring the function main()
Step 21: Creating an object of class First_Next
Step 22: Calling the member the functions [input(), calc(),
print()]
Step 23: STOP

6
Code
import java.util.*;
class First_Next
{
private String name, add;
private int age, dist, amt;
public void input()
{
Scanner sc=new Scanner(System.in);
System.out.print("Name:");
name=sc.nextLine();
System.out.print("Address:");
add=sc.nextLine();
System.out.print("Age:");
age=sc.nextInt();
System.out.print("Distance Travelled:");
dist=sc.nextInt();
}
public void calc()
{
if(dist<=15)
{
amt=20*dist;
}
else if(dist>15&&dist<=30)
{
amt=(20*15)+(25*(dist-15));
}
else if(dist>30&&dist<=50)
{
amt=(20*15)+(25*15)+(30*(dist-30));
}
else if(dist>50)
{
amt=(20*15)+(25*15)+(30*20)+(35*(dist-50));
7
}
}
public void print()
{
System.out.println("Name:"+name);
System.out.println("Address:"+add);
System.out.println("Age:"+age);
System.out.println("Distance Travelled:"+dist);
System.out.println("Fare:"+amt);
}
public static void main(String args[])
{
First_Next fn=new First_Next();
fn.input();
fn.calc();
fn.print();
}
}

Output

Input Output
Name: Sankalp Shrivastava Name: Sankalp Shrivastava
Address: GMS Rd, Dehradun Address: GMS Rd, Dehradun
Age: 16 Age: 16
Distance Travelled: 23
Distance Travelled: 23
Fare: 500

Input Output
Name: Sameer Gupta Name: Sameer Gupta
Address: Majra, Dehradun Address: Majra, Dehradun
Age: 32 Age: 32
Distance Travelled: 146
Distance Travelled: 146
Fare: 4635

8
Design a program to sort an array by using the selection sort
technique.
Algorithm
Step 1: START
Step 2: Importing java.util package
Step 3: Declaring class Sel_Sort
Step 4: Declaring instance variables
Step 5: Declaring the function input()
Step 6: Creating scanner object
Step 7: Taking input of size & array elements
Step 8: Declaring the function sort()
Step 9: Finding the minimum element
Step 10: Swapping the A[i] with A[min]
Step 11: Declaring the function print()
Step 12: Printing the original array
Step 13: Calling the function sort()
Step 14: Printing the sorted array
Step 15: Declaring the function main()
Step 16: Creating object of class Sel_Sort
Step 17: Calling the function input()
Step 18: Calling the function print()
Step 19: STOP

9
Code
import java.util.*;
class Sel_Sort
{
private int A[],n;
public void input()
{
Scanner sc=new Scanner(System.in);
n=sc.nextInt();
A=new int[n];
int i=0;
for(i=0;i<n;i++)
{
A[i]=sc.nextInt();
}
}
public void sort()
{
int i=0,j=0,min=0,temp=0;
for(i=0;i<n-1;i++)
{
min=i;
for(j=i+1;j<n;j++)
{
if(A[j]< A[min])
{
min=j;
}
}
temp=A[min];
A[min]=A[i];
A[i]=temp;
}
}
public void print()
{ 10
int i=0;
System.out.println("Original Array:");
for(i=0;i<n;i++)
{
System.out.print(A[i]+" ");
}
System.out.println();
sort();
System.out.println("Sorted Array:");
for(i=0;i<n;i++)
{
System.out.print(A[i]+" ");
}
}
public static void main(String args[])
{
Sel_Sort ss=new Sel_Sort();
ss.input();
ss.print();
}
}

Output
Input Output
N=10 Original Array:
Enter Array Elements: 42 43 45 35 26 25 6 52 45 62
42 Sorted Array:
43
6 25 26 35 42 43 45 45 52 62
45
35
26
25
6
52
45
62

11
Input Output
N=5 Original Array:
Enter Array Elements: 45637
4 Sorted Array:
5
76543
6
3
7

12
Design a program to sort an array by using the bubble sort
technique.
Algorithm
Step 1: START
Step 2: Importing java.util package
Step 3: Declaring class Bubble_Sort
Step 4: Declaring instance variables
Step 5: Declaring the function input()
Step 6: Creating scanner object
Step 7: Taking input of size & array elements
Step 8: Declaring the function sort()
Step 9: Comparing two adjacent elements
Step 10: If A[j] is greater swapping the elements
Step 11: Declaring the function print()
Step 12: Printing the original array
Step 13: Calling the function sort()
Step 14: Printing the sorted array
Step 15: Declaring the function main()
Step 16: Creating object of class Bubble_Sort
Step 17: Calling the function input()
Step 18: Calling the function print()
Step 19: STOP

13
Code
import java.util.*;
class Bubble_Sort
{
private int A[],n;
public void input()
{
Scanner sc=new Scanner(System.in);
n=sc.nextInt();
A=new int[n];
int i=0;
for(i=0;i<n;i++)
{
A[i]=sc.nextInt();
}
}
public void sort()
{
int i=0,j=0,temp=0;
for(i=0;i<n-1;i++)
{
for(j=0;j<n-i-1;j++)
{
if(A[j]>A[j+1])
{
temp = A[j];
A[j] = A[j+1];
A[j+1] = temp;
}
}
}
}
public void print()
{
int i=0;
System.out.println("Original Array:");
for(i=0;i<n;i++)
14
{
System.out.print(A[i]+" ");
}
System.out.println();
sort();
System.out.println("Sorted Array:");
for(i=0;i<n;i++)
{
System.out.print(A[i]+" ");
}
}
public static void main(String args[])
{
Bubble_Sort bs=new Bubble_Sort();
bs.input();
bs.print();
}
}

Output

Input Output
N=10 Original Array:
Enter Array Elements: 42 43 45 35 26 25 6 52 45 62
42 Sorted Array:
43
6 25 26 35 42 43 45 45 52 62
45
35
26
25
6
52
45
62

15
Input Output
N=5 Original Array:
Enter Array Elements: 45637
4 Sorted Array:
5
76543
6
3
7

16
Design a program to search for a number in an using the binary
search technique.
Algorithm
Step 1: START
Step 2: Importing java.util package
Step 3: Declaring class Binary_Search
Step 4: Declaring instance variables
Step 5: Declaring the function input()
Step 6: Creating scanner object
Step 7: Taking input of size & array elements
Step 8: Taking input of number to be searched
Step 9: Calling the function search
Step 10: Declaring the function search
Step 11: Declaring variables l, m, u
Step 12: Creating Loop for l from 0 to l
Step 13: Finding average of l & u
Step 14: Checking if n is smaller than A[m]
Step 15: If smaller, decreasing u
Step 16: Else Checking if n is greater than A[m]
Step 17: If greater, increasing l
Step 18: Else checking if both are equal
Step 19: If equal, Printing message
Step 20: Checking if number found or not
Step 21: If not found, printing message
Step 22: Declaring the function main()
Step 23: Creating object of class Binary_Search
Step 24: Calling the function input()
Step 25: STOP

17
Code
import java.util.*;
class Binary_Search
{
private int A[],a,n;
public void input()
{
Scanner sc=new Scanner(System.in);
System.out.print("Size Of A:");
a=sc.nextInt();
A=new int[a];
System.out.println("Enter Array Elements");
int i=0;
for(i=0;i<a;i++)
{
A[i]=sc.nextInt();
}
System.out.println("Enter the number to be searched: ");
n=sc.nextInt();
search();
}
public void search()
{
int l=0,u=0,m=0,f=0;
for(l=0;l<=u;)
{
m=(l+u)/2;
if(n<A[m])
{
u=m-1;
}
else if(n>A[m])
{
l=m+1;
}
else
{
18
System.out.print("Yes, Number found at "+(m+1));
f=1;
break;
}
}
if(f!=1)
{
System.out.println("Number not found");
}
}
public static void main(String args[])
{
Binary_Search bs=new Binary_Search();
bs.input();
}
}

Output

Input Output
N=5 Original Array:
Enter Array Elements: 34567
3 Number not found
4
5
6
7
Enter number to be searched:
10

19
Input Output
N=5 Original Array:
Enter Array Elements: 42 43 45 50 76
42 Yes, Number found at 5
43
45
50
76
Enter number to be searched: 76

20
Design a program to search for a number in an using the linear
search technique.
Algorithm
Step 1: START
Step 2: Importing java.util package
Step 3: Declaring class Binary_Search
Step 4: Declaring instance variables
Step 5: Declaring the function input()
Step 6: Creating scanner object
Step 7: Taking input of size & array elements
Step 8: Taking input of number to be searched
Step 9: Calling the function search
Step 10: Declaring the function search
Step 11: Loop for i from 0 to a
Step 12: Checking if n is equal to A[i]
Step 13: If equal, Printing message
Step 14: Checking if number found or not
Step 15: If not found, printing message
Step 16: Declaring the function main()
Step 17: Creating object of class Binary_Search
Step 18: Calling the function input()
Step 19: STOP

21
Code
import java.util.*;
class Linear_Search
{
private int A[],a,n;
public void input()
{
Scanner sc=new Scanner(System.in);
System.out.print("Size Of A:");
a=sc.nextInt();
A=new int[a];
System.out.println("Enter Array Elements");
int i=0;
for(i=0;i<a;i++)
{
A[i]=sc.nextInt();
}
System.out.println("Enter the number to be searched: ");
n=sc.nextInt();
search();
}
public void search()
{
int i=0,f=0;
for(i=0;i<a;i++)
{
if(n==A[i])
{
System.out.print("Yes, Number found at "+(i+1));
f=1;
break;
}
}
if(f!=1)
{
System.out.println("Number not found");
}
} 22
public static void main(String args[])
{
Linear_Search ls=new Linear_Search();
ls.input();
}
}

Output

Input Output
N=5 Original Array:
Enter Array Elements: 45637
4 Number not found
5
6
3
7
Enter number to be searched: 10

Input Output
N=5 Original Array:
Enter Array Elements: 42 43 45 35 26
42 Yes, Number found at 5
43
45
35
26
Enter number to be searched: 26

23
Design a program to merge two arrays into one.
Algorithm
Step 1: START
Step 2: Importing java.util package
Step 3: Declaring class Merging
Step 4: Declaring Instance Variable
Step 5: Declaring the function input()
Step 6: Creating object of scanner class
Step 7: Taking input of Size & array
Step 8: Initializing arrays
Step 9: Declaring the function merge()
Step 10: Putting elements of array A in C
Step 11: Putting elements of array B in C
Step 12: Declaring the function print()
Step 13: Printing Array A
Step 14: Printing Array B
Step 15: Printing Array C
Step 16: Declaring the function main()
Step 17: Creating an object of class Merge
Step 18: Calling the member the functions [input(), merge(),
print()]
Step 19: STOP

24
Code
import java.util.*;
class Merge
{
private int A[],B[],C[],a,b;
public void input()
{
Scanner sc=new Scanner(System.in);
System.out.print("Size Of A:");
a=sc.nextInt();
A=new int[a];
System.out.println("Enter Array A Elements");
int i=0;
for(i=0;i<a;i++)
{
A[i]=sc.nextInt();
}
System.out.println();
System.out.print("Size Of B:");
b=sc.nextInt();
B=new int[b];
System.out.println("Enter Array B Elements");
for(i=0;i<b;i++)
{
B[i]=sc.nextInt();
}
C=new int[a+b];
}
public void merge()
{
int k=0,i=0;
for(i=0;i<a;i++)
{
C[k]=A[i];
k++;
}
for(i=0;i<b;i++)
25
{
C[k]=B[i];
k++;
}
}
public void print()
{
int i=0;
System.out.println("Array A:");
for(i=0;i<a;i++)
{
System.out.print(A[i]+" ");
}
System.out.println();
System.out.println("Array B:");
for(i=0;i<b;i++)
{
System.out.print(B[i]+" ");
}
System.out.println();
System.out.println("Array C:");
for(i=0;i<(a+b);i++)
{
System.out.print(C[i]+" ");
}
System.out.println();
}
public static void main(String args[])
{
Merge mg=new Merge();
mg.input();
mg.merge();
mg.print();
}
}

26
Output

Input Output
Size Of A: 5 Array A:
Enter Array A Elements: 45637
4 Array B:
5
197463
6
3 Array C:
7 45637197463
Size Of B: 6
Enter Array B Elements:
1
9
7
4
6
3

27
Design a program to enter a 4x4 matrix & print the transpose of
it.
Algorithm
Step 1: START
Step 2: Importing java.util package
Step 3: Declaring class Transpose
Step 4: Declaring Instance Variable
Step 5: Declaring the function input()
Step 6: Creating object of scanner class
Step 7: Taking input of array elements
Step 8: Declaring the function display()
Step 9: Printing the original matrix
Step 10: Printing the transpose of matrix by changing row to
column & column to row
Step 11: Declaring the function main()
Step 12: Creating an object of class Transpose
Step 13: Calling the member the functions [input(), display()]
Step 14: STOP

28
Code
import java.util.*;
class Transpose
{
private int A[][]=new int[4][4];
public void input()
{
Scanner sc=new Scanner(System.in);
int i=0,j=0;
System.out.println("Enter Array Elements");
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
A[i][j]=sc.nextInt();
}
}
}
public void display()
{
int i=0,j=0;
System.out.println("Original Matrix:");
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
System.out.print(A[i][j]+"\t");
}
System.out.println();
}
System.out.println("Transpose:");
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
System.out.print(A[j][i]+"\t");
}
29
System.out.println();
}
}
public static void main(String args[])
{
Transpose tr=new Transpose();
tr.input();
tr.display();
}
}

Output

Input Output
Enter Array Elements Original Matrix:
1 1 2 3 4
2 5 6 7 8
3
9 10 11 12
4
5 13 14 15 16
6 Transpose:
7 1 5 9 13
8 2 6 10 14
9 3 7 11 15
10 4 8 12 16
11
12
13
14
15
16

30
Design a program to enter a 4x4 matrix & perform the following:
 Sum of Right Diagonal
 Sum of Left Diagonal
 Sum of All Rows
 Sum of All Columns
 Difference of Left & Right Diagonal
Algorithm
Step 1: START
Step 2: Importing java.util package
Step 3: Declaring class Matrix
Step 4: Declaring Instance Variable
Step 5: Declaring the function input()
Step 6: Creating object of scanner class
Step 7: Taking input of array elements
Step 8: Declaring the function calc()
Step 9: Calculating the sum of left diagonal(i==j) & right
diagonal((i+j)==3)
Step 10: Printing the sum of left & right diagonal
Step 11: Calculating the sum of each row & printing it
Step 12: Calculating the sum of each column & printing it
Step 13: Checking which is greater sum of left diagonal or
sum of right diagonal
Step 14: Printing the difference
Step 15: Declaring the function display()
Step 16: Printing the original matrix
Step 17: Declaring the function main()
Step 18: Creating an object of class Matrix
Step 19: Calling the member the functions [input(),
display(), &
calc()]
Step 20: STOP 31
Code
import java.util.*;
class Matrix
{
private int A[][]=new int[4][4];
public void input()
{
Scanner sc=new Scanner(System.in);
int i=0,j=0;
System.out.println("Enter Array Elements:");
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
A[i][j]=sc.nextInt();
}
}
}
public void calc()
{
int sl=0,sr=0,i=0,j=0;
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
if(i==j)
{
sl=sl+A[i][j];
}
if((i+j)==3)
{
sr=sr+A[i][j];
}
}
}
System.out.println("Sum of left diagonal:"+sl); 32
System.out.println("Sum of right diagonal:"+sr);
int srow=0,scol=0;
for(i=0;i<4;i++)
{
srow=0;
scol=0;
for(j=0;j<4;j++)
{
srow=srow+A[i][j];
scol=scol+A[j][i];
}
System.out.println("Sum of elements of coloumn"+(i+1)+":"+scol);
System.out.println("Sum of elements of row "+(i+1)+":"+srow);
}
System.out.print("Difference of left and right diagonal:");
if(sr>sl)
{
System.out.println(sr-sl);
}
else if(sl>sr)
{
System.out.println(sl-sr);
}
else if(sl==sr)
{
System.out.println("0");
}
}
public void display()
{
int i=0,j=0;
System.out.println("Original Matrix:");
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
System.out.print(A[i][j]+"\t");
} 33
System.out.println();
}
}
public static void main(String args[])
{
Matrix mx=new Matrix();
mx.input();
mx.display();
mx.calc();
}
}

Output

Input Output
Enter Array Elements Original Matrix:
1 1 2 3 4
2 5 6 7 8
3
9 10 11 12
4
5 13 14 15 16
6 Sum of left diagonal:34
7 Sum of right diagonal:34
8 Sum of elements of coloumn 1:28
9 Sum of elements of row 1:10
10 Sum of elements of coloumn 2:32
11 Sum of elements of row 2:26
12
Sum of elements of coloumn 3:36
13
14 Sum of elements of row 3:42
15 Sum of elements of coloumn 4:40
16 Sum of elements of row 4:58
Difference of left and right diagonal:0

34
Design a program to enter a 4x4 matrix. Also find & print the
Saddle Point if present.
Algorithm
Step 1: START
Step 2: Importing java.util package
Step 3: Declaring class Matrix
Step 4: Declaring Instance Variable
Step 5: Declaring the function input()
Step 6: Creating object of scanner class
Step 7: Taking input of array elements
Step 8: Declaring the function display()
Step 9: Printing the original matrix
Step 10: Declaring the function calc()
Step 11: Finding the minimum element of a row
Step 12: Saving the row number of minimum element
Step 13: Finding the maximum element in the column
containing the minimum element
Step 14: Saving the column number of maximum element
Step 15: Checking if the maximum and minimum element
are equal or not
Step 16: If equal printing the element with position and
updating the indicator(‘f’)
Step 17: Checking if f==0
Step 18: If f=0 printing “No Saddle Point”
Step 19: Declaring the function main()
Step 20: Creating an object of class Matrix
Step 21: Calling the member the functions [input(),
display() &
calc()]
Step 22: STOP
35
Code
import java.util.*;
class Saddle_Point
{
private int A[][]=new int[4][4];
public void input()
{
Scanner sc=new Scanner(System.in);
int j=0,i=0;
System.out.print("Enter Array Elements:");
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
A[i][j]=sc.nextInt();
}
}
}
public void display()
{
int i=0,j=0;
System.out.println("Original Matrix:");
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
System.out.print(A[i][j]+"\t");
}
System.out.println();
}
}
public void calc()
{
int i=0,j=0,max=0,min=0;
36
int t=0,f=0,r=0;
for(i=0;i<4;i++)
{
j=0;
min=A[i][j];
for(j=0;j<4;j++)
{
if(min>A[i][j])
{
min=A[i][j];
t=j;
}
}
max=A[i][t];
for(j=0;j<4;j++)
{
if(max<A[j][t])
{
max=A[j][t];
r=j;
}
}
if(max==min)
{
f=1;
System.out.println("Saddle Point:"+max);
System.out.println("Row:"+(r+1)+" Coloumn:"+(t+1));
break;
}
}
if(f==0)
{
System.out.println("No Saddle Point");
}
}
public static void main(String args[])
{
Saddle_Point sp=new Saddle_Point(); 37
sp.input();
sp.display();
sp.calc();
}
}

Output

Input Output
Enter Array Elements: Original Matrix:
1 1 2 3 4
2 5 6 7 8
3
9 10 11 12
4
5 13 14 15 16
6 Saddle Point:13
7 Row:4 Coloumn:1
8
9
10
11
12
13
14
15
16

38
Design a program to enter a number and check if it is a magic
number or not.
A magic number is a number whose eventual sum of digits
equals 1
Algorithm
Step 1: START
Step 2: Importing java.util package
Step 3: Declaring class Magic
Step 4: Declaring Instance Variable
Step 5: Declaring the function input()
Step 6: Creating object of scanner class
Step 7: Taking input of a number
Step 8: Declaring the function sumDigit()
Step 9: Calculating the sum of digits of parameterised
number
Step 10: Declaring the function calc()
Step 11: Finding the sum of digits of number entered
Step 12: Calculating the sum of digits while the sum is
greater than 9
Step 13: Checking if the final sum of digits is 1 or not
Step 14: If it is 1 printing “Magic Number”
Step 15: Else printing “Not A Magic Number”
Step 16: Declaring the function main()
Step 17: Creating an object of class Magic
Step 18: Calling the member the functions [input(), calc()]
Step 19: STOP

39
Code
import java.util.*;
class Magic
{
private int n;
public void input()
{
Scanner sc=new Scanner(System.in);
n=sc.nextInt();
}
public int sumDigit(int n)
{
int k=0,s=0;
for(k=n;k>0;k=k/10)
{
s=s+(k%10);
}
return s;
}
public void calc()
{
int a=sumDigit(n);
while(a>9)
{
a=sumDigit(a);
}
if(a==1)
{
System.out.println("Magic Number");
}
else
{ 40
System.out.println("Not A Magic Number");
}
}
public static void main(String args[])
{
Magic mg=new Magic();
mg.input();
mg.calc();
}
}

Output

Input Output
1234 Magic Number

Input Output
643 Not A Magic Number

Input Output
9991 Magic Number

41
Design a program to enter a number & check if it is a Special
Number or not.
A special number is a number for which the sum of product
of its digits and the sum of its digits is equal to the number
itself.
Algorithm
Step 1: START
Step 2: Importing java.util package
Step 3: Declaring class Special
Step 4: Declaring Instance Variable
Step 5: Declaring the function input()
Step 6: Creating object of scanner class
Step 7: Taking input of a number
Step 8: Declaring the function calc()
Step 9: Calculating the sum of digits and the product of
digits
Step 10: Calculating the sum of sum of digits and the
product of digits
Step 11: Checking if the total sum is equal to the number
itself
Step 12: If equal printing “Special Number”
Step 13: If unequal printing “Not A Special Number”
Step 14: Declaring the function main()
Step 15: Creating an object of class Special
Step 16: Calling the member the functions [input(), calc()]
Step 17: STOP
42
Code
import java.util.*;
class Special
{
private int n;
public void input()
{
Scanner sc=new Scanner(System.in);
n=sc.nextInt();
}
public void calc()
{
int f=0,k=0,s=0,p=1;
for(k=n;k>0;k=k/10)
{
s=s+(k%10);
p=p*(k%10);
}
f=p+s;
if(f==n)
{
System.out.println("Special Number");
}
else
{
System.out.println("Not A Special Number");
}
}
public static void main(String args[])
{
Special sp=new Special();
sp.input();
sp.calc(); 43
}
}

Output

Input Output
19 Special Number

Input Output
87 Not A Special Number

Input Output
99 Special Number

44
Design a program to enter an ISBN number & check if it is a valid
ISBN or not.
Algorithm
Step 1: START
Step 2: Importing java.util package
Step 3: Declaring class Book
Step 4: Declaring Instance Variable
Step 5: Declaring the function input()
Step 6: Creating object of scanner class
Step 7: Taking input of an ISBN number
Step 8: Declaring the function calc()
Step 9: Calculating the length of the entered string
Step 10: Checking if the length is equal to 10 or not
Step 11: If not printing “Invalid ISBN”
Step 12: If equal then finding each character of the ISBN
Step 13: Finding the sum according to:
Eg-
ISBN=ABCDEFGHIJ
Sum=(Ax1)+(Bx2)+(Cx3)+….+(Jx10)
Step 14: Printing the sum
Step 15: Checking if the sum is dividible by 11 or not
Step 16: If it is divisible then printing “Valid ISBN”
Step 17: If not, printing “Invalid ISBN”
Step 18: Declaring the function main()
Step 19: Creating an object of class Book
Step 20: Calling the member the functions [input(), calc()]
Step 21: STOP

45
Code
import java.util.*;
class Book
{
private String ISBN=" ";
public void input()
{
Scanner sc=new Scanner(System.in);
ISBN=sc.nextLine();
}
public void calc()
{
int l=0,s=0,i=0,a=0,j=10;
char ch=' ';
l=ISBN.length();
if(l==10)
{
for(i=0;i<l;i++)
{
ch=ISBN.charAt(i);
if(ch!='X'||ch!='x')
{
a=Integer.valueOf(ch);
a=a-48;
}
else
{
a=10;
}
s=s+(a*j);
j--;
}
System.out.println("Sum="+s);
if(s%11==0)
{
46
System.out.println("LEAVES NO REAMIANDER-VALID
ISBN CODE");
}
else
{
System.out.println("LEAVES REAMIANDER-INVALID ISBN
CODE");
}
}
else
{
System.out.println("INVALID INPUT");
}
}
public static void main(String args[])
{
Book bk=new Book();
bk.input();
bk.calc();
}
}

Output

Input Output
0590353403 Sum=187
LEAVES NO REAMIANDER-VALID ISBN CODE

Input Output
345354254X Sum=248
LEAVES REAMIANDER-INVALID ISBN CODE

47
Design a program to enter a 4x4 matrix & print the transpose of
it.
Algorithm
Step 1: START
Step 2: Importing java.util package
Step 3: Declaring class Transpose
Step 4: Declaring Instance Variable
Step 5: Declaring the function input()
Step 6: Creating object of scanner class
Step 7: Taking input of array elements
Step 8: Declaring the function display()
Step 9: Printing the original matrix
Step 10: Printing the transpose of matrix by changing row to
column & column to row
Step 11: Declaring the function main()
Step 12: Creating an object of class Transpose
Step 13: Calling the member the functions [input(), display()]
Step 14: STOP
Step 15: STOP
Step 16: STOP
Step 17: STOP
Step 18: STOP
Step 19: STOP
Step 20: STOP
Step 21: STOP
Step 22: STOP
Step 23: STOP
Step 24: STOP

48
Code

Output

Input Output

Input Output

49
Design a program to enter a number & check whether it is a
happy number or not.
A happy number is a number for which the eventual sum of
the square of digits is equal to 1
Algorithm
Step 1: START
Step 2: Importing java.util package
Step 3: Declaring class Magic
Step 4: Declaring Instance Variable
Step 5: Declaring the function input()
Step 6: Creating object of scanner class
Step 7: Taking input of a number
Step 8: Declaring the function sumDigit()
Step 9: Calculating the sum of square of digits of
parameterised number
Step 10: Declaring the function calc()
Step 11: Finding the sum of digits of number entered
Step 12: Calculating the sum of digits while the sum is
greater than 9
Step 13: Checking if the final sum of digits is 1 or not
Step 14: If it is 1 printing “Magic Number”
Step 15: Else printing “Not A Magic Number”
Step 16: Declaring the function main()
Step 17: Creating an object of class Magic
Step 18: Calling the member the functions [input(), calc()]
Step 19: STOP

50
Code
import java.util.*;
class Happy
{
private int n;
public void input()
{
Scanner sc=new Scanner(System.in);
n=sc.nextInt();
}
public int sumDigit(int n)
{
int k=0,s=0;
for(k=n;k>0;k=k/10)
{
s=s+((k%10)*(k%10));
}
return s;
}
public void calc()
{
int a=sumDigit(n);
while(a>9)
{
a=sumDigit(a);
}
if(a==1)
{
System.out.println("Happy Number");
}
else
{
System.out.println("Not A Happy Number");
}
51
}
public static void main(String args[])
{
Happy mg=new Happy();
mg.input();
mg.calc();
}
}

Output

Input Output
13 Happy Number

Input Output
35 Not A Happy Number

Input Output
79 Happy Number

52
Design a program to enter a 4x4 matrix & print the transpose of
it.
Algorithm
Step 1: START
Step 2: Importing java.util package
Step 3: Declaring class Transpose
Step 4: Declaring Instance Variable
Step 5: Declaring the function input()
Step 6: Creating object of scanner class
Step 7: Taking input of array elements
Step 8: Declaring the function display()
Step 9: Printing the original matrix
Step 10: Printing the transpose of matrix by changing row to
column & column to row
Step 11: Declaring the function main()
Step 12: Creating an object of class Transpose
Step 13: Calling the member the functions [input(), display()]
Step 14: STOP
Step 15: STOP
Step 16: STOP
Step 17: STOP
Step 18: STOP
Step 19: STOP
Step 20: STOP
Step 21: STOP
Step 22: STOP
Step 23: STOP
Step 24: STOP
53
Code

Output

Input Output

Input Output

54
Design a program to enter a Sentence & change the first letter of
each word to Upper Case.
Algorithm
Step 1: START
Step 2: Importing java.util package
Step 3: Declaring class UpperCase
Step 4: Declaring Instance Variable
Step 5: Declaring the function input()
Step 6: Creating object of scanner class
Step 7: Taking input of the sentence
Step 8: Declaring the function upper()
Step 9: Adding an extra space at the start of the sentence
Step 10: Calculating the length of the string
Step 11: Adding an extra space at the end of the sentence
Step 12: Extracting characters at i and i+1
Step 13: Checking if both characters are spaces
Step 14: Continuing the loop if they are spaces
Step 15: Else checking if character at i is a space or not
Step 16: If it is a space changing character at i+1 to upper
case
Step 17: Adding the character at i+1 to the new string
Step 18: Trimming the new string
Step 19: Declaring the function display()
Step 20: Printing the new sentence
Step 21: Declaring the function main()
Step 22: Creating an object of class UpperCase
Step 23: Calling the member the functions [input(), upper() &
55
display()]
Step 24: STOP

Code
import java.util.*;
class UpperCase
{
private String n=" ",s=" ";
public void input()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter A Sentence:");
s=sc.nextLine();
}
public void upper()
{
int l=0,i=0,a=0;
char ch=' ',ch1=' ';
s=" "+s;
l=s.length();
s=s+" ";
for(i=0;i<l;i++)
{
a=i+1;
ch=s.charAt(i);
ch1=s.charAt(a);
if(ch==' '&&ch1==' ')
{
continue;
}
else
{
if(ch==' ')
{
ch1=Character.toUpperCase(ch1); 56
}
n=n+ch1;
}
}
n=n.trim();
}
public void display()
{
System.out.println(n);
}
public static void main(String args[])
{
UpperCase uc=new UpperCase();
uc.input();
uc.upper();
uc.display();
}
}

Output

Input Output
Enter A Sentence: My Name Is Sankalp Shrivastava
my name is sankalp shrivastava

Input Output
Enter A Sentence: Harry Potter Is Great But Percy
harry potter is great but percy jackson Jackson Isn't Bad
isn't bad

57
Design a program to enter a string & sort it in ascending order
using the Selection Sort Technique.
Algorithm
Step 1: START
Step 2: Importing java.util package
Step 3: Declaring class Sele_Sort
Step 4: Declaring Instance Variable
Step 5: Declaring the function input()
Step 6: Creating object of scanner class
Step 7: Taking input of a string
Step 8: Calling the function store()
Step 9: Declaring the function store()
Step 10: Calculating the length of the string
Step 11: Updating the length of the array
Step 12: Storing the characters of the string in the array
Step 13: Declaring the function sort()
Step 14: Finding the minimum element
Step 15: Converting the element and minimum to lower case
Step 16: Swapping the A[i] with A[min]
Step 17: Declaring the function print()
Step 18: Printing the original matrix
Step 19: Calling the function sort()
Step 20: Printing the original matrix
Step 21: Printing the sorted matrix
Step 22: Declaring the function main()
Step 23: Creating an object of class Sele_Sort
Step 24: Calling the member the functions [input(), print()] 58
Step 25: STOP

Code
import java.util.*;
class Sele_Sort
{
private String n;
private int l;
private char A[];
public void input()
{
Scanner sc=new Scanner(System.in);
n=sc.nextLine();
store();
}
public void store()
{
l=n.length();
A=new char[l];
int i=0;
char ch=' ';
for(i=0;i<l;i++)
{
ch=n.charAt(i);
A[i]=ch;
}
}
public void sort()
{
int i=0,j=0,min=0;
char temp=' ',ch=' ',ch1=' ';
for(i=0;i<l-1;i++)
{ 59
min=i;
for(j=i+1;j<l;j++)
{
ch=Character.toLowerCase(A[j]);
ch1=Character.toLowerCase(A[min]);
if(ch<ch1)
{
min=j;
}
}
temp=A[min];
A[min]=A[i];
A[i]=temp;
}
}
public void print()
{
int i=0;
System.out.println("Original String:");
for(i=0;i<l;i++)
{
System.out.print(A[i]);
}
System.out.println();
sort();
System.out.println("Sorted String:");
for(i=0;i<l;i++)
{
System.out.print(A[i]);
}
}
public static void main(String args[])
{
Sele_Sort ss=new Sele_Sort();
ss.input();
ss.print();
}
} 60
Output

Input Output
Sankalp Shrivastava Original String:
Sankalp Shrivastava
Sorted String:
aaaaahiklnprSsStvv

Input Output
Harry Potter is the greatest. Original String:
Harry Potter is the greatest.
Sorted String:
.aaeeeegHhioPrrrrssttttty

61
Design a program to enter a string & search for a character in the
string using the Linear Search Technique.
Algorithm
Step 1: START
Step 2: Importing java.util package
Step 3: Declaring class Lin_Search
Step 4: Declaring Instance Variable
Step 5: Declaring the function input()
Step 6: Creating object of scanner class
Step 7: Taking input of the string
Step 8: Changing the case of the string to Upper Case
Step 9: Taking input of the character to be searched
Step 10: Changing the case of the character to Upper Case
Step 11: Calling the function store()
Step 12: Declaring the function store()
Step 13: Calculating the length of string
Step 14: Updating the size of array to length ‘l’ of string
Step 15: Storing the characters of the string in the array
Step 11: Calling the function search()
Step 16: Declaring the function search()
Step 17: Comparing the character to be searched with each
element of the array
Step 18: If found printing “Character Found” along with its
position
Step 19: Declaring the function main()
Step 20: Creating an object of class Lin_Search
Step 21: Calling the member the functions [input()]
Step 22: STOP 62
Code
import java.util.*;
class Lin_Search
{
private String s;
private char A[],n;
private int l;
public void input()
{
Scanner sc=new Scanner(System.in);
System.out.print("Enter A String: ");
s=sc.nextLine();
s=s.toUpperCase();
System.out.print("Enter the character to be searched: ");
n=sc.nextLine().charAt(0);
n=Character.toUpperCase(n);
store();
}
public void store()
{
l=s.length();
A=new char[l];
int i=0;
char ch=' ';
for(i=0;i<l;i++)
{
ch=s.charAt(i);
A[i]=ch;
}
search();
}
public void search()
63
{
int i=0,f=0;
for(i=0;i<l;i++)
{
if(n==A[i])
{
System.out.print("Yes, Character found at "+(i+1));
f=1;
break;
}
}
if(f!=1)
{
System.out.println("Character not found");
}
}
public static void main(String args[])
{
Lin_Search ls=new Lin_Search();
ls.input();
}
}

Output

Input Output
Enter A String: Sankalp Shrivastava Yes, Character found at 16
Enter the character to be searched: t

Input Output

64
Enter A String: Harry Potter Yes, Character found at 5
Enter the character to be searched: Y

65

You might also like