Professional Documents
Culture Documents
Input in Java: Name The Following
Input in Java: Name The Following
Input In Java
I. Name the following
Question 1
Question 2
Question 3
Question 4
Question 5
Question 1
Question 2
Question 4
to accept a fraction value 'n' in double data type through Stream Class
Question 5
Question 6
Question 1
Ans.
nextInt( )
It is a Scanner class method which is used to input an integer type data from the
standard input device.
nextFloat( )
It is a Scanner class method which is used to input floating type data from the standard
input device.
Question 2
Question 1
Question 2
Question 3
Question 4
Question 5
Question 6
Question 7
Write down the syntax to input a character through scanner class with an example.
Question 8
Question 9
What are the different types of errors that take place during the execution of a program?
Ans. Logical errors and Run-Time errors occur during the execution of the program.
Question 10
Distinguish between:
(a) Testing and Debugging
Ans.
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.
Ans.
import java.util.Scanner;
class Simple_Pendulum
{
public static void main(String args[])
{
double l, g, t=0.0;
Scanner in = new Scanner(System.in);
System.out.print("Enter length: ");
l = in.nextDouble();
System.out.print("Enter gravity: ");
g = in.nextDouble();
t = 2 * (22.0 / 7.0) * Math.sqrt(l/g);
System.out.println("Time period = " + t);
}
}
Question 2
Write a program by using class 'Employee' to accept Basic Pay of an employee. Calculate the
allowances/deductions as given below.
import java.util.Scanner;
class Employee
{
public static void main(String args[])
{
double bp, da=0.0, hra=0.0, pf=0.0, gp=0.0, np=0.0;
Scanner in = new Scanner(System.in);
System.out.print("Enter Basic Pay: ");
bp = in.nextDouble();
da = 0.3 * bp;
hra = 0.15 * bp;
pf = 0.125 * bp;
gp = bp + da + hra;
np = gp - pf;
System.out.println("Gross Pay = " + gp);
System.out.println("Net Pay = " + np);
}
}
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.
Ans.
import java.util.Scanner;
class Digital_Camera
{
public static void main(String args[])
{
double mrp, d=0.0, p=0.0, gst=0.0, netp=0.0;
Scanner in = new Scanner(System.in);
System.out.println("Enter printed price of Digital Camera:");
mrp = in.nextDouble();
d = mrp * 10 / 100.0;
p = mrp - d;
gst = p * 6 / 100.0;
netp=p+gst;
System.out.println("Amount to be paid: " + netp);
}
}
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.
Ans.
import java.util.Scanner;
class Discounts
{
public static void main(String args[])
{
double p, d1=0.0,amt1=0.0,d2=0.0,amt2=0.0,d3=0.0,amt3=0.0;
Scanner in = new Scanner(System.in);
System.out.print("Enter price of article: ");
p = in.nextDouble();
d1 = p * 30 / 100.0;
amt1 = p - d1;
System.out.println("30% discount = " + d1);
System.out.println("Amount after 30% discount = " + amt1);
d2 = p * 20 / 100.0;
amt2 = p - d2;
d3 = amt2 * 10 / 100.0;
amt2= amt2 - d3;
System.out.println("20% discount = " + d2);
System.out.println("10% discount = " + d3);
System.out.println("Amount after successive discounts = "+amt2);
}
}
Question 5
Mr. Agarwal invests certain sum at 5% per annum compound interest for three years. Write a
program in Java to calculate:
Ans.
import java.util.Scanner;
class CompoundInterest
{
public static void main(String args[])
{
double p, ci=0.0;
Scanner in = new Scanner(System.in);
System.out.print("Enter principal amount: ");
p = in.nextDouble();
ci = p * 5 * 1 / 100.0;
System.out.println("Interest for the first year = " + ci);
p = p + ci;
ci = p * 5 * 1 / 100.0;
System.out.println("Interest for the second year = " + ci);
p = p + ci;
ci = p * 5 * 1 / 100.0;
System.out.println("Interest for the third year = " + ci);
}
}
Question 6
class Shares
{
public static void main(String args[])
{
int sh,sr;
sh = (2000 * 100)/(10 * 10);
System.out.println("No. of shares held currently="+ sh);
sr = 3000 - sh;
System.out.println("No. of shares to purchase="+ sr);
}
}
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
Ans.
import java.util.Scanner;
class Time
{
public static void main(String args[])
{
long s, h=0, m=0;
Scanner in = new Scanner(System.in);
System.out.print("Enter time in seconds: ");
s = in.nextLong();
h = s / 3600;
s = s % 3600;
m = s / 60;
s= s % 60;
System.out.println(h+" Hours "+m+" Minutes "+s+" Seconds");
}
}
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
Ans.
import java.util.Scanner;
class Swap
{
public static void main(String args[])
{
int a,b;
Scanner in = new Scanner(System.in);
System.out.println("Enter two unequal numbers");
System.out.print("Enter first number: ");
a = in.nextInt();
System.out.print("Enter second number: ");
b = in.nextInt();
a = a + b;
b = a - b;
a = a - b;
System.out.println(" a = " + a + " b= " + b);
}
}
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
Ans.
import java.util.Scanner;
class Compound
{
public static void main(String args[])
{
double P,SI=0.0, A=0.0, CI=0.0,;
Scanner in = new Scanner(System.in);
System.out.print("Enter principal Amount: ");
P = in.nextDouble();
SI = P * 10 * 3 / 100;
A = P * Math.pow(1 + (10/100.0), 3);
CI = A - P;
System.out.print("Difference between CI & SI: "+(CI - SI));
}
}
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.
Hint: CP = (SP / (1 + (profit / 100))) (when profit)
CP = (SP / (1 - (loss / 100))) (when loss)
Ans.
import java.util.Scanner;
class Shopkeeper
{
public static void main(String args[])
{
double sp, cp
Scanner in = new Scanner(System.in);
System.out.print("Enter the selling price: ");
sp = in.nextDouble();
cp1 = (sp / (1 + (20 / 100.0)));
cp2 = (sp / (1 - (20 / 100.0)));
tCP = cp1 + cp2;
System.out.println("Total Cost Price = " + tCP);
}
}