CS111 & COM101 - Exercises (7 Problems Sheets - Fall 2024)
CS111 & COM101 - Exercises (7 Problems Sheets - Fall 2024)
Helwan National University – Faculty of Computer Science and Information Technology (FCSIT)
Academic Year [2024/2025] Fall Semester [1st Term]
CS111 – Introduction to Computer Science (the Mainstream and Software Engineering Programmes)
COM101 – Introduction to Computers (the Data Science, Robotics Software Engineering, and Multimedia
Software Engineering Programmes)
Question 3: Draw a suitable block diagram of the basic organisation of a computer hardware
system and describe them in brief.
1/3
CS111 | COM101 Exercises – Problems Sheet # 1 [Fall 2024 – 2025]
Question 4: Given the following solved example for calculating the value of the decimal
number (9804)10:
9 8 0 4
× × × ×
1000 (i.e., 103) 100 (i.e., 102) 10 (i.e., 101) 1 (i.e., 100)
= = = =
9000 + 800 + 0 + 4 = 9804
What is the value of the following binary number (11010)2?
1 1 0 1 0
× × × × ×
____ (i.e., 24) ____ (i.e., 23) ____ (i.e., 22) ____ (i.e., 21) ____ (i.e., 20)
= = = = =
____ + ____ + ____ + ____ + ____ = ____
Question 5: Given the following examples of counting using the Arabic Numerals:
08 28 098 0998
09 29 099 0999
10 30 100 1000
11 31 101 1001
Complete the missing binary numbers in the following table:
0 1 0 [i.e., 2] _ _ _ _ [i.e., 6] _ _ _ _ _ _ _ _ [i.e., 125] _ _ _ _ _ _ _ _ _ [i.e., 253]
0 1 1 [i.e., 3] 0 1 1 1 [i.e., 7] _ _ _ _ _ _ _ _ [i.e., 126] _ _ _ _ _ _ _ _ _ [i.e., 254]
_ _ _ [i.e., 4] _ _ _ _ [i.e., 8] 0 1 1 1 1 1 1 1 [i.e., 127] 0 1 1 1 1 1 1 1 1 [i.e., 255]
_ _ _ [i.e., 5] _ _ _ _ [i.e., 9] _ _ _ _ _ _ _ _ [i.e., 128] _ _ _ _ _ _ _ _ _ [i.e., 256]
Question 6: Explain/Discuss the following concepts in detail (Search the internet to include more
information & details).
1. The properties (characteristics) of an Algorithm.
2. Algorithmic Efficiency (the efficiency of an algorithm).
3. The five computer generations and the key characteristics of computers in each generation.
4. The classification of computer systems (i.e., the six basic categories of computers).
5. How images are represented in a computer system and the relationship between videos and
images.
6. Artificial Intelligence, and mention some of its applications in everyday life.
7. Software Engineering.
8. The three generic types of errors in a computer program.
Question 7: Watch the following video that introduces "Apple Vision Pro," and then answer
the following questions [ https://www.youtube.com/watch?v=TX9qSaGXFyg ]:
1. "Your photos can be life-size or any size, so your living room becomes a gallery. And panoramas
wrap around you as if you're right where you took them." This functionality is an application of:
(a) Digital Image Processing. (b) Computer Graphics. (c) Computer Vision. (d) None of the
above.
2/3
CS111 | COM101 Exercises – Problems Sheet # 1 [Fall 2024 – 2025]
2. "Apple Vision Pro is Apple's first-ever 3D camera. Now you can capture photos and videos with
remarkable depth." This feature is an example of ____ devices. (a) Storage. (b) Communication.
(c) Output. (d) Input.
3. "Since your eyes see the world with incredible resolution, we built a micro-OLED display system
that fits 64 pixels in the same amount of space as a single iPhone pixel and packs 23 million into
two panels the size of a postage stamp. That's more than a 4K TV for each eye, giving you jaw-
dropping, life-like clarity." This feature is an example of _____ devices. (a) Storage.
(b) Communication. (c) Output. (d) Input.
4. "Experiences on Vision Pro can also expand in three dimensions, filling the entirety of your
space. Like in the Mindfulness app, where you can create a moment of calm." This application
can be considered as an example of: (a) Bio-Informatics. (b) Psycho-Informatics. (c) Eco-
Informatics. (d) Engineering Informatics.
5. "To power a spatial computer like Apple Vision Pro required an innovative dual-chip design. M2
provides phenomenal performance, and a brand-new chip, R1, processes sensor data at
incredible speed, virtually eliminating lag, so experiences take place in real-time right in front of
your eyes." Both M2 and R1 are examples of _____ devices. (a) Storage. (b) Communication.
(c) Output. (d) Input. (e) Processing.
6. "Spatial Audio surrounds you and makes you feel like you're a part of the action." This is mainly
an application of: (a) Databases. (b) Data Structures. (c) Digital Signal Processing. (d) Computer
Vision.
7. "And Safari expands so you can see all your open tabs." Safari is considered a: (a) System
Software. (b) General-purpose Application. (c) Special-Purpose Application. (d) Utility Program.
(e) Library Program.
8. "This is visionOS, Apple's first-ever spatial operating system. It's familiar yet groundbreaking.
You navigate with your eyes. Simply tap to select, flick to scroll, and use your voice to dictate."
VisionOS is considered a: (a) System Software. (b) General-purpose Application. (c) Special-
Purpose Application. (d) Utility Program. (e) Library Program.
9. "Your Persona dynamically reflects your face and hand movements, so when you're chatting,
people see your eyes, hands, and true expressions." Creating a persona/avatar that captures and
reflects your movements and expressions is an application of both Computer Vision and
Computer Graphics. (a) True. (b) False.
10. "We engineered a system that uses advanced machine learning to represent you realistically
when you're in FaceTime." Machine learning is a subset/subfield of AI that focuses on the
development of algorithms and statistical models that allow computers to learn and make
predictions or decisions without being explicitly programmed. (a) True. (b) False.
11. "The web comes to life at a fantastic scale. Text is crisp and easy to read. Browsing the internet
feels new." These features are all aspects that ____ specialists and designers may work on to
optimise the user's experience. (a) Databases. (b) Programming Languages. (c) Human-
Computer Interaction. (d) Artificial Intelligence. (e) Computer Vision.
3/3
CS111 | COM101 Exercises – Problems Sheet # 1 [Fall 2024 – 2025]
Helwan University – Faculty of Computing and Artificial Intelligence (FCAI) – Computer Science Department
Helwan National University – Faculty of Computer Science and Information Technology (FCSIT)
Academic Year [2024/2025] Fall Semester [1st Term]
CS111 – Introduction to Computer Science (the Mainstream and Software Engineering Programmes)
COM101 – Introduction to Computers (the Data Science, Robotics Software Engineering, and Multimedia
Software Engineering Programmes)
Question 3: Given the following solved example for tracing an algorithm (represented as a
pseudocode):
Step n i factorial Display
# (Screen)
Pseudocode 0: Calculate and display the 1 5 - - -
factorial of a +ve integer using a loop. 2 5 - 1 -
1. Input n // Assume the user input 5 3 5 1 1 -
2. Set factorial = 1 4 5 1 1 -
3. For i from 1 to n DO 3 5 2 1 -
4. Set factorial = factorial * i 4 5 2 2 -
5. End For 3 5 3 2 -
6. Print factorial 4 5 3 6 -
7. End 3 5 4 6 -
4 5 4 24 -
3 5 5 24 -
3 5 5 120 120
1/6
CS111 | COM101 Exercises – Problems Sheet # 2 [Fall 2024 – 2025]
Trace (and determine the output) of the following 4 pseudocodes by completing the tables:
Step # num num < 2 i sqrt(num) num mod i num mod i == 0 Display (Screen)
1 25 - - - - - -
2
6
7
6
7
6
7
6
7
8
9
2/6
CS111 | COM101 Exercises – Problems Sheet # 2 [Fall 2024 – 2025]
Pseudocode 3: Approximate Pi (π) [using the Gregory-Leibniz series]
1. Input: The number of terms 'n' to use in the approximation // Note: The more
terms you use (higher 'n'), the more accurate the approximation will be.
2. Output: An approximation of π
3. Initialize pi = 0
4. Initialize sign = 1
5. Initialize denominator = 1
6. For i from 1 to n do
7. pi = pi + sign * 4 / denominator
8. denominator = denominator + 2
9. sign = sign * -1
10. End For
11. Print Approximation of π after n terms is: pi
Step # n pi sign denominator i Display (Screen)
1 8 - - - - -
2
3
4
5
6
7
8
9
6
7
8
9
6
7
8
9
6
7
8
9
6
7
8
9
6
7
8
9
6
7
8
9
6
7
8
9
10
3/6
CS111 | COM101 Exercises – Problems Sheet # 2 [Fall 2024 – 2025]
Pseudocode 4: Display the Fibonacci series up to the nth term using loops.
1. Prompt "Please input a positive integer: "
2. Input: A positive integer 'n' // Assume the user inputs 7
3. Output: The first 'n' terms of the Fibonacci series
4. Initialize first = 0
5. Initialize second = 1
6. Print first
7. Print second
8. For i from 3 to n do
9. next_term = first + second
10. Print next_term
11. first = second
12. second = next_term
13. End For
Question 4: Draw a suitable block diagram explaining the basic steps for compiling a program,
then briefly describe them (i.e., these steps).
4/6
CS111 | COM101 Exercises – Problems Sheet # 2 [Fall 2024 – 2025]
Question 5: Given the following solved example for representing an algorithm using both a
pseudocode and a flowchart:
1. INPUT arr
2. INPUT target
3. FOR i from 0 to length(arr) - 1 DO
4. IF arr[i] == target THEN
5. RETURN i
6. END
7. END IF
8. END FOR
9. RETURN "Not Found"
10. END
For each of the following, write down an algorithm using pseudocode, and represent it
graphically (i.e., visually) using a flowchart:
1) Hello, World: Write an algorithm for a program that displays "Hello, World!" on the screen.
2) Sum of Two Numbers: Create an algorithm for a program that adds two numbers provided
by the user and displays their sum.
[Hint: Prompt the user for input, perform the addition, and display the result.]
3) Calculate Area: Write an algorithm for a program that calculates and displays the area of a
rectangle with user-provided length and width.
[Equation: Area = Length x Width.]
[Hint: Prompt the user for the length and width, then calculate and display the area.]
5/6
CS111 | COM101 Exercises – Problems Sheet # 2 [Fall 2024 – 2025]
4) Count to N: Write an algorithm for a program that counts from 1 to N, where N is a user-
entered number.
[Hint: Use a loop to count from 1 to N and display the numbers.]
5) Even or Odd: Create an algorithm for a program that checks if a user-entered number is
even or odd.
[Equation: Number % 2.]
[Hint: Use the modulo operator to check if the number is even or odd.]
6) Fibonacci Sequence: Write an algorithm for a program that displays the first N numbers in
the Fibonacci sequence.
[Equation: Fibonacci(n) = Fibonacci(n-1) + Fibonacci(n-2).]
[Hint: Use a loop – or recursion – to generate the Fibonacci sequence.]
7) Factorial Calculation: Create an algorithm for a program that calculates the factorial of a
user-entered number.
[Hint: Use a loop – or recursion – to calculate the Factorial.]
8) Prime Number Checker: Write an algorithm for a program that checks if a number
provided by the user is prime.
[Hint: By definition, a prime number is a positive integer greater than 1 with exactly two
distinct positive divisors: 1 and itself. Thus, 1 is not considered a prime number, since 1 only
has one distinct positive divisor (itself); it does not meet the criteria for being a prime
number. Prime numbers start from 2 and go up from there.]
10) Simple Calculator: Develop an algorithm for a basic calculator program that can perform
addition, subtraction, multiplication, and division.
[Hint: Use if-else statements to perform different operations.]
11) Find Maximum Number: Create an algorithm for a program that finds the largest number
among three user-entered numbers.
[Hint: Use conditional statements to compare the numbers.]
12) Leap Year Checker: Write an algorithm for a program that checks if a year entered by the
user is a leap year.
[Equations: Leap year if (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0).]
13) Guess the Number: Develop an algorithm for a game where the computer generates a
random number, and the user tries to guess it.
6/6
CS111 | COM101 Exercises – Problems Sheet # 2 [Fall 2024 – 2025]
Helwan University – Faculty of Computing and Artificial Intelligence (FCAI) – Computer Science Department
Helwan National University – Faculty of Computer Science and Information Technology (FCSIT)
Academic Year [2024/2025] Fall Semester [1st Term]
CS111 – Introduction to Computer Science (the Mainstream and Software Engineering Programmes)
COM101 – Introduction to Computers (the Data Science, Robotics Software Engineering, and Multimedia
Software Engineering Programmes)
Question 1: You are tasked with creating a simple C program that functions as a basic
calculator. The program should provide a menu of four operations: Addition, Subtraction,
Multiplication, and Division. Users will be prompted to choose an operation, enter two
numbers, and then receive the result with a tax rate applied. Your program should adhere to
the following program requirements and provide a user-friendly calculator interface.
1/2
CS111 | COM101 Exercises – Problems Sheet # 3 [Fall 2024 – 2025]
Question 2: What is the output of the following program? Justify your choice.
#include <stdio.h>
int main() {
int length = 25, width = 60, height;
if (length = 50)
height = 4;
else
height = 8;
printf ("Length = %d, Width = %d, and Height = %d.", length, width, height);
return 0;
}
a) The program will display: Length = 25, Width = 60, and Height = 8.
b) The program will display: Length = 25, Width = 60, and Height = 4.
c) The program will display: Length = 50, Width = 60, and Height = 8.
d) The program will display: Length = 50, Width = 60, and Height = 4.
e) The program won’t run (it will result in a syntax/compilation error).
f) The program will run, but it will print nothing since "length = 50" is an assignment statement
(not an equality) that will result in true. Therefore, the printf statement will not be executed
because it belongs to the else block.
Question 3: For the given arithmetic expressions, determine the order of precedence of the
arithmetic operations. Clearly provide the sequence in which the arithmetic operations should
be performed within each expression to illustrate the order of precedence.
a) 2 * 3 + 2 * (3 * 4 + 5 * 6)
b) 8 - 4 + (6 + 9 / (3 * 2))
c) 5 * (7 + 4 * (8 + 2)) / 6 * (9 + 3)
2/2
CS111 | COM101 Exercises – Problems Sheet # 3 [Fall 2024 – 2025]
Helwan University – Faculty of Computing and Artificial Intelligence (FCAI) – Computer Science Department
Helwan National University – Faculty of Computer Science and Information Technology (FCSIT)
Academic Year [2024/2025] Fall Semester [1st Term]
CS111 – Introduction to Computer Science (the Mainstream and Software Engineering Programmes)
COM101 – Introduction to Computers (the Data Science, Robotics Software Engineering, and Multimedia
Software Engineering Programmes)
Question 1: State whether the following statement is true or false. If it is false, explain why.
The following code should print whether a given integer is odd or even:
switch ( value % 2 )
{
case 0:
printf( "An even integer\n" );
case 1:
printf( "An odd integer\n" );
}
2) Even or Odd (using a conditional/ternary operator): Write a C program that checks if a user-
entered number is even or odd, using a conditional (ternary) operator.
3) Leap Year Checker (using an if statement): Write the C code for a program that checks if a
year entered by the user is a leap year using an if statement.
[Equations: Leap year if (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0).]
4) Leap Year Checker (using a conditional/ternary operator): Write the C code for a program
that checks if a year entered by the user is a leap year, using a conditional/ternary operator.
5) Find the Maximum Number (using if–else statements): Write a C program that finds the
largest number among three user-entered numbers, using if–else statements.
6) Find the Maximum Number (using a conditional/ternary operator): Write a C program that
finds the largest number among three user-entered numbers using a conditional (ternary)
operator.
1/2
CS111 | COM101 Exercises – Problems Sheet # 4 [Fall 2024 – 2025]
7) Simple Calculator (using if–else statements): Write the C code for a basic calculator program
that can perform addition, subtraction, multiplication, modulo, and division.
[Hint: Use if-else statements to perform the different operations.]
8) Simple Calculator (using a switch–case statement): Write the C code for a basic calculator
program that can perform addition, subtraction, multiplication, modulo, and division.
[Hint: Use a switch-case statement to perform the different operations.]
9) Determine the Letter Grade (using a switch–case statement): Write the C code for a basic
program that takes a mark (from 0 to 100) as input and provides a corresponding letter grade
("Excellent," "Very Good," "Good," "Pass," or "Fail" for the following ranges respectively: 85 to
100, 75 to 84, 65 to 74, 50 to 64, and 0 to 49).
[Hint: Use a range in the switch-case statement]
Question 3: Trace the following program (while trying different values for both variables a
and b). What does this code do?
#include <stdio.h>
int main()
{
unsigned short int a, b;
a = a ^ b;
b = a ^ b;
a = a ^ b;
return 0;
}
With my best wishes;
Dr. Amr S. Ghoneim
2/2
CS111 | COM101 Exercises – Problems Sheet # 4 [Fall 2024 – 2025]
Helwan University – Faculty of Computing and Artificial Intelligence (FCAI) – Computer Science Department
Helwan National University – Faculty of Computer Science and Information Technology (FCSIT)
Academic Year [2024/2025] Fall Semester [1st Term]
CS111 – Introduction to Computer Science (the Mainstream and Software Engineering Programmes)
COM101 – Introduction to Computers (the Data Science, Robotics Software Engineering, and Multimedia
Software Engineering Programmes)
1/6
CS111 | COM101 Exercises – Problems Sheet # 5 [Fall 2024 – 2025]
7) Write a C program that uses a ‘for’ loop to print the multiplication table of a given number.
Enter a number: 7
Multiplication Table for 7:
7 x 1 = 7
7 x 2 = 14
7 x 3 = 21
7 x 4 = 28
7 x 5 = 35
7 x 6 = 42
7 x 7 = 49
7 x 8 = 56
7 x 9 = 63
7 x 10 = 70
8) Create a C program using a ‘while’ loop to display the prime numbers between 1 and 50.
Prime numbers between 1 and 50:
2 3 5 7 11 13 17 19 23 29 31 37 41 43 47
9) Develop a C program with a ‘do while’ loop to reverse a given number.
Enter a number: 12345
Reversed number: 54321
10) Write a C program that uses a ‘for’ loop to print a pattern of stars:
*
**
***
****
11) Write a C program using a ‘while’ loop to check whether a given number is a palindrome or not.
Enter a number: 121
121 is a palindrome.
12) Write a C program that uses a ‘for’ loop to generate the Fibonacci series up to a certain term.
Enter the number of terms in the Fibonacci series: 8
Fibonacci Series up to 8 terms:
0, 1, 1, 2, 3, 5, 8, 13,
13) Create a C program using a ‘while’ loop to find the Greatest Common Divisor of two numbers.
Enter the first number: 48
Enter the second number: 18
The Greatest Common Divisor (GCD) is: 6
2/6
CS111 | COM101 Exercises – Problems Sheet # 5 [Fall 2024 – 2025]
14) Develop a C program with a ‘do while’ loop to print a menu and perform different operations
based on the user's choice (e.g., add, subtract, multiply, divide).
Menu:
1. Add
2. Subtract
3. Multiply
4. Divide
0. Exit
Enter your choice: 1
Enter two numbers to add: 5 3
Result: 8.00
Menu:
1. Add
2. Subtract
3. Multiply
4. Divide
0. Exit
Enter your choice: 3
Enter two numbers to multiply: 4 2
Result: 8.00
Menu:
1. Add
2. Subtract
3. Multiply
4. Divide
0. Exit
Enter your choice: 4
Enter two numbers to divide: 10 2
Result: 5.00
Menu:
1. Add
2. Subtract
3. Multiply
4. Divide
0. Exit
Enter your choice: 0
Exiting the program. Goodbye!
3/6
CS111 | COM101 Exercises – Problems Sheet # 5 [Fall 2024 – 2025]
15) Write a C program that uses a ‘for’ loop to print a triangle pattern of numbers:
1
22
333
4444
16) Create a C program using a ‘while’ loop to implement a guessing game. The program should
generate a random number, and the user has to guess it.
Welcome to the Guessing Game!
Enter your guess (between 1 and 100): 50
Too low. Try again.
Enter your guess (between 1 and 100): 75
Too high. Try again.
Enter your guess (between 1 and 100): 60
Too low. Try again.
Enter your guess (between 1 and 100): 70
Too high. Try again.
Enter your guess (between 1 and 100): 65
Congratulations! You guessed the correct number (65) in 5 attempts.
17) Write a C program that uses a ‘for’ loop to print the ASCII values and corresponding characters
from 65 to 90.
ASCII value: 65, Character: A
ASCII value: 66, Character: B
ASCII value: 67, Character: C
ASCII value: 68, Character: D
ASCII value: 69, Character: E
ASCII value: 70, Character: F
ASCII value: 71, Character: G
..
..
..
ASCII value: 84, Character: T
ASCII value: 85, Character: U
ASCII value: 86, Character: V
ASCII value: 87, Character: W
ASCII value: 88, Character: X
ASCII value: 89, Character: Y
ASCII value: 90, Character: Z
4/6
CS111 | COM101 Exercises – Problems Sheet # 5 [Fall 2024 – 2025]
18) Create a C program using a ‘while’ loop to find the sum of digits of a given number.
Enter an integer: 12345
The sum of digits of 12345 is: 15
19) Develop a C program with a nested ‘for’ loop to print a rectangular pattern of stars:
*****
*****
*****
20) Write a C program using nested loops to print a multiplication table for numbers 1 to 5.
1 2 3 4 5
2 4 6 8 10
3 6 9 12 15
4 8 12 16 20
5 10 15 20 25
21) Write a C program using a ‘for’ loop and the continue keyword to print the odd numbers
between 1 and 10.
1 3 5 7 9
22) Write a C program using a nested ‘while’ loop and switch-case statements to print a menu-
driven calculator. The program should also utilise the keywords break, continue, and default.
• The program is a simple calculator that allows users to perform basic arithmetic
operations such as addition, subtraction, multiplication, and division. It utilises a menu-
driven approach, prompting the user to select an operation and input two numbers for the
calculation. The program continues to run until the user chooses to exit.
• The user is prompted to enter a choice corresponding to a specific arithmetic operation.
The valid choices are 1, 2, 3, 4, and 0 (for exit).
• If the user chooses an arithmetic operation (1 to 4), they are required to enter two integer
values for the calculation.
• For division (choice 4), the program checks if the denominator is not zero to avoid
division by zero errors. If the user attempts division with a denominator of zero, an error
message is displayed, and the user is prompted to enter the denominator again.
• Invalid choices for arithmetic operations result in an error message, and the user is
prompted to enter a valid choice.
• Upon selecting an operation and providing the required input, the program prints the
result of the operation.
• If the user chooses to exit (entering 0), the program prints a farewell message and
terminates.
• The program uses a while (1) loop to allow continuous operation until the user chooses to
exit.
• The break statement is used to exit the loop when the user selects 0 to exit the program.
5/6
CS111 | COM101 Exercises – Problems Sheet # 5 [Fall 2024 – 2025]
Calculator Menu:
1. Add
2. Subtract
3. Multiply
4. Divide
0. Exit
Enter your choice: 1
Enter two numbers: 5 3
Result: 8
Calculator Menu:
1. Add
2. Subtract
3. Multiply
4. Divide
0. Exit
Enter your choice: 4
Enter two numbers: 15 0
Error: Division by zero is not allowed.
Calculator Menu:
1. Add
2. Subtract
3. Multiply
4. Divide
0. Exit
Enter your choice: 5
Invalid choice. Please enter a number between 0 and 4.
Calculator Menu:
1. Add
2. Subtract
3. Multiply
4. Divide
0. Exit
Enter your choice: 0
Exiting the calculator. Goodbye!
6/6
CS111 | COM101 Exercises – Problems Sheet # 5 [Fall 2024 – 2025]
Helwan University – Faculty of Computing and Artificial Intelligence (FCAI) – Computer Science Department
Helwan National University – Faculty of Computer Science and Information Technology (FCSIT)
Academic Year [2024/2025] Fall Semester [1st Term]
CS111 – Introduction to Computer Science (the Mainstream and Software Engineering Programmes)
COM101 – Introduction to Computers (the Data Science, Robotics Software Engineering, and Multimedia
Software Engineering Programmes)
7) Write a C program that defines a function ‘average’ to calculate the average of three numbers.
The function should accept three floating-point arguments and return the average.
Average of 4.5, 3.0, and 6.5: 4.33
1/4
CS111 | COM101 Exercises – Problems Sheet # 6 [Fall 2024 – 2025]
8) Write a C program that defines a function ‘max’ to find the maximum of two numbers. The
function should accept two integer arguments and return the maximum.
Maximum of 8 and 5: 8
9) Write a C program that defines a function ‘sumOfSquares’ to calculate the sum of squares of
two numbers. The function should accept two arguments (integers) and return the sum of their
squares.
Sum of squares of 3 and 4: 25
10) Write a C program that defines a function ‘printPattern’ to print a pattern of stars. The function
should not accept any arguments and should print a rectangle of stars (e.g., 5x3).
* * *
* * *
* * *
* * *
* * *
11) Write a C program that defines a function ‘isPrime’ to check if a given number is prime. The
function should accept one integer argument and return a Boolean value.
13 is prime.
12) Write a C program that defines a function ‘printMenu’ to print a menu for a calculator program.
The function should not accept any arguments.
Calculator Menu:
1. Add
2. Subtract
3. Multiply
4. Divide
0. Exit
13) Write a C program that defines a function ‘printTable’ to print the multiplication table of a
given number. The function should accept one integer argument.
5 x 1 = 5
5 x 2 = 10
5 x 3 = 15
5 x 4 = 20
5 x 5 = 25
5 x 6 = 30
5 x 7 = 35
5 x 8 = 40
5 x 9 = 45
5 x 10 = 50
2/4
CS111 | COM101 Exercises – Problems Sheet # 6 [Fall 2024 – 2025]
14) Write a C program that defines a function ‘printPatternWithSize’ to print a pattern of stars with
a given number of rows and columns. The function should accept two integer arguments.
Printing Pattern with Size 4 by 6:
* * * * * *
* * * * * *
* * * * * *
* * * * * *
15) Write a C program that defines a function ‘reverseNumber’ to reverse the digits of a given
integer. The function should accept one integer argument and return the reversed number.
Original number: 12345
Reversed number: 54321
16) Write a C program that defines a function ‘gcd’ to find the GCD (Greatest Common Divisor) of
two numbers using Euclid's algorithm.
GCD of 24 and 36 is: 12
17) Write a C program that defines a function ‘factorial’ to calculate the factorial of a number using
recursion. The function should accept one integer argument and return the factorial.
Factorial of 5: 120
18) Write a C program that defines a function ‘powerRecursive’ to calculate the power of a number
using recursion. The function should accept two arguments, the base (integer) and the exponent
(integer), and return the result.
2^3 = 8
19) Write a C program that defines a function ‘fibonacci’ to generate the first N terms of the
Fibonacci sequence using recursion. The function should accept one integer argument (N) and
print the sequence.
Fibonacci sequence (first 7 terms): 0 1 1 2 3 5 8
20) Write a C program that defines a function ‘swap’ to swap the values of two variables. The
function should accept two integer arguments and swap their values. [Hint: This is a C++
program.]
Before swapping: x = 5, y = 8
After swapping: x = 8, y = 5
21) Write a C program that defines a function ‘calculateSumAndAverage’ to calculate the sum and
average of two numbers. The function should accept two integer arguments and return the sum
and average using pass by reference. [Hint: This is a C++ program.]
The Sum of 5 and 8: 13
The Average of 5 and 8: 6.50
3/4
CS111 | COM101 Exercises – Problems Sheet # 6 [Fall 2024 – 2025]
22) Research the keyword ‘static’ in C, the trace the following code. What is the effect of using
‘static’?
#include <stdio.h>
// Function prototype
void fibonacci(int N);
int main() {
int terms = 7;
printf("Fibonacci sequence (first %d terms): ", terms);
printf("\n");
return 0;
}
// Function definition
void fibonacci(int N) {
static int a = 0, b = 1, sum;
if (N > 0) {
sum = a + b;
printf("%d ", sum);
a = b;
b = sum;
fibonacci(N - 1);
}
}
4/4
CS111 | COM101 Exercises – Problems Sheet # 6 [Fall 2024 – 2025]
Helwan University – Faculty of Computing and Artificial Intelligence (FCAI) – Computer Science Department
Helwan National University – Faculty of Computer Science and Information Technology (FCSIT)
Academic Year [2024/2025] Fall Semester [1st Term]
CS111 – Introduction to Computer Science (the Mainstream and Software Engineering Programmes)
COM101 – Introduction to Computers (the Data Science, Robotics Software Engineering, and Multimedia
Software Engineering Programmes)
b) 110011111 d) 1110110.101
1/2
CS111 | COM101 Exercises – Problems Sheet # 7 [Fall 2024 – 2025]
Question 10: Derive the truth tables for the following expressions:
c) (NOT A) OR B
Question 11: Find the 1’s and 2’s complement of the following:
Question 12: Write a C program that reads a decimal number and a target base (either 8
for octal or 16 for hexadecimal) as input from the user. Implement a function
‘convertToBase’ that converts the given decimal number to the specified target base and
prints the result. If the target base is neither 8 nor 16, print "Unsupported target base." Use
Question 13: Trace the following C program and explain what it does.
#include <stdio.h>
int x(int a, int b) {
if (b == 0) { return a; }
int sum = a ^ b;
int carry = (a & b) << 1;
return x(sum, carry);
}
int main() {
int num1, num2;
printf("Enter the first integer: "); scanf("%d", &num1);
printf("Enter the second integer: "); scanf("%d", &num2);
int result = x(num1, num2); printf("%d\n", result);
return 0;
}
With my best wishes;
Dr. Amr S. Ghoneim
2/2
CS111 | COM101 Exercises – Problems Sheet # 7 [Fall 2024 – 2025]