0% found this document useful (0 votes)
14 views45 pages

10-Practical Programs Solution

The document outlines various Java programming experiments, including number conversions, pattern printing, prime number listing, and more. Each experiment includes the aim, program code, and results indicating successful execution. The experiments cover a range of topics such as Fibonacci series, perfect cubes, special numbers, and abundant numbers.

Uploaded by

jojo639729
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views45 pages

10-Practical Programs Solution

The document outlines various Java programming experiments, including number conversions, pattern printing, prime number listing, and more. Each experiment includes the aim, program code, and results indicating successful execution. The experiments cover a range of topics such as Fibonacci series, perfect cubes, special numbers, and abundant numbers.

Uploaded by

jojo639729
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 45

EXPERIMENT NO: 01

DATE:

Aim: To write a menu driven program in java for number conversion with
following options:
a. Decimal to binary
b. Decimal to octal
c. Decimal to hexadecimal
Program:
import java.util.*;
public class NumberConverter
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
int choice,num;
char ch;
String s1="",s2="",s3="";
do
{
System.out.println("1.Decimal to Binary"
+ "\n2.Decimal to Octal"
+ "\n3.Decimal to Hexadecimal"
+ "\nEnter your choice:");
choice=sc.nextInt();
switch(choice)
{
case 1: System.out.println("Enter decimal number ");
num = sc.nextInt();
int a;
String s="";
while(num>0)
{
a = num%2;
s = a+""+s;
num= num/2;
}
System.out.println("Binary number: "+s);
break;
case 2: {
int octalNum = 0, countval = 1;
int deciNum;
System.out.println("Enter the decimal number:");
deciNum=sc.nextInt();
while (deciNum != 0)
{
int remainder = deciNum % 8;
octalNum += remainder * countval;
countval = countval * 10;
deciNum /= 8;
}
System.out.println(octalNum);
}
break;
case 3: System.out.print("Enter a decimal number : ");
num =sc.nextInt();
int rem;
String str2="";
char hex[]={'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
while(num>0)
{
rem=num%16;
str2=hex[rem]+str2;
num=num/16;
}
System.out.println("Hexa Decimal Equivalent is: "+str2);
break;
}
System.out.println("Do you want to continue(Y/N):");
ch=sc.next().charAt(0);
}
while(ch=='Y'||ch=='y');
}

Result:
The menu driven program in java for number system conversion is written, executed
and output was verified successfully.
EXPERIMENT NO: 02(a)
DATE:

Aim: Write a java program to print the following patterns.


a. Diamond shape

Program:
import java.util.*;
public class DiamondPattern
{
public static void main(String args[])
{
int n, i, j, space = 1;
System.out.print("Enter the number of rows: ");
Scanner s = new Scanner(System.in);
n = s.nextInt();
space = n - 1;
for (j = 1; j<= n; j++)
{
for (i = 1; i<= space; i++)
{
System.out.print(" ");
}
space--;
for (i = 1; i <= 2 * j - 1; i++)
{
System.out.print("*");
}
System.out.println("");
}
space = 1;
for (j = 1; j<= n - 1; j++)
{
for (i = 1; i<= space; i++)
{
System.out.print(" ");
}
space++;
for (i = 1; i<= 2 * (n - j) - 1; i++)
{
System.out.print("*");
}
System.out.println("");
}
}
}

Result:
Java program to print the diamond pattern is written, executed and output was
verified successfully.
EXPERIMENT NO: 02(b)

DATE:

b. Reversed Pyramid

Program:
import java.util.*;
public class ReversedPyramid
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter the number of rows: ");
int rows = sc.nextInt();
for (int i= 0; i<= rows-1 ; i++)
{
for (int j=0; j<=i; j++)
{
System.out.print(" ");
}
for (int k=0; k<=rows-1-i; k++)
{
System.out.print("*" + " ");
}
System.out.println();
}
sc.close();
}
}
EXPERIMENT NO: 02 (c)
DATE:
Aim: To write a java program to print the Right Pascal Triangle pattern
c. Right Pascal Triangle

