Chapter 4:Operators in Java
Unsolved Programmes
1)Write a program to find and display the value of the given
expression:
A. (x + 3) / 6 - (2x + 5) / 3;
taking the value of x = 5
public class Expression
{
public static void main(String args[]) {
int x = 5;
double value = ((x + 3) / 6.0) - ((2 * x + 5) / 3.0);
System.out.println("Result = " + value);
}
}
B. a2 + b2 + c2 / abc;
taking the values a=5, b=4, c=3
public class Expression
{
public static void main(String args[]) {
int a = 5, b = 4, c = 3;
double value = (a * a + b * b + c * c) / (double)(a * b * c);
System.out.println("Result = " + value);
}
}
2) A person is paid ₹350 for each day he works and fined ₹30 for
each day he remains absent. Write a program to calculate and
display his monthly income, if he is present for 25 days and
remains absent for 5 days.
public class Salary
{
public static void main(String args[]) {
int salary = 25 * 350;
int fine = 5 * 30;
int netSalary = salary - fine;
System.out.println("Monthly Income = " + netSalary);
}
}
3) In a competitive examination, there were 150 questions. One
candidate got 80% correct and the other candidate 72% correct.
Write a program to calculate and display the correct answers
each candidate got.
public class CompetitiveExam
{
public static void main(String args[]) {
int totalQuestions = 150;
int c1 = (int)(80 / 100.0 * totalQuestions);
int c2 = (int)(72 / 100.0 * totalQuestions);
System.out.println("Correct Answers of Candidate 1 = " + c1);
System.out.println("Correct Answers of Candidate 2 = " + c2);
}
}
4) Write a program to find and display the percentage difference,
when a number is updated from 80 to 90
public class PercentIncrease
{
public static void main(String args[]) {
int orgNum = 80;
int newNum = 90;
int inc = newNum - orgNum;
double p = inc / (double)orgNum * 100;
System.out.println("Percentage Difference = " + p + "%");
}
}
5) The normal temperature of human body is 98.6°F. Write a
program to convert the temperature into degree Celsius and
display the output.
Hint: c / 5 = f - 32 / 9
public class Celsius
{
public static void main(String args[]) {
double f = 98.6;
double c = 5 * (f - 32) / 9.0;
System.out.println("Temperature in Degree Celsius = " + c);
}
}
6) The angles of a quadrilateral are in the ratio 3:4:5:6. Write a
program to find and display all of its angles. [Hint: The sum of
angles of a quadrilateral = 360°]
public class QuadRatio
{
public static void main(String args[]) {
int r1 = 3, r2 = 4, r3 = 5, r4 = 6;
double x = 360 / (double)(r1 + r2 + r3 + r4);
double a = r1 * x;
double b = r2 * x;
double c = r3 * x;
double d = r4 * x;
System.out.println("Angle A = " + a);
System.out.println("Angle B = " + b);
System.out.println("Angle C = " + c);
System.out.println("Angle D = " + d);
}
}
5. INPUT IN JAVA
1.
import java.util.Scanner;
public class SimplePendulum
{
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 Gravity: ");
double g = in.nextDouble();
double t = 2 * 3.14159 * Math.sqrt(l / g);
System.out.println("Time Period = " + t);
in.close();
}
}
Output
Output
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
15% of Basic Pay
(HRA)
12.5% of Basic
Provident Fund (PF)
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 static void main(String args[]) {
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
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 CameraPrice
{
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
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 Discounts
{
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
5.
import java.util.Scanner;
public class CompoundInterest
{
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
6
public class Shares
{
public static void main(String args[]) {
int sharesHeld = (2000 * 100)/(10 * 10);
System.out.println("No. of shares held currently = "
+ sharesHeld);
int sharesRequired = 3000 - sharesHeld;
System.out.println("No. of shares to purchase = "
+ sharesRequired);
}
}
Output
7
Write a program to input time in seconds. Display the time after converting
them into hours, minutes and seconds.
Sample Input: Time in seconds: 5420
Sample Output: 1 Hour 30 Minutes 20 Seconds
import java.util.Scanner;
public class Time
{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.print("Time in seconds: ");
int inputTime = in.nextInt();
int hrs = inputTime / 3600;
int mins = (inputTime % 3600) / 60;
int secs = (inputTime % 3600) % 60;
System.out.println(hrs
+ " Hours "
+ mins
+ " Minutes "
+ secs
+ " Seconds");
}
}
Output
8
import java.util.Scanner;
public class NumberSwap
{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.println("Enter two unequal numbers");
System.out.print("Enter first number: ");
int a = in.nextInt();
System.out.print("Enter second number: ");
int b = in.nextInt();
a = a + b;
b = a - b;
a = a - b;
System.out.println("a = " + a + " b = " + b);
}
}
Output
9
import java.util.Scanner;
public class InterestDifference
{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.print("Enter Amount: ");
double p = in.nextDouble();
double si = p * 10 * 3 / 100;
double ciAmt = p * Math.pow(1 + (10/100.0), 3);
double ci = ciAmt - p;
System.out.print("Difference between CI & SI: " + (ci - si));
}
}
10
import java.util.Scanner;
public class Shopkeeper
{
public static void main(String args[])
{
Scanner in = new Scanner(System.in);
System.out.print("Enter the selling price: ");
double sp = in.nextDouble();
double cp1 = (sp / (1 + (20 / 100.0)));
double cp2 = (sp / (1 - (20 / 100.0)));
double totalCP = cp1 + cp2;
System.out.println("Total Cost Price = " + totalCP);
}
}
Output
Chapter 6
QUESTION 1
import java.util.Scanner;
public class GreatestNumber
{
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);
}
}
QUESTION 2
import java.util.Scanner;
public class Hypotenuse
{
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);
}
}
QUESTION 3
import java.util.Scanner;
public class MathMethods
{
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());
}
}
Question 4
import java.util.Scanner;
public class AvgMarks
{
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);
}
}
QUESTION 5
import java.util.Scanner;
public class CircleRadius
{
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);
}
}