Programs of Chapter 7
Programs of Chapter 7
"input from the user", it usually refers to taking input using either:
1. Scanner class
OR
2. BufferedReader class
Program 5.
import java.util.Scanner;
else
}
}
Program 6.
import java.util.Scanner;
// Input
} else {
} else {
}
🔍 Explanation:
OR
It is divisible by 400.
Program 7.
import java.util.Scanner;
public class LargestSmallest {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter first number: ");
int a = sc.nextInt();
System.out.print("Enter second number: ");
int b = sc.nextInt();
System.out.print("Enter third number: ");
int c = sc.nextInt();
int largest, smallest;
if (a >= b && a >= c) {
largest = a;
} else if (b >= a && b >= c) {
largest = b;
} else {
largest = c;
}
if (a <= b && a <= c) {
smallest = a;
} else if (b <= a && b <= c) {
smallest = b;
} else {
smallest = c;
}
System.out.println("Largest number: " + largest);
System.out.println("Smallest number: " + smallest);
}
}
🔍 Explanation:
Program 9.
import java.util.Scanner;
public class checknumber
{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.println("1. Palindrome number");
System.out.println("2. Perfect number");
System.out.print("Enter your choice: ");
int choice = in.nextInt();
System.out.print("Enter number: ");
int num = in.nextInt();
switch (choice) {
case 1:
int copyNum = num;
int revNum = 0;
while(copyNum != 0) {
int digit = copyNum % 10;
copyNum /= 10;
revNum = revNum * 10 + digit;
}
if (revNum == num)
System.out.println(num + " is palindrome");
else
System.out.println(num + " is not palindrome");
break;
case 2:
int sum = 0;
for (int i = 1; i <= num / 2; i++) {
if (num % i == 0) {
sum += i;
}
}
if (num == sum)
System.out.println(num + " is a perfect number");
else
System.out.println(num + " is not a perfect number");
break;
default:
System.out.println("Incorrect Choice");
break;
}
}
}
Program 17.
import java.util.Scanner;
public class KboatBuzzGCD
{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.println("1. Buzz number");
System.out.println("2. Calculate GCD");
System.out.print("Enter your choice: ");
int choice = in.nextInt();
switch (choice) {
case 1:
System.out.print("Enter a number: ");
int num = in.nextInt();
if (num % 10 == 7 || num % 7 == 0)
System.out.println(num + " is a Buzz Number");
else
System.out.println(num + " is not a Buzz Number");
break;
case 2:
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);
break;
default:
System.out.println("Incorrect Choice");
break;
}
}
}
Example Program :-
The electricity board charges the bill according to the number of units consumed and the rate as
given below:
Write a program in Java to accept the total units consumed by a customer and calculate the bill.
Assume that a meter rent of Rs. 500 is charged from the customer.