0% found this document useful (0 votes)
28 views7 pages

SP InLab03 Sec15

This document outlines exercises for a structured programming lab on C operators, if/else statements, and switch statements. It includes 3 exercises: 1. Using conditional statements and if statements to determine if a number is positive or negative. 2. Developing a function to find the minimum number among 5 input integers using nested if/else statements. 3. Creating a function using a switch statement to print the name of the day corresponding to a number from 1-7 representing the day of the week.

Uploaded by

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

SP InLab03 Sec15

This document outlines exercises for a structured programming lab on C operators, if/else statements, and switch statements. It includes 3 exercises: 1. Using conditional statements and if statements to determine if a number is positive or negative. 2. Developing a function to find the minimum number among 5 input integers using nested if/else statements. 3. Creating a function using a switch statement to print the name of the day corresponding to a number from 1-7 representing the day of the week.

Uploaded by

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

King Hussein Faculty of Computing Sciences

Department of Computer Science

Structured Programming Lab


In-Lab Assignment #03 – Writing Simple C Programs
Spring 2022/2023

Lab Exercises
Exercise Ex. 1 Ex. 2 Ex. 3
Mark /3 /4 /3
Total Mark / 10

Lab #3 Objectives

✔ Equality and relational operators.

✔ Logical operators (AND, OR, NOT).

✔ Equality (==) and assignment (=) Operators.

✔ Precedence of operators.

✔ If selection statement.

✔ If ...else selection statement.

✔ The conditional operator ( ?:).

✔ Nested if ... else statements.

✔ Switch multiple-selection statement.

Page 1 of 7
o

Page 2 of 7
Part I: Lab Tasks

Task 1 – C Operators and their Precedence


What is the Output of the Following C Codes?

A.
#include <stdio.h>
void main()
{
int x=(20 || 40 ) && (10);
printf("x= %d",x);
}

Answer: ---------------

B.
#include <stdio.h>
void main()
{
int h = 8;
int b = 4 * 6 + 3 * 4 < 3 ? 4 : 3;
printf("%d\n", b);
}

Answer: ---------------

C.
#include <stdio.h>
void main()
{
int x = 1, y = 0, z = -1;
int a = x && y || z++;
printf("%d, %d", z,a);
}

Answer: ---------------

D.
#include <stdio.h>
int main()
{
int a = 10, b = 5, c = 5;
int d;
d = b + c == a;
printf("%d", d);
}
Answer: ---------------

Page 3 of 7
Task 2 – if/if-else/nested if / Statements, and Conditional Operator ( ?: )
What is the Output of the Following C Codes?
A.
#include <stdio.h>

void main()

{
int x = 5;

if (x < 1)

printf("hello");
if (x == 5)
printf("hi");
else
printf("no");
}

Answer: ---------------
B.
#include <stdio.h>
void main()
{
int x = 0;
if (x == 0)
printf("hi\n");
else
printf("how are u\n");
printf("hello\n");
}

Answer: ---------------
Task 3 – Switch Statement
What is the Output of the Following C Codes?

#include <stdio.h>
void main()
{
int ch;
printf("enter a value between 1 to 2:");
scanf("%d", &ch);
switch (ch){
case 1:
printf("1\n");
break;
printf("Hi");
default:
printf("2\n");
}
}

Answer: -----------------------------

Page 4 of 7
PART II: Lab Excercises

❖ On the desktop, create a folder named Lab3.


Exercise 1 – Even or Odd
Exercise Objectives
✔ Using Conditional statement (? :)

✔ If selection statement.

Problem Description
A. Write and run a C program that performs the following:
o Define a function void showWelcome () used to print the welcome statement.
o Define a function int zeroOrOne (int num) that receives one integer value and returns 1
if the value is Positive and returns 0 if the value is Negative.
o In main:
⮚ Call function showWelcome() to print the welcome statement.
⮚ Prompt the user to input one integer number num.
⮚ Call the function zeroOrOne and pass num to the function, THEN receives the
returned value in variable x.
⮚ IF x is 1. Print “is a positive number”.
⮚ IF x is 0: print “is a negative number”.

⮚ Note: Using ONLY conditional statement (?:), to check if the value is positive or
negative.

Sample Output 1:

Exercise 2 –Find the Minimum Number between five Numbers


Exercise Objectives
✔ nested if ... else statements

Problem Description
Inside the “Lab3” folder, create a project named “Lab3Ex2”.
⮚ Add your name and your University ID as a comment at the top of your source program file.
⮚ Use this project to develop a C program that defines a function called findMin ().
⮚ The function reads a 5 int numbers and prints the minimum number between of them.

Page 5 of 7
⮚ In the main program, call the function to find the minimum number.

Show your work as shown in the sample output


Sample Output
Enter number 1:
100
Enter number 2:
20
Enter number 3:
2
Enter number 4:
2
Enter number 5:
9

The minimum number is 2

Exercise 3 – Simple Calculator


Exercise Objectives
✔ switch multiple-selection statement

Problem Description
Inside the “Lab3” folder, create a project named “Lab3Ex3”. Use this project to develop a C program that
performs the following:
Write a C function named getDays that takes an integer argument day representing a day of the
week (1-7) and print the name of day. Use a switch statement to print the name of the day,
where 1 indicates to Sunday and 7 indicates to Saturday
The result should look like the sample output.

Sample Output
Enter number:
1
Sunday

Sample Output
Enter number:
7
Saturday

Sample Output
Enter number:
3
Tuesday

Page 6 of 7
Page 7 of 7

You might also like