www.thekiranacademy.com JBKCORE9006-scanner-class-logical No.
:8888809416
Usage of scanner class with
more logical examples.
1. Calculate Average of two numbers use double as a datatype.
public class Exercise1 {
public static void main(String[] args) {
double sum = 23;
double n = 7;
double avg;
avg = sum / n;
System.out.println("Average = " + avg);
}
}
2. Understanding character datatype in java it prints ASCII code of a
characters.
public class Exercise2 {
public static void main(String[] args) {
int chr = 'Z';
System.out.println("The ASCII value of Z is :" + chr);
}
}
1|Page www.jbktest.com
www.thekiranacademy.com JBKCORE9006-scanner-class-logical No.:8888809416
3. Write a Java program to add two characters.
public class Exercise3 {
public static void main(String[] args) {
int a = 10;
char ch = 'h';
int sum = a + ch;
System.out.println(sum);
}
}
4. Write a Java program to find average of two numbers use double
datatype not int.
public class Exercise4 {
public static void main(String[] args) {
double sum =
23;double n =
7; double avg;
avg = sum / n;
System.out.println("Average = " +
avg);
}
}
5. Write a Java program for perimeter of Rectangle
public class Exercise5 {
public static void main(String[] args) {
int width = 11;
int length = 10;
int perimeter;
System.out.println("first no (length): " + length);
System.out.println("second no (width): " + width);
2|Page www.jbktest.com
www.thekiranacademy.com JBKCORE9006-scanner-class-logical No.:8888809416
perimeter = 2 * (length + width);
System.out.println("Perimeter of a rectangle is " + perimeter);
}
}
6. Write a Java program to Calculate Area of Rectangle
import java.util.Scanner;
public class Exercise6 {
static Scanner sc = new Scanner(System.in);
public static void main(String args[]) {
double length = 4.5;
double width = 8.0;
double area = length * width;
System.out.println("Area of Rectangle is:" + area);
}
7. Write a Java program to Calculate Area of Rectangle by scanner
class
import java.util.Scanner;
public class Exercise7 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter the length of Rectangle:");
double length = scanner.nextDouble();
System.out.println("Enter the width of Rectangle:");
double width = scanner.nextDouble();
// Area = length*width;
double area = length * width;
System.out.println("Area of Rectangle is:" + area);
}
}
3|Page www.jbktest.com
www.thekiranacademy.com JBKCORE9006-scanner-class-logical No.:8888809416
8. Write a Java program to calculate area and circumference of circle
import java.util.Scanner;
public class Exercise8 {
static Scanner sc = new Scanner(System.in);
public static void main(String args[]) {
System.out.print("Enter the radius: ");
/*
* We are storing the entered radius in double
* because a user can enter radius
* in decimals
*/
double radius = sc.nextDouble();
// Area = PI*radius*radius
double area = Math.PI * (radius * radius);
System.out.println("The area of circle is: " + area);
// Circumference = 2*PI*radius
double circumference = Math.PI * 2 * radius;
System.out.println("The circumference of the circle is:" +
circumference);
}
9. Write a Java program calculate area of Square
public class Exercise9 {
public static void main(String[] args) {
// Value specified in the program itself
double side = 4.5;
// Area of Square = side*side
double area = side * side;
System.out.println("Area of Square is: " + area);
}
}
4|Page www.jbktest.com
www.thekiranacademy.com JBKCORE9006-scanner-class-logical No.:8888809416
10.Write a Java program calculate area of Square by scanner
import java.util.Scanner;
public class Exercise10 {
public static void main(String[] args) {
System.out.println("Enter Side of Square:");
// Capture the user's input
Scanner scanner = new Scanner(System.in);
// Storing the captured value in a variable
double side = scanner.nextDouble();
// Area of Square = side*side
double area = side * side;
System.out.println("Area of Square is: " + area);
}
}
Homework
1. Write a program to calculate cube of a number by scanner class
2. Write a program to calculate simple interest
3. Write a program to calculate compound interest
4. The marks obtained by a student in 5 different subjects are input
through the keyboard. The student gets a division as per the following
rules:
a. Percentage above or equal to 60 - First division
b. Percentage between 50 and 59 - Second division
c. Percentage between 40 and 49 - Third division
d. Percentage less than 40 - Fail
e. Write a program to calculate the division obtained by the
student.
Download
https://drive.google.com/drive/folders/1pzCRBpGvAOhke lu8sKZL2chY
KkVzWd?usp=sharing
5|Page www.jbktest.com