Program:
import java.util.*;
public class RigthPascalTriangle
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter the number of rows: ");
int rows = sc.nextInt();
for (int i= 0; i<= rows-1 ; i++)
{
for (int j=0; j<=i; j++)
{
System.out.print("*"+ " ");
}
System.out.println("");
}
for (int i=rows-1; i>=0; i--)
{
for(int j=0; j <= i-1;j++)
{
System.out.print("*"+ " ");
}
System.out.println("");
}
sc.close();
}
}
EXPERIMENT NO: 03
DATE:

Aim: To write a java program to list all prime numbers up to a given limit.
Program:
import java.util.*;
public class Prime_Numbers
{
public static void main(String args[])
{
int min = 2;
int max;
Scanner s=new Scanner(System.in);
System.out.println("Enter the limit:");
max=s.nextInt();
System.out.println("The prime numbers are:");
for(int n=min;n<=max;n++)
{
if(isPrime(n))
{
System.out.println(n);
}
}
}

public static boolean isPrime(int num)


{
for(int i = 2; i <= num/i; ++i)
{
if(num % i == 0)
{
return false;
}
}
return true;
}
}
EXPERIMENT NO: 04
DATE:

Aim: To write a java program to find the number of days in a month for a given
year. (For February check whether it is leap year or not, if it’s a leap year
February has 29 days otherwise 28 days)
Program:
import java.util.*;
public class LeapYear
{
public static void main(String args[])
{
int month,year;
Scanner s=new Scanner(System.in);
System.out.println("Enter the month:");
month=s.nextInt();
System.out.println("Enter the year:");
year=s.nextInt();
if(month==2)
{
if((year%4==0)||(year%100==0))
{
System.out.println("February "+year+" has 29 days");
}
else
{
System.out.println("February "+year+" has 28 days");
}
}
else
{
switch(month)
{
case 1:System.out.println("January "+year+ " has 31 days");break;
case 3:System.out.println("March "+year+ " has 31 days");break;
case 4:System.out.println("April "+year+ " has 30 days");break;
case 5:System.out.println("May "+year+ " has 31 days");break;
case 6:System.out.println("June "+year+ " has 30 days");break;
case 7:System.out.println("July "+year+ " has 31 days");break;
case 8:System.out.println("August "+year+ " has 31 days");break;
case 9:System.out.println("September "+year+ " has 30 days");break;
case 10:System.out.println("October "+year+ " has 31 days");break;
case 11:System.out.println("November "+year+ " has 30 days");break;
case 12:System.out.println("December "+year+ " has 31 days");break;
}

}
}
EXPERIMENT NO: 05
DATE:

Aim: Write a program to print the sum of the following series?


S = 1 + (1+3) + (1+3+5) + ……… + (1+3+5+…………...+n) up to n terms.
Program:
import java.util.*;
public class SumOfSeries5
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
int n;
System.out.println("Enter the number of terms:");
n=sc.nextInt();
int sum = 0;
for (int i = 1; i <= n; i++)
{
int k = 1;
for (int j = 1; j <= i; j++)
{
sum += k;
k += 2;
}
}
System.out.println("Sum of the Series is:"+sum);
}
}
EXPERIMENT NO: 06
DATE:

Aim: Write a java program to print whether an inputted number is a perfect cube
or not. (Use Mathematical function).
Program:
import java.util.*;
public class Perfect_Cube
{
public static void main(String args[])
{
int num;
Scanner s=new Scanner(System.in);
System.out.println("Enter the number:");
num=s.nextInt();
int n = (int) Math.round(Math.pow(num, 1.0/3.0));
if((num == n * n * n))
{
System.out.print("Number is a perfect cube.");
}
else
{
System.out.print("Number is not a cube.");
}
System.out.println("\n");
}
}
EXPERIMENT NO: 07
DATE:

