Assignment Final Draft
Assignment Final Draft
2025 - 2026
COMPUTER
APPLICATIONS
PROJECT FILE
page no. 1
Computer Applications Project
Academic Year 2025 - 2026
Submitted By
NAME- ……………………………………………..................................
SOUMALYA GHOSH
CLASS- X (ICSE) SECTION- ………. B
REGISTRATION NUMBER- ……………………………..
Unique ID- ……………………………..
SCHOOL- CLARET SCHOOL, SAHANAGAR
page no. 2
BONAFIDE CERTIFICATE
CERTIFICATE
………………………………………
Signature of
Teacher in Charge
…………………………. …………………………
Signature of Principal Signature of Examiner
page no. 3
Acknowledgement
Date: ……………….
Place: ………………
………………………………….
Sign. of Student
page no. 4
CONTENTS
COMPUTER APPLICATION
CLASS -X
1. Write a program to input the basic salary of a person. He gets 15% of the basics as HRA, 15%
of the basics as Conveyance allowance and 10% of the basic as Entertainment allowance. The
total salary is calculated by adding Basic + HRA + Conveyance + Entertainment Allowance.
Calculate and print the total salary of a person.
2. Write a program to input employee name and his salary of a month and print his annual
salary. (Stream Class)
3. Write a program to find the difference between Compound Interest and Simple Interest.
(Functional Argument)
4. Write a program to input two integers using Scanner and find the product of their sum and
difference. (Scanner Class)
5. Write a program to find the value of the given expression P = (a2+b2) / (a – b). (Input using
Command line arguments)
6. Write a program to input a number and check and print whether it is a Pronic number or not.
(Pronic number is the number which is the product of two consecutive integers)
7. Write a program to accept a number and check and display whether it is a spy number or
not. (A number is spy if the sum of its digits equals the product of its digits.
8. Write a program to input two unequal positive numbers and check whether they are perfect
square numbers or not.
9. Write a program to accept a number then check whether the given number is Armstrong number or
not. (E.g. 153 is a Armstrong number because 153 = 13 + 53 +33).
10. Write a program to display first 10 terms of the following series: 2, 5,10,17, 26…..
11. Write a program to check if the number entered by user is prime or not.
12. Using switch-case write a menu driven program to i) Area of a right-angled triangle,
ii) Area of a square, iii) Area of rectangle.
13. Using a switch statement, write a menu driven program to convert a given temperature from
Fahrenheit to Celsius and vice versa. For an incorrect choice, an appropriate error message
should be displayed.
(HINT: C = 5 9 × (F − 32) and F = 1.8 × C + 32)
14. Define a class named BookFair with the following description : Instance variables /Data
members :
String Bname - stores the name of the book double price - stores the price of the book
Member methods :
(i) input() - To input and store the name and the price of the book.
(ii) calculate() - To calculate the price after discount.
Discount is calculated based on the following criteria Price
Discount
Less than or equal to `1000 2% of price
More than ` 1000 and less than or equal to ` 3000 10% of price More than ` 3000 15% of price
page no. 5
ASSIGNMENT
COMPUTER APPLICATION
CLASS -X
(iii) display() - To display the name and price of the book after discount.
Write a main() to create an object of the class and call above member methods.
15. Define a class ‘Student’ described as below:
Data members/instance variables:
name, age, m1, m2, m3 (marks in 3 subjects), maximum, average Member methods:
page no. 6
ASSIGNMENT
COMPUTER APPLICATION
CLASS -X
21. Write a JAVA program : The method takes an integer argument n and prints the following pattern
(n=4)
a
a a
a a a
a a a a
a a a
a a
a
22. Write a program to accept a mark obtained by a student in computer science and print the
grades accordingly:
Marks Grade
Above 90 A
70 to 89 B
50 to 69 C
below 50 D
23. Write a Java program to implement binary search.
page no. 7
QUESTION-1:
QUESTION-30:
Write
Write aa program
program to to input thea basic
accept mark salary of a
obtained byperson. He in
a student gets 15% ofscience
computer the basics
andas HRA,
print the15%
grades
of the basics as Conveyance allowance and 10% ofthe basic as Entertainment allowance. The
accordingly:
totalsalary is calculated by adding Basic + HRA + Conveyance + Entertainment Allowance.
Marks
Calculate and print Grade
the totalsalary of a person.
Above
ANSWER: 90 A
70 to 89 B
import
50 to 69 java.util.Scanner;
C
below
public 50 D
class Salary_Calculator {
public static void main(String[] args) {
ANSWER: Scanner sc = new Scanner(System.in);
/* PRG-30-
// InputConcept of Logical Operators
basic salary
System.out.print("Enter
A program the basicbysalary:
to accept a mark obtained "); in computer science and print the grades
a student
double
accordingly basic = sc.nextDouble();
*/ // Calculate allowances
importdouble hra = 0.15 * basic;
java.util.*;
class grades conveyance = 0.15 * basic;
double
double entertainment = 0.10 * basic;
{
// Calculate
public static voidtotal salary
main(String args[])
{ double totalSalary = basic + hra + conveyance + entertainment;
Scanner
// Outputsc=new
the resultScanner(System.in);
System.out.println("Total
int c; Salary = " + totalSalary);
} System.out.println("Enter marks in Computer science:");
}
c=sc.nextInt();
//if-else if-else Ladder is used to precisely determine
VARIABLE DESCRIPTION:
//the grade obtained according to marks
SL.if(c>=90) System.out.println("Grade=A");
VARIABLE NAME TYPE PURPOSE
NOelse if(c>=70 && c<90)
1. System.out.println("Grade=B");
sc Scanner Scanner object to take input from the user
else if(c>=50 && c<70)
2. System.out.println("Grade=C");
basic double The basic salary entered by the user
else System.out.println("Grade=D");
}3. hra double House Rent Allowance, calculated
as 15% of the basic salary
}
4. conveyance double Conveyance Allowance, calculated as 15% of the
basic salary
5. entertainment double Entertainment Allowance, calculated as 10% of
the basic salary
6. totalSalary double The total salary calculated as basic + hra +
conveyance + entertainment
SAMPLE INPUT OUTPUT:
page no.8
QUESTION-2:
Write a program to input employee name and his salary of a month and print his annual
salary.
ANSWER:
// PRG 2- Input taken using Stream Class
//Program to input employee name and his salary of a month and print his annual salary.
import java.io.*;
public class Employee
{
public static void main(String args[]) throws IOException
{
int salary;
long asal;
String name;
// Stream class objects used as ‘read’ and ‘br’
InputStreamReader read = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(read);
System.out.println("Enter Employee name:");
name = br.readLine();
System.out.println("Enter Employee's monthly salary:");
salary = Integer.parseInt(br.readLine());
asal = salary*12;
System.out.println("Employee "+name+" has Annual Salary of Rs."+asal);
}
}
VARIABLE DESCRIPTION:
SL. NO VARIABLE NAME TYPE PURPOSE
page no.9
QUESTION-3:
Write a program to find the difference between Compound Interest and Simple Interest.
ANSWER:
// PRG 3- Input taken using Functional Argument
//To find the difference between Compound Interest and Simple Interest
public class IntDiff
{
public static void main(int p, int r, int t)
{
double si, ci=0, amt, diff=0; //initializing variables
si=p*r*t/100.0;
amt=p*(Math.pow(1+r/100.0,t)); //Use of Math library function POW()
ci=amt-p;
diff=ci-si;
//Explicit type conversion from INT type to FOALT type
System.out.println("The Compound Interest, CI= Rs."+(float)ci);
System.out.println("The Simple Intterest, SI= Rs."+si);
System.out.println("The difference between Ci an SI= Rs."+(float)diff);
}
}
VARIABLE DESCRIPTION:
SL. NO VARIABLE NAME TYPE PURPOSE
page no. 10
QUESTION-4:
Write a program to input two integers using Scanner and find the product of their sum and difference.
ANSWER:
/* PRG 4- Input taken using Scanner Class
Program to input two integers using Scanner and find the product of their sum and difference.*/
import java.util.Scanner;
public class Arithmaic
{
static void main()
{
Scanner sc=new Scanner(System.in);//Scanner class object named as ‘sc’
int a,b,s,d,p;
System.out.println("Enter two integers:");
a=sc.nextInt();//initialize input value1
b=sc.nextInt();//initialize input value2
s=a+b;
d=a-b;
p=s*d;
System.out.println("Sum="+s);
System.out.println("Difference="+d);
System.out.println("Product of Sum and Difference="+p);
}
}
VARIABLE DESCRIPTION:
SL. NO VARIABLE NAME TYPE PURPOSE
page no. 11
QUESTION-5:
Write a program to find the value of the given expression P = (a2+b2) / (a – b).
ANSWER:
/* PRG 5- Input taken using Command line arguments
Program to find the value of the given expression P */
public class Expression
{
public static void main(String args[])
{
int a,b;
double p;
//the values of a and b are entered through Command line argument in String form
a = Integer.parseInt(args[0]);
b = Integer.parseInt(args[1]);
p = (double)(a*a+b*b)/(a-b);
System.out.println("The value of Expression ="+p);
}
}
VARIABLE DESCRIPTION:
SL. NO VARIABLE NAME TYPE PURPOSE
page no. 12
QUESTION-6:
Write a program to input a number and check and print whether it is a Pronic number or not. (Pronic
number is the number which is the product of two consecutive integers)
ANSWER:
/*PRG 6- Concept of 'if-else if' statements
A program to input a number and display if the number is Pronic number or not */
import java.util.*;
public class Pronic
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter the number:");
int n = sc.nextInt();
int i = 1;
// ‘while’ block increases multiplier numbers to such extent till product is lower than given number
while(i * (i + 1) < n)
{
i++;
}
//final checking done for Pronic number conditions
if(i *(i + 1) == n)
{
System.out.println(n + " is a Pronic Number.");
}
else
{
System.out.println(n + " is not a Pronic Number.");
}
}
}
VARIABLE DESCRIPTION:
SL. NO VARIABLE NAME TYPE PURPOSE
page no.13
QUESTION-7:
Write a program to accept a number and check and display whether it is a spy number or not. (A
number is spy if the sum of its digits equals the product of its digits, e.g. 123, here 1+2+3=6, and
1*2*3=6)
ANSWER:
/* PRG7- Concept of while loop
A program to input a number and check if it is a Spy number or not
(A number is spy if the sum of its digits equals the product of its digits,
e.g. 123, here 1+2+3=6, and 1*2*3=6) */
class Spy {
public static void main(int n)
{
int sum = 0;
int multiple = 1;
int a;
int p = n;
while(n!=0)
{
a = n % 10;//fetching the digits of the numbers
sum = sum + a;//calculating sum of digits
multiple = multiple * a;//calculating products of digits
n = n/10;
}
System.out.println("The sum of " +p +" is "+sum);
System.out.println("The product of "+p +" is " + multiple);
if(sum == multiple) {
System.out.println("It is a Spy Number Where Sum = Product");
}
else {
System.out.println(" It is NOT a Spy Number");
}
}
}
VARIABLE DESCRIPTION:
SL. NO VARIABLE NAME TYPE PURPOSE
page no.14
SAMPLE INPUT OUTPUT:
page no.15
QUESTION-8:
Write a program to input two unequal positive numbers and check whether they are perfect square
numbers or not. If the user enters a negative number, then the program displays the message
'Square root of a negative number can't be determined'.
Sample Input: 81, 100
Sample Output: They are perfect square numbers.
Sample Input: 225, 99
Sample Output: 225 is a perfect square number. 99 is not a perfect square number.
ANSWER:
/* +
. If the user enters a negative number then
the program displays the message 'Square root of a negative number
can't be determined'. */
import java.util.Scanner;
public class PerfectSquare
{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.print("Enter first number: ");
int a = in.nextInt();
System.out.print("Enter second number: ");
int b = in.nextInt();
if (a < 0 || b < 0) {
System.out.println("Square root of a negative number can't be determined");
}
else {
double sqrtA = Math.sqrt(a);
double sqrtB = Math.sqrt(b);
double isAPerfectSq = sqrtA - Math.floor(sqrtA);
double isBPerfectSq = sqrtB - Math.floor(sqrtB);
if (isAPerfectSq == 0 && isBPerfectSq == 0) {
System.out.println("They are perfect square numbers.");
}
else if (isAPerfectSq == 0) {
System.out.println(a + " is a perfect square number.");
System.out.println(b + " is not a perfect square number.");
}
else if (isBPerfectSq == 0) {
System.out.println(a + " is not a perfect square number.");
System.out.println(b + " is a perfect square number.");
}
else {
System.out.println("Both are not perfect square numbers.");
}
page no. 16
}
}
}
VARIABLE DESCRIPTION:
SL. NO VARIABLE NAME TYPE PURPOSE
page no. 17
QUESTION-9:
Write
Writeaaprogram
Write programtotoinput
a program to acceptthea abasic
accept
mark salary
obtained
numberthen of abyperson.
a student
check He in
gets
whetherthe 15%
computer
givenofscience
the basics
number
andas HRA,
print the15%
grades
of
is the basics as
Armstrong
accordingly: Conveyance
number or not.allowance
(E.g. 153 and
is a 10% ofthe basic
Armstrong number as Entertainment
because 153 allowance.
= 13 + 53 The+33).
totalsalary is calculated by adding Basic + HRA + Conveyance + Entertainment Allowance.
Marks Grade
Calculate and print the totalsalary of a person.
ANSWER: A
70 to 89 B
import java.util.Scanner;
50 to 69class Armstrong C
public
{below
public 50 D
class Salary_Calculator {
public static void main(int
public static void main(String[] args)n){
{
ANSWER: Scanner sc = new Scanner(System.in);
int
/* PRG-30- a,num,s=0;
// InputConcept of Logical Operators
basic salary
num=n;
A programSystem.out.print("Enter the basicbysalary:
to accept a mark obtained "); in computer science and print the grades
a student
while
double (n>0)
basic = sc.nextDouble();
accordingly
{
*/ a=n%10;
// Calculate allowances
importdouble hra
s=s+a*a*a;
java.util.*; = 0.15 * basic;
double
class grades conveyance
n=n/10; = 0.15 * basic;
double entertainment = 0.10 * basic;
{ }
if(num==s)
// Calculate
public static voidtotal salary
main(String args[])
{ System.out.println("the
double totalSalary = basic + hranumber
+ conveyance "+num+" is a Armstrong Number");
+ entertainment;
else sc=new Scanner(System.in);
Scanner
// Output the result
System.out.println("the number "+num+" is a not Armstrong Number");
}} System.out.println("Total
int c; Salary = " + totalSalary);
}} System.out.println("Enter marks in Computer science:");
c=sc.nextInt();
//if-else if-else Ladder is used to precisely determine
VARIABLE DESCRIPTION:
//the grade obtained according to marks
SL.if(c>=90) System.out.println("Grade=A");
VARIABLE NAME TYPE PURPOSE
NOelse if(c>=70 && c<90)
1. System.out.println("Grade=B");
sc Scanner Stores the input number (originally entered by the user).
Scanner object tofor
take
digitinput from the user
n int In the loop,it is used extraction.
else if(c>=50 && c<70)
2. System.out.println("Grade=C");
basic
num double
int Stores
The a copysalary
basic of the original
entered number
by(`n`)
theforuser
comparison after calculation.
else System.out.println("Grade=D");
}3. hraa double
int
Stores Rent
House
loop
the last
(`a of
=n
digit of `n`calculated
Allowance,
% 10`).
during each iteration of the
as 15% the basic salary
} Stores the sum of the cubes ofcalculated
the digits as 15% of the
4. conveyance Conveyance Allowance,
s double
int (used to check
basic salary if the number is an Armstrong number).
page no. 18
QUESTION-10:
Write a program to display first 10 terms of the following series: 2, 5,10,17, 26……
ANSWER:
/* PRG-10 Concept of loops
A program to display first 10 terms of the following series: 2, 5,10,17, 26…… */
class series_gen
{
public static void main (String args[])
{
for (int i=1; i<=10; i++)
{
int result=(i*i)+1;
System.out.print(result + " ");
}
}
}
VARIABLE DESCRIPTION:
SL. NO VARIABLE NAME TYPE PURPOSE
page no. 19
QUESTION-11:
Write a program to check if the number entered by user is prime or not.
ANSWER:
/* PRG-11- Concept of loops and conditionals
A program to check if an entered number is Prime or not */
import java.util.*;
class isPrime
{
public static void main(String args[])
{
Scanner sc =new Scanner(System.in);
//Accepting an user input
System.out.println("Enter a whole number:");
int num = sc.nextInt();
//A boolean flag variable demarcates the result of the test conditions
boolean flag = true;
//Loop generates the possible divisors for given number
for(int i = 2; i<=num/2; i++)
{
if(num%i==0)
{
//Once a divisor is found immediately it makes the flag False,
//i.e. it indicates the input number is Not Prime
flag = false;
break;
}
}
//Conditional statements used to display the result as per the satus of Flag variable
if(flag == true)
System.out.println("The entered number "+num+" IS a Prime number");
else
System.out.println("The entered number "+num+" IS NOT a Prime number");
}
}
VARIABLE DESCRIPTION:
SL. NO VARIABLE NAME TYPE PURPOSE
page no. 20
SAMPLE INPUT OUTPUT:
page no. 21
QUESTION-12:
Using switch-case write a menu driven program to (i) Area of a right-angled triangle, (ii) Area of a
square and (iii) Area of rectangle.
ANSWER:
/* PRG-12-Concept of SWITCH CASE Statements
A program Using switch-case write a menu driven program to display the following:
(i) Area of a right-angled triangle
(ii) Area of a square
(iii) Area of rectangle
*/
import java.util.Scanner;
class SWC2
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
int choice;
double area;
int l,b;
int a;
case 2:
System.out.println("Measurements for Square:");
System.out.print("Enter length of side= ");
a=sc.nextInt();
area=a*a;
page no. 22
System.out.println("Area of Square ="+area);
break;
case 3:
System.out.println("Measurements for Rectangle:");
System.out.print("Enter length = ");
l=sc.nextInt();
System.out.print("Enter breadth = ");
b=sc.nextInt();
area=l*b;
System.out.println("Area of Rectangle="+area);
break;
//Default statement is used here to protect from sudden stopping of program
// due to wrong user input
default:
System.out.print("Enter a valid number.");
}
}
}
VARIABLE DESCRIPTION:
SL. VARIABLE NAME TYPE PURPOSE
NO
page no. 23
page no. 24
QUESTION-13:
Using a switch statement, write a menu driven program to convert a given temperature from
Fahrenheit to Celsius and vice versa. For an incorrect choice, an appropriate error message should
be displayed.
(HINT: C = 5 9 × (F − 32) and F = 1.8 × C + 32)
ANSWER:
/*PRG-13- Concept of SWITCH CASE Statements
Using a switch statement, write a menu driven program to convert a given temperature
from Fahrenheit to Celsius and vice versa.
*/
import java.util.*;
public class celtofar
{
public static void main(String args[])
{ int n;
double c,f;
Scanner sc= new Scanner(System.in);
System.out.println("1. Celsius to Fahrenheit");
System.out.println("2. Fahrenheit to Celsius");
System.out.println("ENTER YOUR OPTION");
n=sc.nextInt();
switch(n)
{
case 1 :
System.out.println("Enter temperature in Celsius");
c= sc.nextDouble();
f= 1.8*c+32;//Formula to finf=d Fahrenheit from Celcious
System.out.println("Temperature after converting into Fahrenheit "+f);
System.out.println("--------------------------------------------- ");
break;
case 2 : System.out.println("Enter temperature in Fahrenheit ");
f=sc.nextDouble();
c= 5*((f-32)/9);//use of operator precedence
System.out.println("Temperature after converting into Celsius "+c);
System.out.println("--------------------------------------------- ");
break;
default: System.out.println("Wrong Entry ");
}
}
}
page no. 25
VARIABLE DESCRIPTION:
SL. VARIABLE NAME TYPE PURPOSE
NO
page no. 26
QUESTION-14:
Define a class named BookFair with the following description :
Instance variables /Data members :
String Bname - stores the name of the book
double price - stores the price of the book
Member methods :
(i) input() - To input and store the name and the price of the book.
(ii) calculate() - To calculate the price after discount.
(iii) display() - To display the name and price of the book after discount.
Discount is calculated based on the following criteria:
Price Discount
Less than or equal to Rs.1000 2% of price
More than Rs.1000 and less than or equal to Rs.3000 10% of price
More than Rs.3000 15% of price
Write a main() to create an object of the class and call above member methods.
ANSWER:
/*PRG-14- Concept of returning multiple values by a function using array, User Defined Methods
A program to display a book name, price, percentage of discount applied (based on given criteria)
and discounted price
*/
import java.util.Scanner;
public class bookFair
{
String Bname;
static double price,discount,amount,flag;
double calval[] = new double[2];
//defined method that accepts inputs
void input()
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter name of the book:");
Bname=sc.nextLine();
System.out.println("Enter price of the book:");
price = sc.nextDouble();
}
//defined methods that calculates the discount amount based on given criteria
double[] calculate(double a)
{
if (a<=1000)
{
calval[0] = 0.02 * price;
calval[1] = 2.0;
}
else if ( a>1000 && a<3000)
{
calval[0] = 0.1*price;
page no. 27
calval[1] = 10.0;
}
else
{
calval[0] = 0.15 * price;
calval[1] = 15.0;
}
return calval;
}
VARIABLE DESCRIPTION:
SL. VARIABLE NAME TYPE PURPOSE
NO
page no. 28
SAMPLE INPUT OUTPUT:
page no. 29
QUESTION-15&16:
Define a class ‘Student’ described as below:
Data members/instance variables:
name, age, m1, m2, m3 (marks in 3 subjects), maximum, average
Member methods:
i. To accept the details of a student.
ii. To compute the average and the maximum out of three marks.
iii. To display the name, age, marks in three subjects, maximum and average.
Write a main method to call the above member methods.
ANSWER:
/*PRG-15&16-Concept of User Defined Functions
A program to accept the details of a student and display the name, age,
marks in three subjects, maximum and average marks.
*/
import java.util.*;
class Student
{
String name;
int age,m1,m2,m3,max;
float avg;
void input()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter student's name:");
name=sc.nextLine();
//storing three subject marks
System.out.println("Enter marks in 3 subjects:");
m1=sc.nextInt();
m2=sc.nextInt();
m3=sc.nextInt();
System.out.println("Enter student’s age:");
age=sc.nextInt();
}
void display()
{
System.out.println("Name:"+name);
System.out.println("Marks:"+m1+","+m2+ " and " +m3);
System.out.println("Maximum Marks:"+max);
System.out.println("Average:"+ avg);
}
void compute()
{
//Using Math library function Math.max()
max=Math.max(Math.max(m1,m2),m3);
avg=(float)(m1+m2+m3)/3;
page no. 30
}
public static void main(String args[])
{
//Creating objects and calling the defined functions through object
Student st = new Student();
st.input();
st.compute();
st.display();
}
}
VARIABLE DESCRIPTION:
SL. VARIABLE NAME TYPE PURPOSE
NO
page no. 31
QUESTION-17:
Define a class busfare having the following description:
Data members/instance variables:
int busno - to store bus number
String name - to store passengers name
int km - to store number of kilometers travelled.
Member functions:
input() - - to store bus number, name, km
calculate() - - to calculate bill for a customer according to given conditions
i) Up to 5km: distance X 4
ii) 6 to 15 km: first compute 5kms fare then (distance – 5) X 6.
iii) 16 km and above: first compute 5 kms fare then compute 10 kms fare and then compute
(distance – 15 ) X 8
display() - - To display the details in the following format.
Busno || Name || KilometersTravelled || Bill amount ||
ANSWER:
/*PRG-17- Concept of User Defined Function
A program to take details of a bus, travelled distance and based on
the given criteria find the bill amount
*/
import java.util.*;
public class busfare
{
int busno,km ;
String name;
//method to take inputs
public void input()
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter Name:");
name = sc.nextLine();
System.out.println("Enter Bus number:");
busno=sc.nextInt();
System.out.println("Enter distance:");
km=sc.nextInt();
}
//method to calculate results
int calculate(int dist)
{
if(dist<=5)
return dist*4;
else if(dist>6 && dist <=15)
return 20+(dist-5)*6;
page no. 32
else
return 80 + (dist-15) *8;
}
//method to disply result
void display()
{
input();
int bill;
bill= calculate(km);
System.out.println("Busno Name Kilometers Bill amount");
System.out.println(busno+" "+ name+" "+km+" "+bill);
}
//driver method
public static void main(String args[])
{
//Creating objects and calling the defined functions through object
busfare bs = new busfare();
bs.display();
}
}
VARIABLE DESCRIPTION:
SL. VARIABLE NAME TYPE PURPOSE
NO
page no. 33
QUESTION-18:
Write a program to accept a word and print whether the word is a palindrome or not.
ANSWER:
/*PRG-18 - Concept of String functions
Write a program to accept a word and print whether the word is a palindrome or not
*/
import java.util.*;
import java.io.*;
public class Palindrome
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.print("Please enter a word: ");
String str = sc.next();
str = str.toUpperCase();
int len = str.length();
//test condition begins for Palindrome when first letter match with last letter
//if not match then else block displays Ordinary String
if(str.charAt(0) == str.charAt(len-1))
{
//inner letters are also tested for by running a loop
//if mismatch found then displays Ordinary String and program terminates prematurely.
for(int i=1;i<len/2;i++)
{
if(str.charAt(i) != str.charAt(len - 1 - i))
{
System.out.println("The word is an Ordinary String.");
System.exit(0);//terminates the program from this line
}
}
//if no mismatch of inner letters happen then for loop terminates maturely
//and control of flow reaches this line to display Palindrome
System.out.println("The word is a Palindrome");
}
else
{
System.out.println("The word is an Ordinary String");
}
}
}
page no. 34
VARIABLE DESCRIPTION:
SL. VARIABLE NAME TYPE PURPOSE
NO
2. len int To calculate the length of the word given by the user
page no. 35
QUESTION-19:
Write a program using the switch statement, write a menu driven program for the following:
(a) To print the Floyd’s triangle:
(b) To display the following pattern:
I
IC
ICS
ICSE
For an incorrect option, an appropriate error message should be displayed.
ANSWER:
/* Concept of SWITCH CASE statement and Nested Loops
A Program to Print different pattern according the choice entered.
*/
import java.util.Scanner;
class Pattern
{
public static void main(String args[])
{
Scanner in = new Scanner(System.in);
System.out.println("Type 1 for Floyd's triangle");
System.out.println("Type 2 for an ICSE pattern");
System.out.print("Enter your choice: ");
int ch = in.nextInt();
switch (ch)
{
case 1:
//The loop will print the numbers from a number range
int a = 1;
for (int i = 1; i <= 5; i++)
{
for (int j = 1; j <= i; j++)
{
System.out.print(a++ + "\t");
}
System.out.println();
}
break;
case 2:
//The loop will print the letters given
String s = "ICSE";
for (int i = 0; i < s.length(); i++)
{
for (int j = 0; j <= i; j++)
{
System.out.print(s.charAt(j) + " ");
page no. 36
}
System.out.println();
}
break;
default:
System.out.println("Incorrect Choice");
}
}
}
VARIABLE DESCRIPTION:
SL. VARIABLE NAME TYPE PURPOSE
NO
page no. 37
QUESTION-20:
Write
Write aaprogram
JAVAprogramto display
to display first the
10 terms of following
the followingpattern
series: 2, 5,10,17, 26……
A
BB
CCC
DDDD
/* PRG-14 Concept of loops
EEEEE
A program to display first 10 terms of the following series: 2, 5,10,17, 26…… */
ANSWER:
class series_gen
{
//to generate the given pattern
public
class static void main (String args[])
patern
{{
public
for static
(int i=1; void
i<=10; main()
i++)
{
{ int i,j;
for(i=65;i<=69;i++)
int result=(i*i)+1;
{
System.out.print(result + " ");
for(j=65;j<=i;j++)
} System.out.print((char)i);
} System.out.println();
} }
}
}
VARIABLE DESCRIPTION:
SL. NO VARIABLE NAME TYPE PURPOSE
page no. 38
QUESTION-21:
Write a program to display first 10 terms of the following series: 2, 5,10,17, 26……
Write a JAVA program: The method takes an integer argument n and
prints the following pattern (n=4)
/* a
PRG-14 Concept of loops
a a
A program to display first 10 terms of the following series: 2, 5,10,17, 26…… */
a a a
class series_gen
a{ a a a
a a public
a static void main (String args[])
a{ a
a for (int i=1; i<=10; i++)
ANSWER:
{
public
intclass LetterPattern {
result=(i*i)+1;
System.out.print(result + " ");
public
} static void printPattern(int n) {
} // Upper part including the middle row
} for (int i = 1; i <= n; i++) {
// Print spaces
for (int s = 0; s < n - i; s++) {
System.out.print(" ");
SL. NO VARIABLE NAME TYPE PURPOSE
}
1. // Print 'a's
i with space
int Loop control variable
for (int j = 1; j <= i; j++) {
2. System.out.print("a
result int "); To store the series number
}
System.out.println();
}
// Lower part
for (int i = n - 1; i >= 1; i--) {
// Print spaces
for (int s = 0; s < n - i; s++) {
System.out.print(" ");
}
// Print 'a's with space
for (int j = 1; j <= i; j++) {
System.out.print("a ");
}
System.out.println();
}
}
page no. 39
VARIABLE DESCRIPTION:
page no. 40
QUESTION-22:
Write a program to accept a mark obtained by a student in computer science and print the grades
accordingly:
Marks Grade
Above 90 A
70 to 89 B
50 to 69 C
below 50 D
ANSWER:
/* PRG-22- Concept of Logical Operators
A program to accept a mark obtained by a student in computer science and print the grades
accordingly
*/
import java.util.*;
class grades
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
int c;
System.out.println("Enter marks in Computer science:");
c=sc.nextInt();
//if-else if-else Ladder is used to precisely determine
//the grade obtained according to marks
if(c>=90) System.out.println("Grade=A");
else if(c>=70 && c<90)
System.out.println("Grade=B");
else if(c>=50 && c<70)
System.out.println("Grade=C");
else System.out.println("Grade=D");
}
}
VARIABLE DESCRIPTION:
SL. VARIABLE NAME TYPE PURPOSE
NO
page no. 41
SAMPLE INPUT OUTPUT:
**************************************************** ********************************************************
page no.42
QUESTION-23:
Write a Java toprogram
Write a program display firstto
10 implement binary
terms of the following 2, 5,10,17, 26……
search.
series:
ANSWER:
/* a
import
PRG-14 java.util.Scanner;
Concept of loops
A a
classa ArrayOperation
{ program to display first 10 terms of the following series: 2, 5,10,17, 26…… */
a a a
int binarySearch(int[]
class series_gen a,int target)
a{{ a a a
a a public
a static void main (String args[])
bubbleSort(a);
int n=a.length;
a{ a
int start=0;
int last=n-1;
for (int i=1; i<=10; i++)
while(start<=last) {
int mid=(start+last)/2;
if(a[mid]==target)
public intclass return mid; {
LetterPattern
result=(i*i)+1;
else if(a[mid]<target) {
System.out.print(result + " ");
start=mid+1;
} public
} static void printPattern(int n) {
else { // Upper part including the middle row
}
}last=mid-1;
for (int i = 1; i <= n; i++) {
}
} // Print spaces
return -1; for (int s = 0; s < n - i; s++) {
} System.out.print(" ");
SL. NO
void VARIABLE NAME
bubbleSort(int[] a) { TYPE PURPOSE
}
int n=a.length;
for(int // Print 'a's
1. i=n-1;i>=0;i--) i { with space int Loop control variable
for (int
for(int j=0;j<=i-1;j++) { j = 1; j <= i; j++) {
if(a[j]>a[j+1])
2. System.out.print("a
{ result int "); To store the series number
}
int temp=a[j];
a[j]=a[j+1];System.out.println();
}
a[j+1]=temp;
}
} // Lower part
}
} for (int i = n - 1; i >= 1; i--) {
} // Print spaces
public classforBinarySearch
(int s = 0; s < n - i; s++) {
{ System.out.print(" ");
public static void main (String arg[ ])
{ }
// Print
int a[] = new 'a's with space
int[5];
for (int j("Insert
System.out.println = 1; jthe<=values
i; j++) {
in array:");
Scanner sc =System.out.print("a
new Scanner (System.in); ");
for(int i=0;i<a.length;i++)
{ }
System.out.println();
a[i] = sc.nextInt();
} }
System.out.println("Insert
} the element which you want to find?");
int f = sc.nextInt();
ArrayOperation ao = new ArrayOperation();
public
int flag static void main(String[] args) {
= ao.binarySearch(a,f);
int n = 4;
if(flag>0)
System.out.println("We
printPattern(n); found element in the array:");
else}System.out.println("Not found");
}
}
}
page no. 43
VARIABLE DESCRIPTION:
page no. 44
QUESTION-24:
5,10,17, 26……
Write a Java program to implement selection sort.
ANSWER:
/* a
import
PRG-14 java.util.Scanner;
Concept of loops
a
classa
A program
ArrayOperation
importto java.util.Scanner;
display first 10 terms of the following series: 2, 5,10,17, 26…… */
intclass Search{ a,int target)
binarySearch(int[]
eries_gen
a{{ avoid
a a selectionSort(int [] a) {
a a an=a.length;
bubbleSort(a);
int
public static void main (String args[])
int n=a.length;
a a
int{for(int
start=0;i=0;i<n-1;i++) {
intintformini=i;
last=n-1;
(int i=1; i<=10; i++)
while(start<=last)
for(int j=i+1;j<n;j++) { {
intif(a[j]<a[mini])
mid=(start+last)/2;{
if(a[mid]==target)
public intclass return mid; {
LetterPattern
result=(i*i)+1;
mini=j;
else if(a[mid]<target) {
System.out.print(result + " ");
}}
start=mid+1;
} }public static void printPattern(int n) {
} { // Upper part including the middle row
elseint temp=a[mini];
}last=mid-1;
for (int i = 1; i <= n; i++) {
} a[mini]=a[i];
} a[i]=temp; // Print spaces
} -1; for (int s = 0; s < n - i; s++) {
return
} } System.out.print(" ");
SL. NO
void VARIABLE NAME
bubbleSort(int[] a) { TYPE PURPOSE
void }
display(int [] a) {
int n=a.length;
for(int
for(int //i=0;i<a.length;i++)
Print 'a's
1. i=n-1;i>=0;i--) i { with space int{ Loop control variable
for(int for (int j
j=0;j<=i-1;j++) {
System.out.print(a[i]+" = 1; j <= i;
"); j++) {
if(a[j]>a[j+1])
} 2. System.out.print("a
{ result int "); To store the series number
}
intSystem.out.println();
temp=a[j];
} System.out.println();
a[j]=a[j+1];
} }
a[j+1]=temp;
}
} // Lower
} public class part
Selection_Sort {
for (int
} public static voidi = n - 1; i >= 1; i--)arg[])
main(String { {
} // Print spaces
public forBinarySearch
class (intint[5];//12345
s = 0; s < n - i; s++) {
{ int a[]=new System.out.print("data "); in array!");
System.out.println("Insert
public static void main (String arg[ ])
}
{ Scanner sc=new Scanner(System.in);
intfor(int //i=0;i<a.length;i++)
a[] = new Print 'a's with space{
int[5];
for (int j("Insert
System.out.println
a[i] =sc.nextInt(); = 1; jthe
<=values
i; j++) {
in array:");
Scanner sc =System.out.print("a
new Scanner (System.in); ");
} i=0;i<a.length;i++)
for(int
{ }
a[i] System.out.println();
= sc.nextInt();
Search s=new Search();
}
} System.out.println("Before sorting:");
System.out.println("Insert
} the element which you want to find?");
ints.display(a);
f = sc.nextInt();
s.selectionSort(a);
ArrayOperation ao = new ArrayOperation();
public static void main(String[]
intSystem.out.println("After
flag = ao.binarySearch(a,f); args) {
sorting:");
int n = 4;
if(flag>0)
s.display(a);
System.out.println("We
printPattern(n); found element in the array:");
} System.out.println("Not
else found");
} }}
page no.45
VARIABLE DESCRIPTION:
page no. 46
QUESTION-25:
Write a program using the switch statement, write a menu driven program for the following:
Write a Java
(a) To print program
the Floyd’s triangle:to implement bubble sort.
ANSWER:
(b) To display the following pattern:
I
IC
ICS
I Cimport
SE java.util.Scanner;
class Sort{
For an incorrect option, an appropriate error message should be displayed.
void bubbleSort(int[] a) {
int n=a.length;
for(int i=n-1;i>=0;i--) {
/* Concept of SWITCH CASE statement and Nested Loops
for(int j=0;j<=i-1;j++) {
if(a[j]>a[j+1])
A Program { pattern according the choice entered.
to Print different
*/ int temp=a[j];
a[j]=a[j+1];
import java.util.Scanner;
a[j+1]=temp;
class Pattern
{ }
}
public static void main(String args[])
{}
} Scanner in = new Scanner(System.in);
void display(int [] a)1 {
System.out.println("Type for Floyd's triangle");
for(int i=0;i<a.length;i++)
System.out.println("Type { pattern");
2 for an ICSE
System.out.print(a[i]+" "); ");
System.out.print("Enter your choice:
} int ch = in.nextInt();
System.out.println();
switch (ch)
}{
} case 1:
page no. 47
System.out.println();
}
break;
default:
System.out.println("Incorrect Choice");
}
}
}
VARIABLE DESCRIPTION:
SL. VARIABLE NAME TYPE PURPOSE
NO
page no. 48
BIBLIOGRAPHY
page no. 49