Input in Java
Input in Java
Input in Java
Question 1
Question 2
Question 3
Question 4
Question 5
Question 1
1|P ag e
InputStreamReader read = new InputStreamReader(System.in);
BufferedReader in = new BufferedReader(read);
int p = Integer.parseInt(in.readLine());
Question 2
Question 3
Question 4
to accept a fraction value 'n' in double data type through Stream Class
Question 5
Question 6
2|P ag e
Scanner in = new Scanner(System.in);
Question 1
nextInt( ) nextFloat( )
Scans the next token of input as an int Scans the next token of input as a float
Question 2
Syntax Errors occur when we violate the rules of writing Logical Errors occur due to our mistakes in
the statements of the programming language. programming logic.
Question 1
3|P ag e
Question 2
Question 3
Question 4
Question 5
Question 6
4|P ag e
What is a compound statement? Give an example.
Two or more statements can be grouped together by enclosing them between opening
and closing curly braces. Such a group of statements is called a compound statement.
if (a < b) {
/*
* All statements within this set of braces
* form the compound statement
*/
Question 7
Write down the syntax to input a character through scanner class with an example.
Syntax:
Example:
Question 8
5|P ag e
program which can only be resolved at runtime are called Run Time errors.
Consider the below example:
import java.util.Scanner;
class RunTimeError
{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.print("Enter a number: ");
int n = in.nextInt();
int result = 100 / n;
System.out.println("Result = " + result);
}
}
This program will work fine for all non-zero values of n entered by the user. When the
user enters zero, a run-time error will occur as the program is trying to perform an
illegal mathematical operation of division by 0. When we are compiling the program, we
cannot say if division by 0 error will occur or not. It entirely depends on the state of the
program at run-time.
Question 9
What are the different types of errors that take place during the execution of a program?
Logical errors and Run-Time errors occur during the execution of the program.
Question 10
Distinguish between:
(a) Testing and Debugging
Testing Debugging
6|P ag e
Testing Debugging
Question 1
T = 2π√(l/g)
Write a program to calculate the time period of a Simple Pendulum by taking length and
acceleration due to gravity (g) as inputs.
import java.util.Scanner;
7|P ag e
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 g: ");
double g = in.nextDouble();
double t = 2 * (22.0 / 7.0) * Math.sqrt(l/g);
System.out.println("T = " + t);
}
}
Output
Question 2
Write a program by using class 'Employee' to accept Basic Pay of an employee. Calculate the
allowances/deductions as given below.
8|P ag e
Allowance / Deduction Rate
import java.util.Scanner;
Output
9|P ag e
Question 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;
10 | P a g e
Output
Question 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;
11 | P a g e
System.out.println("Amount after 30% discount = " + amt1);
Output
Question 5
Mr. Agarwal invests certain sum at 5% per annum compound interest for three years. Write a
program in Java to calculate:
12 | P a g e
(a) the interest for the first year
(b) the interest for the second year
(c) the amount after three years.
import java.util.Scanner;
Output
13 | P a g e
Question 6
14 | P a g e
Output
Question 7
Write a program to input the 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;
Output
Question 8
Write a program to input two unequal numbers. Display the numbers after swapping their
values in the variables without using a third variable.
Sample Input: a = 23, b = 56
Sample Output: a = 56, b = 23
import java.util.Scanner;
16 | P a g e
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
Question 9
A certain amount is invested at the rate 10% per annum for 3 years. Find the difference
between Compound Interest (CI) and Simple Interest (SI). Write a program to take amount as
an input.
Hint: SI = (P * R * T) / 100
A = P * (1 + (R/100))T
CI = A - P
import java.util.Scanner;
17 | P a g e
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));
}
}
Output
Question 10
A shopkeeper sells two calculators for the same price. He earns 20% profit on one and suffers
a loss of 20% on the other. Write a program to find his total cost price of the calculators by
taking selling price as input.
18 | P a g e
Hint: CP = (SP / (1 + (profit / 100))) (when profit)
CP = (SP / (1 - (loss / 100))) (when loss)
import java.util.Scanner;
Output
19 | P a g e