You are on page 1of 63

Topic :- Input in Java

Question 1

Write a program to calculate the time period of a Simple Pendulum by taking length and acceleration due to gravity (g) as inputs.
The time period of a Simple Pendulum is given by the formula:
T = 2π√(l/g)
import java.util.Scanner;

public class KboatSimplePendulum


{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.print("Enter length: ");
double l = in.nextDouble();
System.out.print("Enter g: ");
double g = in.nextDouble();
double t = 2 * (22.0 / 7.0) * Math.sqrt(l/g);
System.out.println("T = " + t);
}
}

Output

Question 2

Write a program by using class 'Employee' to accept Basic Pay of an employee. Calculate the allowances/deductions as given below.

Allowance / Deduction Rate

Dearness Allowance (DA) 30% of Basic Pay

House Rent Allowance (HRA) 15% of Basic Pay

Provident Fund (PF) 12.5% of Basic Pay

Finally, find and print the Gross and Net pay.


Gross Pay = Basic Pay + Dearness Allowance + House Rent Allowance
Net Pay = Gross Pay - Provident Fund

import java.util.Scanner;
public class Employee
{
public void computePay() {
Scanner in = new Scanner(System.in);
System.out.print("Enter Basic Pay: ");
double bp = in.nextDouble();
double da = 0.3 * bp;
double hra = 0.15 * bp;
double pf = 0.125 * bp;
double gp = bp + da + hra;
double np = gp - pf;
System.out.println("Gross Pay = " + gp);
System.out.println("Net Pay = " + np);
}
}

Output

Question 3

A shopkeeper offers 10% discount on the printed price of a Digital Camera. However, a customer has to pay 6% GST on the remaining amount.
Write a program in Java to calculate the amount to be paid by the customer taking printed price as an input.

import java.util.Scanner;

public class KboatCameraPrice


{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.println("Enter printed price of Digital Camera:");
double mrp = in.nextDouble();
double disc = mrp * 10 / 100.0;
double price = mrp - disc;
double gst = price * 6 / 100.0;
price += gst;
System.out.println("Amount to be paid: " + price);
}
}

Output
Question 4

A shopkeeper offers 30% discount on purchasing articles whereas the other shopkeeper offers two successive discounts 20% and 10% for
purchasing the same articles. Write a program in Java to compute and display the discounts.
Take the price of an article as the input.

import java.util.Scanner;

public class KboatDiscounts


{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.print("Enter price of article: ");
double price = in.nextDouble();

double d1 = price * 30 / 100.0;


double amt1 = price - d1;
System.out.println("30% discount = " + d1);
System.out.println("Amount after 30% discount = " + amt1);

double d2 = price * 20 / 100.0;


double amt2 = price - d2;
double d3 = amt2 * 10 / 100.0;
amt2 -= d3;
System.out.println("20% discount = " + d2);
System.out.println("10% discount = " + d3);
System.out.println("Amount after successive discounts = " + amt2);
}
}

Output
Question 5

Mr. Agarwal invests certain sum at 5% per annum compound interest for three years. Write a program in Java to calculate:

(a) the interest for the first year


(b) the interest for the second year
(c) the amount after three years.

Take sum as an input from the user.


Sample Input: Principal = ₹5000, Rate =10%, Time = 3 yrs
Sample Output: Interest for the first year: ₹500
Interest for the second year: ₹550
Interest for the third year: ₹605

import java.util.Scanner;

public class KboatCompoundInterest


{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.print("Enter sum of money: ");
double p = in.nextDouble();
double interest = p * 5 * 1 / 100.0;
System.out.println("Interest for the first year = " + interest);
p += interest;
interest = p * 5 * 1 / 100.0;
System.out.println("Interest for the second year = " + interest);
p += interest;
interest = p * 5 * 1 / 100.0;
System.out.println("Interest for the third year = " + interest);
}
}

Output
Topic :- Mathematical Library Methods
Question 1

Write a program in Java to input three numbers and display the greatest and the smallest of the two numbers.
Hint: Use Math.min( ) and Math.max( )
Sample Input: 87, 65, 34
Sample Output: Greatest Number 87
Smallest number 34

import java.util.Scanner;

public class KboatGreatestNumber


{
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();
System.out.print("Enter Third Number: ");
int c = in.nextInt();

int g = Math.max(a, b);


g = Math.max(g, c);

int s = Math.min(a, b);


s = Math.min(s, c);

System.out.println("Greatest Number = " + g);


System.out.println("Smallest Number = " + s);
}
}

Output

Question 2

Write a program in Java to calculate and display the hypotenuse of a Right-Angled Triangle by taking perpendicular and base as inputs.
2 2
Hint: h = √p + b

import java.util.Scanner;

public class KboatHypotenuse


{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.print("Enter Perpendicular: ");
double p = in.nextDouble();
System.out.print("Enter Base: ");
double b = in.nextDouble();

double h = Math.sqrt(Math.pow(p, 2) + Math.pow(b, 2));

System.out.println("Hypotenuse = " + h);


}
}

