Worksheet 2

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 4

King Hussein School of Computing Sciences

Decision Structures, Repetition Structures

ACADEMIC YEAR: 2021-2022


SPRING SEMESTER 2021
OBJECTIVES
 Two way and multiway (Decision Structures and Boolean Logic)
 Repetition Structures ( for, while, and do-while)
Two way and multiway
Question 1 Write a C program to check whether a number is divisible by 5 and 11 or not, using if-else. How to
check the divisibility of any number in C programming. C program to enter any number and check whether it is
divisible by 5 and 11 or not. Logic to check the divisibility of a number in C program.

Example
Input
Input number: 55
Output
The number is divisible by 5 and 11

Question 2 Write a C program to check whether a character is an alphabet or not.


Example
Input
Input character: a

Output

'a' is alphabet

Question 3 Write a C program to input all sides of a triangle and check whether triangle is valid or not.
Hint
A triangle is valid if the sum of its two sides is greater than the third side. This means if a, b, and c are three sides of a
triangle. Then the triangle is valid if all three conditions are satisfied
a+b>c
a + c > b and
b+c>a
Example
Input
Input first side: 7
Input second side: 10
Input third side: 5

Output

Triangle is valid
Question 4 Write a C program to input electricity unit charges and calculate the total electricity bill according
to the given condition:
For first 50 units Rs. 0.50/unit
For next 100 units Rs. 0.75/unit
For next 100 units Rs. 1.20/unit
For unit above 250 Rs. 1.50/unit
An additional surcharge of 20% is added to the bill
Hint
Logic to calculate net electricity bill
Step by step descriptive logic to compute electricity bill.
1. Input unit consumed by the customer in some variable say unit.
2. If the unit consumed less or equal to 50 units. Then amt = unit * 0.50.
3. If the unit consumed more than 50 units but less than 100 units. Then add the first 50 units amount i.e. 25 to
the final amount and compute the rest 50 units amount. Which is given by amt = 25 + (unit-50) * 0.75. I have
used units-50 since I already calculated the first 50 units which is 25.
4. Similarly check the rest of the conditions and calculate the total amount.
5. After calculating total amount. Calculate the surcharge amount i.e. sur_charge = total_amt * 0.20. Add
surcharge amount to net amount. Which is given by net_amt = total_amt + sur_charge.
Question 5
Write a C program to find the sum of all odd numbers from 1 to n using for loop.
Hint
How to find the sum of all odd numbers in a given range in C programming. Logic to find the sum of odd numbers in a
given range using a loop in C programming.
Example

Example
Input
Input upper limit: 10
Output
Sum of odd numbers from 1-10: 25

Question 6
Write a program in C to input a number and check whether the number is a prime number or not using for loop.

Hint
A prime number is a whole number greater than 1 whose only factors are 1 and itself.
Example

Input

Input any number: 17

Output

17 is a prime number

Question 7
Write C code to read an integer number then find and print the sum of digits. (Using while loop)
For example,

Input

Input any number: 17

Output

Sum of digits = 18

You might also like