Tutorial 2 Solution
Tutorial 2 Solution
Tutorial 2
Covers: Lectures 3 and 4
Questions:
1. What is the output of the following the following programs:
a) #include <stdio.h>
int main(void)
{
int x = 5;
if (x >= 8 && x < 100)
/* x >= 8 : False, “&&” is used -> second condition is skipped ->
-> conditional if is exited */
printf("%d", x);
return 0;
}
b) #include <stdio.h>
int main(void)
{
int x = 10;
if (x >= 15 || x < 80)
/* x >= 15 : False, “||” is used -> second condition is checked ->
x < 80 : True -> (False || True) : True -> conditional if is
executed */
printf("%d", x);
return 0;
}
The Output: 10
© Dr. Ahmed Amer Shahin CSE121: Computer Programming (2), Fall 2024 1/19
c) #include <stdio.h>
int main(void)
{
int x = 10, y = 40;
if ((x >= 15 || x < 80) && y != 40)
/* x >= 15 : False, “||” is used -> second condition is checked ->
x < 80 : True -> (False || True) : True, “&&” is used -> second
part is checked -> y != 40 : False -> (True && False) : False ->
else will be executed */
printf("%d", x);
else
printf("%d", y);
return 0;
}
The Output: 40
d) #include <stdio.h>
int main(void)
{
int x = 10;
printf("%d", (x > 5 && x != 20) ? 30 : 50);
/* x >= 8 : True, “&&” is used -> second condition is checked ->
x != 20 : True -> (True && True) : True -> “30” will be assigned
to be printed */
return 0;
}
The Output: 30
e) #include <stdio.h>
int main(void)
{
int x = 10;
if (x == 10) // x == 10 : True -> conditional if is executed
x = 5; // x <- 5
if (x == 5) // x == 5 : True -> conditional if is executed
x = 6; // x <- 6
printf("%d", x);
return 0;
}
The Output: 6
© Dr. Ahmed Amer Shahin CSE121: Computer Programming (2), Fall 2024 2/19
f) #include <stdio.h>
int main(void)
{
int x = 10;
if (x == 10) // x == 10 : True -> conditional if is executed
x = 5; // x <- 5
else if (x == 5) // else if -> will be skipped
x = 6;
printf("%d", x);
return 0;
}
The Output: 5
g) #include <stdio.h>
int main(void)
{
int x = 10, y = 70;
if (x = 20)
/* x = 20 : assignment operation is done -> if(x) -> if(20) ->
if(True) -> conditional if is executed*/
printf("x = %d", x);
else // else -> will be skipped
printf("y = %d", y);
return 0;
}
The Output: 20
© Dr. Ahmed Amer Shahin CSE121: Computer Programming (2), Fall 2024 3/19
h) #include <stdio.h>
int main(void)
{
int x = 10;
while (x < 50) // x < 50 : True -> Loop is entered
{
x *= 1.5;
printf("%d ", x);
/* 1st Iteration:
x = 10 * 1.5 = 15 -> 15 will be printed
x < 50 : True -> Loop is entered
2nd Iteration:
x = 15 * 1.5 = 22.5 but x is integer so implicit casting is
performed -> x = 22 -> 22 will be printed
x < 50 : True -> Loop is entered
3rd Iteration:
x = 22 * 1.5 = 33 -> 33 will be printed
x < 50 : True -> Loop is entered
4th Iteration:
x = 33 * 1.5 = 49.5 but x is integer so implicit casting is
performed -> x = 49 -> 49 will be printed
x < 50 : True -> Loop is entered
5th Iteration:
x = 49 * 1.5 = 73.5 but x is integer so implicit casting is
performed -> x = 73 -> 73 will be printed
x < 50 : False -> Loop is exited */
}
return 0;
}
The Output: 15 22 33 49 73
© Dr. Ahmed Amer Shahin CSE121: Computer Programming (2), Fall 2024 4/19
i) #include <stdio.h>
int main(void)
{
int x = 1;
int y = x++; // y = x = 1, x = x + 1 = 2
while (x != y) // x != y : True -> Loop is entered
{
printf("x= %d, y = %d\n", x, y);
y = (x++ % ++y) == 0 ? ++x : --x;
/* 1st Iteration:
printf function is executed
(x++ % ++y) -> (2 % (1+1) = 0), x = 2+1 = 3, y = 1+1 = 2 ->
0 == 0 : True -> ++x is executed -> x = 3+1 = 4 ->
Assignment operation is executed -> y = ++x = 4
x != y : False -> Loop is exited */
}
return 0;
}
The Output: x= 2, y = 1
j) #include <stdio.h>
int main(void)
{
int x = 4;
/* do {...} while (...); is used thus loop will be performed at
least once */
do
{
printf("%d ", x);
x++;
/* 1st Iteration:
printf function is executed -> 4
x++ is executed -> x = 4+1 = 5
x <= 5 : True -> Loop is entered
2nd Iteration:
printf function is executed -> 5
x++ is executed -> x = 5+1 = 6
x <= 5 : False -> Loop is exited */
} while (x <= 5);
return 0;
}
The Output: 4 5
© Dr. Ahmed Amer Shahin CSE121: Computer Programming (2), Fall 2024 5/19
k) #include <stdio.h>
int main(void)
{
unsigned short x;
for (unsigned short i = 0; i < 4; i++)
// i < 4 : True -> Loop is entered
{
x = i * -5;
printf("%u ", x);
/* 1st Iteration:
x = i * -5 = 0 * -5 = 0 -> 0 will be printed
i=1
i < 4 : True -> Loop is entered
2nd Iteration:
x = i * -5 = 1 * -5 = -5 -> but x is unsigned, and
-5 is represented in 2’s complement as (1111 1111 1111 1011)
which represent the unsigned number 65531 -> 65531 is printed
<note that: (1111 1111 1111 1111) represent 65535, thus,
(1111 1111 1111 1011) will be 65535 – 4 = 65531 >
i=2
i < 4 : True -> Loop is entered
3rd Iteration:
x = i * -5 = 2 * -5 = -10 -> but x is unsigned, and
-10 is represented in 2’s complement as (1111 1111 1111 0110)
which represent the unsigned number 65526 -> 65526 is printed
<note that: (1111 1111 1111 0110) will be 65535 – 9 = 65526 >
i=3
i < 4 : True -> Loop is entered
4th Iteration:
x = i * -5 = 3 * -5 = -15 -> but x is unsigned, and
-15 is represented in 2’s complement as (1111 1111 1111 0001)
which represent the unsigned number 65521 -> 65521 is printed
<note that: (1111 1111 1111 0001) will be 65535 – 14 = 65521 >
i=4
i < 4 : False -> Loop is exited
Note that x is printed as unsigned integer, thus, the
Unsigned value of x is used while printing */
}
return 0;
}
© Dr. Ahmed Amer Shahin CSE121: Computer Programming (2), Fall 2024 6/19
l) #include <stdio.h>
int main(void)
{
for (int i = 0;; ++i)
// no condition, thus, infinite for loop is entered
/* Note that “++i” does not differ from “i++” as the increment
process is done at the end of the iteration, thus, the value
of “i” at the 1st iteration is still “0” in this loop */
{
if (i < 7)
printf("%d ", i);
else
break;
/* This loop will continue printing the value of “i” starting
from “0” until “6”, but once “i” reaches “7” the condition
(i < 7 : False), then the code within “else” will be
executed and the loop will be exited */
}
return 0;
}
The Output: 0 1 2 3 4 5 6
© Dr. Ahmed Amer Shahin CSE121: Computer Programming (2), Fall 2024 7/19
2. Write a C program that continuously receives a lowercase letter from the user, then displays
the letter, and its uppercase equivalent. To avoid incorrect operation, check the case of the
received letter and if something other than a lowercase letter is entered display a message to
the user stating that “A lowercase letter is required”. The user should have the ability to stop
the program by pressing the “ESC” key, so each time display a prompt to the user stating,
“Enter a lowercase letter, or ‘ESC’ to end the program:”. Hint: use the ASCII values of letters
for checking and converting cases. Also, use the function getch() from the header file conio.h
when reading a letter from the user.
- Solution:
#include <stdio.h>
#include <conio.h>
void main()
{
char entered_ch = 0;
© Dr. Ahmed Amer Shahin CSE121: Computer Programming (2), Fall 2024 8/19
Program Output Example:
© Dr. Ahmed Amer Shahin CSE121: Computer Programming (2), Fall 2024 9/19
3. Write a C program that asks the user to enter an integer number and returns the prime
numbers between 1 and that number. Make sure to print suitable prompt messages to the user.
The output should be printed using the following format:
…, …, …, …
- Solution:
#include <stdio.h>
void main()
{
unsigned int Entered_Num = 0, i = 0, j = 0;
if (Entered_Num >= 2)
{
printf("\n The prime numbers between 1 and %u are : ", Entered_Num);
printf("\n ");
© Dr. Ahmed Amer Shahin CSE121: Computer Programming (2), Fall 2024 10/19
- Another more optimized Solution (using a Flag and math library):
#include <stdio.h>
#include <math.h>
void main()
{
unsigned int Entered_Num = 0, i = 0, j = 0;
unsigned char isPrime_Flag = 0;
printf("\n Please, Enter a Positive Integer Number greater than \"1\" : ");
scanf("%u", &Entered_Num);
if (Entered_Num >= 2)
{
printf("\n The prime numbers between 1 and %u are : ", Entered_Num);
printf("\n ");
© Dr. Ahmed Amer Shahin CSE121: Computer Programming (2), Fall 2024 11/19
Program Output Example:
© Dr. Ahmed Amer Shahin CSE121: Computer Programming (2), Fall 2024 12/19
4. Write a C program that solves a quadratic equation (𝑎𝑥 2 + 𝑏𝑥 + 𝑐 = 0) and returns its roots.
Make sure to print suitable prompt messages to the user to enter the three coefficients of the
equation (a, b, and c). The output should be printed using the following format:
The roots of the quadratic equation that have the coeff …, …, … are:
X1 = ……………
X2 = ……………
−𝑏±√𝑏 2 −4𝑎𝑐
Hint: use the quadratic formula 𝑥 = . The square root in C could be returned using
2𝑎
the pow() function, which requires including the math.h header file.
- Solution:
#include <stdio.h>
#include <math.h>
void main()
{
float a = 0.0, b = 0.0, c = 0.0; // The coefficients
float X1 = 0.0, X2 = 0.0, disc = 0.0; // The Roots and the Discriminator
float Real = 0.0, Imag = 0.0;
if (a != 0)
/* To avoid division by zero which is a fetal error */
{
printf("\n The roots of the quadratic equation that have the ");
printf("Coefficients %.3f, %.3f, %.3f are: ", a , b, c);
disc = (b * b) - (4 * a * c);
if (disc >= 0)
{
X1 = (-b + pow(disc,0.5)) / (2 * a);
X2 = (-b - pow(disc,0.5)) / (2 * a);
printf("\n X1 = %.3f \n X2 = %.3f \n", X1 , X2);
}
else
{
Real = -b / (2 * a);
Imag = pow(-disc,0.5) / (2 * a);
printf("\n X1 = %.3f + %.3fi ", Real, Imag);
printf("\n X2 = %.3f - %.3fi \n", Real, Imag);
}
© Dr. Ahmed Amer Shahin CSE121: Computer Programming (2), Fall 2024 13/19
}
else
{
printf("\n a = 0, thus, this is not a quadratic equation \n");
}
© Dr. Ahmed Amer Shahin CSE121: Computer Programming (2), Fall 2024 14/19
5. Consider the following problem statement:
A college offers a course that prepares students for the state licensing exam for real estate
brokers. Last year, 10 of the students who completed this course took the licensing
examination. Naturally, the college wants to know how well its students did on the exam.
You’ve been asked to write a program to summarize the results. You’ve been given a list of
these 10 students. Next to each name a 1 is written if the student passed the exam and a 2 if the
student failed.
Your program should analyze the results of the exam as follows:
• Input each test result (i.e., a 1 or a 2). Display the prompting message “Enter result”
each time the program requests another test result.
• Count the number of test results of each type.
• Display a summary of the test results indicating the number of students who passed and
the number who failed.
• If more than eight students passed the exam, print the message “Bonus to instructor!”
- Solution:
#include <stdio.h>
void main()
{
int itr = 0, Result = 0;
int passed = 0, failed = 0;
printf("\n");
for (itr = 0; itr < 10; itr++)
{
printf(" Enter Result: ");
scanf("%d", &Result);
if (Result == 1) {
passed++;
}
else {
failed++;
}
}
printf("\n The number of students who passed = %d ", passed);
printf("\n The number of students who failed = %d \n", failed);
if (passed >= 8)
{
printf("\n Bonus to instructor! \n");
}
else { /* Do Nothing */ }
© Dr. Ahmed Amer Shahin CSE121: Computer Programming (2), Fall 2024 15/19
- Another more optimized Solution:
#include <stdio.h>
void main()
{
int Result = 0;
/* Can’t be declared as unsigned char and scanned with “scanf” as int (%d) */
unsigned char passed = 0, itr = 0;
printf("\n");
for (itr = 0; itr < 10; itr++)
{
printf(" Enter Result: ");
scanf("%d", &Result);
if ((Result != 1) && (Result != 2))
/* In case of a wrong entry that is other than “1” or “2” we can
reduce the loop counter by 1, thus, the user can have another
chance to enter the exam result */
{
itr--;
}
else { /* Do Nothing */ }
if (Result == 1)
{
passed++;
}
else { /* Do Nothing */ }
}
© Dr. Ahmed Amer Shahin CSE121: Computer Programming (2), Fall 2024 16/19
Program Output Example:
© Dr. Ahmed Amer Shahin CSE121: Computer Programming (2), Fall 2024 17/19
6. Write a C program that asks the user to enter how many cups of coffee he consumes per day
and prints one of the messages shown in the table below. Make sure to use switch selection
statement to select the correct message and puts() function to print the message.
- Solution:
#include <stdio.h>
void main()
{
int coffeeCups = 0;
printf("\n Please enter the number of cups of coffee you drink per day: ");
scanf("%d", & coffeeCups);
switch (coffeeCups)
{
case 0 :
puts(" Good to know ");
break;
case 1 :
puts(" Getting some energy ");
break;
case 2 :
puts(" More is better ");
break;
case 3 :
puts(" Sleepy head ");
break;
case 4 :
puts(" Bad for your health ");
break;
default :
puts(" You should see a doctor ");
break;
}
© Dr. Ahmed Amer Shahin CSE121: Computer Programming (2), Fall 2024 18/19
Program Output Example:
© Dr. Ahmed Amer Shahin CSE121: Computer Programming (2), Fall 2024 19/19