Thanks to visit codestin.com
Credit goes to www.scribd.com

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

ICSE Class 10 Java Projects

25 programs of computer

Uploaded by

rajanya1108
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)
30 views45 pages

ICSE Class 10 Java Projects

25 programs of computer

Uploaded by

rajanya1108
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/ 45

ICSE COMPUTER APPLICATION

PROJECT 2024-25

NAME: RAJANYA BASU

CLASS: 10

SECTION: D

ROLL NUMBER: 5
PROGRAM 1:
Question:
Write a program to input three numbers and print their sum of cubes.

Ans:
import java.util.*;

class Sum_of_cubes

public static void main()

Scanner sc=new Scanner(System.in); //Input

System.out.println("Enter Number 1:");

int num1=sc.nextInt();

System.out.println("Enter Number 2:");

int num2=sc.nextInt();

System.out.println("Enter Number 3:");

int num3=sc.nextInt();

int sum=(int)(Math.pow(num1,3)+Math.pow(num2,3)+Math.pow(num3,3));

System.out.println("Three numbers are:"+num1+","+num2+","+num3);

System.out.println("Sum of their cubes is:"+sum); //Output

Variable Data type Use


num1 int Store first number
num2 int Store second number
num3 Int Store third number
sum Int Store sum of cube
Input-Output screen
Enter Number 1:
12
Enter Number 2:
5
Enter Number 3:
7
Three numbers are:12,5,7
Sum of their cubes is:2196
==================================================================================
Enter Number 1:
10
Enter Number 2:
8
Enter Number 3:
19
Three numbers are:10,8,19
Sum of their cubes is:8371

PROGRAM 2
Question:
Write a program that computes the area and circumference of a circle of an accepted radius.Use 3.14
as the value of pi.

Ans:
import java.util.*;

class Circle

public static void main()

double area,circumference,radius; //use double for precision

Scanner sc=new Scanner(System.in);

System.out.println("Enter radius of circle");

radius=sc.nextDouble();

circumference=2*3.14*radius; //circumference=2*pi*radius

area=3.14*radius*radius; //area=pi*radius*radius

System.out.println("The radius of the circle is:"+ radius);

System.out.println("It's circumference is:"+ circumference);

System.out.println("It's area is:"+area);

} }
Variable Data type Use
radius double Store radius of circle
circumference double Store the value of circumference
area double Store the area of circle

Input-Output screen
Enter radius of circle
2.1
The radius of the circle is:2.1
It' s circumference is:13.188
It's area is:13.8474
=======================================================================

Enter radius of circle


3.0
The radius of the circle is:3.0
It's circumference is:18.84
It's area is:28.259999999999998

PROGRAM 3
Question:
Write a program to input the runs conceded by a bowler in 4 overs and compute his economy.

Ans:
import java.util.*;

class Economy_rate

public static void main()

int ov_1,ov_2,ov_3,ov_4;

double eco;

Scanner sc=new Scanner(System.in);

System.out.println("Enter runs conceded in first over");

ov_1=sc.nextInt();

System.out.println("Enter runs conceded in second over"); //Input

ov_2=sc.nextInt();

System.out.println("Enter runs conceded in third over");

ov_3=sc.nextInt();

System.out.println("Enter runs conceded in last over");

ov_4=sc.nextInt();
eco=(ov_1+ov_2+ov_3+ov_4)/4.0;

System.out.println("Economy rate of the bowler is:"+ eco); //output

Variable Data type Use


ov_1 int Runs conceded in first over
ov_2 int Runs conceded in second over
ov_3 int Runs conceded in third over
ov_4 int Runs conceded in fourth over
eco double Economy of bowler

Input-Output screen
Enter runs conceded in first over
5
Enter runs conceded in second over
12
Enter runs conceded in third over
7
Enter runs conceded in last over
10
Economy rate of the bowler is:8.5
=======================================================================
Enter runs conceded in first over
3
Enter runs conceded in second over
17
Enter runs conceded in third over
4
Enter runs conceded in last over
9
Economy rate of the bowler is:8.25

