0% found this document useful (0 votes)
16 views

mehnat

Uploaded by

memojey375
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)
16 views

mehnat

Uploaded by

memojey375
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/ 62

ACKNOWLEGEMENT

I would like to express my sincere gratitude to all those who have supported and
helped me in completing this Computer Science project for Class 10 at Rani
Laxmibai Public School.

First and foremost, I would like to thank my Computer Science teacher, Archna
Ma’am, for guiding me throughout the project. Their valuable insights,
encouragement, and patience have been instrumental in enhancing my
understanding of the subject and ensuring the success of this project.

I also extend my heartfelt thanks to the principal, Ashok Sir, and the entire school
staff for providing the necessary resources and creating a conducive learning
environment.

I am grateful to my classmates and friends for their constant support, ideas, and
constructive feedback, which helped me improve the quality of my work.

Lastly, I would like to thank my family for their unwavering support and
encouragement throughout the course of this project. Their understanding and
motivation have been a constant source of strength.

This project would not have been possible without the help and encouragement of
all the above-mentioned individuals.

Kamakhya Singh Kushwaha


Class 10, Rani Laxmibai Public School

Page | 2
S. No. Particulars Page No.
1. Program to find the net price based on the amount after 4
reducing a suitable discount
2. Program to sort a 1D Array by using Selection Sort 8
3. Program to overload a function named JoyString() 11
4. Program to form patterns 14
5. Program to accept the vehicle number, hours parked and 17
calculate and print the vehicle number, hours parked, and
the calculated bill.
6. Program to check if the entered number Is ISBN number or 20
not.
7. Program to find the price of fruit juice and display its price 22
8. Program to encode a word into Piglatin 26
9. Program to overload and print series 28
10. Menu-driven Program to check composite nature or find the 30
largest digit
11. Program to calculate Income tax and remaining salary 34
12. Program to convert the string into upper case and count 37
number of double letter sequences in the string
13. Program with overloading to print patterns 39
14. Menu driven program to display the first 10 terms of the 42
Fibonacci series and find the sum of the digits of an integer
15. Program to calculate the wages of a Mobike 46
16. Program to input a number and check whether the number is 49
a special number
17. Program to accept a word and convert it into lower case 52
and print new word by replacing only the vowels with the
letter following.
18. Menu driven program to print series 54
19. Program to merge two arrays of length 6 and 4.Create new 57
array of length 10.
20. Program to find an element using Linear Search 60
INDEX

QUESTION 1:

Page | 3
Define a class called with the following specifications:

Class name: Eshop Member variables:

String name: name of the item purchased

double price: Price of the item purchased

Member methods:

void accept(): Accept the name and the price of the item using the methods of Scanner class.

void calculate(): To calculate the net amount to be paid by a customer, based on the following
criteria

Price Discount
1000 – 25000 5.0%
25001 – 57000 7.5 %
57001 – 100000 10.0%
More than 100000 15.0 %

void display(): To display the name of the item and the net amount to be paid.

Write the main method to create an object and call the above methods.

Page | 4
ANSWER 1:
import java.util.Scanner;

public class Eshop

String name;

double price;

double disc;

double amount;

public void accept()

Scanner in = new Scanner(System.in);

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

name = in.nextLine();

System.out.print("Enter price of item: ");

price = in.nextDouble();

