0% found this document useful (0 votes)
7 views7 pages

Programs of Chapter 7

The document provides several Java programs that demonstrate user input handling using the Scanner class. Key programs include checking divisibility by 7 and 8, determining leap years, finding the largest and smallest among three numbers, checking for palindromes and perfect numbers, and calculating GCD. Additionally, it includes an example program for calculating an electricity bill based on units consumed.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views7 pages

Programs of Chapter 7

The document provides several Java programs that demonstrate user input handling using the Scanner class. Key programs include checking divisibility by 7 and 8, determining leap years, finding the largest and smallest among three numbers, checking for palindromes and perfect numbers, and calculating GCD. Additionally, it includes an example program for calculating an electricity bill based on units consumed.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Important Note :

"input from the user", it usually refers to taking input using either:

1. Scanner class

OR

2. BufferedReader class

Chapter – 7 Important Programs (Page 127 and 128)

Program 5.

import java.util.Scanner;

public class Check

public static void main(String[] args)

Scanner sc = new Scanner(System.in);

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

int num = sc.nextInt();

if (num % 7 == 0 && num % 8 == 0)

System.out.println(num + " is divisible by both 7 and 8.");

else

System.out.println(num + " is NOT divisible by both 7 and 8.");

}
}

Program 6.

import java.util.Scanner;

public class LeapYearCheck {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

// Input

System.out.print("Enter a 4-digit year: ");

int year = sc.nextInt();

// Check if it's a 4-digit year

if (year >= 1000 && year <= 9999) {

// Leap year condition

if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) {

System.out.println(year + " is a Leap Year.");

} else {

System.out.println(year + " is NOT a Leap Year.");

} else {

System.out.println("Invalid input! Please enter a 4-digit year.");

}
🔍 Explanation:

A leap year occurs if:

It is divisible by 4 and not divisible by 100,

OR

It is divisible by 400.

Program 7.

Java Program: Find Largest and Smallest Among Three Numbers

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:

 Uses Scanner to take input from the user.


 Compares the three numbers using if...else to find the largest and smallest.
 Works for equal numbers too.

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:

Units Consumed Rate Per Unit

First 100 units 80 Paisa per unit

Next 200 units Rs. 1 per unit

Above 300 units Rs. 2.50 per unit

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.

You might also like