SP InLab03 Sec15
SP InLab03 Sec15
Lab Exercises
Exercise Ex. 1 Ex. 2 Ex. 3
Mark /3 /4 /3
Total Mark / 10
Lab #3 Objectives
✔ Precedence of operators.
✔ If selection statement.
Page 1 of 7
o
Page 2 of 7
Part I: Lab Tasks
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
✔ 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:
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.
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