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

Conditional Programs (If, If-Else, Switch)

Uploaded by

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

Conditional Programs (If, If-Else, Switch)

Uploaded by

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

Level : BSIT Course Name: Programming Fundamentals

Course Code : COSC-1101 Session : 2024-2028 (Fall 2024)

Conditional Programs (if, if-else, if-else-if, Nested-if, switch)


Simple-if
1- Write a program that request the user to type a number, if number is not -999 then print its triple.
2- Write a program that request the user to type 5 numbers, and then find how many numbers are b/w 1
and 100.
3- Write a program that should print the even numbers b/w 10 and 20 (both inclusive) by using if
condition. (NOTE: Must use Increment Operator).
4- Write a program that should print the odd numbers b/w 25 and 15 (both inclusive) by using if
condition. (NOTE: Must use Decrement Operator). (simple-if)
5- Write a program that request the user to enter 2 integers, and then find whether first integer is
successor or predecessor of second integer.
6- Write a program that request the user to enter 2 integers, and then find whether second integer is
successor or predecessor of first integer.
7- Write a program that requests the user to enter 5 numbers, and then find and display the maximum
number. Also display the name of variable which is maximum number.
8- Write a program that requests the user to enter 5 numbers, and then find and display the minimum
number. Also display the name of variable which is minimum number.
If-else
9- Write a program that request the user to type a number, and then find and display whether it is an
Armstrong number or not. (Example: 153 is an Armstrong because its digits square’s sum is
equal to 153 (12+52+32=153))
10- Write a program that requests the user to type an integer and writes “YOU WIN” if the value is between
52 and 88 (both included). In the other case it writes “YOU LOSE”
11- Write a program that request the user to enter 2 integers, and then find whether both of the
numbers are consecutive to each other or not.
12- Write a program that read a Character from keyboard and check whether it is in Uppercase or not by
using 2 different methods. (Note: ASCII for Uppercase letters is from 65 to 90)
13- Write a program that read a Character from keyboard and check whether it is in Lowercase or not by
using 2 different methods. (Note: ASCII for Lowercase letters is from 97 to 122)
14- Write a program that request the user to enter a number, and then find and display whether it is even
or odd by using 4 different methods.
15- Write a program that requests the user to enter 2 numbers, and then find and display whether
both are consecutive to each other or not.
If-else-if
16- Write a program that is able to compute some operations on an integer.
The program writes the value of the integer and writes the following menu :
1. Add 1
2. Multiply by 2
3. Subtract 4
4. Quit
The programs ask the user to type a value between 1 and 4.
If the user types a value from 1 to 3 the operation is computed, the integer is written and the menu is
displayed again. If the user types 4, the program quits.
Nested-if
17- Write a program about Coffee drinking habits based on the following criteria:
• 0 cups = Tea drinker
• 1-5 cups = Coffee Drinker
• More than 5 cups = Normal
Copy the function down and check that is works.
Name Cups of Coffee Description
A 0 Tea Drinker
B 6 Coffee Drinker
C 3 Normal
Think carefully about how to structure the if function.
Try modifying the if function so that if Cups of coffee is a negative number you see an appropriate
error message.
Name Cups of Coffee Description
A -1 You can’t have less than 0 cups!
B 6 Coffee Drinker
Switch
18- Write a program to get a month number from user, then compute and display how many days it has.
(Use 2 different methods to solve it)
19- Write a program that request the user to enter a number, and then find and display whether it is even
or odd by using switch statement.
20- Write a program that request the user to enter a year, and then find and display whether it is leap year
or not.
21- On your phone keypad, the alphabets are mapped to digits as follows: ABC(2), DEF(3), GHI(4), JKL(5),
MNO(6), PQRS(7), TUV(8), WXYZ(9). WAP called PhoneKeyPad, which prompts user for a digit b/w 2
and 9 (both inclusive) and converting to a sequence of Strings.
22- A sequence of six tests, all of scored out of 100, are to be given different weightings in determining a
final mark. WAP fragment that computes the appropriate weighted score for one test. The fragment
should first read values of testNumber and score. Using a switch statement, it should then compute
and print the appropriate value of weightedScore using the weightings given in the following table.

Test Number Weight


1 10%
2 20%
3 20%
4 15%
5 15%
6 20%

For Example, input of 3 and 27 should produce the following output:


A score of 27 on test 3 gives a weighted score of 5.4

*****Hard worker never worries & finds fruitful result always*****

You might also like