Output

Question 3

Write a program to input a number and evaluate the results based on the number entered by the user:
(a) Natural logarithm of the number
(b) Absolute value of the number
(c) Square root of the number
(d) Cube of the number
(e) Random numbers between 0 (zero) and 1 (one).

import java.util.Scanner;

public class KboatMathMethods


{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.print("Enter Number: ");
double n = in.nextDouble();

System.out.println("Natural logarithm = " + Math.log(n));


System.out.println("Absolute value = " + Math.abs(n));
System.out.println("Square root = " + Math.sqrt(n));
System.out.println("Cube root= " + Math.cbrt(n));
System.out.println("Random number = " + Math.random());
}
}

Output
Question 4

In an examination, you have appeared for three subjects i.e. Physics, Chemistry and Biology. Write a program in Java to calculate the average
mark obtained and finally display the marks in rounded-off form.
Take Physics, Chemistry. and Biology marks as inputs.

import java.util.Scanner;

public class KboatAvgMarks


{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.println("Enter Marks");
System.out.print("Physics: ");
int p = in.nextInt();
System.out.print("Chemistry: ");
int c = in.nextInt();
System.out.print("Biology: ");
int b = in.nextInt();

double avg = (p + c + b) / 3.0;


long roundAvg = Math.round(avg);

System.out.println("Rounded Off Avg Marks = " + roundAvg);


}
}

Output
Question 5

You want to calculate the radius of a circle by using the formula:


2
Area = (22/7) * r ; where r = radius of a circle
Hence the radius can be calculated as:
r = √((7 * area) / 22)
Write a program in Java to calculate and display the radius of a circle by taking area as an input.

import java.util.Scanner;

public class KboatCircleRadius


{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.print("Enter Area of Circle: ");
double area = in.nextDouble();
double r = Math.sqrt(7 * area / 22);
System.out.print("Radius of Circle = " + r);
}
}