Aim: Write a java program to check whether a given number is a two-digit special
number or not.
Hint: A number is said to be special number is sum of sum of its digits and
product of digit is equal to the number itself.
Program:
import java.util.*;
public class Special_Number
{
public static void main(String args[])
{
int temp,a,b,num,sum,product;
Scanner s=new Scanner(System.in);
System.out.println("Enter the number:");
num=s.nextInt();
a=num/10;
b=num%10;
sum=a+b;
product=a*b;
temp=sum+product;
if(temp==num)
System.out.println(num+" is a two digit special number");
else
System.out.println(num+" is not a special number");
}
}
EXPERIMENT NO: 08
DATE:

Aim: To write a menu driven program in java to:


a. Generate and display the first 10 terms of the Fibonacci series.
b. Find the sum of the digits of an integer.
For an incorrect choice, an appropriate error message should be displayed.
Program:
import java.util.*;
public class MenuDriven
{
public static void main(String args[])
{
int num,rem=0,sum=0,choice;
Scanner s=new Scanner(System.in);
do
{
System.out.println("1.Fibonacci Series");
System.out.println("2.Sum of the digits");
System.out.println("3.Exit the program");
System.out.println("Enter your choice:");
choice=s.nextInt();
switch(choice)
{
case 1:int n1=0,n2=1,n3,i,count=10;
System.out.print(n1+" "+n2);
for(i=2;i<count;++i)
{
n3=n1+n2;
System.out.print(" "+n3);
n1=n2;
n2=n3;
}
break;
case 2:System.out.println("Enter the number:");
num=s.nextInt();
while(num>=1)
{
rem=num%10;
sum+=rem;
num=num/10;
}
System.out.println("Sum of the Terms= "+sum);
break;
case 3:System.exit(0);break;
}
}
while(choice!=1||choice!=2);
}

}
EXPERIMENT NO: 09
DATE:

Aim: To write a java program to display the pattern:


*
*#
*#*
*#*#
*#*#*
Program:
import java.util.Scanner;
import java.util.*;
public class Pattern9
{
public static void main(String args[])
{
Scanner s=new Scanner(System.in);
int row;
System.out.println("Enter the number of row:");
row=s.nextInt();
for(int i=1;i<=row;i++)
{
System.out.println();
for(int j=1;j<=i;j++)
{
if(j%2==1)
{
System.out.print("* ");
}
else
{
System.out.print("# ");
}

}
System.out.println();
}
}
}
EXPERIMENT NO: 10
DATE:

Aim: Write a java program to check whether a given number is an abundant


number or not. (An abundant number is a number for which the sum of its
proper factors is greater than the number itself.)

Program:
import java.util.*;
public class AbundantNumber
{
public static void main(String args[])
{
Scanner s=new Scanner(System.in);
int num,sum=0;
System.out.println("Enter the number:");
num=s.nextInt();
for(int i=1;i<num;i++)
{
if(num%i==0)
{
sum+=i;
}
}
if(sum>num)
System.out.println(num+" is an abundant number");
else
System.out.println(num+" is not an abundant number");
}

}
EXPERIMENT NO: 11
DATE:

Aim: To write a java program to print the sum of the following series.
𝟏 𝟐 𝟑
S= - + - ………………………. up to n terms.
𝟏! 𝟐! 𝟑!

Program:

import java.util.*;
public class SumOfSeries11
{
public static double factorial(int i)
{
if (i == 1)
{
return 1;
}
return i * factorial(i - 1);
}
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
int n;
System.out.println("Enter the number of terms:");
n=sc.nextInt();
double sum=0.0d;
for (int i = 1; i <= n; i++)
{
sum = sum + (Math.pow(-1,(i+1)))*((double)i / factorial(i));
}
System.out.println("Sum of the series is: "+sum);
}
}
EXPERIMENT NO: 12
DATE:

Aim: A bank intends to design a program to display the denomination of an input


amount up to 5 digits. The available denomination with the bank is of rupees
2000, 500, 200, 100, 50, 20, 10, 5, 2 and 1.
Note: Preference should be given to the highest denomination available.
Program:

import java.util.*;
public class Denominations
{
public static void main(String args[])
{
int amount,D2000,D500,D200,D100,D50,D20,D10,D5,D2,D1;
Scanner s=new Scanner(System.in);
System.out.println("Enter the amount:");
amount=s.nextInt();
int temp=amount;
while(amount>=1)
{
if(amount>=2000)
{
D2000=amount/2000;
amount=amount%2000;
System.out.println(D2000+"\t*2000\t="+D2000*2000);
}
else if(amount>=500)
{
D500=amount/500;
amount=amount%500;
System.out.println(D500+"\t*500\t="+D500*500);
}
else if(amount>=200)
{
D200=amount/200;
amount=amount%200;
System.out.println(D200+"\t*200\t="+D200*200);
}
else if(amount>=100)
{
D100=amount/100;
amount=amount%100;
System.out.println(D100+"\t*100\t="+D100*100);
}
else if(amount>=50)
{
D50=amount/50;
amount=amount%50;
System.out.println(D50+"\t*50\t="+D50*50);
}
else if(amount>=20)
{
D20=amount/20;
amount=amount%20;
System.out.println(D20+"\t*20\t="+D20*20);
}
else if(amount>=10)
{
D10=amount/10;
amount=amount%10;
System.out.println(D10+"\t*10\t="+D10*10);
}
else if(amount>=5)
{
D5=amount/5;
amount=amount%5;
System.out.println(D5+"\t*5\t="+D5*5);
}
else if(amount>=2)
{
D2=amount/2;
amount=amount%2;
System.out.println(D2+"\t*2\t="+D2*2);
}
else
{
D1=amount/1;
System.out.println(D1+"\t*1\t="+D1*1);break;
}
}
System.out.println("Total\t\t="+temp);

}
}
EXPERIMENT NO: 13
DATE:

Aim: To write a java program to accept a number and check whether it is a spy
number or not. (A spy number is a number whose sum of the digits is equals
to the product of the digits)
Program:
import java.util.*;
public class SpyNumber
{
public static void main(String args[])
{
int num,sum=0,product=1;
Scanner s=new Scanner(System.in);
System.out.println("Enter a number:");
num=s.nextInt();
while(num>0)
{
int rem=num%10;
sum+=rem;
product*=rem;
num=num/10;
}
if(sum==product)
System.out.println("It is a Spy Number");
else
System.out.println("It is not a Spy Number");
}

}
EXPERIMENT NO: 14
DATE:

Aim: To write a java program to input a number and check whether it is a Harshad
number or not. (A number is said to be Harshad, if it is divisible by the sum
of its digits)
Program:
import java.util.*;
public class HarshadNumber
{
public static void main(String args[])
{
int num,sum=0;
Scanner s=new Scanner(System.in);
System.out.println("Enter the number:");
num=s.nextInt();
int temp=num;
while(num>0)
{
int rem=num%10;
sum+=rem;
num=num/10;
}
if(temp%sum==0)
{
System.out.println("It is a Harshad Number");
}
else
System.out.println("It is not a Harshad Number");
}
}
EXPERIMENT NO: 15
DATE:

Aim: To write a java program to print the following pattern:


123454321
1234 4321
123 321
12 21
1 1
Program:
public class Pattern15
{
public static void main(String args[])
{
for (int i = 1; i <= 5; i++)
{
for (int j = 1; j <=6- i; j++)
{
System.out.print(j);
}
for (int j = 3; j <= 2 * i-1; j++)
{
System.out.print(" ");
}
for (int j = 6-i; j >= 1; j--)
{
if (i == 1 && j == 5)
{
continue;
}
System.out.print(j);
}
System.out.println();
}
}
}
EXPERIMENT NO: 16
DATE:

Aim: To write a java program to perform matrix multiplication and print the result
in the form of matrix.
Program:
import java.util.*;
public class MatrixMultiplication
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
int[][] a,b,c;
int m,n,p,q,i,j,k;
System.out.println("Enter the order of first matrix:");
m=sc.nextInt();
n=sc.nextInt();
System.out.println("Enter the order of second matrix:");
p=sc.nextInt();
q=sc.nextInt();
if(n==p)
{
a=new int[m][n];
b=new int[p][q];
c=new int[m][q];
System.out.println("Enter the elements of first matrix:");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
a[i][j]=sc.nextInt();
}
}
System.out.println("Enter the elements of second matrix:");
for(i=0;i<p;i++)
{
for(j=0;j<q;j++)
{
b[i][j]=sc.nextInt();
}
}
for(i=0;i<m;i++)
{
for(j=0;j<q;j++)
{
for(k=0;k<n;k++)
{
c[i][j]+=a[i][k]*b[k][j];
}
}
}
System.out.println("The product of the two given matrix is:");
for(i=0;i<m;i++)
{
System.out.println();
for(j=0;j<q;j++)
{
System.out.print(c[i][j]+" ");
}
}

}
else
System.out.println("Order mismatch-Multiplication not possible:");
}
}
EXPERIMENT NO: 17
DATE:

Aim: To write a class using function overloading that computes the volume of a
cube, a sphere and a cuboid.
Hint:
Volume of a Cube = s * s * s
Volume of a Cuboid = l * b * h
𝟒
Volume of a Sphere = * *r*r*r
𝟑
Use switch case and BufferedReader or Scanner method for accepting the
values of radius, length, breadth and height etc.
import java.util.*;
public class Volume
{
int volume(int n)
{
return n*n*n;
}
int volume(int a,int b, int c)
{
return a*b*c;
}
double volume(double r)
{
return (3.14*4*r*r*r/3);
}
public static void main(String args[])
{ Volume v=new Volume();
Scanner s=new Scanner(System.in);
int side,length,breadth,height,ch;
float radius;
do
{
System.out.println("1.Volume of Cube");
System.out.println("2.Volume of Cuboid");
System.out.println("3.Volume of Sphere");
System.out.println("4.Exit");
System.out.println("Enter your choice:");
ch=s.nextInt();
switch(ch)
{
case 1:System.out.println("Enter the side of Cube:");
side=s.nextInt();
System.out.println(v.volume(side));
break;
case 2:System.out.println("Enter the length:");
length=s.nextInt();
System.out.println("Enter the breadth:");
breadth=s.nextInt();
System.out.println("Enter the height:");
height=s.nextInt();
System.out.println(v.volume(length,breadth,height));
break;
case 3:System.out.println("Enter the Radium of sphere");
radius=s.nextFloat();
System.out.println(v.volume(radius));
break;
case 4:System.exit(0);break;

}
}
while(ch==1||ch==2||ch==3||ch==4);
}

}
EXPERIMENT NO: 18
DATE:

Aim: To write a java program to generate 10 characters (char data) between A and
J randomly?
Program:
import java.util.Random;

public class RandomChar


{
private static char RandomChar()
{
Random r = new Random();
return (char)(r.nextInt(10) + 'A');
}

public static void main(String[] args)


{
for(int i=0; i<10;i++)
{
char c = RandomChar();
System.out.print(" "+c);
}
}

}
EXPERIMENT NO: 19
DATE:

Aim: To define a class ElectricityBill with the following specifications:


Class name : ElectricityBill
Date Members/Instance Variables :
String n -> stores the name of the customer
Int units ->stores the number of units consumed
Double bill ->stores the amount to be paid
Member methods:
void accept() -> to input the name of the customer and no of units
consumed.
void calculate() ->to calculate the bill as per the following tariff
void print() -> To display the details
Display the details as follows:
Name of the customer:…………………………………………..
Number of units consumed:…………………………………….
Bill Amount: ………………………………………………………
Number of units Rate per unit
First 100 units Rs. 2.00
Next 200 units Rs. 3.00
Above 300 units Rs. 5.00
A surcharge of 2.5% is charged if the units consumed is above 300 units.
Write a main method to create an object of the class and call the above member
methods.
Program:
import java.util.*;
public class ElectricityBill
{
String name;
int units;
double bill;
void accept()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter name of customer:");
name=sc.nextLine();
System.out.println("Enter the units of electricity used:");
units=sc.nextInt();
}
void calculate()
{
if(units<=100)
bill=units*2;
else if(units<300)
bill=(100*2)+(units-100)*3;
else
bill=(100*2)+(200*3)+(units-300)*5+((units-300)*0.025);

}
void print()
{
System.out.println("Name of the Customer\t\t:"+name);
System.out.println("Number of units consumed\t:"+units);
System.out.println("Electricity Bill\t\t:"+bill);
}
public static void main(String args[])
{
ElectricityBill eb=new ElectricityBill();
eb.accept();
eb.calculate();
eb.print();
}
}
EXPERIMENT NO: 20
DATE:

Aim: To design a class in java to overload a method Number() as follows:


void Number(int num, int d) : To count and display the frequency of the digit
in a number
void Number(int n1) : To find and display the sum of even digits of a number.
Write a main method to create an object an invoke the above methods.
Program:
import java.util.*;
public class Number
{
static int count=0;
int sum;
void Number(int num,int d)
{
while(num>0)
{
int n=num%10;
if(n==d)
count++;
num=num/10;
}
System.out.println("The digit"+d+" present"+count+"times");
}
void Number(int num)
{
while(num>0)
{
int rem=num%10;
if(rem%2==0)
sum+=rem;
num=num/10;
}
System.out.println("Sum of Even digits="+sum);
}
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
int ch,num,d,n;
Number Num=new Number();
do
{
System.out.println("1. Frequency of digit");
System.out.println("2. Sum of Even digits");
System.out.println("3.Exit");
System.out.println("Enter your choice:");
ch=sc.nextInt();
switch(ch)
{
case 1: System.out.println("Enter a number:");
num=sc.nextInt();
System.out.println("enter the number to check:");
d=sc.nextInt();
Num.Number(num,d);break;
case 2: System.out.println("Enter the number:");
n=sc.nextInt();
Num.Number(n);break;
case 3:System.exit(0);
default:System.out.print("Enter a correct choice");
}
}
while(ch==1|ch==2|ch==3);

}
EXPERIMENT NO: 21
DATE:

Aim: Write a program to store 10 names in an array. Arrange these in alphabetical


order by sorting. Print the sorted list. Take single word names all in capital
letters.
Program:
import java.util.*;
public class BubbleSortName
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
String names[] = new String[10];
System.out.println("Enter 10 names:");
for (int i = 0; i < names.length; i++)
{
names[i] = sc.nextLine();
}
System.out.println("Names before Sorting: ");
for (int i = 0; i < names.length; i++)
{
System.out.println(names[i]);
}
for (int i = 0; i < names.length - 1; i++)
{
for (int j = 0; j < names.length - 1 - i; j++)
{
if (names[j].compareToIgnoreCase(names[j + 1]) > 0)
{
String temp = names[j + 1];
names[j + 1] = names[j];
names[j] = temp;
}
}}
System.out.println("Sorted Names: ");
for (int i = 0; i < names.length; i++)
{
System.out.println(names[i]);
}
}
}
EXPERIMENT NO: 22
DATE:

Aim: To write a java program to find the inputted word is a special word or not.
(String function)
Hint: Special words are those words which starts and end with the same letter.
Program:
import java.util.*;
public class SpecialWords
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter a word: ");
String str = sc.nextLine();
str = str.toUpperCase();
int len = str.length();
if (str.charAt(0) == str.charAt(len - 1))
{
System.out.println("It is a Special word");
}
else
{
System.out.println("It is not a Special word");
}
}
}
EXPERIMENT NO: 23
DATE:
Aim: To write a java program to find whether a given word is palindrome or
not.(String function)
Hint: A palindrome word is one which reads same from left to right and right
to left.
Program:
import java.util.*;
public class StringPallindrome
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
boolean p = true;
System.out.println("Enter the word to check palinndrome:");
String word=sc.nextLine();
int wl=word.length();
for (int i = 0; i < wl; i++){
String str = word.toUpperCase();
int strLen = str.length();
for (int j = 0; j < strLen / 2; j++)
{
if (str.charAt(j) != str.charAt(strLen - 1 - j))
{
p = false;
break;
}
}
}
if (p)
{
System.out.println("It is a Palindrome word");
}
else
{
System.out.println("It is not a palindrome word");
}
}
}
EXPERIMENT NO: 24
DATE:
Aim: To write a java program to accept a sentence and print only the first letter of
each word of the sentence in capital letter separated by a full stop.
Program:
import java.util.*;
public class ShortForm
{
public static void main(String[] args) throws Exception
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter any sentence: ");
String s=sc.nextLine();
s=" "+s;
s=s.toUpperCase();
int l=s.length();
System.out.println("Output: ");
for(int i=0;i<l;i++)
{
char x=s.charAt(i);
if(x==' ')
{
System.out.print(s.charAt(i+1)+".");
}
}
}

}
EXPERIMENT NO: 25
DATE:
Aim: To write a java program to accept a name and display it in in the given
format.
Input: RAJ PRATAP SINGH CHAUHAN
Output : R.P.S CHOUHAN
Program:
import java.util.*;
public class ShortName
{
public static void main(String args[])throws Exception
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter a name having 3 or more words:");
String name = sc.nextLine();
sc.close();
String str1 = "",str2 = "";
System.out.println("The full name is: " + name);
System.out.print("Initials with surname is: ");
int len = name.length();
name = name.trim();
for (int i = 0; i < len; i++)
{
char ch = name.charAt(i);
if (ch != ' ')
{
str1 = str1 + ch;
}
else
{
System.out.print(Character.toUpperCase(str1.charAt(0))+". ");
str1 = "";
}
}
int surlen=str1.length();
for (int j = 0; j < surlen; j++)
{
if (j == 0)
{
str2+= Character.toUpperCase(str1.charAt(0));
}
else
{
str2+= Character.toLowerCase(str1.charAt(j));
}
}
System.out.println(str2);
}

EXPERIMENT NO: 26
DATE:

Aim: A private cab service company provides service within the city at the
following rates:
KM AC Car Non-AC Car
Up to 5km Rs. 150/- Rs. 120/-
Beyond 5km Rs.10 per KM Rs. 8 per KM
Design a class CabService with the following description:
Member variables/data members:
String car_type -> to store the type of car (AC or non-AC)
Double km ->to store the kilometres travelled
Double bill -> to calculate and store the bill amount
Member methods:
CabServive() -> Default constructor to initialize data members
String data members to “ “ and double data to 0.0
void accept() -> to accept car type and kilometres
void calculate() -> to calculate the bill as per the rules given above.
void display() -> to display the bill
Display the output bill as per the following format:
Car type : ……………………………….
Kilometres travelled :…………………
Total bill : ………………………………
Create an object of the class in the main method and invoke the member
methods.

