Chapter 4 (unit 3 unsolved programs)
Input using Scanner class
Write a program in Java to input two numbers (say, n and m)
using the Scanner class. Display the results of n raised to the
power m (nm) and mn.
import java.util.*;
public class Exponent{
public static void main(String args[]){
int n, m;
double expo;
Scanner in = new Scanner(System.in);
System.out.print("Enter a number (n): ");
n = in.nextInt();
System.out.print("Enter a number (m): ");
m = in.nextInt();
expo = Math.pow(n,m);
System.out.println(n + " raised to the power "
+ m + " = " + expo);
expo = Math.pow(m,n);
System.out.println(m + " raised to the power "
+ n + " = " + expo);
}}
2) Write a program in Java to input perpendicular and base of a
right angle triangle using the Scanner class. Calculate and display
its hypotenuse using the formula given below:
h = √(p2 + b2)
import java.util.Scanne
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);
}}
3) 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 Profit
public static void main(String args[]) {
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");}}}
4) A triangle is said to be an 'Equable Triangle', if the area of the
triangle is equal to its perimeter. Write a program to enter three
sides of a triangle. Check and print whether the triangle is
equable or not.
For example, a right angled triangle with sides 5, 12 and 13 has
its area and perimeter both equal to 30.
import java.util.Scanner;
public class EquableTriangle
{ public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.println("Please enter the 3 sides of the triangle.");
System.out.print("Enter the first side: ");
double a = in.nextDouble();
System.out.print("Enter the second side: ");
double b = in.nextDouble();
System.out.print("Enter the third side: ");
double c = in.nextDouble();
double p = a + b + c;
double s = p / 2;
double area = Math.sqrt(s * (s - a) * (s - b) * (s - c));
if (area == p)
System.out.println("Entered triangle is equable.");
else
System.out.println("Entered triangle is not equable.");}}
5) A special two-digit number is such that when the sum of its
digits is added to the product of its digits, the result is equal to
the original two-digit number.
Example: Consider the number 59.
Sum of digits = 5 + 9 = 14
Product of digits = 5 * 9 = 45
Total of the sum of digits and product of digits = 14 + 45 = 59
Write a program to accept a two-digit number. Add the sum of its
digits to the product of its digits. If the value is equal to the
number input, display the message "Special 2 - digit number"
otherwise, display the message "Not a special two-digit number".
import java.util.Scanner;
public class SpecialNumber
{ public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.print("Enter a 2 digit number: ");
int orgNum = in.nextInt();
if (orgNum < 10 || orgNum > 99) {
System.out.println("Invalid input. Entered number is not a 2 digit
number");
System.exit(0);
}int num = orgNum;
int digit1 = num % 10;
int digit2 = num / 10;
num /= 10;
int digitSum = digit1 + digit2;
int digitProduct = digit1 * digit2;
int grandSum = digitSum + digitProduct;
if (grandSum == orgNum)
System.out.println("Special 2-digit number");
else
System.out.println("Not a special 2-digit number");}}
6) 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);
}}
7) A library charges a fine for returning a book late after the due
date as per the conditions given below:
No. of days Fine
First five ₹ 2.00 per
days day
Six to ten ₹ 5.00 per
No. of days Fine
days day
Above ten ₹ 10.00 per
days day
Design a program in Java assuming that a book is returned N days
late. Input the value of N using the Scanner class. Calculate and
display the fine to be paid to the library.
import java.util.*;
public class LibraryFine
public static void main(String args[])
Scanner in = new Scanner(System.in);
System.out.print("Enter the number of days : ");
int n = in.nextInt();
double fine = 0.0;
if (n <= 5)
fine = n * 2.0 ;
else if (n <= 10)
fine = 10 + (n - 5) * 5.0 ;
else
fine = 10 + 25 + (n - 10) * 10 ;
System.out.println("Fine to be paid = ₹" + fine);
}}