0% found this document useful (0 votes)
29 views10 pages

One Way (Decision Structures and Boolean Logic) Two Way and Multiway (Decision Structures and Boolean Logic)

The document contains examples of C code programs that use if/else statements to: 1) Compare two integers and print the maximum 2) Check if a temperature is below 15 and print "cool" or "hot" 3) Check for division by zero and print an error message instead of dividing

Uploaded by

jayy yy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views10 pages

One Way (Decision Structures and Boolean Logic) Two Way and Multiway (Decision Structures and Boolean Logic)

The document contains examples of C code programs that use if/else statements to: 1) Compare two integers and print the maximum 2) Check if a temperature is below 15 and print "cool" or "hot" 3) Check for division by zero and print an error message instead of dividing

Uploaded by

jayy yy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

OBJECTIVES

 One way (Decision Structures and Boolean Logic)


 Two way and multiway (Decision Structures and Boolean Logic)
starting from if-statements

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;

printf("The maximum is: %d\n", maximum);


return 0;
}

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;

printf("Enter two numbers: ");


scanf("%d%d", &x, &y);

if (y != 0)
printf("%d / %d = %d\n", x, y, x/y);
else
printf("Can't divide by zero!");

return 0;
}

// another correct solution proposed by a student:


//
// if (y == 0) {
// printf("Can't divide by zero!\n");
// return 0;
// }
//
// printf("%d / %d = %d\n", x, y, x/y);
4. Write a program that reads three integers and prints their maximum.

#include <stdio.h>

int main(void) {
int num1, num2, num3;
scanf("%d%d%d", &num1, &num2, &num3);

// Method 1:
int max = num1;

if (num2 > max)


max = num2;

if (num3 > max)


max = num3;

printf("Max = %d", max);

// 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);

// There are also several other possible methods.


return 0;
}
5. Write a program that reads two integers and the operation to be performed. The operation should be
read as a character: +, -, * or /. The program must perform the operation and print the result.
- If the operation is division and the second integer is 0, the program must output “can’t divide by
zero” without dividing.
- If the user enters any character other than +, -, * and /, the program must print an error message
and exit the program.

#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!")

// Another equivalent statement (poor style!):


if (!(a != 0 && b != 0))
printf("at least one of them is zero!")
7. Which of the following is logically correct?

(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!

(C) This is correct!

(D) This is also correct, but has unnecessary checks.


Previous Exam question

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

Note : the large container can only be used if filled 100%

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.

Metals Normal boiling point (°C)


Copper 1187
Silver 2193
Gold 2660

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

You might also like