Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
24 views

Lab Assignment 2

Uploaded by

Bismit Maharana
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views

Lab Assignment 2

Uploaded by

Bismit Maharana
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

CSE 1001: Introduction to Computer Programming

Programming Assignment-II
(Elementary Programming)

Using Keyboard Input

Question-1:
Write a java program that reads a Fahrenheit degree in a double value from the console, then
converts it to Celsius and displays the result. The formula for the conversion is as follows:
Celsius = (Fahrenheit-32) *(5/9)
Hint: Hint: In Java, 5 / 9 is 0, but 5.0 / 9 is 0.556
Here is a sample run:
Enter a degree in Fahrenheit: 54
54 Fahrenheit is 12.22 Celsius

Question-2:
The distance between two cities (in km.) is input through the keyboard. Write a java program
to convert and print this distance in meters, feet, inches and centimetres.

Hint:
1km=1000meter, 1km=3280.8399feet, 1km= 39370.0787 inch,
1km= 100000 centimetre

Here is the sample run:

Enter the distance in km=165

165 km is 165000 meters


165 km is 541338.5835 feet
165 km is 6496062.9854999995 inch
165 km is 16500000 centimetres

Question-3:
Enter the basic salary of an employee of an organization through the keyboard. His dearness
allowance (DA) is 40% of basic salary, and house rent allowance (HRA) is 20% of basic
salary. Write a java program to calculate his gross salary. Print the DA, HRA and Gross
salary.
Here is the sample run:
Enter basic salary: 15600
DA is 6240.0
HRA is 3120.0
Gross salary is 24960

Question-4:
Write a java program that reads an integer between 0 and 1000 and adds all the digits in the
integer. For example, if an integer is 749, the sum of all its digits is 20.
Hint: Use the % operator to extract digits, and use the /
operator to remove the extracted digit.
For instance, 749 % 10 = 9 and 749 / 10 = 74.
Here is a sample run:
Enter a number between 0 and 1000: 999 The sum of the
digits is 27

Question-5:
Write a java program that reads the radius of a hemisphere and computes the surface area
and volume using the following formulas:
Surface Area of Hemisphere = 3 π r 2. Volume of a hemisphere = (2/3)πr 3
Where π is a constant whose value is equal to 3.14 approximately. “r” is the radius of the
hemisphere. Hint: Use Math.PI.

Here is a sample run:


Enter the radius of the hemisphere: 7.0
The surface area of the hemisphere is 461.814
The volume area of the hemisphere is 718.377

Question-6:
When a brick is dropped from a tower, it falls faster and faster until it hits the earth. The
distance it travels is given by d = (1/2) gt 2
Here d is in feet, t is the time in seconds, and g is 32.174.
Write a program that asks the user for the number of seconds and then prints out the distance
travelled.

Here is the sample run:


Enter the number of seconds: 5.4
Distance travelled: 469.096
Question-7:
Write a java program that displays the following table. Cast floating-point numbers into
integers.
a b pow(a, b)
1 2 1
2 3 8
3 4 81
4 5 1024
5 6 15625

Home Assignment (Using Keyboard Input)


Question-1:
Write a java program that prompts the user to enter the minutes (e.g., 1 billion), and displays
the number of years and days for the minutes.
For simplicity, assume a year has 365 days.
Here is a sample run:
Enter the number of minutes: 1000000000
1000000000 minutes is approximately 1902 years and 214 days.

Question -2:
If you have N eggs, then you have N/12 dozen eggs, with N%12 eggs left over. (This is
essentially the definition of the / and % operators for integers.) Write a java program that asks
the user how many eggs she has and then tells the user how many dozen eggs she has and how
many extra eggs are left over. A gross of eggs is equal to 144 eggs. Extend your program so
that it will tell the user how many gross, how many dozen, and how many left over eggs she
has. For example, if the user says that she has 1342 eggs, and then your program would respond
with Your number of eggs is 9 gross, 3 dozen, and 10.

Question-3:
Write a java program that prompts the user to enter three points (x1, y1), (x2, y2), (x3, y3) of
a triangle and displays its area. The formula for computing the area of a triangle is
s = (side1 + side2 + side3)/2;

area=√𝑠 ∗ (𝑠 − 𝑎) ∗ (𝑠 − 𝑏) ∗ (𝑠 − 𝑐)
Here is a sample run:
Enter three points for a triangle: 1.5 -3.4 4.6 5 9.5
-3.4
The area of the triangle is 33.6
Using Command-Line Argument

Question-1:
Write a java program that takes two int values from the command line as dividend and divisor
and print the quotient and remainder.

Question-2:
Write a java program that takes two positive integers from command-line arguments and
prints the result of first number raise to the power of second number.

Question-3:
Write a java program that prints the sum of two random integers between 1 and 6 (such as
you might get when rolling dice)

Question-4:
Write a java program that takes a double value t from the command line and prints the value
of cos (5t) + sin (7t). Use Math.cos() and math.sin()

Question-5:
Write a java program that takes three int values from the command line and prints them in
ascending order. Use Math.min() and Math.max().

Question-6:
Write a java program to input a character from command line and display the ASCII value of
the entered character.

Question-7:
Write a java program that takes three positive integers from command-line arguments and
prints true if any one of them is less than or equal to the product of the other two and false
otherwise.
Home Assignment (Using Command-Line Argument)

Question-1:
Write a java program to take three inputs from command line argument as principle, rate and
time. Find Simple interest.

Question-2:
The surface area of a cylinder can be defined as A= πr 2+2πrh, where r and h are the radius
height of the cylinder respectively. Write a java program to find the area where r and h are
inputted from command line argument. Hint: Use Math.PI.

Question-3:
Write a java program to input a four-digit number from command line argument and find sum
of the first and last digit of the number.

You might also like