PROGRAM 4
Question:
Write a program to accept an integer whether it is odd or even. Also check whether it is divisible by 3
or not.
Ans:
import java.util.*;

class Odd_even

public static void main()

int num;

Scanner sc=new Scanner(System.in); //Input

System.out.println("Enter an integer");

num=sc.nextInt();

System.out.println("The number is:"+num);

if(num%2==1) //Conditions

System.out.println("The number is odd");

else

System.out.println("The number is even");

if(num%3==0)

System.out.println("The number is divisible by 3");

else

System.out.println("The number is not divisible by 3");

Input-Output screen
Enter an integer
65
The number is:65
The number is odd
The number is not divisible by 3
=======================================================================
Enter an integer
36
The number is:36
The number is even
The number is divisible by 3

Variable Data type Use


num int Store the integer
PROGRAM 5
Question:
Write a program to input 3 numbers and print the largest number among them.

Ans:
import java.util.*;

class Largest {

public static void main() {

int n1,n2,n3,largest;

Scanner sc=new Scanner(System.in);

System.out.println("Enter 1st number"); //Input

n1=sc.nextInt();

System.out.println("Enter 2nd number");

n2=sc.nextInt();

System.out.println("Enter 3rd number");

n3=sc.nextInt();

largest=Math.max(n1,n2); //Finding largest number

largest=Math.max(largest,n3);

System.out.println("The largest number is:"+largest);

} }
Variable Data type Use
n1 int First number
n2 int Second number
n3 int Third number
largest int Store the largest number

Input-Output screen
Enter 1st number
5
Enter 2nd number
77
Enter 3rd number
91
The largest number is:91
=======================================================================
Enter 1st number
66
Enter 2nd number
78
Enter 3rd number
33
The largest number is:78

PROGRAM 6
Question:
Write a program to input three unequal numbers and display the second smallest number.

Ans:
import java.util.*;

class Smallest

public static void main()

int a,b,c;

Scanner sc=new Scanner(System.in); //Input

System.out.println("Enter three numbers");

a=sc.nextInt();

b=sc.nextInt();

c=sc.nextInt();

if((a<b)&&(a<c)) //Conditions

if (b<c)

System.out.println("The second smallest number is:"+b);

else

System.out.println("The second smallest number is:"+c);

if((b<c)&&(b<a))

if(c<a)

System.out.println("The second smallest number is:"+c);

else

System.out.println("The second smallest number is:"+a);

if((c<a)&&(c<b))
{

if(a<b)

System.out.println("The second smallest number is:"+a);

else

System.out.println("The second smallest number is:"+b);

} //Main closed

} //Class closed

Variable Data type Use


a int Store first number
b int Store second number
c int Store third number

Input-Output screen
Enter three numbers
65
41
98
The second smallest number is:65
=======================================================================
Enter three numbers
12
4
89
The second smallest number is:12
PROGRAM 7
Question:
Write a program to input three angles of a triangle and check whether a triangle is possible or not. If
possible then check whether it is an acute-angled triangle, right-angled or an obtuse-angled triangle
otherwise, display 'Triangle not possible'.

Ans:
import java.util.*;

class TriangleAngle1

public static void main()

Scanner sc = new Scanner(System.in);

System.out.print("Enter first angle: ");

int a1 = sc.nextInt();

System.out.print("Enter second angle: ");

int a2 = sc.nextInt();

System.out.print("Enter third angle: ");

int a3 = sc.nextInt();

int Sum = a1 + a2 + a3;