public void calculate() {

double d = 0.0;

if (price < 1000)

d = 0.0;

else if (price <= 25000)

d = 5.0;

else if (price <= 57000)

d = 7.5;

else if (price <= 100000)

Page | 5
d = 10.0;

else

d = 15.0;

disc = price * d / 100.0;

amount = price - disc;

public void display() {

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

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

public static void main(String args[])

Eshop obj = new Eshop();

obj.accept();

obj.calculate();

obj.display();

VARIABLE DESCRIPTION:
Name of the Data Type Purpose/description
Variable
name String To store the name of the product
price double To store the price of the product
disc double To store the discount on price of the product
amount double To store the amount of the product

Page | 6
SAMPLE INPUT:

SAMPLE OUTPUT:

Page | 7
QUESTION 2:
Define a class to accept values in integer array of size 10. Sort them in an ascending order using
selection sort technique. Display the sorted array.

ANSWER 2:
import java.util.Scanner;

class Arrray

void main()

Scanner abc=new Scanner(System.in);

int []A=new int[10];

System.out.print("Enter the Array\t");

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

A[i]=abc.nextInt();

int t=0;

System.out.print("Unsorted Array");

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

System.out.print(A[i]+"\t");

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

for(int j=i+1;j<10;j++)

Page | 8
{

if(A[i]>A[j])

t=A[i];

A[i]=A[j];

A[j]=t;

System.out.print("\nSorted Array");

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

System.out.print(A[i]+"\t");

VARIABLE DESCRIPTION:
Name of the Data Type Purpose/description
Variable
abc object To use the Scanner
A[] int To store array
i int To use in outer loop
j int To use in inner loop

Page | 9
SAMPLE INPUT:

SAMPLE OUTPUT:

Page | 10
QUESTION 3:
Design a class to overload a function Joystring( ) as follows:

void Joystring(String s, char ch1, char ch2) with one string argument and two character
arguments that replaces the character argument ch1 with the character argument ch2 in the given
String s and prints the new string.

Example: Input value of s = "TECHNALAGY" ch1 = 'A' ch2 = 'O'

Output: "TECHNOLOGY"

void Joystring(String s) with one string argument that prints the position of the first space and the
last space of the given String s.

Example: Input value of s = "Cloud computing means Internet based computing"

Output: First index: 5

Last Index: 36

void Joystring(String s1, String s2) with two string arguments that combines the two strings with
a space between them and prints the fant string.

Example: Input value of s1 = "COMMON WEALTH"

Input value of s2 = "GAMES" Output: COMMON WEALTH GAMES

ANSWER 3:
public class Joystring

void Joystring(String s, char ch1, char ch2)

String f = s.replace(ch1, ch2);

System.out.println(f);

void Joystring(String s)

Page | 11
int fI = s.indexOf(' ');

int lI = s.lastIndexOf(' ');

System.out.println("First index: " + fI);

System.out.println("Last index: " + lI);

void Joystring(String s1, String s2)

String f = s1 + " " + s2; System.out.println(f);

VARIABLE DESCRIPTION:

Name of the Data Type Purpose/description


Variable
s char This is the input string in which the characters are
going to be replaced.
ch1 char This is the character in the string s that we want to
replace.
char ch2 char This is the character that will replace ch1 in the
string s.
f String This variable stores the new string after
replacing all occurrences of ch1 with ch2.
s String This is the input string where we will find the
positions of the first and last spaces.
fI int This variable stores the position (index) of the
first space character ' ' in the string s. It is
obtained using the indexOf(' ') method.
lI int This variable stores the position (index) of the last
space character ' ' in the string s. It is obtained
using the lastIndexOf(' ')method.
s1 String This is the first string input.
s2 String This is the second string input.
f String This variable stores the concatenated string that is
formed by combining s1 and s2 with a space
between them.

Page | 12
SAMPLE INPUT:

SAMPLE OUTPUT:

SAMPLE INPUT:

SAMPLE OUTPUT:

SAMPLE INPUT:

SAMPLE OUTPUT:

Page | 13
QUESTION 4:
Write two separate programs to generate the following patterns using iteration (loop) statements:

(a)

*#

*#*

*#*#

*#*#*

(b)

54321

5432

543

54

ANSWER 4:
(a)

class Pattern

void main()

for (int i = 1; i <= 5; i++)

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

if (j % 2 != 0) {

Page | 14
System.out.print("* ");

} else {

System.out.print("# ");

System.out.println();

(b)

class Pattern

void main()

for (int i = 1; i <=5; i++)

for (int j = 5; j >= i; j--)

System.out.print(j + " ");

System.out.println();

Page | 15
VARIABLE DESCRIPTION:
Name of the Data Type Purpose/description
Variable
i int Controls the number of rows
j int Controls the number of columns
i int Controls the number of rows
j int Controls the number of columns

SAMPLE OUTPUT:

(a)

(b)

Page | 16
QUESTION 5:
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:

void input() — To input and store the vno and hours.

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.

void display() — To display the detail.

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

ANSWER 5:
import java.util.Scanner;

class ParkingLot

int vno;

int hours;

double bill;

void input()

Scanner sc = new Scanner(System.in);

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

vno = sc.nextInt();

System.out.print("Enter the number of hours the vehicle is parked: ");

Page | 17
hours = sc.nextInt();

void calculate()

if (hours <= 1) {

bill = 3;

} else {

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

void display()

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

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

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

public static void main(String[] args)

ParkingLot parking = new ParkingLot();

parking.input();

parking.calculate();

parking.display();

Page | 18
VARIABLE DESCRIPTION:
Name of the Data Type Purpose/description
Variable
vno int To store the vehicle number.
hours int To store the number of hours the vehicle is parked
in the parking lot
bill double To store the bill amount.

SAMPLE INPUT:

SAMPLE OUTPUT:

Page | 19
QUESTION 6:
The International Standard Book Number (ISBN) is a unique numeric book identifier which is
printed on every book. The ISBN is based upon a 10-digit code. The ISBN is legal if: 1 × digit1
+ 2 × digit2 + 3 × digit3 + 4 × digit4 + 5 × digit5 + 6 × digit6 + 7 × digit7 + 8 × digit8 + 9 ×
digit9 + 10 × digit10 is divisible by 11.

Example: For an ISBN 1401601499

Sum = 1 × 1 + 2 × 4 + 3 × 0 + 4 × 1 + 5 × 6 + 6 × 0 + 7 × 1 + 8 × 4 + 9 × 9 + 10 × 9 = 253
which is divisible by 11.

Write a program to: Input the ISBN code as a 10-digit integer.

If the ISBN is not a 10-digit integer, output the message "Illegal ISBN" and terminate the
program.

If the number is divisible by 11, output the message "Legal ISBN". If the sum is not divisible by
11, output the message "Illegal ISBN".

ANSWER 6:
import java.util.Scanner;

public class ISBN

public static void main()

Scanner scanner = new Scanner(System.in);

System.out.print("Enter the 10-digit ISBN: ");

long n = scanner.nextLong();

int c = 0;

for (long i = n; i > 0; i /= 10)

c++;

Page | 20
if (c != 10) {

System.out.println("Illegal ISBN");

int sum = 0;

for (long i = n, j = 10; j > 0; j--) {

int d = (int) (i % 10);

sum += d * j;

i /= 10;

if(sum%11==0)

System.out.println("Legal ISBN");

else

System.out.println("Illegal ISBN");

VARIABLE DESCRIPTION:
Name of the Data Type Purpose/description
Variable
vno int To store the vehicle number.
hours int To store the number of hours the vehicle is parked
in the parking lot
bill double To store the bill amount.

SAMPLE INPUT:

SAMPLE OUTPUT:

Page | 21
QUESTION 7:
Define a class named FruitJuice with the following description:

Data Members Purpose


int product_code stores the product code number
String flavour stores the flavour of the juice (e.g., orange, apple,
etc.)
String pack_type stores the type of packaging (e.g., tera-pack, PET
bottle, etc.)
int pack_size stores package size (e.g., 200 mL, 400 mL, etc.)
int product_price stores the price of the product
Member Methods Purpose
FruitJuice() constructor to initialize integer data members to 0
and string data members to ""
void input() to input and store the product code, flavour, pack
type, pack size and product price
void discount() to reduce the product price by 10
void display() to display the product code, flavour, pack type,
pack size and product price

ANSWER 7:
import java.util.Scanner;

class FruitJuice {

int product_code;

String flavour;

String pack_type;

int pack_size;

int product_price;

FruitJuice()

product_code = 0;

flavour = "";

Page | 22
pack_type = "";

pack_size = 0;

product_price = 0;

void input()

Scanner abc = new Scanner(System.in);

System.out.print("Enter Product Code: ");

product_code = abc.nextInt();

abc.nextLine();

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

flavour = abc.nextLine();

System.out.print("Enter Pack Type: ");

pack_type = abc.nextLine();

System.out.print("Enter Pack Size (mL): ");

pack_size = abc.nextInt();

System.out.print("Enter Product Price: ");

product_price = abc.nextInt();

void discount()

product_price -= 10;

void display()

Page | 23
System.out.println("Product Code: " + product_code);

System.out.println("Flavour: " + flavour);

System.out.println("Pack Type: " + pack_type);

System.out.println("Pack Size: " + pack_size + " mL");

System.out.println("Product Price: ₹" + product_price);

public static void main(String[] args)

FruitJuice juice = new FruitJuice();

juice.input();

juice.discount();

juice.display();

VARIABLE DESCRIPTION:
Variable Description
int product_code To store the product code number
String flavour To store the flavour of the juice (e.g., orange,
apple, etc.)
String pack_type To store the type of packaging (e.g., tera-pack, PET
bottle, etc.)
int pack_size To store package size (e.g., 200 mL, 400 mL, etc.)
int product_price To store the price of the product

Page | 24
SAMPLE INPUT:

SAMPLE OUTPUT:

Page | 25
QUESTION 8:
Write a program that encodes a word into Piglatin.

To translate word into Piglatin word, convert the word into uppercase and then place the first
vowel of the original word as the start of the new word along with the remaining alphabets.The
alphabets present before the vowel being shifted towards the end followed by "AY".

Sample Input 1: London Output: ONDONLAY

Sample Input 2: Olympics Output: OLYMPICSAY

ANSWER 8:
import java.util.Scanner;

class PIGLATIN {

public static void main(String[] args) {

Scanner abc = new Scanner(System.in);

String s = abc.next();

String n = s.toUpperCase();

char ch = n.charAt(0);

String o = "";

if (ch == 'A' || ch == 'E' || ch == 'I' || ch == 'O' || ch == 'U') {

o = n + "AY";

System.out.println(o);

else

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

ch = n.charAt(i);

if (ch == 'A' || ch == 'E' || ch == 'I' || ch == 'O' || ch == 'U')

Page | 26
o = n.substring(i) + n.substring(0, i) + "AY";

break;

System.out.println(o);

VARIABLE DESCRIPTION:
Name of the Data Type Purpose/description
Variable
s String Store original string
n String Store capitalised string
ch char Store characters of string
o String Stores new String
abc object Scanner object

SAMPLE INPUT:

SAMPLE OUTPUT:

Page | 27
QUESTION 9:
Design a class to overload a function series( ) as follows:

double series(double n) with one double argument and returns the sum of the series.

sum = (1/1) + (1/2) + (1/3) + .......... + (1/n)

double series(double a, double n) with two double arguments and returns the sum of the series.

sum = (1/a2 ) + (4/a5 ) + (7/a8 ) + (10/a11) + .......... to n terms

ANSWER 9:
class SeriesCalculator

public double series(double n)

double s = 0;

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

s += 1.0 / i;

return s;

public double series(double a, double n)

double s=0;

double i=1;

double d=2;

for (int j = 0; j < n; j++)

Page | 28
s += i / Math.pow(a, d);

i += 3;

d +=3;

return s;

VARIABLE DESCRIPTION:
Name of the Data Type Purpose/description
Variable
s double Stores the sum of the series. It accumulates the
. results as terms are added in each series method
n double Represents the number of terms for the first series
method
a double Represents the base value used for exponentiation
in the second series method
i double Used as the numerator in the second series
method

d double Represents the exponent in the second series


method
j int Loop counter used in the second series method to
iterate through n terms

SAMPLE INPUT:

SAMPLE OUTPUT:

Page | 29
QUESTION 10:
Using the switch statement, write a menu driven program:

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...

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.

ANSWER 10:
import java.util.Scanner;

public class MenuDrivenProgram

public static void main(String[] args)

Scanner sc = new Scanner(System.in);

int choice;

for (;;)

{ // Infinite loop until user selects option 3

System.out.println("Menu:");

System.out.println("1. Check if a number is composite");

System.out.println("2. Find the smallest digit of a number");

System.out.println("3. Exit");

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

choice = sc.nextInt();

switch (choice)

Page | 30
{

case 1:

System.out.print("Enter a number to check if it's composite: ");

int number = sc.nextInt();

if (isComposite(number)) {

System.out.println(number + " is a composite number.");

} else {

System.out.println(number + " is not a composite number.");

break;

case 2:

System.out.print("Enter a number to find the smallest digit: ");

int n = sc.nextInt();

System.out.println("Smallest digit is: " + findSmallestDigit(n));

break;

case 3:

System.out.println("Exiting the program.");

sc.close();

return; // Exit the program

default:

System.out.println("Invalid choice. Please select a valid option.");

Page | 31
}

public static boolean isComposite(int number) {

if (number <= 1) {

return false;

for (int i = 2; i <= Math.sqrt(number); i++) {

if (number % i == 0) {

return true;

return false;

public static int findSmallestDigit(int number)

int smallest = 9;

while (number > 0)

int digit = number % 10;

if (digit < smallest) {

smallest = digit;

number /= 10;

return smallest;

Page | 32
}

VARIABLE DESCRIPTION:
Name of the Data Type Purpose/description
Variable
choice Stores the user's choice from the menu to
perform specific actions.

int

number int Stores the number input by the user to check


if it's a composite number.

a int Stores the number input by the user to find


the smallest digit.
method
sc . Scanner Used to take user input from the console

smallest int Stores the smallest digit found in the number


entered by the user.
digit . int Temporarily stores each digit of the number
as the program checks each digit
i int Loop counter variable used in the
isComposite method to check divisibility.
j int Loop counter variable used to iterate through
the digits of the number in the
findSmallestDigit method.

SAMPLE INPUT:

SAMPLE OUTPUT:

Page | 33
QUESTION 11:
Given below is a hypothetical table showing rates of income tax for male citizens below the age
of 65 years:

Taxable income (TI) in ₹ Income Tax in ₹

Does not exceed Rs. 1,60,000 Nil


Is greater than Rs. 1,60,000 and less than or equal (TI - 1,60,000) x 10%
to Rs. 5,00,000.
Is greater than Rs. 5,00,000 and less than or equal [(TI - 5,00,000) x 20%] + 34,000
to Rs. 8,00,000
Is greater than Rs. 8,00,000 [(TI - 8,00,000) x 30%] + 94,000

Write a program to input the age, gender (male or female) and Taxable Income of a person. If the
age is more than 65 years or the gender is female, display “wrong category”. If the age is less
than or equal to 65 years and the gender is male, compute and display the income tax payable as
per the table given above.

ANSWER 11:
import java.util.Scanner;

public class IncomeTaxCalculator

public static void main(String[] args)

Scanner sc = new Scanner(System.in);

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

int age = sc.nextInt();

System.out.print("Enter gender (male/female): ");

sc.nextLine();

String gender = sc.nextLine().toLowerCase();

System.out.print("Enter taxable income (₹): ");

double taxableIncome = sc.nextDouble();

Page | 34
if (age > 65 || gender.equals("female"))

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

} else

double tax = 0;

if (taxableIncome <= 160000) {

tax = 0;

} else if (taxableIncome <= 500000) {

tax = (taxableIncome - 160000) * 0.10;

} else if (taxableIncome <= 800000) {

tax = (taxableIncome - 500000) * 0.20 + 34000;

} else {

tax = (taxableIncome - 800000) * 0.30 + 94000;

System.out.println("Income tax payable: ₹" + tax);

VARIABLE DESCRIPTION:
Name of the Data Type Purpose/description
Variable
age Stores the age of the person.

int

Page | 35
gender String Stores the gender of the person (male or female).

taxableIncome double Stores the taxable income of the person.

sc . Scanner Used to take user input from the console

tax double Stores the calculated income tax based on taxable


income.

SAMPLE INPUT:

SAMPLE OUTPUT:

Page | 36
QUESTION 12:
Write a program to accept a string. Convert the string into upper case letters. Count and output
the number of double letter sequences that exist in the string.

Sample Input: "SHE WAS FEEDING THE LITTLE RABBIT WITH AN APPLE" Sample
Output: 4

ANSWER 12:
import java.util.Scanner;

public class DoubleLetterSequences

public static void main(String[] args)

Scanner sc = new Scanner(System.in);

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

String input = sc.nextLine();

String uS = input.toUpperCase();

int count = 0;

for (int i = 0; i < uS.length() - 1; i++)

if (uS.charAt(i) == uS.charAt(i + 1))

count++;

System.out.println("The number of double letter sequences: " + count);

Page | 37
VARIABLE DESCRIPTION:
Name of the Data Type Purpose/description
Variable
sc Scanner Used to take user input from the console.

input String Stores the input string entered by the user.

uS String Stores the input string converted to uppercase.

count int Stores the number of double letter sequences found in


the string.
i int Loop counter used to iterate through the string.

SAMPLE INPUT:

SAMPLE OUTPUT:

Page | 38
QUESTION 13:
Design a class to overload a function polygon() as follows:

void polygon(int n, char ch) — with one integer and one character type argument to draw a filled
square of side n using the character stored in ch.

void polygon(int x, int y) — with two integer arguments that draws a filled rectangle of length x
and breadth y, using the symbol '@'.

void polygon() — with no argument that draws a filled triangle shown below:

Example: Input value of n=2, ch = 'O'

Output:

OO

OO

Input value of x = 2, y = 5

Output:

@@@@@

@@@@@

Output:

**

***

ANSWER 13:
import java.util.Scanner;

public class Polygon

void polygon(int n, char ch)

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

Page | 39
{

for (int j = 0; j < n; j++)

System.out.print(ch);

System.out.println();

void polygon(int x, int y) {

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

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

System.out.print('@');

System.out.println();

void polygon()

for (int i = 1; i <= 5; i++)

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

System.out.print('*');

System.out.println();

Page | 40
}

VARIABLE DESCRIPTION:
Name of the Data Type Purpose/description
Variable
n int Stores the side length of the square for drawing a
filled square.

ch char Stores the character to be used to draw the square

x int Stores the length of the rectangle for drawing a


filled rectangle.
y int Stores the breadth of the rectangle for drawing a
filled rectangle
sc Scanner Used to take input from the user via console

choice int Stores the user's choice for the type of polygon to
be drawn.

SAMPLE INPUT: SAMPLE OUTPUT:

SAMPLE INPUT: SAMPLE OUTPUT:

SAMPLE INPUT: SAMPLE OUTPUT:

Page | 41
QUESTION 14:
Using a switch statement, write a menu driven program to:

(a) Generate and display the first 10 terms of the Fibonacci series 0, 1, 1, 2, 3, 5 The first two
Fibonacci numbers are 0 and 1, and each subsequent number is the sum of the previous two.

(b) Find the sum of the digits of an integer that is input. Sample Input: 15390 Sample Output:
Sum of the digits = 18 For an incorrect choice, an appropriate error message should be displayed.

ANSWER 14:
import java.util.Scanner;

public class MenuDrivenProgram {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

while (true) {

System.out.println("Menu:");

System.out.println("1. Generate the first 10 terms of the Fibonacci series");

System.out.println("2. Find the sum of the digits of an integer");

System.out.println("3. Exit");

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

int choice = sc.nextInt();

switch (choice)

case 1:

generateFibonacci();

break;

case 2:

Page | 42
sumOfDigits();

break;

case 3:

System.out.println("Exiting the program.");

sc.close();

System.exit(0);

default:

System.out.println("Invalid choice! Please enter a valid option.");

break;

public static void generateFibonacci() {

int n = 10;

int a = 0, b = 1;

System.out.print("First 10 terms of Fibonacci series: ");

System.out.print(a + " " + b + " ");

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

int c = a + b;

System.out.print(c + " ");

a = b;

b = c;

System.out.println();

Page | 43
public static void sumOfDigits() {

Scanner sc = new Scanner(System.in);

System.out.print("Enter an integer: ");

int num = sc.nextInt();

int sum = 0;

while (num != 0) {

sum += num % 10;

num /= 10;

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

VARIABLE DESCRIPTION:
Name of the Data Type Purpose/description
Variable
Used for taking input from the user via the
sc Scanner console.
choice int Stores the user's choice from the menu options.

n int Used to store the number of terms in the


Fibonacci series (fixed to 10 in this case).
a, b int Stores the first two numbers in the Fibonacci
sequence (initially 0 and 1).
c int . Stores the next Fibonacci number during the
sequence generation
num int Stores the integer entered by the user to compute
the sum of its digits.
sum . int Stores the sum of the digits of the entered integer

SAMPLE INPUT:

Page | 44
SAMPLE OUTPUT:

SAMPLE INTPUT:

SAMPLE OUTPUT:

Page | 45
QUESTION 15:
Define a class called 'Mobike' with the following specifications:

Data Members Purpose

int bno To store the bike number


int phno To store the phone number of the customer
String name To store the name of the customer
int days To store the number of days the bike is taken on
rent
int charge To calculate and store the rental charge
Member Methods Purpose
void input() To input and store the details of the customer
void compute() To compute the rental charge
void display() To display the details in the given format
The rent for a mobike is charged on the following basis:

Days Charge
For first five days ₹500 per day
For next five days ₹400 per day
Rest of the days ₹200 per day

Output:

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

Xxxxxxx xxxxxxxx xxxx xxx xxxxxx

ANSWER 15:
import java.util.Scanner;

class Mobike {

int bno;

int phno;

String name;

int days;

int charge;

void input()

Page | 46
{

Scanner sc = new Scanner(System.in);

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

bno = sc.nextInt();

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

phno = sc.nextInt();

sc.nextLine();

System.out.print("Enter Customer Name: ");

name = sc.nextLine();

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

days = sc.nextInt();

void compute() {

if (days <= 5) {

charge = days * 500;

} else if (days <= 10) {

charge = (5 * 500) + ((days - 5) * 400);

} else {

charge = (5 * 500) + (5 * 400) + ((days - 10) * 200);

void display()

System.out.println("\nBike No. " + bno + " Phone No. " + phno + " Name " + name + " No.
of days " + days + " Charge ₹" + charge);

Page | 47
}

public static void main(String[] args) {

Mobike bike = new Mobike();

bike.input();

bike.compute();

bike.display();

VARIABLE DESCRIPTION:
Name of the Data Type Purpose/description
Variable
Stores the bike number
bno int

phno int Stores the customer's phone number


.
name String Stores the name of the customer

days int Stores the number of days the bike is


rented
charge int Stores the computed rental charge based
on the number of days rented

SAMPLE INTPUT:

SAMPLE OUTPUT:

Page | 48
QUESTION 16:
Write a program to input a number and print whether the number is a special number or not. (A
number is said to be a special number, if the sum of the factorial of the digits of the number is
same as the original number).

Example: 145 is a special number, because 1! + 4! + 5! = 1 + 24 + 120 = 145.

(Where ! stands for factorial of the number and the factorial value of a number is the product of
all integers from 1 to that number, example 5! = 1 * 2 * 3 * 4 * 5 = 120)

ANSWER 16:
import java.util.Scanner;

class SpecialNumber

public static int factorial(int n)

int fact = 1;

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

fact *= i;

return fact;

public static void main(String[] args)

Scanner sc = new Scanner(System.in);

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

int num = sc.nextInt();

int originalNum = num;

Page | 49
int sum = 0;

while (num > 0)

int digit = num % 10;

sum += factorial(digit);

num /= 10;

if (sum == originalNum)

System.out.println(originalNum + " is a special number.");

} else {

System.out.println(originalNum + " is not a special number.");

VARIABLE DESCRIPTION:
Name of the Data Type Purpose/description
Variable
num int . Stores the user input number to be checked
for being specia

originalNum int . Stores the original number to compare


with the sum of factorials
sum int Stores the sum of the factorial of the digits
of the number.
digit int Stores each individual digit of the
number during the iteration.
sc Scanner . Used to take input from the user

fact int Stores the factorial value of a digit inside


the factorial() method.

Page | 50
SAMPLE INTPUT:

SAMPLE OUTPUT:

Page | 51
QUESTION 17:
Write a program to accept a word and convert it into lower case, if it is in upper case. Display the
new word by replacing only the vowels with the letter following it. Sample Input: computer
Sample Output: cpmpvtfr

ANSWER 17:
import java.util.Scanner;

class VowelReplacement

public static void main(String[] args)

Scanner sc = new Scanner(System.in);

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

String word = sc.nextLine();

word = word.toLowerCase();

StringBuilder result = new StringBuilder();

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

char ch = word.charAt(i);

if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u')

result.append((char)(ch + 1));

} else {

result.append(ch);

Page | 52
System.out.println("Modified word: " + result.toString());

VARIABLE DESCRIPTION:
Name of the Data Type Purpose/description
Variable
word String . Stores the input word entered by the user

result StringBuilder Stores the modified word after vowel


replacements.
ch char Stores the current character being checked
from the word.
i int Index variable used to loop through each
character in the word.

SAMPLE INTPUT:

SAMPLE OUTPUT:

Page | 53
QUESTION 18:
Write a menu driven program to perform the following tasks by using Switch case statement:

(a) To print the series: 0, 3, 8, 15, 24, ............ to n terms. (value of 'n' is to be an input by the
user)

(b) To find the sum of the series: S = (1/2) + (3/4) + (5/6) + (7/8) + ........... + (19/20)

ANSWER 18:
import java.util.Scanner;

class SeriesOperations {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

int choice;

for (;;) {

System.out.println("Menu:");

System.out.println("1. Print the series: 0, 3, 8, 15, 24, ... up to n terms");

System.out.println("2. Find the sum of the series: (1/2) + (3/4) + (5/6) + ... + (19/20)");

System.out.println("3. Exit");

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

choice = sc.nextInt();

switch (choice) {

case 1:

System.out.print("Enter the value of n: ");

int n = sc.nextInt();

System.out.print("Series: ");

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

Page | 54
System.out.print(i * i + " ");

System.out.println();

break;

case 2:

double sum = 0;

for (int i = 1; i <= 10; i++)

sum += (2 * i - 1) / (double) (2 * i);

System.out.println("Sum of the series: " + sum);

break;

case 3:

System.out.println("Exiting...");

return;

default:

System.out.println("Invalid choice, please try again.");

Page | 55
VARIABLE DESCRIPTION:
Name of the Data Type Purpose/description
Variable
Stores the user's menu choice to decide
choice int the action (1, 2, or 3).

n int Stores the number of terms for the first


series to be printed.
sum double Stores the sum of the second series, i.e.,
(1/2) + (3/4) + ...
i int Used as the loop counter for iterating
over the series terms.

SAMPLE INTPUT:

SAMPLE OUTPUT:

SAMPLE INPUT:

SAMPLE OUTPUT:

Page | 56
QUESTION 19:
Write a program to store 6 elements in an array P and 4 elements in an array Q. Now, produce a
third array R, containing all the elements of array P and Q. Display the resultant array.

Input P[ ] Input Output


Q[ ] R[ ]
10 1 6
19 7 2
4 1 8
8 2 23
23 4
6 10
19
1
7
1

ANSWER 19:
import java.util.Scanner;

public class MergeArrays

public static void main(String[] args)

Scanner sc = new Scanner(System.in);

int[] P = new int[6];

int[] Q = new int[4];

System.out.println("Enter 6 elements for array P:");

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

P[i] = sc.nextInt();

System.out.println("Enter 4 elements for array Q:");

Page | 57
for (int i = 0; i < Q.length; i++)

Q[i] = sc.nextInt();

int[] R = new int[P.length + Q.length];

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

R[i] = P[i];

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

R[P.length + i] = Q[i];

System.out.println("Resultant array R contains:");

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

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

VARIABLE DESCRIPTION:
Name of the Data Type Purpose/description
Variable
To store 6 elements for the first array.
P int[]
.

Page | 58
Q int[] To store 4 elements for the second array.

R int[] To store the merged array containing


elements from P and Q.
sc Scanner To take input from the user

i int Used as an index for iterating through the


arrays.

SAMPLE INPUT:

SAMPLE OUTPUT:

Page | 59
QUESTION 20:
Define a class to accept values into an array of double data type of size 20. Accept a double
value from user and search in the array using linear search method.

If value is found display message "Found" with its position where it is present in the array.
Otherwise display message "not found"

ANSWER 19:
import java.util.Scanner;

class LinearSearchArray

public static void main(String[] args)

Scanner sc = new Scanner(System.in);

double[] arr = new double[20];

System.out.println("Enter 20 double values:");

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

arr[i] = sc.nextDouble();

System.out.print("Enter a value to search: ");

double value = sc.nextDouble();

boolean found = false;

int position = -1;

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

if (arr[i] == value)

Page | 60
found = true;

position = i;

break;

if (found)

System.out.println("Found at position " + (position + 1));

else

System.out.println("Not found");

VARIABLE DESCRIPTION:
Name of the Data Type Purpose/description
Variable
arr double[] To store the 20 double values entered by the
user.

i int Used as an index for iterating through the


array.
sc Scanner To take input from the user.
value double To store the value to be searched in the array.
found boolean To indicate if the value was found in the
array or not.
position int To store the index position of the found
value.

Page | 61
SAMPLE INPUT:

SAMPLE OUTPUT:

Page | 62

You might also like