0% found this document useful (0 votes)
97 views

Programming Assignment 3

The document contains 10 questions related to programming in Java using conditional statements. The questions cover topics like checking voter eligibility based on age, comparing water consumption to doctor's advice, determining order of numbers, guessing games, checking for leap years, calculating electricity bills, identifying quadrants or axes of points, finding largest and second largest numbers, grading exam scores using switch-case, and playing a rock paper scissors game. It also includes 5 homework questions involving generating random numbers, converting days to future dates, displaying month names, checking number divisibility, and appropriately addressing people based on gender and other attributes.

Uploaded by

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

Programming Assignment 3

The document contains 10 questions related to programming in Java using conditional statements. The questions cover topics like checking voter eligibility based on age, comparing water consumption to doctor's advice, determining order of numbers, guessing games, checking for leap years, calculating electricity bills, identifying quadrants or axes of points, finding largest and second largest numbers, grading exam scores using switch-case, and playing a rock paper scissors game. It also includes 5 homework questions involving generating random numbers, converting days to future dates, displaying month names, checking number divisibility, and appropriately addressing people based on gender and other attributes.

Uploaded by

Suraj Kushwaha
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

CSE 1001: Introduction to Computer Programming

Programming Assignment-III
(Conditional Statements)
Question-1:
Write a program to input the age of a person and check if the age of the person is
greater than or equal to 18 then print the message:
“You are eligible to cast your vote”.

Question-2:
Alice visited SUM hospital to get treatment for her fever and illness. Doctor advised
her to drink at least 5000 ml of water each day. Alice drank x ml of water today. Write
a program that print the following message depending on the value of x.

“Yes, Alice is following doctor’s advice”


OR
“No, Alice is not following doctor’s advice”

Question-3:
Write a program that reads three integers from the user and prints "Increasing" if the
numbers are in increasing order, "Decreasing" if the numbers are in decreasing order,
and "Neither increasing nor decreasing order" otherwise.

Here is sample run:

Input first number: 241


Input second number:345
Input third number: 4563
“Increasing”

Input first number: 345


Input second number:145
Input third number: 563
“Neither Increasing nor decreasing”

Input first number: 45


Input second number:14
Input third number: 3
“Decreasing”
Question-4:
Make a simple game involving a computer and a user. The computer first guesses a
number between 1 and 9 inclusive, then ask the user to enter a number between 1 and
9 inclusive. If the user guess is correct then display “You got it right”, if the guess is
close (+1, -1) “Almost got it “, Otherwise “You got it wrong”.

Here are some sample runs.

Enter user number: 2


Computer guesses: 3
“Almost got it”

Enter user number: 4


Computer guesses: 4
“You got it right”

Enter user number: 1


Computer guesses: 5
“You got it wrong”
Question-5:
Write a Java program that takes a year from user and print true if that year is a leap year
otherwise print false.
Here is a sample run:

Input the year: 2016


2016 is a leap year: true

Input the year: 2008


2008 is a leap0 year: false

Input the year: 1900


1900 is a leap year: false

Question-6:
Write a java program to calculate the monthly electricity bill. The tariff is given as
follows:
Price per unit Unit range
Rs. 3/- First 50 units
Rs. 4.80/- 50-200 units
Rs. 5.80/- 200-400 units
Rs. 6.20/- Above 400 units
Question-7:
From the above question no. (6) write a java program with a choice if the consumer
wants to pay bill online. Consumer who pays their electricity bill online will get a
discount of 3%.
Here is the sample output:
No. of units consumed: 867
Do you want to pay online(y/n): y
Total amount: 4925.4
Discount: 147.762
Amount payable: 4777.638

Question-8:
Write a java program that takes the x – y coordinates of a point in the Cartesian plane
and prints a message telling either an axis on which the point lies or the quadrant in
which it is found.
Here is the sample output:
(-1.0, -2.5) is in quadrant III
(0.0, 4.8) is on the y-axis

Question-9:
Write a program to input 3 integer number a, b, c. Find the largest number among 3.
Also find the 2nd largest number among 3. Here is the sample output:
Enter the value of a, b, c:10 30 50
Largest number: 50
2nd largest number: 30

Question-10:
A University conducts a 100-mark exam for its student and grades them as follows.
Assigns a grade based on the value of the marks. Write a java program to print the
grade according to the mark secured by the student. [Use switch-case].

Mark Range Letter Grade


>=90 O
>=80 AND <90 A
>=70 AND <80 B
>=60 AND <70 C
>=50 AND <60 D
>=50 AND <40 E
<40 F
HOME ASSIGMENT
Question-1:
Write a java program that plays the popular scissor-rock-paper game. (A scissor can cut
a paper, a rock can knock a scissor, and a paper can wrap a rock.) The program
randomly generates a number 0, 1, or 2 representing scissor, rock, and paper. The
program prompts the user to enter a number 0, 1, or 2 and displays a message indicating
whether the user or the computer wins, loses, or draws.
Here are sample runs:
scissor (0), rock (1), paper (2): 1
The computer is scissor. You are rock. You won
scissor (0), rock (1), paper (2): 2
The computer is paper. You are paper too. It is a draw

Question-2:
Write a java program that prompts the user to enter an integer for today’s day of the
week (Sunday is 0, Monday is 1… and Saturday is 6). Also prompt the user to enter
the number of days after today for a future day and display the future day of the week.
Here is a sample run:
Enter today's day: 1
Enter the number of days elapsed since today: 3
Today is Monday and the future day is Thursday
Enter today's day: 0 Enter the number of days elapsed
since today: 31
Today is Sunday and the future day is Wednesday

Question-3:
Write a java program that randomly generates an integer between 1 and 12 and displays
the English month name January, February… December for the number 1, 2… 12,
accordingly.
Question-4:
Write a java program that prompts the user to enter an integer and determines whether
it is divisible by 5 and 6, whether it is divisible by 5 or 6, and whether it is divisible
by 5 or 6, but not both.
Here is a sample run of this program:
Enter an integer: 10
Is 10 divisible by 5 and 6? false
Is 10 divisible by 5 or 6? true
Is 10 divisible by 5 or 6, but not both? True

Question-5:
Write a java program which displays an appropriate name for a person, using a
combination of nested ifs and compound conditions. Ask the user for a gender, first
name, last name and age. If the person is female and 20 or over, ask if she is married.
If so, display "Mrs." in front of her name. If not, display "Ms." in front of her name. If
the female is under 20, display her first and last name. If the person is male and 20 or
over, display "Mr." in front of his name. Otherwise, display his first and last name. Note
that asking a person if they are married should only be done if they are female and 20
or older, which means you will have a single if and else nested inside one of your if
statements. Also, did you know that with an if statements (or else), the curly braces are
optional when there is only one statement inside?
What is your gender (M or F): F
First name: Gita
Last name: Pattanayak
Age: 32
Are you married, Gita (y or n)? y
Then I shall call you Mrs. Gita Pattanayak.
What is your gender (M or F): F
First name: Anjali
Last name: Mishra
Age: 48
Are you married, Anjali (y or n)? n
Then I shall call you Ms. Anjali.
What is your gender (M or F): M
First name: Ashok
Last name: Mohanty
Age: 23
Then I shall call you Mr. Ashok.
What is your gender (M or F): M
First name: Rahul
Last name: Pati
Age: 15
Then I shall call you Rahul Pati

***********************

You might also like