Program:
import java.util.*;
import java.lang.*;
public class CabService
{
String car_type;
double km,bill;
CabService()
{
car_type="";
km=0.0;
bill=0.0;
}
void input()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter the Type of car used: AC or NON AC");
System.out.println("Type as AC or NON AC");
car_type=sc.nextLine();
car_type=car_type.toUpperCase();
System.out.println("Enter the km travelled: ");
km=sc.nextDouble();
}
void calculate()
{
if(car_type.equals("AC"))
{
if(km>0 && km<=5)
{
bill=150;
}
else
{
bill=150+(km-5)*10;
}
}
else if(car_type.equals("NON AC"))
{
if(km>0 && km<=5)
{
bill=120;
}
else{
bill=120+(km-5)*8;
}
}
else
{
System.out.println("Wrong Car type typed");
}
}
void display()
{
System.out.println("Cat Type: "+car_type);
System.out.println("Kilometer travelled: "+km);
System.out.println("Total bill: "+bill);
}
public static void main(String args[])throws Exception
{
CabService CS= new CabService();
CS.input();
CS.calculate();
CS.display();
}

}
EXPERIMENT NO: 27
DATE:
Aim: To write a class program with the following specifications
Class name : Piglatin
Data members/instances : String wd
Member methods:
Piglatin() :Constructor to initialize the String wd.
void input(String wd) :Function to get the input from the user.
void display() : Convert the word in piglatin form and display it.
Program:
import java.util.*;
public class PigLatin
{
String wd;
PigLatin()
{
wd=" ";
}
void input()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter the string to be converted to piglatin");
wd=sc.nextLine();
}
void display()
{
int strlen=wd.length();
int index=-1;
for (int i=0; i<strlen; i++)
{
switch(wd.charAt(i))
{
case 'a':
case 'A':
case 'e':
case 'E':
case 'i':
case 'I':
case 'o':
case 'O':
case 'u':
case 'U':
index=i;
break;
default:
continue;
}
break;
}
if(index==-1)
{
System.out.println("Piglatin is not possible. since the string dosen't have any
vowel");
}
else
{
System.out.println((wd.substring(index) + wd.substring(0, index) + "ay"));
}
}
public static void main(String args[])throws Exception
{
PigLatin p=new PigLatin();
p.input();
p.display();
}

}
EXPERIMENT NO: 28
DATE:

Aim: Write a java program to accept 10 integer numbers in to an array. Bubble


sorts and the print the array.
Program:
import java.util.*;
public class BubbleSort
{
public static void main(String args[])
{
int[] a=new int[10];
int temp;
Scanner s=new Scanner(System.in);
System.out.println("Enter 10 elements into the array");
for(int i=0;i<10;i++)
{
a[i]=s.nextInt();
}
for(int i=0;i<10;i++)
{
for(int j=0;j<10-i-1;j++)
{
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
System.out.println("Sorted array is:");
for(int i=0;i<10;i++)
{
System.out.println(a[i]);
}
}

}
EXPERIMENT NO: 29
DATE:

Aim: To write a java program to accept the names of 10 cities in a single dimension
string array and their STD codes in another single dimensional integer
array. Search for a name of a city input by the user in the list. If found,
display “Search successful” and print the name of the city along with its STD
code, or else display the message “Search Unsuccessful, No such city in the
list”.
Program:
import java.util.*;
public class LinearSearch
{
public static void main(String args[])
{
final int size = 10;
int k;
Scanner sc = new Scanner(System.in);
String cities[] = new String[size];
String stdCodes[] = new String[size];
System.out.println("Enter " + size + " cities and their STD codes:");
for (int i = 0; i < size; i++)
{
System.out.print("Enter City Name: ");
cities[i] = sc.nextLine(); // First array of City Names
System.out.print("Enter its STD Code: ");
stdCodes[i] = sc.nextLine(); // Second array of STD Codes
}
System.out.print("Enter name of city to search: ");
String city = sc.nextLine(); // Name of the city for Searchingint k;
// Indexing for linear searching
for (k = 0; k < size; k++)
{
if (city.compareToIgnoreCase(cities[k]) == 0)
{
break;
}
}
if (k < size)
{
System.out.println("Search Successful");
System.out.println("City: " + cities[k]);
System.out.println("STD Code: " + stdCodes[k]);
}
else
{
System.out.println("Search Unsuccessful");
}
}
}
EXPERIMENT NO: 30
DATE:

Aim: To write a java program to accept any 10 integer elements in to an array and
print the reverse of the array.
Program:
import java.util.*;
public class ArrayReverse
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
int[] a=new int[10];
int i;
System.out.println("Enter 10 elements into the array:");
for(i=0;i<10;i++)
{
a[i]=sc.nextInt();
}
System.out.println("Reverse of the Array is:");
for(i=9;i>=0;i--)
{
System.out.println(a[i]);
}
}
}

You might also like