Sie sind auf Seite 1von 72

PROGRAM 1

Write a program to compute the amount that the customer


pays on the taxi that he hires as per the following criteria:
Distance Rate
10km $5
20km $10
>20km $15

ALGORITHM:
STEP 1: START
STEP 2: Importing java.util package
STEP 3: Declaring class Tax
STEP 4: Creating object of scanner class
STEP 5: Declaring instance variable
STEP 6: Declaring function accept()
STEP 7: Taking input of details
STEP 8: Declaring function cal()
STEP 9: Caculating taxi fare
STEP 10: Checking range below 10km
STEP 11: Checking range between 10-20km
STEP 12: Checking range above 20km
STEP 13: Declaring function display()
STEP 14: Printing the tax payable
STEP 15: Declaring main function
STEP 16: Creating an object of class Tax
STEP 17: Calling the member function {accept()}
STEP 18: Calling the member function {cal()}
STEP 19: Calling the member function {display()}
STEP 20: STOP
//Program to compute taxi fair
import java.util.Scanner;
class Tax
{//start of class
Scanner sc=new Scanner(System.in);
int km;
double amt=0.0d;
public void accept()//accepting the distance covered
{
System.out.println("Enter the distance covered by the
taxi.");
km=sc.nextInt();
}
public void cal()//calculating the fair
{
if(km<=10)
amt=km*5;
else if(km>10&&km<=20)
amt=(10*5)+(km-10)*10;
else if(km>20)
amt=(10*5)+(10*10)+(km-20)*15;
}
public void display()//dsiplaying the amount
{
System.out.println("Amount to be paid by customer =
"+amt);
}
public static void main(String args[])
{
Tax obj=new Tax();//creating the object of the class
obj.accept();
obj.cal();
obj.display();
}
}

OUTPUT:
Enter the distance covered by the taxi.
25
Amount to be paid by customer = 225.0
PROGRAM 2
WAP class Mobike with the following specifications:
class name : Mobike
Data memebers:
int bno->To store the bike number
int phno->To store phone number
String name->To store the name of the customer
int days->To store the number of days bike is taken on rent
int charge->To calculate and store the rental charge
Member functions:
void input()->To input and store the details of the customer
void compute()->To compute the rental charge as follows:-

Days Charge
1st 5 days $500 per day
next 5 days $500 per day
rest of the days $200 per day

void display()->To display the details in the following manner:-


Bike No. Phone No Name No of days Charge
- - - - -

ALGORITHM:
STEP 1: START
STEP 2: Importing java.util package
STEP 3: Declaring class Mobike
STEP 4: Creating object of scanner class
STEP 5: Declaring instance variable
STEP 6: Declaring function input()
STEP 7: Taking input of details
STEP 8: Checking range below 5 days
STEP 9: Checking range for the next 5 days
STEP 10: Checking range for the rest of the days
STEP 11: Declaring function compute()
STEP 12: Calculating charge
STEP 13: Declaring function display()
STEP 14: Printing the details of the customer
STEP 15: Declaring main function
STEP 16: Creating an object of class Mobike
STEP 17:Calling the member function {input()}
STEP 18: Calling the member function {compute()}
STEP 19: Calling the member function {display()}
STEP 20: STOP
//Program to compute rental charge
import java.util.Scanner;
class Mobike
{//start of class
Scanner sc=new Scanner(System.in);
private int bno;
private long phno;
private String name;
private int days;
int charge=0;
public void input()
{
System.out.println("Enter the name of the customer");
name=sc.nextLine();
System.out.println("Enter the bike number");
bno=sc.nextInt();
System.out.println("Enter customer's phone number");
phno=sc.nextLong();
System.out.println("Enter the number of days bike is taken
on rent");
days=sc.nextInt();
}
public void compute()
{
if(days<=5)
charge=days*500;
else if(days<=10)
charge=(5*500)+(days-5)*400;
else if(days>10)
charge=(5*500)+(5*400)+(days-10)*200;
}
public void display()
{

System.out.println("BikeNo\tPhoneNo\tName\tNoOfDays\tCar
ge");

System.out.println(bno+"\t"+phno+"\t"+name+"\t"+days+"\t"+
charge);
}
public static void main(String args[])
{
Mobike obj=new Mobike();
obj.input();
obj.compute();
obj.display();
}
}
OUTPUT:
Enter the name of the customer
Adarsh
Enter the bike number
3932
Enter customer's phone number
9410788872
Enter the number of days bike is taken on rent
8
BikeNo PhoneNo Name NoOfDays Carge
3932 9410788872 Adarsh 8 3700
PROGRAM 3
Write a program to accept ten different numbers in single
dimensional array and arrange the numbers in ascending order
using selection sort technique and display them.

ALGORITHM:
STEP 1: START
STEP 2: Importing java.util package
STEP 3: Declaring class Selection
STEP 4: Creating object of scanner class
STEP 5: Declaring array arr[][]
STEP 6: Declaring loop variables
STEP 7: Declaring temporary variable temp
STEP 8: Declaring variable for searching lowest index
STEP 9: Declaring function initialize()
STEP 10: Initialization of array
STEP 11: Accepting array elemnts
STEP 12: Declaring function sort()
STEP 13: Performing selection sort
STEP 14: Declaring function display()
STEP 15: Printing the array in the sorted manner
STEP 16: Declaring main function
STEP 17: Creating an object of class Selection
STEP 18:Calling the member function {initialize()}
STEP 19: Calling the member function {sort()}
STEP 20: Calling the member function {display()}
STEP 21: STOP
//Program to perform selection sort
import java.util.Scanner;
class Selection
{//start of class
Scanner sc=new Scanner(System.in);
//Declaration of array
int arr[]=new int[10];
int i,j,min=0,temp=0;
public void initialize()//Initialization of array
{
System.out.println("Enter 10 numbers to be arranged in
asecending order");
for(i=0;i<10;i++)
{
arr[i]=sc.nextInt();
}
}
public void sort()//Performing selection sort
{
for(i=0;i<9;i++)
{
min=i;
for(j=i+1;j<10;j++)
{
if(arr[j]<arr[min])
min=j;//searching for lowest index
}
temp=arr[i];
arr[i]=arr[min];
arr[min]=temp;
}
}
public void display()//Printing the array in sorted form
{
System.out.println("Elements arranged in the ascending
order are as follows:");
for(i=0;i<10;i++)
{
System.out.println(arr[i]);
}
}
public static void main(String args[])
{//start of main
Selection obj=new Selection();//Creating object of the class
obj.initialize();
obj.sort();
obj.display();
}//end of main
}//end of class

OUTPUT:
Enter 10 numbers to be arranged in asecending order
7
9
12
5
6
48
32
16
7
2
Elements arranged in the ascending order are as follows:
2
5
6
7
7
9
12
16
32
4
PROGRAM 4
Write a program to accept ten different numbers in single
dimensional array and arrange the numbers in ascending order
using bubble sort technique and display them.

ALGORITHM:
STEP 1: START
STEP 2: Importing java.util package
STEP 3: Declaring class Bubble
STEP 4: Creating object of scanner class
STEP 5: Declaring array arr[][]
STEP 6: Declaring loop variables
STEP 7: Declaring temporary variable temp
STEP 8: Declaring function initialize()
STEP 9: Initialization of array
STEP 10: Accepting array elemnts
STEP 11: Declaring function sort()
STEP 12: Performing bubble sort
STEP 13: Declaring function display()
STEP 14: Printing the array in the sorted manner
STEP 15: Declaring main function
STEP 16: Creating an object of class Bubble
STEP 17:Calling the member function {initialize()}
STEP 18: Calling the member function {sort()}
STEP 19: Calling the member function {display()}
STEP 20: STOP
//Program to perform bubble sort
import java.util.Scanner;
class Bubble
{//start of class
Scanner sc=new Scanner(System.in);
//Declaration of array
int arr[]=new int[10];
int i,j,temp=0;
public void initialize()//Initialization of array
{
System.out.println("Enter 10 numbers to be arranged in
asecending order");
for(i=0;i<10;i++)
{
arr[i]=sc.nextInt();
}
}
public void sort()//Performing bubble sort
{
for(i=0;i<9;i++)
{
for(j=0;j<9-i;j++)
{
if(arr[j]>arr[j+1])
{
temp=arr[j];
arr[j]=arr[j+1];
arr[j+1]=temp;
}
}
}
}
public void display()//Printing the array in sorted form
{
System.out.println("Elements arranged in the ascending
order are as follows:");
for(i=0;i<10;i++)
{
System.out.println(arr[i]);
}
}
public static void main(String args[])
{//start of main
Bubble obj=new Bubble();//Creating object of the class
obj.initialize();
obj.sort();
obj.display();
}//end of main
}//end of class

OUTPUT:
Enter 10 numbers to be arranged in asecending order
8
4
9
7
6
12
35
16
3
1
Elements arranged in the ascending order are as follows:
1
3
4
6
7
8
9
12
16
35
PROGRAM 5
Write a program to accept ten different numbers in a single
dimensional array. Enter a number and search whether the
number is present or not in the list of array elements by using
linear search.

ALGORITHM:
STEP 1: START
STEP 2: Importing java.util package
STEP 3: Declaring class Linear
STEP 4: Creating object of scanner class
STEP 5: Declaring instance variable
STEP 6: Declaring function initialize()
STEP 7: Initialization of array
STEP 8: Accepting the array elemnts
STEP 9: Declaring search()
STEP 10: Taking input of number to be searched
STEP 11: Loop for i from 0 to 10
STEP 12: Checking if number is equal to arr[i]
STEP 13: Declaring function display()
STEP 14: Printing message accordingly
STEP 15: Declaring main function
STEP 16: Creating an object of class Linear
STEP 17:Calling the member function {initialize()}
STEP 18: Calling the member function {search()}
STEP 19: Calling the member function {display()}
STEP 20: STOP
//Program to perform linear search
import java.util.Scanner;
class Linear
{//start of class
Scanner sc=new Scanner(System.in);
//Declaration of array
int arr[]=new int[10];
int i,no,pos=0;
public void initialize()//Initialization of array
{
System.out.println("Enter 10 numbers");
for(i=0;i<10;i++)
{
arr[i]=sc.nextInt();
}
}
public void search()//Performing linea search
{
System.out.println("Enter the number to be searched");
no=sc.nextInt();
for(i=0;i<10;i++)
{
if(arr[i]==no)
pos=1;
}
}
public void display()//Printing the result
{
if(pos==1)
System.out.println("Number present");
else
System.out.println("Number not present");
}
public static void main(String args[])
{//start of main
Linear obj=new Linear();//Creating object of the class
obj.initialize();
obj.search();
obj.display();
}//end of main
}//end of class

OUTPUT:
Enter 10 numbers
8
4
1
2
6
7
5
9
3
12
Enter the number to be searched
27
Number not present
PROGRAM 6
Write a program to accept ten different numbers in single
dimensional array. Enter a number and by using binary search
technique whether the number is present or not in the list. If the
number is present in the list then the display the message
“Search successful” otherwise display “Search unsuccessful”.

ALGORITHM:
STEP 1: START
STEP 2: Importing java.util package
STEP 3: Declaring class Binary
STEP 4: Creating object of scanner class
STEP 5: Declaring instance variable
STEP 6: Declaring function initialize()
STEP 7: Initialization of array
STEP 8: Accepting the array elemnts
STEP 9: Declaring search()
STEP 10: Taking input of number to be searched
STEP 11: Loop for i from 0 to 10
STEP 12: Checking if number is equal to arr[i]
STEP 13: Declaring function display()
STEP 14: Printing message accordingly
STEP 15: Declaring main function
STEP 16: Creating an object of class Binary
STEP 17:Calling the member function {initialize()}
STEP 18: Calling the member function {search()}
STEP 19: Calling the member function {display()}
STEP 20: STOP
//Program to perform binary search
import java.util.Scanner;
class Binary
{//start of class
Scanner sc=new Scanner(System.in);
//Declaration of array
int arr[]=new int[10];
int i,no,pos=0,mid,ub=9,lb=0;
public void initialize()//Initialization of array
{
System.out.println("Enter 10 numbers");
for(i=0;i<10;i++)
{
arr[i]=sc.nextInt();
}
}
public void search()//Performing binary search
{
System.out.println("Enter the number to be searched");
no=sc.nextInt();
while(lb<=ub)
{
mid=lb+ub/2;
if(arr[mid]<no)
lb=mid+1;
if(arr[mid]>no)
ub=mid-1;
if(arr[mid]==no)
{
pos=1;
break;
}
}
}
public void display()//Printing the result
{
if(pos==1)
System.out.println("Number present");
else
System.out.println("Number not present");
}
public static void main(String args[])
{//start of main
Binary obj=new Binary();//Creating object of the class
obj.initialize();
obj.search();
obj.display();
}//end of main
}//end of class

OUTPUT:
Enter 10 numbers
1
2
3
4
5
6
7
8
9
10
Enter the number to be searched
4
Number present
PROGRAM 7
WAP to store 6 elements in an array P and 4 elements in an
array Q and produce a third array R containing all the elements
of array P and Q. Display the resultant array.
Example:
P Q R
4 19 4
6 23 6
1 7 1
2 8 2
3 3
10 10
19
23
7
8

ALGORITHM:
STEP 1: START
STEP 2: Importing java.util package
STEP 3: Declaring class Merging
STEP 4: Creating object of scanner class
STEP 5: Declaring array P
STEP 6: Declaring array Q
STEP 7: Declaring array R
STEP 8: Declaring function initialize()
STEP 9: Initialization of array
STEP 10: Declaring merge()
STEP 11: Putting elements of array P in R
STEP 12: Putting elements of array Q in R
STEP 13: Declaring function display()
STEP 14: Printing the merged array
STEP 15: Declaring main function
STEP 16: Creating an object of class Binary
STEP 17:Calling the member function {initialize()}
STEP 18: Calling the member function {search()}
STEP 19: Calling the member function {display()}
STEP 20: STOP
//Program to perform merging of two arrays
import java.util.Scanner;
class Merging
{//start of class
Scanner sc=new Scanner(System.in);
//Declaration of array
int P[]=new int[6];
int Q[]=new int[4];
int R[]=new int[10];
int m=0,k,i;
public void initialize()//Initialization of array
{
System.out.println("Enter 6 numbers in array P");
for(i=0;i<6;i++)
{
P[i]=sc.nextInt();
}
System.out.println("Enter 4 numbers in array Q");
for(i=0;i<4;i++)
{
Q[i]=sc.nextInt();
}
}
public void merge()//Performing merging
{
for(k=0;k<6;k++)
{
R[k]=P[k];
m++;
}
for(k=0;k<4;k++)
{
R[m]=Q[k];
m++;
}
}
public void display()//Printing the merged array
{
System.out.println("Elements after merging are as follows
:");
for(i=0;i<10;i++)
{
System.out.println(R[i]);
}
}
public static void main(String args[])
{//start of main
Merging obj=new Merging();//Creating object of the class
obj.initialize();
obj.merge();
obj.display();
}//end of main
}//end of class

OUTPUT:
Enter 6 numbers in array P
1
2
3
4
5
6
Enter 4 numbers in array Q
7
8
9
10
Elements after merging are as follows :
1
2
3
4
5
6
7
8
9
10
PROGRAM 8
WAP to create a 4*4 matrix and print transpose of the matrix.
ALGORITHM:
STEP 1: START
STEP 2: Importing java.util package
STEP 3: Declaring class Transpose
STEP 4: Creating object of scanner class
STEP 5: Declaring array arr
STEP 6: Declaring array arr1
STEP 7: Declaring loop variables I,j
STEP 8: Declaring function input()
STEP 9: Initialization of array
STEP 10: Declaring compute()
STEP 11: Finding the transpose of the matrix
STEP 12: Declaring function print()
STEP 13: Printing the original matrix
STEP 14: Printing the transposed matrix
STEP 15: Declaring main function
STEP 16: Creating an object of class Transpose
STEP 17:Calling the member function {input()}
STEP 18: Calling the member function {compute()}
STEP 19: Calling the member function {print()}
STEP 20: STOP
//Program to find the transpose of a matrix
import java.util.Scanner;
class Transpose
{//start of class
Scanner sc=new Scanner(System.in);
//Declaration of array
int arr[][]=new int[3][3];
int arr1[][]=new int[3][3];
int i,j;
public void input()
{
//Accessing the array
System.out.println("Enter the elements");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
arr[i][j]=sc.nextInt();
}
}
}
public void compute()
{
//Finding the transpose
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
arr1[j][i]=arr[j][i];
}
}
}
public void print()
{
//Printing the original matrix
System.out.println("Original matrix is ");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
System.out.print(arr[i][j]+"");
}
System.out.println("");
}
//Printing the traspose of matrix
System.out.println("Transposed matrix is ");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
System.out.print(arr1[j][i]+"");
}
System.out.println("");
}
}
public static void main(String args[])
{//start of main
Transpose obj=new Transpose();
obj.input();
obj.compute();
obj.print();
}//end of main
}//end of class
OUTPUT:
Enter the elements
1
2
3
4
5
6
7
8
9
Original matrix is
123
456
789
Transposed matrix is
147
258
369
PROGRAM 9
WAP to store 4*4 matrix in 2D array and display sum of left
diagonal, sum of right diagonal, sum of column, sum of row and
difference b/w sum of left diagonal and right diagonal.
ALGORITHM:
STEP 1: START
STEP 2: Importing java.util package
STEP 3: Declaring class SumN
STEP 4: Creating object of scanner class
STEP 5: Declaring array arr
STEP 6: Declaring loop variable I,j
STEP 7: Declaring variable ld,rd,sC,sR
STEP 8: Declaring function accept()
STEP 9: Initialization of array
STEP 10: Declaring printDiag()
STEP 11: Printing the sum of diagonals
STEP 12: Declaring function printRowCo()
STEP 13: Printing sum of rows
STEP 14: Printing sum of columns
STEP 15: Declaring main function
STEP 16: Creating an object of class SaddlePoint
STEP 17:Calling the member function {accept()}
STEP 18: Calling the member function {printDiag()}
STEP 19: Calling the member function {diff()}
STEP 20: STOP
//Program to find the sum of diagonals,rows and columns of a
matrix.
import java.util.Scanner;
class SumN
{//start of class
Scanner sc=new Scanner(System.in);
//Declaration of array
int arr[][]=new int[4][4];
int i,j,rd=0,ld=0,sR=0,sC=0;
public void accept()
{
//Accessing the array
System.out.println("Enter the elements");
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
arr[i][j]=sc.nextInt();
}
}
}
public void printDiag()
{
//Printing sum of right diagonal
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
if(i==j)
rd=rd+arr[i][j];
}
}
System.out.println("The sum of right diagonal is "+rd);
//Printing sum of left diagonal
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
if((i+j)==3)
ld=ld+arr[i][j];
}
}
System.out.println("The sum of left diagonal is "+ld);
}
public void printRowCo()
{
//Printing sum of row
System.out.println("Sum of row:");
for(i=0;i<4;i++)
{
sR=0;
for(j=0;j<4;j++)
{
sR=sR+arr[i][j];
}
System.out.println("The sum of elements of "+(i+1)+"
row="+sR);
}
//Printing sum of column
System.out.println("Sum of column:");
for(i=0;i<4;i++)
{
sC=0;
for(j=0;j<4;j++)
{
sC=sC+arr[j][i];
}
System.out.println("The sum of elements of "+(i+1)+"
column="+sC);
}
}
public void diff()
{
System.out.println("Difference between sum of left and
right diagonal = "+(ld-rd));
}
public static void main(String args[])
{//start of main
SumN obj=new SumN();
obj.accept();
obj.printDiag();
obj.printRowCo();
obj.diff();
}//end of main
}//end of class
OUTPUT:
Enter the elements
1
2
3
7
8
9
4
5
6
7
4
1
5
9
2
6
The sum of right diagonal is 20
The sum of left diagonal is 23
Sum of row:
The sum of elements of 1 row=13
The sum of elements of 2 row=26
The sum of elements of 3 row=18
The sum of elements of 4 row=22
Sum of column:
The sum of elements of 1 column=20
The sum of elements of 2 column=27
The sum of elements of 3 column=13
The sum of elements of 4 column=19
Difference between sum of left and right diagonal = 3
PROGRAM 10
WAP to print 4*4 matrix and find the saddle point.
ALGORITHM:
STEP 1: START
STEP 2: Importing java.util package
STEP 3: Declaring class SaddlePoint
STEP 4: Creating object of scanner class
STEP 5: Declaring array arr
STEP 6: Declaring loop variable I,j
STEP 7: Declaring variable min,max,x,f
STEP 8: Declaring function input()
STEP 9: Initialization of array
STEP 10: Declaring printori()
STEP 11: Printing the original matrix
STEP 12: Declaring function calSaddle()
STEP 13: Finding minimum element of row
STEP 14: Finding maximum element of column
STEP 15: Declaring main function
STEP 16: Creating an object of class SaddlePoint
STEP 17:Calling the member function {input()}
STEP 18: Calling the member function {print()}
STEP 19: Calling the member function {calSaddle()}
STEP 20: STOP
//Program to find saddle point in a matrix
import java.util.Scanner;
class SaddlePoint
{//start of class
Scanner sc=new Scanner(System.in);
//Declaration of array
int arr[][]=new int[3][3];
int i,j,k,max,min,x=0,f=0;
public void input()
{
//Initialization of array
System.out.println("Enter the elements : ");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
arr[i][j]=sc.nextInt();
}
}
}
public void printori()
{
//Printing the original matrix
System.out.println("Original matrix : ");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
System.out.print(arr[i][j]+" ");
}
System.out.println(" ");
}
}
public void calSaddle()
{
//Finding the minimum element of row
for(i=0;i<3;i++)
{
min=arr[i][0];//Initializing min with first element of every
row
for(j=0;j<3;j++)
{
if(arr[i][j]<min)
{
min=arr[i][j];
x=j;//Saving the column position of the minimum
element of the row
}
}
//Finding the maximum element in the column
corresponding to the minimum element of row
max=arr[0][x];//Initializing max with first element of that
column
for(k=0;k<3;k++)
{
if(arr[k][x]>max)
{
max=arr[k][x];
}
}
/*
* If the minimum of a row is same as maximum of the
corresponding column,then we have that
* element as the Saddle point.
*/
if(max==min)
{
System.out.println("Saddle point = "+max);
f=1;
}
}
if(f==0)
System.out.println("No saddle point");
}
public static void main(String args[])
{//start of main
SaddlePoint obj=new SaddlePoint();
obj.input();
obj.printori();
obj.calSaddle();
}//end of main
}//end of class
OUTPUT:
Enter the elements :
6
3
1
9
8
7
1
2
3
Original matrix :
631
987
123
Saddle point = 7
PROGRAM 11
WAP to check whether a given number is magic or not.
ALGORITHM:
STEP 1: START
STEP 2: Importing java.util package
STEP 3: Declaring class Magic
STEP 4: Creating object of scanner class
STEP 5: Declaring variable no to store number
STEP 6: Declaring variable digit to get digits of the no
STEP 7: Declaring variable sum to compute sum of digits
STEP 8: Declaring function accept()
STEP 9: Inputting the number
STEP 10: Declaring check()
STEP 11: Calculating the sum of digits
STEP 12: Checking whether the no is magic or not
STEP 13: Declaring function display()
STEP 14: Displaying whether the number is magic or not
STEP 15: Declaring main function
STEP 16: Creating an object of class Magic
STEP 17:Calling the member function {accept()}
STEP 18: Calling the member function {check()}
STEP 19: Calling the member function { display()}
STEP 20: STOP
//Program to check whether a number is a magic number or
not
import java.util.Scanner;
class Magic
{//start of class
Scanner sc=new Scanner(System.in);
int no,dig=0,sum=0;
public void accept()
{
System.out.println("Enter the number to be checked");
no=sc.nextInt();
}
public void check()
{
sum=no;
while(sum>9)
{
no=sum;
sum=0;
while(no!=0)
{
dig=no%10;
sum=sum+dig;
no=no/10;
}
}
}
public void display()
{
if(sum==1)
System.out.println("Number is a magic number");
else
System.out.println("Number is not a magic number");
}
public static void main(String args[])
{//start of main
Magic obj=new Magic();//Creating object of the class
obj.accept();
obj.check();
obj.display();
}//end of main
}//end of class
OUTPUT:
Enter the number to be checked
55
Number is a magic number
Enter the number to be checked
87
Number is not a magic number
PROGRAM 12
WAP to check whether a given number is special or not.
ALGORITHM:
STEP 1: START
STEP 2: Importing java.util package
STEP 3: Declaring class Special
STEP 4: Creating object of scanner class
STEP 5: Declaring variable no to store number
STEP 6: Declaring variable digit,I,f
STEP 7: Declaring variable sum to compute sum of digits
STEP 8: Declaring function accept()
STEP 9: Inputting the number
STEP 10: Declaring check()
STEP 11: Calculating the sum of digits
STEP 12: Checking whether the no is special or not
STEP 13: Declaring function display()
STEP 14: Displaying whether the number is special or not
STEP 15: Declaring main function
STEP 16: Creating an object of class Special
STEP 17:Calling the member function {accept()}
STEP 18: Calling the member function {check()}
STEP 19: Calling the member function { display()}
STEP 20: STOP
//Program to check whether a number is a special number or
not
import java.util.Scanner;
class Special
{//start of class
Scanner sc=new Scanner(System.in);
int no,n1,i,f=1,dig=0,sum=0;
public void accept()
{
System.out.println("Enter the number to be checked");
no=sc.nextInt();
}
public void check()
{
n1=no;
while(no!=0)
{
dig=no%10;
for(i=1;i<=dig;i++)
{
f=f*i;
}
sum=sum+f;
f=1;
no=no/10;
}
}
public void display()
{
if(sum==n1)
System.out.println("Number is a special number");
else
System.out.println("Number is not a special number");
}
public static void main(String args[])
{//start of main
Special obj=new Special();//Creating object of the class
obj.accept();
obj.check();
obj.display();
}//end of main
}//end of class
OUTPUT:
Enter the number to be checked
145
Number is a special number
Enter the number to be checked
57
Number is not a special number
PROGRAM 13
WAP 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:
[Example-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
//Program to check the validity of an ISBN number.
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)
{
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:
Enter the ISBN no
0552122998
Sum=176
LEAVES NO REAMIANDER-VALID ISBN CODE
Enter the ISBN no
9119061547
Sum=234
LEAVES REAMIANDER-INVALID ISBN CODE
PROGRAM 14
WAP to check whether a given number is special two digit
number or not.
ALGORITHM:
STEP 1: START
STEP 2: Importing java.util package
STEP 3: Declaring class SpecialTwo
STEP 4: Creating object of scanner class
STEP 5: Declaring variable no to store number
STEP 6: Declaring variable sum,pro
STEP 7: Declaring variable last,first
STEP 8: Declaring function accept()
STEP 9: Inputting the number
STEP 10: Declaring checkDisp()
STEP 11: Finding the first digit
STEP 12: Finding the last digit
STEP 13: Finding the sum of digits
STEP 14: Finding the product of digits
STEP 15: Displaying whether the number is special two digit
number or not
STEP 16: Declaring main function
STEP 17: Creating an object of class Special
STEP 18: Calling the member function {accept()}
STEP 19: Calling the member function {checkdisp()}
STEP 20: STOP
//Program to check for a Special Two-Digit Number
import java.util.Scanner;
class SpecialTwo
{//start of class
Scanner sc=new Scanner(System.in);
int no,first,last,sum,pro;
public void accept()
{
System.out.println("Enter a two digit number");
no=sc.nextInt();
}
public void checkDisp()
{
if(no<10||no>99)//Checking whethered entered number is
two digit or not
System.out.println("Invalid input! Number should have two
digits only");
else
{
first=no/10;//Finding the first digit
last=no%10;//Finding the last digit
sum=first+last;//Finding the sum of digits
pro=first*last;//Finding the product of digits
if((sum+pro)==no)
{
System.out.println("The number "+no+" is a Special
Two-Digit Number.");
}
else
{
System.out.println("The number is not a Special Two-
Digit Number.");
}
}
}
public static void main(String args[])
{//start of main
SpecialTwo obj=new SpecialTwo();//Creating object of the
class
obj.accept();
obj.checkDisp();
}//end of main
}//end of class
OUTPUT:
Enter a two digit number
59
The number 59 is a Special Two-Digit Number.
Enter a two digit number
27
The number is not a Special Two-Digit Number.
PROGRAM 15
WAP to check whether a given number is happy or not.
ALGORITHM:
STEP 1: START
STEP 2: Importing java.util package
STEP 3: Declaring class Happy
STEP 4: Creating object of scanner class
STEP 5: Declaring isHappy()
STEP 6: Determining whether the no is happy or not
STEP 7: Declaring variables rem,sum
STEP 8: Calculating the sum of square of digits
STEP 9: Inputting the number
STEP 10: Returning sum
STEP 11: Declaring main function
STEP 12: Inputting the number from the user
STEP 13: Checking if result=1
STEP 14: if so then printing no is happy
STEP 15: if result=4
STEP 16: then printing no is unhappy
STEP 17: STOP
//Program to check whether a number is happy or not
import java.util.Scanner;
class Happy
{//start of class
Scanner sc=new Scanner(System.in);
//isHappy() will determine whether a no is happy or not
public static int isHappy(int num)
{//start of main
int rem=0,sum=0;
//calculates the sum of square of digits
while(num>0)
{
rem=num%10;
sum=sum+(rem*rem);
num=num/10;
}
return sum;
}
public static void main(String args[])
{
int num;
System.out.println("Enter number:");
num=sc.nextInt();
int result=num;
while(result!=1&&result!=4)
{
result=isHappy(result);
}
//happy no always ends with 1
if(result==1)
System.out.print(num + " is a happy number");
//unhappy no ends in a cylce of repeating numbers which
contains 4
else if(result==4)
System.out.print(num + " is not a happy number");
}//end of main
}//end of class
OUTPUT:
Enter number:
32
32 is a happy number
Enter number:
7
7 is a happy number
Enter number:
25
25 is not a happy number
PROGRAM 16
WAP to accept a word and check and print whether the word is
pallindrome or only a special word.
ALGORITHM:
Step 1: START
Step 2: Importing java.util package
Step 3: Declaring class SpecialPalin
Step 4: Declaring Instance Variable
Step 5: Declaring the function input()
Step 6: Creating object of scanner class
Step 7: Taking input of a word
Step 8: Declaring function isSpecial()
Step 9: Checking if the word is special or not
Step 10: Returning if function is special or not
Step 11: Declaring function isPalin()
Step 12: Reversing the word
Step 13: Checking if reverse of the word and the original
word is same or not
Step 14: Returning if word is a palindrome or not
Step 15: Declaring function display()
Step 16: Printing the appropriate message after checking the
required functions
Step 17: Declaring the main function
Step 18: Creating object of class SpecialPalin
Step 19: Calling the member functions [input(), & display()]
Step 20: Stop
//Program to check whether a word is a palindrome or not
import java.util.*;
class SpecialPalin
{
private String n;
private int len;
public void input()
{
Scanner sc=new Scanner(System.in);
n=sc.nextLine();
}
public boolean isSpecial()
{
len=n.length();
if(n.charAt(0)==n.charAt(len-1))
{
return true;
}
else
{
return false;
}
}
public boolean isPalin()
{
int i=0;
len=n.length();
String rev=" ";
for(i=len-1;i>=0;i--)
{
rev=rev+n.charAt(i);
}
rev=rev.trim();
if(rev.equalsIgnoreCase(n))
{
return true;
}
else
{
return false;
}
}
public void display()
{
if(isSpecial()==true&&isPalin()==true)
{
System.out.println("The Word is a Special Plaindrome
Word");
}
else
{
System.out.println("The Word is not a Special
Palindrome Word");
}
}
public static void main(String args[])
{
SpecialPalin sp=new SpecialPalin();
sp.input();
sp.display();
}
}

OUTPUT:

Malyalam
The Word is not a Special Palindrome Word
malyalam
The Word is not a Special Palindrome Word
madam
The Word is a Special Plaindrome Word
roonir
The Word is not a Special Palindrome Word
PROGRAM 17
WAP in java to accept a string in lower case and change the first
letter of every word to upper case. Display the result.
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: Declaring the function main()
Step 21: Creating an object of class UpperCase
Step 22: Calling the member functions [input(), upper()
& display()]
Step 23: STOP
//Program to change the first letter of every word in upper
case
import java.util.*;
class UpperCase
{
Scanner sc=new Scanner(System.in);
private String n=" ",s=" ";
public void input()
{
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);
}
n=n+ch1;
}
}
n=n.trim();
}
public void display()
{
System.out.println(n);
}
public static void main(String args[])
{
UpperCase obj=new UpperCase();
obj.input();
obj.upper();
obj.display();
}
}
OUTPUT:
Enter A Sentence:
we are cyber in world
We Are Cyber In World
PROGRAM 18
WAP to arrange the name of students in ascending order using
selection sort technique.
ALGORITHM:
STEP 1: START
STEP 2: Importing java.util package
STEP 3: Declaring class Sort
STEP 4: Creating object of scanner class
STEP 5: Declaring variable names
STEP 6: Declaring variable i,j
STEP 7: Declaring variable min,temp
STEP 8: Declaring function accept()
STEP 9: Accepting the names of students
STEP 10: Declaring compute()
STEP 11: Using selection sort
STEP 12: Arranging the names in ascending order
STEP 13: Declaring display()
STEP 14: Printing the names in arranged order
STEP 15: Declaring main function
STEP 16: Creating an object of class Search
STEP 17: Calling the member function {accept()}
STEP 18: Calling the member function {(compute)}
STEP 19: Calling the member function {display()}
STEP 20: STOP
//Program to print names in ascending order using selection
sort technique.
import java.util.Scanner;
class Sort
{//start of class
Scanner sc=new Scanner(System.in);
String names[]=new String[3];
int i,j,min;
String temp;
public void accept()
{
System.out.println("Enter the names :");
for(i=0;i<3;i++)
{
System.out.print("names["+i+"] :");
names[i]=sc.nextLine();
}
}
public void compute()
{
for(i=0;i<2;i++)
{
min=i;
for(j=i+1;j<3;j++)
{
if(names[j].compareTo(names[min])<0)
{
min=j;
}
temp=names[i];
names[i]=names[min];
names[min]=temp;
}
}
}
public void display()
{
System.out.println("Sorted array is ");
for(i=0;i<3;i++)
{
System.out.println(names[i]);
}
}
public static void main(String args[])
{//start of main
Sort obj=new Sort();//Creating object of class
obj.accept();
obj.compute();
obj.display();
}//end of main
}//end of class
OUTPUT:
Enter the names :
names[0] :Devansh
names[1] :Ramesh
names[2] :Adarsh
Sorted array is
Adarsh
Devansh
Ramesh
PROGRAM 19
WAP to accept the names of 10 countries in a 1D string array
and their capitals in another 1D string array. Search for a name
of a country input by the user in the list. If found, display
"Search successful" and print the name of the country along
with its capital, or else display the message “Search
unsuccessful. No such country in the list."
ALGORITHM:
STEP 1: START
STEP 2: Importing java.util package
STEP 3: Declaring class Search
STEP 4: Creating object of scanner class
STEP 5: Declaring variable coun[]
STEP 6: Declaring variable cap[]
STEP 7: Declaring variable I,country
STEP 8: Declaring function accept()
STEP 9: Inputting the countries and their capitals
STEP 10: Declaring calDisp()
STEP 11: Inputting the country to be searched
STEP 12: Checking whether coun equals country
STEP 13: Printing successful message
STEP 14: Printing country and capital
STEP 15: Printing unsuccessful message
STEP 16: Declaring main function
STEP 17: Creating an object of class Search
STEP 18: Calling the member function {accept()}
STEP 19: Calling the member function {checkdisp()}
STEP 20: STOP
//Program to search for a country.
import java.util.Scanner;
class Search
{//start of class
Scanner sc=new Scanner(System.in);
String coun[]=new String[3];
String cap[]=new String[3];
int i;
String country;
public void accept()
{
for(i=0;i<3;i++)
{
System.out.println("Enter country:");
coun[i]=sc.next();
System.out.println("Enter capital:");
cap[i]=sc.next();
}
}
public void CalDisp()
{
System.out.println("Enter the country to be searched
for:");
country=sc.next();
int c=0;
for(i=0;i<3;i++)
{
if(coun[i].equals(country))
{
System.out.println("Serach successful");
System.out.println("Country : " +coun[i]);
System.out.println("Capital : " +cap[i]);
c=1;
break;
}
}
if(c==0)
System.out.println("Search unsuccessful.No such country
in the list.");
}
public static void main(String args[])
{//start of main
Search obj=new Search();//Creating object of class
obj.accept();
obj.CalDisp();
}//end of main
}//end of class
OUTPUT:
Enter country:
India
Enter capital:
Delhi
Enter country:
China
Enter capital:
Bejing
Enter country:
Srilanka
Enter capital:
Columbo
Enter the country to be searched for:
India
Serach successful
Country : India
Capital : Delhi

Das könnte Ihnen auch gefallen