Output
Topic :- Conditional Constructs in Java (
Question 1

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'.
Sample Input: Enter three angles: 40, 50, 90
Sample Output: Right=angled Triangle

import java.util.Scanner;

public class KboatTriangleAngle


{
public void checkTriangle() {
Scanner in = new Scanner(System.in);
System.out.print("Enter first angle: ");
int a1 = in.nextInt();
System.out.print("Enter second angle: ");
int a2 = in.nextInt();
System.out.print("Enter third angle: ");
int a3 = in.nextInt();
int angleSum = a1 + a2 + a3;

if (angleSum == 180 && a1 > 0 && a2 > 0 && a3 > 0) {


if (a1 < 90 && a2 < 90 && a3 < 90) {
System.out.println("Acute-angled Triangle");
}
else if (a1 == 90 || a2 == 90 || a3 == 90) {
System.out.println("Right-angled Triangle");
}
else {
System.out.println("Obtuse-angled Triangle");
}
}
else {
System.out.println("Triangle not possible");
}
}
}

Output

Question 2
Write a program to input the cost price and the selling price of an article. If the selling price is more than the cost price then calculate and
display actual profit and profit per cent otherwise, calculate and display actual loss and loss per cent. If the cost price and the selling price are
equal, the program displays the message 'Neither profit nor loss'.

import java.util.Scanner;

public class KboatProfit


{
public void calculateProfit() {
Scanner in = new Scanner(System.in);
System.out.print("Enter cost price of the article: ");
double cp = in.nextDouble();
System.out.print("Enter selling price of the article: ");
double sp = in.nextDouble();
double pl = sp - cp;
double percent = Math.abs(pl) / cp * 100;
if (pl > 0) {
System.out.println("Profit = " + pl);
System.out.println("Profit % = " + percent);
}
else if (pl < 0) {
System.out.println("Loss = " + Math.abs(pl));
System.out.println("Loss % = " + percent);
}
else {
System.out.println("Neither profit nor loss");
}
}
}

Output
Question 3

Write a program to input three numbers and check whether they are equal or not. If they are unequal numbers then display the greatest
among them otherwise, display the message 'All the numbers are equal'.
Sample Input: 34, 87, 61
Sample Output: Greatest number: 87
Sample Input: 81, 81, 81
Sample Output: All the numbers are equal.

import java.util.Scanner;

public class Kboat3Numbers


{
public void findGreatest() {
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();
System.out.print("Enter third number: ");
int c = in.nextInt();

if (a == b && b == c) {
System.out.println("All the numbers are equal");
}
else {
int g = a;

if (b > g)
g = b;

if (c > g)
g = c;

System.out.println("Greatest number: " + g);


}
}
}

Output
Question 4

Write a program to accept a number and check whether the number is divisible by 3 as well as 5. Otherwise, decide:
(a) Is the number divisible by 3 and not by 5?
(b) Is the number divisible by 5 and not by 3?
(c) Is the number neither divisible by 3 nor by 5?
The program displays the message accordingly.

import java.util.Scanner;

public class Divisor


{
public void checkDivisibility() {
Scanner in = new Scanner(System.in);
System.out.print("Enter number: ");
int num = in.nextInt();

if (num % 3 == 0 && num % 5 == 0)


System.out.println("Divisible by 3 and 5");
else if (num % 3 == 0)
System.out.println("Divisible by 3 but not by 5");
else if (num % 5 == 0)
System.out.println("Divisible by 5 but not by 3");
else
System.out.println("Neither divisible by 3 nor by 5");
}
}

Output

Question 5

Write a program to input three unequal numbers. Display the greatest and the smallest number.
Sample Input: 28, 98, 56
Sample Output: Greatest Number: 98
Smallest Number: 28

import java.util.Scanner;

public class KboatMinMaxNumbers


{
public void findMinMax() {
Scanner in = new Scanner(System.in);
System.out.println("Enter 3 unequal numbers");
System.out.print("Enter first number: ");
int a = in.nextInt();
System.out.print("Enter second number: ");
int b = in.nextInt();
System.out.print("Enter third number: ");
int c = in.nextInt();

int min = a, max = a;


min = b < min ? b : min;
min = c < min ? c : min;

max = b > max ? b : max;


max = c > max ? c : max;

System.out.println("Greatest Number: " + max);


System.out.println("Smallest Number: " + min);

}
}
Output

Question 6

A Pre-Paid taxi charges from the passenger as per the tariff given below:

Distance Rate

Up to 5 km ₹ 100

For the next 10 km ₹ 10/km

For the next 10 km ₹ 8/km

More than 25 km ₹ 5/km

Write a program to input the distance covered and calculate the amount paid by the passenger. The program displays the printed bill with the
details given below:
Taxi No. :
Distance covered :
Amount :

import java.util.Scanner;

public class KboatPrePaidTaxi


{
public void computeBill() {
Scanner in = new Scanner(System.in);
System.out.print("Enter Taxi Number: ");
String taxiNo = in.nextLine();
System.out.print("Enter distance travelled: ");
int dist = in.nextInt();

int fare = 0;
if (dist <= 5)
fare = 100;
else if (dist <= 15)
fare = 100 + (dist - 5) * 10;
else if (dist <= 25)
fare = 100 + 100 + (dist - 15) * 8;
else
fare = 100 + 100 + 80 + (dist - 25) * 5;

System.out.println("Taxi No: " + taxiNo);


System.out.println("Distance covered: " + dist);
System.out.println("Amount: " + fare);

}
}

Output

Question 7

A cloth showroom has announced festival discounts and the gifts on the purchase of items, based on the total cost as given below:

Total Cost Discount Gift

Up to ₹ 2,000 5% Calculator

₹ 2,001 to ₹ 5,000 10% School Bag

₹ 5,001 to ₹ 10,000 15% Wall Clock

Above ₹ 10,000 20% Wrist Watch

Write a program to input the total cost. Compute and display the amount to be paid by the customer along with the gift.

import java.util.Scanner;

public class KboatClothDiscount


{
public void computeBill() {
Scanner in = new Scanner(System.in);
System.out.print("Enter total cost: ");
double cost = in.nextDouble();
String gift;
double amt;

if (cost <= 2000.0) {


amt = cost - (cost * 5 / 100);
gift = "Calculator";
}
else if (cost <= 5000.0) {
amt = cost - (cost * 10 / 100);
gift = "School Bag";
}
else if (cost <= 10000.0) {
amt = cost - (cost * 15 / 100);
gift = "Wall Clock";
}
else {
amt = cost - (cost * 20 / 100);
gift = "Wrist Watch";
}

System.out.println("Amount to be paid: " + amt);


System.out.println("Gift: " + gift);

}
}

Output

Question 8

Given below is a hypothetical table showing rate of income tax for an India citizen, who is below or up to 60 years.

Taxable income (TI) in ₹ Income Tax in ₹

Up to ₹ 2,50,000 Nil

More than ₹ 2,50,000 and less than or equal to ₹ 5,00,000 (TI - 1,60,000) * 10%

More than ₹ 5,00,000 and less than or equal to ₹ 10,00,000 (TI - 5,00,000) * 20% + 34,000

More than ₹ 10,00,000 (TI - 10,00,000) * 30% + 94,000

Write a program to input the name, age and taxable income of a person. If the age is more than 60 years then display the message "Wrong
Category". If the age is less than or equal to 60 years then compute and display the income tax payable along with the name of tax payer, as
per the table given above.

import java.util.Scanner;

public class KboatIncomeTax


{
public void computeIncomeTax() {
Scanner in = new Scanner(System.in);
System.out.print("Enter Name: ");
String name = in.nextLine();
System.out.print("Enter age: ");
int age = in.nextInt();
System.out.print("Enter taxable income: ");
double ti = in.nextDouble();
double tax = 0.0;

if (age > 60) {


System.out.print("Wrong Category");
}
else {
if (ti <= 250000)
tax = 0;
else if (ti <= 500000)
tax = (ti - 160000) * 10 / 100;
else if (ti <= 1000000)
tax = 34000 + ((ti - 500000) * 20 / 100);
else
tax = 94000 + ((ti - 1000000) * 30 / 100);
}

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


System.out.println("Tax Payable: " + tax);
}
}

Output

Question 9

Mr. Kumar is an LIC agent. He offers discount to his policy holders on the annual premium. However, he also gets commission on the sum
assured as per the given tariff.

Sum Assured Discount Commission

Up to ₹ 1,00,000 5% 2%

₹ 1,00,001 and up to ₹ 2,00,000 8% 3%

₹ 2,00,001 and up to ₹ 5,00,000 10% 5%

More than ₹ 5,00,000 15% 7.5%


Write a program to input name of the policy holder, the sum assured and first annual premium. Calculate the discount of the policy holder and
the commission of the agent. The program displays all the details as:
Name of the policy holder :
Sum assured :
Premium :
Discount on the first premium :
Commission of the agent :

import java.util.Scanner;

public class KboatLICPolicy


{
public void compute() {
Scanner in = new Scanner(System.in);
System.out.print("Enter Name: ");
String name = in.nextLine();
System.out.print("Enter Sum Assured: ");
double sum = in.nextDouble();
System.out.print("Enter First Premium: ");
double pre = in.nextDouble();
double disc = 0.0, comm = 0.0;

if(sum <= 100000){


disc = pre * 5.0 / 100.0;
comm = sum * 2.0 / 100.0;
}
else if(sum <= 200000){
disc = pre * 8.0 / 100.0;
comm = sum * 3.0 / 100.0;
}
else if(sum <= 500000){
disc = pre * 10.0 / 100.0;
comm = sum * 5.0 / 100.0;
}
else{
disc = pre * 15.0 / 100.0;
comm = sum * 7.5 / 100.0;
}

System.out.println("Name of the policy holder: " + name);


System.out.println("Sum assured: " + sum);
System.out.println("Premium: " + pre);
System.out.println("Discount on the first premium: " + disc);
System.out.println("Commission of the agent: " + comm);

}
}

Output
Question 10

A company announces revised Dearness Allowance (DA) and Special Allowances (SA) for their employees as per the tariff given below:

Basic Dearness Allowance (DA) Special Allowance (SA)

Up to ₹ 10,000 10% 5%

₹ 10,001 - ₹ 20,000 12% 8%

₹ 20,001 - ₹ 30,000 15% 10%

₹ 30,001 and above 20% 12%

Write a program to accept name and Basic Salary (BS) of an employee. Calculate and display gross salary.
Gross Salary = Basic + Dearness Allowance + Special Allowance
Print the information in the given format:
Name Basic DA Spl. Allowance Gross Salary
xxx xxx xxx xxx xxx

import java.util.Scanner;

public class KboatSalary


{
public void computeRevisedSalary() {
Scanner in = new Scanner(System.in);
System.out.print("Enter name: ");
String name = in.nextLine();
System.out.print("Enter basic salary: ");
double bs = in.nextDouble();
double da = 0.0, sa = 0.0;

if (bs <= 10000){


da = bs * 10.0 / 100.0;
sa = bs * 5.0 / 100.0;
}
else if (bs <= 20000){
da = bs * 12.0 / 100.0;
sa = bs * 8.0 / 100.0;
}
else if (bs <= 30000){
da = bs * 15.0 / 100.0;
sa = bs * 10.0 / 100.0;
}
else{
da = bs * 20.0 / 100.0;
sa = bs * 12.0 / 100.0;
}

double gs = bs + da + sa;
System.out.println("Name\tBasic\tDA\tSpl. Allowance\tGross Salary");
System.out.println(name + "\t" + bs + "\t" + da + "\t" + sa + "\t" + gs);
}
}

Output

Question 11

Using a switch case statement, write a menu driven program to convert a given temperature from Fahrenheit to Celsius and vice-versa. For an
incorrect choice, an appropriate message should be displayed.
Hint: c = 5/9*(f-32) and f=1.8*c+32
import java.util.Scanner;

public class KboatTemperature


{
public void convertTemperature() {
Scanner in = new Scanner(System.in);
System.out.println("Type 1 to convert from Fahrenheit to Celsius");
System.out.println("Type 2 to convert from Celsius to Fahrenheit");
int choice = in.nextInt();
double ft = 0.0, ct = 0.0;

switch (choice) {
case 1:
System.out.print("Enter temperature in Fahrenheit: ");
ft = in.nextDouble();
ct = 5 / 9.0 * (ft - 32);
System.out.println("Temperature in Celsius: " + ct);
break;

case 2:
System.out.print("Enter temperature in Celsius: ");
ct = in.nextDouble();
ft = 1.8 * ct + 32;
System.out.println("Temperature in Fahrenheit: " + ft);
break;

default:
System.out.println("Incorrect Choice");
break;
}
}
}

Output
Question 12

The volume of solids, viz. cuboid, cylinder and cone can be calculated by the formula:

1. Volume of a cuboid (v = l*b*h)


2
2. Volume of a cylinder (v = π*r *h)
2
3. Volume of a cone (v = (1/3)*π*r *h)

Using a switch case statement, write a program to find the volume of different solids by taking suitable variables and data types.

import java.util.Scanner;

public class KboatMenuVolume


{
public void calculateVolume() {
Scanner in = new Scanner(System.in);
System.out.println("1. Volume of cuboid");
System.out.println("2. Volume of cylinder");
System.out.println("3. Volume of cone");
System.out.print("Enter your choice: ");
int choice = in.nextInt();

switch(choice) {
case 1:
System.out.print("Enter length of cuboid: ");
double l = in.nextDouble();
System.out.print("Enter breadth of cuboid: ");
double b = in.nextDouble();
System.out.print("Enter height of cuboid: ");
double h = in.nextDouble();
double vol = l * b * h;
System.out.println("Volume of cuboid = " + vol);
break;

case 2:
System.out.print("Enter radius of cylinder: ");
double rCylinder = in.nextDouble();
System.out.print("Enter height of cylinder: ");
double hCylinder = in.nextDouble();
double vCylinder = (22 / 7.0) * Math.pow(rCylinder, 2) * hCylinder;
System.out.println("Volume of cylinder = " + vCylinder);
break;

case 3:
System.out.print("Enter radius of cone: ");
double rCone = in.nextDouble();
System.out.print("Enter height of cone: ");
double hCone = in.nextDouble();
double vCone = (1 / 3.0) * (22 / 7.0) * Math.pow(rCone, 2) * hCone;
System.out.println("Volume of cone = " + vCone);
break;

default:
System.out.println("Wrong choice! Please select from 1 or 2 or 3.");
}
}
}

Output
Question 13

A Mega Shop has different floors which display varieties of dresses as mentioned
below:
1. Ground floor : Kids Wear
2. First floor : Ladies Wear
3. Second floor : Designer Sarees
4. Third Floor : Men's Wear

The user enters floor number and gets the information regarding different items of the Mega shop. After shopping, the customer pays the
amount at the billing counter and the shopkeeper prints the bill in the given format:

Name of the Shop: City Mart


Total Amount:
Visit Again!!

Write a program to perform the above task as per the user's choice.

import java.util.Scanner;

public class KboatMegaShop


{
public void doShopping() {
Scanner in = new Scanner(System.in);
System.out.println("1. Ground floor");
System.out.println("2. First floor");
System.out.println("3. Second floor");
System.out.println("4. Third floor");

System.out.print("Select a floor: ");


int floor = in.nextInt();

boolean isFloorValid = true;

switch (floor) {
case 1:
System.out.println("Kids Wear");
break;
case 2:
System.out.println("Ladies Wear");
break;
case 3:
System.out.println("Designer Sarees");
break;
case 4:
System.out.println("Men's Wear");
break;
default:
isFloorValid = false;
System.out.println("Incorrect Floor");
break;
}

if (isFloorValid) {
System.out.print("Enter bill amount: ");
double amt = in.nextDouble();

System.out.println("Name of the Shop: City Mart");


System.out.println("Total Amount: " + amt);
System.out.println("Visit Again!!");
}
}
}

Output
Question 14

The equivalent resistance of series and parallel connections of two resistances are given by the formula:

(a) R1 = r1 + r2 (Series)
(b) R2 = (r1 * r2) / (r1 + r2) (Parallel)

Using a switch case statement, write a program to enter the value of r1 and r2. Calculate and display the equivalent resistances accordingly.

import java.util.Scanner;

public class KboatResistance


{
public void computeEqResistance() {
Scanner in = new Scanner(System.in);
System.out.println("1. Series");
System.out.println("2. Parallel");

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


int choice = in.nextInt();
boolean isChoiceValid = true;

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


double r1 = in.nextDouble();
System.out.print("Enter r2: ");
double r2 = in.nextDouble();
double eqr = 0.0;

switch (choice) {
case 1:
eqr = r1 + r2;
break;
case 2:
eqr = (r1 * r2) / (r1 + r2);
break;
default:
isChoiceValid = false;
System.out.println("Incorrect choice");
break;
}

if (isChoiceValid)
System.out.println("Equivalent resistance = " + eqr);
}
}

Output

Question 15

The Simple Interest (SI) and Compound Interest (CI) of a sum (P) for a given time (T) and rate (R) can be calculated as:
T
(a) SI = (p * r * t) / 100 (b) CI = P * ((1 + (R / 100)) - 1)

Write a program to input sum, rate, time and type of Interest ('S' for Simple Interest and 'C' for Compound Interest). Calculate and display the
sum and the interest earned.
import java.util.Scanner;

public class KboatInterest


{
public void computeInterest() {
Scanner in = new Scanner(System.in);
System.out.print("Enter the sum: ");
double p = in.nextDouble();
System.out.print("Enter rate: ");
double r = in.nextDouble();
System.out.print("Enter time: ");
int t = in.nextInt();
System.out.println("Enter type of Interest");
System.out.print("('S'- Simple Interest 'C'- Compound Interest): ");
char type = in.next().charAt(0);
boolean isTypeValid = true;

double interest = 0.0;

switch (type) {
case 'S':
interest = p * r * t / 100;
break;

case 'C':
interest = p * (Math.pow((1 + (r / 100)), t) - 1);
break;

default:
isTypeValid = false;
System.out.println("Incorrect Interest type");
break;
}

if (isTypeValid) {
double amt = p + interest;
System.out.println("Sum = " + p);
System.out.println("Interest = " + interest);
System.out.println("Sum + Interest = " + amt);
}
}
}

Output
Question 16

'Kumar Electronics' has announced the following seasonal discounts on purchase of certain items.

Purchase Amount Discount on Laptop Discount on Desktop PC

Up to ₹ 25000 0.0% 5.0%

₹ 25,001 to ₹ 50,000 5% 7.5%

₹ 50,001 to ₹ 1,00,000 7.5% 10.0%

More than ₹ 1,00,000 10.0% 15.0%

Write a program to input name, amount of purchase and the type of purchase (`L' for Laptop and 'D' for Desktop) by a customer. Compute and
print the net amount to be paid by a customer along with his name.
(Net amount = Amount of purchase - discount)

import java.util.Scanner;

public class KboatElectronicsSale


{
public void computeDiscount() {
Scanner in = new Scanner(System.in);
System.out.print("Enter Name: ");
String name = in.nextLine();
System.out.print("Enter Amount of Purchase: ");
double amt = in.nextDouble();
System.out.println("Enter Type of Purchase");
System.out.print("'L'- Laptop or 'D'- Desktop: ");
char type = in.next().charAt(0);
type = Character.toUpperCase(type);
double disc = 0.0;

if (amt <= 25000)


disc = type == 'L' ? 0.0 : 5.0;
else if (amt <= 50000)
disc = type == 'L' ? 5.0 : 7.0;
else if (amt <= 100000)
disc = type == 'L' ? 7.5 : 10.0;
else
disc = type == 'L' ? 10.0 : 15.0;

double netAmt = amt - (disc * amt / 100);

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


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

Output
Topic :- Iterative Constructs in Java
Question 11

The Greatest Common Divisor (GCD) of two integers is calculated by the continued division method. Divide the larger number by the smaller,
the remainder then divides the previous divisor. The process repeats unless the remainder reaches to zero. The last divisor results in GCD.
Sample Input: 45, 20
Sample Output: GCD=5

import java.util.Scanner;

public class KboatGCD


{
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();
while (b != 0) {
int t = b;
b = a % b;
a = t;
}
System.out.println("GCD=" + a);
}
}

Output

Question 2

Write a program to input any 50 numbers (including positive and negative).


Perform the following tasks:
(a) Count the positive numbers
(b) Count the negative numbers
(c) Sum of positive numbers
(d) Sum of negative numbers

import java.util.Scanner;

public class KboatIntegers


{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
int pSum = 0, pCount = 0, nSum = 0, nCount = 0;
System.out.println("Enter 50 numbers");

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


int n = in.nextInt();
if (n >= 0) {
pSum += n;
pCount++;
}
else {
nSum += n;
nCount++;
}
}

System.out.println("Positive Count = " + pCount);


System.out.println("Positive Sum = " + pSum);
System.out.println("Negative Count = " + nCount);
System.out.println("Negative Sum = " + nSum);
}
}

Output
Question 3

Write a program to calculate the sum of all odd numbers and even numbers between a range of numbers from m to n (both inclusive) where
m < n. Input m and n (where m<n).

import java.util.Scanner;

public class KboatOddEvenSum


{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.print("Enter m: ");
int m = in.nextInt();
System.out.print("Enter n: ");
int n = in.nextInt();
long sumOdd = 0, sumEven = 0;

if (m > n) {
System.out.println("m should be less than n");
}
else {
for (int i = m; i <=n; i++) {
if (i % 2 == 0)
sumEven += i;
else
sumOdd += i;
}

System.out.println("Even Sum: " + sumEven);


System.out.println("Odd Sum: " + sumOdd);
}
}
}

Output

Question 4

Write a program to enter any 50 numbers and check whether they are divisible by 5 or not. If divisible then perform the following tasks:
(a) Display all the numbers ending with the digit 5.
(b) Count those numbers ending with 0 (zero).

import java.util.Scanner;

public class KboatDivisibleBy5


{
public static void main(String args[]) {
final int COUNT = 50;
Scanner in = new Scanner(System.in);
int n, c = 0;
System.out.println("Enter " + COUNT + " numbers");
for (int i = 1; i <= COUNT; i++) {
n = in.nextInt();
if (i % 5 == 0) {
if (i % 10 == 5)
System.out.println("Number end with the digit 5");
if (i % 10 == 0)
c++;
}
}
System.out.println("Count of numbers ending with 0: " + c);
}
}

Output
Question 5

Write a program to display all the numbers between m and n input from the keyboard (where m<n, m>0, n>0), check and print the numbers
that are perfect square. e.g. 25, 36, 49, are said to be perfect square numbers.

import java.util.Scanner;

public class KboatPerfectSquare


{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.print("Enter m: ");
int m = in.nextInt();
System.out.print("Enter n: ");
int n = in.nextInt();

if (m < n && m > 0 && n > 0) {


for (int i = m; i <= n; i++) {
System.out.println("Number = " + i);
double sroot = Math.sqrt(i);
if (sroot == Math.floor(sroot))
System.out.println(i + " is a perfect square");
}
}
else {
System.out.println("Invalid input");
}
}
}

Output
Question 6

Write a program to display all the 'Buzz Numbers' between p and q (where p<q). A 'Buzz Number' is the number which ends with 7 or is
divisible by 7.

import java.util.Scanner;

public class KboatBuzzNumber


{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.print("Enter p: ");
int p = in.nextInt();
System.out.print("Enter q: ");
int q = in.nextInt();
if (p < q) {
System.out.println("Buzz Numbers between "
+ p + " and " + q);
for (int i = p; i <= q; i++) {
if (i % 10 == 7 || i % 7 == 0)
System.out.println(i);
}
}
else {
System.out.println("Invalid Inputs!!!");
System.out.println("p should be less than q");
}

}
}

Output

Question 7

Write a program to input marks in English, Maths and Science of 40 students who have passed ICSE Examination 2014. Now, perform the
following tasks:
(a) Number of students, who have secured 95% or more in all the subjects.
(b) Number of students, who have secured 90% or more in English, Maths and Science.

import java.util.Scanner;

public class KboatExamResult


{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
int ta = 0, te = 0, tm = 0, ts = 0;
for (int i = 1; i <= 40; i++) {
System.out.println("Enter marks of student " + i);
System.out.print("English: ");
int eng = in.nextInt();
System.out.print("Maths: ");
int maths = in.nextInt();
System.out.print("Science: ");
int sci = in.nextInt();

if (eng >= 95 && maths >= 95 && sci >= 95)


ta++;
if (eng >= 90)
te++;

if (maths >= 90)


tm++;

if (sci >= 90)


ts++;
}
System.out.println("No. of students >= 95% in all subjects: " + ta);
System.out.println("No. of students >= 90% in English: " + te);
System.out.println("No. of students >= 90% in Maths: " + tm);
System.out.println("No. of students >= 90% in Science: " + ts);
}
}

Output
Question 8
Write a program in Java to find the sum of the given series :
(a) 1 + 4 + 9 + ...... + 400

public class KboatSeries


{
public static void main(String args[]) {
int sum = 0;
for (int i = 1; i <= 20; i++)
sum += (i*i);
System.out.println("Sum = " + sum);
}
}

Output

(b) 1 + (1/2) + (1/3) + ...... + (1/20)

public class KboatSeries


{
public static void main(String args[]) {
double sum = 0.0;
for (int i = 1; i <= 20; i++)
sum += (1.0 / i);
System.out.println("Sum = " + sum);
}
}

Output
(c) 1 + (1/3) + (1/5) + ...... + (1/19)

public class KboatSeries


{
public static void main(String args[]) {
double sum = 0.0;
for (int i = 1; i <= 19; i = i + 2)
sum += (1.0 / i);
System.out.println("Sum = " + sum);
}
}

Output

(d) (1/2) + (2/3) + (3/4) + ...... + (19/20)

public class KboatSeries


{
public static void main(String args[]) {
double sum = 0.0;
for (int i = 1; i <= 19; i++)
sum += (i / (double)(i + 1));
System.out.println("Sum = " + sum);
}
}
Output

(e) 2 - 4 + 6 - 8 + ...... - 20

public class KboatSeries


{
public static void main(String args[]) {
double sum = 0.0;
for (int i = 1; i <= 10; i++) {
if (i % 2 == 0)
sum -= i * 2;
else
sum += i * 2;
}
System.out.println("Sum = " + sum);
}
}

Output

(f) (1*2) + (2*3) + ...... + (19*20)

public class KboatSeries


{
public static void main(String args[]) {
int sum = 0;
for (int i = 1; i <= 19; i++)
sum += i * (i + 1);
System.out.println("Sum = " + sum);
}
}

Output

Question 9

Write a program to input a number and count the number of digits. The program further checks whether the number contains odd number of
digits or even number of digits.
Sample Input: 749
Sample Output: Number of digits=3
The number contains odd number of digits.

import java.util.Scanner;

public class KboatDigitCount


{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.print("Enter number: ");
int n = in.nextInt();
int dc = 0;

while (n != 0) {
dc++;
n /= 10;
}

System.out.println("Number of digits = " + dc);

if (dc % 2 == 0)
System.out.println("The number contains even number of digits");
else
System.out.println("The number contains odd number of digits");
}
}

Output
Question 10

Write a program to input a number and display the new number after reversing the digits of the original number. The program also displays
the absolute difference between the original number and the reversed number.
Sample Input: 194
Sample Output: 491
Absolute Difference= 297

import java.util.Scanner;

public class KboatDigitReverse


{
public static void main(String args[]) {

Scanner in = new Scanner(System.in);


System.out.print("Enter Number: ");
int orgNum = in.nextInt();

int copyNum = orgNum;


int revNum = 0;

while(copyNum != 0) {
int digit = copyNum % 10;
copyNum /= 10;
revNum = revNum * 10 + digit;
}

int diff = revNum - orgNum;


System.out.println("Reversed Number = " + revNum);
System.out.println("Absolute Difference = " + Math.abs(diff));
}
}

Output
Topic :- Nested For Loops
Question 1

Write the programs in Java to display the following pattern:

1
2 1
3 21
4 321
5 4321

public class KboatPattern


{
public void displayPattern() {
for (int i = 1; i <= 5; i++) {
for (int j = i; j >= 1; j--) {
System.out.print(j + " ");
}
System.out.println();
}
}
}

Output
Question 2

Write the programs in Java to display the following pattern:

1 2345
1 234
1 23
1 2
1

public class KboatPattern


{
public void displayPattern() {
for (int i = 5; i >= 1; i--) {
for (int j = 1; j <= i; j++) {
System.out.print(j + " ");
}
System.out.println();
}
}
}

Output
Question 3

Write the programs in Java to display the following pattern:

5 4321
5 432
5 43
5 4
5

public class KboatPattern


{
public void displayPattern() {
for (int i = 1; i <= 5; i++) {
for (int j = 5; j >= i; j--) {
System.out.print(j + " ");
}
System.out.println();
}
}
}

Output
Question 4

Write the programs in Java to display the following pattern:

1 3579
1 357
1 35
1 3
1

public class KboatPattern


{
public void displayPattern() {
for (int i = 9; i >= 1; i -= 2) {
for (int j = 1; j <= i; j += 2) {
System.out.print(j + " ");
}
System.out.println();
}
}
}

Output
Question 5

Write the programs in Java to display the following pattern:

5
5 4
5 43
5 432
5 4321

public class KboatPattern


{
public void displayPattern() {
for (int i = 5; i >= 1; i--) {
for (int j = 5; j >= i; j--) {
System.out.print(j + " ");
}
System.out.println();
}
}
}

Output
Question 6

Write the programs in Java to display the following pattern:

1 2345
2 345
3 45
4 5
5

public class KboatPattern


{
public void displayPattern() {
for (int i = 1; i <= 5; i++) {
for (int j = i; j <= 5; j++) {
System.out.print(j + " ");
}
System.out.println();
}
}
}

Output
Question 7

Write the programs in Java to display the following pattern:

9 9 9 9 9
7 7 7 7 7
5 5 5 5 5
3 3 3 3 3
1 1 1 1 1

public class KboatPattern


{
public void displayPattern() {
for (int i = 9; i >= 1; i -= 2) {
for (int j = 1; j <= 5; j++) {
System.out.print(i + " ");
}
System.out.println();
}
}
}

Output
Question 8

Write the programs in Java to display the following pattern:

9
7 9
5 79
3 579
1 3579

public class KboatPattern


{
public void displayPattern() {
for (int i = 9; i >= 1; i -= 2) {
for (int j = i; j <= 9; j += 2) {
System.out.print(j + " ");
}
System.out.println();
}
}
}

Output
Question 9

Write the programs in Java to display the following pattern:

9
9 7
9 75
9 753
9 7531

public class KboatPattern


{
public void displayPattern() {
for (int i = 9; i >= 1; i -= 2) {
for (int j = 9; j >= i; j -= 2) {
System.out.print(j + " ");
}
System.out.println();
}
}
}

Output
Question 10

Write the programs in Java to display the following pattern:

1
23
456
7 8 9 10
11 12 13 14 15

public class KboatPattern


{
public void displayPattern() {
int term = 1;
for (int i = 1; i <= 5; i++) {
for (int j = 1; j <= i; j++) {
System.out.print(term++ + " ");
}
System.out.println();
}
}
}

Output

You might also like