One Way (Decision Structures and Boolean Logic) Two Way and Multiway (Decision Structures and Boolean Logic)
One Way (Decision Structures and Boolean Logic) Two Way and Multiway (Decision Structures and Boolean Logic)
1. Write a program that reads two integers and prints out the maximum of them.
#include <stdio.h>
int main() {
int num1, num2. maximum;
scanf("%d%d", &num1, &num2);
if (num1 > num2)
maximum =num1;
else
maximum =num2;
2. Write a program that reads the temperature and prints “cool” if it is less than 15 and “hot”
otherwise.
#include <stdio.h>
int main() {
int temp;
scanf("%d", &temp);
if (temp < 15)
printf("cool!");
else
printf("hot!");
return 0;
}
3. Write a program that reads the two numbers x and y and prints the result of x/y. If y is 0, the program
must print “Can’t divide by zero!” and exit without dividing.
#include <stdio.h>
int main() {
int x, y;
if (y != 0)
printf("%d / %d = %d\n", x, y, x/y);
else
printf("Can't divide by zero!");
return 0;
}
#include <stdio.h>
int main(void) {
int num1, num2, num3;
scanf("%d%d%d", &num1, &num2, &num3);
// Method 1:
int max = num1;
// Method 2:
if (num1 >= num2 && num1 >= num3)
printf("Max = %d", num1);
else if (num2 >= num1 && num2 >= num3)
printf("Max = %d", num2);
else
printf("Max = %d", num3);
#include <stdio.h>
int main(void) {
int num1, num2;
char op;
scanf("%d %c%d", &num1, &op, &num2);
if (op == '+')
printf("%d + %d = %d\n", num1, num2, num1 + num2);
else if (op == '-')
printf("%d - %d = %d\n", num1, num2, num1 - num2);
else if (op == '*')
printf("%d * %d = %d\n", num1, num2, num1 * num2);
else if (op == '/') {
if (num2 == 0)
printf("Can't divide by 0!\n");
else
printf("%d / %d = %d\n", num1, num2, num1 / num2);
} else
printf("Invalid operation!");
// we can also check for the validity of the operation at the beginning
// like this:
//
// if (op != '+' && op != '-' && op != '*' && op != '/') {
// printf("Invalid operation");
// return 0;
// }
return 0;
}
6. Write an equivalent if-statement without the use of && or ||.
// 1.
if (a == 0 && b == 0)
printf("both are zero!")
// Equivalent statement:
if (a == 0)
if (b == 0)
printf("both are zero!");
// 2.
if (a == 0 || b == 0)
printf("at least one of them is zero!")
// Equivalent statement:
if (a == 0)
printf("at least one of them is zero!")
else if (b == 0)
printf("at least one of them is zero!")
(A) Because there are no “else if” statements, the program will print “A+ A B” if the input is 100!
(A) Because there are no “else if” statements, the program will print “A+ F” if the input is 100!
Question 1
The vaccine of Covid-19 can be shipped in large or small containers. Larger containers can hold up to 100
vaccine bottles whereas small containers can hold up to 48 vaccine bottles.
Write a C code (or pseudocode) for a program that reads the number of the vaccine bottles to be shipped
then calculates and prints the minimum number of containers needed to ship them all
for example: if you have 250 vaccine bottles then you need 2 Large containers and 2 Small containers
Question 2
What will the following pseudocode program display? ( 0.5 mark for each)
Module main()
Declare Integer x,y
Declare Real a,b,c
Set a = 2
Set b = 7-15/5
Set c = b*2/2*10/5 - 18.0/2
If(a==b) Then
Set x= 15%4;
Else
Set x=0;
End If
Set y=10*4/5/2-(1+2)*2;
Display c
Display x
Display y
If(b>3) Then
If (x>=3) Then
Display "Good Luck"
Else
Display " Best wishes "
End If
End If
End Module
Question 3
Consider the following flow chart. Write down equivalent code in C programing language using the instructions you
learned in this course.
Question-4
The table below shows the normal boiling points of three metals in °C. Draw a flowchart write pseudocode, and
c code that takes as an argument the observed boiling point of a metal in °C and identifies the metal if the
observed boiling point is within 5% of the expected boiling point. If the data input is more than 5% higher or
lower than any of the boiling points in the table, the module should output the message Substance unknown.
Question-5
Write a pseudo-code or C Code and draw flowchart for the main module that
declares 2 integers named first and second
check if the if one of the two integers is a multiple of other