if (Sum == 180 && a1 > 0 && a2 > 0 && a3 > 0) { //check for triangle

if (a1 < 90 && a2 < 90 && a3 < 90) {

System.out.println("Acute-angled Triangle");

else if (a1 == 90 || a2 == 90 || a3 == 90) { //check for type of triangle

System.out.println("Right-angled Triangle");

else if(a1>90&&a1<180||a2>90&&a2<180||a3>90&&a3<180)

System.out.println("Obtuse-angled Triangle");

else {
System.out.println("Triangle not possible");

Variable Data type Use


a1 int Store 1st angle
a2 int Store 2nd angle
a3 int Store 3rd angle
Sum int Sum of angles

Input-Output screen
Enter first angle: 45
Enter second angle: 90
Enter third angle: 45
Right-angled Triangle
=======================================================================
Enter first angle: 31
Enter second angle: 29
Enter third angle: 120
Obtuse-angled Triangle

PROGRAM 8
Question:
Write a menu based program accepting 2 numbers and entering: -

1 for adding the two numbers

2 for subtracting the first number from the second number

3 for multiplying the 2 numbers

4 for dividing the second number by the first number

Ans:
import java.util.*;

class calc {

public static void main() {

int n1,n2,c;

Scanner sc=new Scanner(System.in);

System.out.println("Enter first number");

n1=sc.nextInt();

System.out.println("Enter second number");

n2=sc.nextInt();
System.out.println("Enter 1 for addition,2 for subtraction,3 for multiplication and 4 for division");

c=sc.nextInt(); //Input of choice

switch (c) //menu(case based structure)

{ case 1:

int s=n1+n2;

System.out.println("The sum of the numbers is:"+s);

break;

case 2:

int diff=n2-n1;

System.out.println("The first number subtracted from the second number is:"+diff);

break;

case 3:

int pro=n1*n2;

System.out.println("The product of the numbers is:"+pro);

break;

case 4:

double quo=n2/n1;

System.out.println("The second number divided by the first number is:"+quo);

break;

default:

System.out.println("Wrong choice entered");

Variable Data type Use


n1 int First number
n2 int Second number
c int Input choice
s int Find sum
diff int Evaluate difference
pro int Find product Input-Output
Quo double Evaluate division screen
Enter first number
5
Enter second number
55
Enter 1 for addition,2 for subtraction,3 for multiplication and 4 for
division
4
The second number divided by the first number is:11.0
=======================================================================
Enter first number
12
Enter second number
14
Enter 1 for addition,2 for subtraction,3 for multiplication and 4 for
division
2
The first number subtracted from the second number is:2
=======================================================================
Enter first number
66
Enter second number
0
Enter 1 for addition,2 for subtraction,3 for multiplication and 4 for
division
3
The product of the numbers is:0

PROGRAM 9
Question:
Using the switch statement, write a menu driven program:

1. To check and display whether a number input by the user is a composite number or not.
A number is said to be composite, if it has one or more than one factors excluding 1 and the
number itself.
Example: 4, 6, 8, 9...

2. To find the smallest digit of an integer that is input:


Sample input: 6524
Sample output: Smallest digit is 2
For an incorrect choice, an appropriate error message should be displayed.

Ans:
import java.util.Scanner;

class Menu

public static void main() {

Scanner sc = new Scanner(System.in);

System.out.println("Type 1 for Composite Number");

System.out.println("Type 2 for Smallest Digit");

System.out.print("Enter your choice: ");


int ch = sc.nextInt();

switch (ch) {

case 1: //case1

System.out.print("Enter Number: ");

int n = sc.nextInt();

int c = 0;

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

if (n % i == 0)

c++; }

if (c > 2)

System.out.println("Composite Number");

else

System.out.println("Not a Composite Number");

break;

case 2: //case2

System.out.print("Enter Number: ");

int num = sc.nextInt();

int s = 10;

while (num != 0) {

int d = num % 10;

if (d < s)

s = d;

num /= 10;

System.out.println("Smallest digit is " + s);

break;

default:

System.out.println("Wrong choice");

} } }

Variable Data type Use


ch int Accept choice
n int Accept number for 1st case
c int Count factors
i int Lcv
num int Accept number in case 2
s int Smallest digit
d int Extracting digit

Input-Output screen
Type 1 for Composite Number
Type 2 for Smallest Digit
Enter your choice: 1
Enter Number: 71
Not a Composite Number
=======================================================================
Type 1 for Composite Number
Type 2 for Smallest Digit
Enter your choice: 2
Enter Number: 98106
Smallest digit is 0

PROGRAM 10
Question:
Write a program to accept a number and print its factorial.

Ans:
import java.util.*;

class Factorial

public static void main()

int n,i,f=1;

Scanner sc=new Scanner(System.in); //Input

System.out.println("Enter a number");

n=sc.nextInt();

for(i=1;i<=n;i++){

f=f*i; //Factorial

System.out.println("The factorial of the number is:"+f);

}
}

Variable Data type Use


n int Accept number
i int Loop control variable
f int Store factorial

Input-Output screen
Enter a number
7
The factorial of the number is:5040
=======================================================================
Enter a number
13
The factorial of the number is:1932053504

PROGRAM 11
Question:
Write a program to accept a number and check whether the given number is prime or not.

Ans:
import java.util.*;

class Prime

public static void main()

int num,i,flag=0;

Scanner sc=new Scanner(System.in); //Input

System.out.println("Enter a number");

num=sc.nextInt();

for(i=2;i<=(int)num/2;i++)

if(num%i==0)

flag=1;
break;

if(flag ==0) //Checking prime

System.out.println("The number is prime");

else

System.out.println("The number is not prime");

} Variable Data type Use


num int Accept number
i int Loop control variable
flag int Check prime number

Input-Output screen
Enter a number
19
The number is prime
=======================================================================
Enter a number
91
The number is not prime

PROGRAM 12
Question:
Write a program to accept a number and print the sum of its digits.

Ans:
import java.util.*;

class Sum_of_digi

public static void main()

int num,s=0;

Scanner sc=new Scanner(System.in); //Input

System.out.println("Enter a number");

num=sc.nextInt();

while(num!=0)
{

s=s+(num%10); //Evaluating sum

num/=10;

System.out.println("The sum of the digits is:"+s);

Variable Data type Use


num int Accept number
s int Store sum of digits

Input-Output screen
Enter a number
227
The sum of the digits is:11
=======================================================================
Enter a number
777949
The sum of the digits is:43

PROGRAM 13
Question:
Design a class name ShowRoom with the following description:

Instance variables / Data members:


String name — To store the name of the customer
long mobno — To store the mobile number of the customer
double cost — To store the cost of the items purchased
double dis — To store the discount amount
double amount — To store the amount to be paid after discount

Member methods:
ShowRoom() — default constructor to initialize data members
public static void input() — To input customer name, mobile number, cost
public static void calculate() — To calculate discount on the cost of purchased items, based on
following criteria

Cost Discount (in percentage)

Less than or equal to ₹10000 5%

More than ₹10000 and less than or equal to ₹20000 10%


Cost Discount (in percentage)

More than ₹20000 and less than or equal to ₹35000 15%

More than ₹35000 20%

public static void display() — To display customer name, mobile number, amount to be paid after
discount.

Write a main method to create an object of the class and call the above member methods.

Ans:
import java.util.Scanner;

class ShowRoom

String name;

long mobno;

double cost;

double dis;

double amount;

ShowRoom()

name = "";

mobno = 0;

cost = 0.0;

dis = 0.0;

amount = 0.0;

public static void input() {

Scanner sc = new Scanner(System.in);

System.out.print("Enter customer name: ");

name = sc.nextLine();

System.out.print("Enter customer mobile no: ");


mobno = sc.nextLong();

System.out.println("Enter cost: ");

cost = sc.nextDouble();

public static void calculate() {

int disPercent = 0;

if (cost <= 10000)

disPercent = 5;

else if (cost <= 20000)

disPercent = 10;

else if (cost <= 35000)

disPercent = 15;

else

disPercent = 20;

dis = cost * disPercent / 100.0;

amount = cost - dis;

public static void display() {

System.out.println("Customer Name: " + name);

System.out.println("Mobile Number: " + mobno);

System.out.println("Amount after discount: " + amount);

public static void main()

ShowRoom obj = new ShowRoom();

obj.input();

obj.calculate();

obj.display();

}
Variable Data type Use
name String To store name
mobno long To store mobile number
cost int To input cost
dis int Calculate discount amount
disPercent int Percentage of discount
amount int Final amount

Input-Output screen
Enter customer name: Rajanya Basu
Enter customer mobile no: 8881929392
Enter cost:
27500
Customer Name: Rajanya Basu
Mobile Number: 8881929392
Amount after discount: 23375.0
=======================================================================
Enter customer name: Rahul Ghosh
Enter customer mobile no: 98716265355
Enter cost:
43000
Customer Name: Rahul Ghosh
Mobile Number: 98716265355
Amount after discount: 34400.0

PROGRAM 14
Question:
Define a class called ParkingLot with the following description:
Instance variables/data members:
int vno — To store the vehicle number.
int hours — To store the number of hours the vehicle is parked in the parking lot.
double bill — To store the bill amount.

Member Methods:
public static void input() — To input and store the vno and hours.
public static void calculate() — To compute the parking charge at the rate of ₹3 for the first hour or
part thereof, and ₹1.50 for each additional hour or part thereof.
public static void display() — To display the detail.

Write a main() method to create an object of the class and call the above methods.

Ans:
import java.util.Scanner;

public class ParkingLot

int vno;

int hours;

double bill;

public static void input() { //Input

Scanner sc = new Scanner(System.in);

System.out.print("Enter vehicle number: ");

vno = sc.nextInt();

System.out.print("Enter hours: ");

hours = sc.nextInt();

public static void calculate() { //Calculate

if (hours <= 1)

bill = 3;

else

bill = 3 + (hours - 1) * 1.5;

public static void display() { //Display

System.out.println("Vehicle number: " + vno);

System.out.println("Hours: " + hours);

System.out.println("Bill: " + bill);


}

public static void main() {

ParkingLot obj = new ParkingLot();

obj.input();

obj.calculate();

obj.display();

Variable Data type Use


vno int Store vehicle number
hours int Store number of hours
bill double Calculate bill

Input-Output screen
Enter vehicle number: 8456
Enter hours: 3
Vehicle number: 8456
Hours: 3
Bill: 6.0
=======================================================================
Enter vehicle number: 7932
Enter hours: 5
Vehicle number: 7932
Hours: 5
Bill: 9.0

PROGRAM 15
Question:
Write a program to accept a two-digit number. Add the sum of its digits to the product of its digits. If
the value is equal to the number input, then display the message "Special two—digit number"
otherwise, display the message "Not a special two-digit number".

Ans:
import java.util.Scanner;

class Special_2digit
{

public static void main() {

Scanner sc = new Scanner(System.in); //Input

System.out.print("Enter a 2 digit number: ");

int orgNum = sc.nextInt();

int num = orgNum;

int count = 0, sum = 0, pro = 1;

while (num != 0) { //Digit extraction

int digit = num % 10;

num /= 10;

sum += digit;

pro *= digit;

count++;

if (count != 2)

System.out.println("Invalid input, please enter a 2-digit number");

else if ((sum + pro) == orgNum)

System.out.println("Special 2-digit number");

else

System.out.println("Not a special 2-digit number");

Variable Data type


orgNum int To input the number
num int To make copy of the number
count int Count number of digits
sum int Store sum of digits
pro int Store product of digits
digit int Extract digits

Input-Output screen
Enter a 2 digit number: 45
Not a special 2-digit number
=======================================================================
Enter a 2 digit number: 59
Special 2-digit number

PROGRAM 16
Question:
Design a class to overload a function sumSeries() as follows:

(i) public static void sumSeries(int n, double x): with one integer argument and one double argument
to find and display the sum of the series given below:

s=x1−x2+x3−x4+x5... ... ... to n termss=1x−2x+3x−4x+5x... ... ... to n terms

(ii) public static void sumSeries(int n): to find and display the sum of the following series:

s=1+(1×2)+(1×2×3)+... ... ... +(1×2×3×4... ... ... ×20)s=1+(1×2)+(1×2×3)+... ... ... +(1×2×3×4... ... ... ×n)

Ans:
class Overload
{

public static void sumSeries(int n, double x) { //First function

double sum = 0;

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

double t = x / i;

if (i % 2 == 0)

sum -= t;

else

sum += t;

System.out.println("Sum = " + sum);

public static void sumSeries(int n) { //Second function

long sum = 0, term = 1;

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

term *= i;

sum += term;

System.out.println("Sum=" + sum);

Variable Data type Use


n int Number of terms
x double Enter number
i int Lcv
t double Dividing the numbers
sum double Store sum
n int Number of terms
term long Graduate the factorial
sum long Sum of series

Input-Output screen
Sum = 9.49404761904762
=======================================================================
Sum=378011820620313
PROGRAM 17
Question:
Write a program to initialize the seven Wonders of the World along with their locations in two
different arrays. Search for a name of the country input by the user. If found, display the name of the
country along with its Wonder, otherwise display "Sorry not found!".

Seven Wonders:
CHICHEN ITZA, CHRIST THE REDEEMER, TAJ MAHAL, GREAT WALL OF CHINA, MACHU PICCHU, PETRA,
COLOSSEUM

Locations:
MEXICO, BRAZIL, INDIA, CHINA, PERU, JORDAN, ITALY

Examples:
Country name: INDIA
Output: TAJ MAHAL

Ans:
import java.util.*;
class Wonders_of_world {

public static void main() {

String wonders[] = {"CHICHEN ITZA", "CHRIST THE REDEEMER", "TAJMAHAL",

"GREAT WALL OF CHINA", "MACHU PICCHU", "PETRA", "COLOSSEUM"}; //Array of wonders

String locations[] = {"MEXICO", "BRAZIL", "INDIA", "CHINA", "PERU", "JORDAN",

"ITALY"}; //Array of countries

Scanner sc = new Scanner(System.in);

System.out.print("Enter country: ");

String c = sc.nextLine();

for (int i = 0; i < locations.length; i++) {

if (locations[i].equalsIgnoreCase(c)) {

System.out.println(locations[i] + " - " + wonders[i]);

break;

if (i == locations.length)

System.out.println("Sorry Not Found!");

Variable Data type Use


wonders String Store wonders of the world
locations String Store countries
c String Input country name
i int Loop control variable

Input-Output screen
Enter country: Brazil
BRAZIL - CHRIST THE REDEEMER
=======================================================================
Enter country: India
INDIA – TAJMAHAL
=======================================================================
Enter country: France
Sorry Not Found!
=======================================================================
Enter country: Italy
ITALY – COLOSSEUM
PROGRAM 18
Question:
Write a program to input five words in an array. Arrange these words in descending order of
alphabets, using selection sort technique. Print the sorted array.

Ans:
import java.util.*;

class Selsort{

public static void main() {

Scanner sc = new Scanner(System.in);

String a[] = new String[5];

int n = a.length;

System.out.println("Enter 5 Names: ");

for (int i = 0; i < n; i++) { //Input

a[i] = sc.nextLine();

for (int i = 0; i < n - 1; i++) { //sorting of array


int pos = i;

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

if (a[j].compareToIgnoreCase(a[pos]) < 0) {

pos = j;

String t = a[pos];

a[pos] = a[i];

a[i] = t;

System.out.println("Sorted Names:");

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

System.out.println(a[i]);

Variable Data type Use


a String Array of string
n int Length of array
i int Loop control variable
pos int For sorting
j int Inner lcv
t String 3rd variable for swapping

Input-Output screen
Enter 5 Names:

Rahul

Ravi

Ashwin

Meena

Pinandiksha

Sorted Names:

Ashwin

Meena
Pinandiksha

Rahul

Ravi

PROGRAM 19
Question:
Write a program to input 15 integer elements in an array and sort them in ascending order using the
bubble sort technique.

Ans:
import java.util.Scanner;

class BubbleSort

public static void main() {

Scanner sc = new Scanner(System.in);

int n = 15;

int arr[] = new int[n];

System.out.println("Enter the elements of the array:");

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

arr[i] = sc.nextInt(); //Input of array

//Bubble Sort

for (int i = 0; i < n - 1; i++) {

for (int j = 0; j < n - i - 1; j++) {


if (arr[j] > arr[j + 1]) {

int t = arr[j];

arr[j] = arr[j+1];

arr[j+1] = t;

System.out.println("Sorted Array:");

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

System.out.print(arr[i] + " ");

Variable Data type Use


n int Length of array
arr int Array
i int Loop control variable
j int Inner loop control variable
t int Swapping

Input-Output screen
Enter the elements of the array:
7
19
22
10
0
112
23
59
26
9
55
8
45
3
444
Sorted Array:
0 3 7 8 9 10 19 22 23 26 45 55 59 112 444
PROGRAM 20
Question:
Write a program to perform binary search on a list of integers given below, to search for an element
input by the user. If it is found display the element along with its position, otherwise display the
message "Search element not found".

5, 7, 9, 11, 15, 20, 30, 45, 89, 97

Ans:
import java.util.*;

class Binsearch

public static void main()

int n,l,h,index,m;

Scanner sc = new Scanner(System.in);

int arr[] = {5, 7, 9, 11, 15, 20, 30, 45, 89, 97};

System.out.println("Enter number to search: ");

n = sc.nextInt();

l = 0; h = arr.length – 1; index = -1;

while (l <= h) {

m = (l + h) / 2;

if (arr[m] < n)

l = m + 1;
else if (arr[m] > n)

h = m - 1;

else {

index = m;

break;

if (index == -1) {

System.out.println("Search element not found");

else {

System.out.println(n + " found at position " + (index+1));

Input-Output screen
Enter number to search:
20
20 found at position 6
=======================================================================
Enter number to search:
31
Search element not found

Variable Data type Use


n int Number to search for
l int Lower limit
h int Upper limit
index int Position of found number
m int Mediator for searching
arr int Store array
PROGRAM 21
Question:
Define a class to accept values into a 3 × 3 array and check if it is a special array. An array is a special
array if the sum of the even elements = sum of the odd elements.

Ans:
import java.util.*;

class Special_arr

public static void main()

Scanner sc = new Scanner(System.in);

int arr[][] = new int[3][3];

int evenSum = 0, oddSum = 0;

System.out.println("Enter the elements of 3 x 3 DDA: ");

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

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

arr[i][j] = sc.nextInt();

for (int i = 0; i < 3; i++) { //Evaluation

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

if (arr[i][j] % 2 == 0)

evenSum += arr[i][j];
else

oddSum += arr[i][j];

System.out.println("Sum of even elements = " + evenSum);

System.out.println("Sum of odd elements = " + oddSum);

if (evenSum == oddSum) //Checking

System.out.println("Special Array");

else

System.out.println("Not a Special Array");

Variable Data type Use


arr int Store array
evenSum int Sum of even numbers
oddSum int Sum of odd numbers
i int Row number
j int Column number

Input-Output screen
Enter the elements of 3 x 3 DDA:
5
5
5
3
4
2
4
2
6
Sum of even elements = 18
Sum of odd elements = 18
Special Array
PROGRAM 22
Question:
Define a class to accept a string and convert it into uppercase. Count and display the number of
vowels in it.

Ans:
import java.util.*;

class Vowels

public static void main()

Scanner sc = new Scanner(System.in);

System.out.print("Enter the string: ");

String str = sc.nextLine();

str = str.toUpperCase();

int count = 0;

int len = str.length();

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

char ch = str.charAt(i);

if("AEIOU".indexOf(ch)>=0) //vowel checker

count++;

System.out.println("String : " + str); //Output

System.out.println("Number of vowels : " + count);

Variable Data type Use


str String Input string
count int Number of vowels
len int Length of string
i int Loop control variable
ch char Extract each character of string
Input-Output screen
Enter the string: This is a good book
String : THIS IS A GOOD BOOK
Number of vowels : 7
=======================================================================
Enter the string: The moon is beautiful.
String : THE MOON IS BEAUTIFUL.
Number of vowels : 9
PROGRAM 23
Question:
Write a program to accept a word. Check and display whether the word is a palindrome or only a
special word or none of them.

Ans:
import java.util.*;

class SpecialPalindrome

public static void main() {

Scanner sc = new Scanner(System.in);

System.out.print("Enter a word: ");

String str = sc.next();

str = str.toUpperCase();

int len = str.length();

if (str.charAt(0) == str.charAt(len - 1)) { //checking special

boolean isPalin = true;

for (int i = 1; i < len / 2; i++) { //checking pallindrome

if (str.charAt(i) != str.charAt(len - 1 - i)) {

isPalin = false;

break;

if (isPalin) {

System.out.println("Palindrome");

else {

System.out.println("Special");

else {

System.out.println("Neither Special nor Palindrome");


} } }

Variable Data type Use


str String Input string
len int Length of the string
isPallin boolean Checks pallindrime
i int Loop control variable

Input-Output screen
Enter a word: MALAYALAM
Palindrome
=======================================================================
Enter a word: COMIC
Special
PROGRAM 24
Question:
Write a program to accept a word and display its piglatin form.

Ans:
import java.util.*;

class PigLatin

public static void main() {

Scanner sc = new Scanner(System.in);

System.out.print("Enter word: ");

String word = sc.next(); //Input

int len = word.length();

word=word.toUpperCase();

String piglatin="";

int flag=0;

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

char ch = word.charAt(i);

if("AEIOU".indexOf(ch)>=0)

piglatin=word.substring(i) + word.substring(0,i) + "AY"; //Process

flag=1;

break;

if(flag == 0) {

piglatin = word + "AY";

System.out.println(word + " in Piglatin format is " + piglatin); //Output

}
Variable Data type Use
word String Input the word
len int Length of string
piglatin String Store piglatin word
flag int Counts the presence of any vowel
i int Loop control variable
ch char Character extraction

Input-Output screen
Enter word: London
LONDON in Piglatin format is ONDONLAY
=======================================================================
Enter word: Giraffe
GIRAFFE in Piglatin format is IRAFFEGAY

PROGRAM 25
Question:
Write a program to input a sentence and arrange each word of the string in alphabetical order.
Ans:
import java.util.Scanner;

class WordsSort

public static public static void main() {

Scanner sc = new Scanner(System.in);

System.out.println("Enter a sentence:");

String str = sc.nextLine();

str += " ";

int len = str.length();

String word = "";

int wc = 0;

char ch;

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

if (str.charAt(i) == ' ')

wc++;

String wordArr[] = new String[wc];

int index = 0;

for (int i = 0; i < len; i++) {

ch = str.charAt(i);

if (ch != ' ') { //Storing words in array

word = word + ch;

else {

wordArr[index++] = word;

word = "";

for (int i = 0; i < wc - 1; i++) { //Bubble sort


for (int j = 0; j < wc - i - 1; j++) {

if (wordArr[j].compareToIgnoreCase(wordArr[j + 1]) > 1) {

String t = wordArr[j];

wordArr[j] = wordArr[j+1];

wordArr[j+1] = t;

for(int i = 0; i < wc; i++) //Printing

System.out.print(wordArr[i] + " ");

Variable Data type Use


str String Input string
len int Length of string
word String Extract individual words
wc int Word count
ch char Character extraction
i int Loop control variable
wordArr String Array for storing words
index int Used for entering the words in array
j int Used for sorting
t String Used for swapping

Input-Output screen
Enter a sentence:
The day is beautiful
beautiful day is The
=======================================================================
Enter a sentence:
The sun is shining brightly
brightly is The shining sun

You might also like