Final Revision Sheet (Questions & Answers)
Final Revision Sheet (Questions & Answers)
#include <iostream>
int main() {
int number = 10;
std::cout << "The value of number is: " << number << std::endl;
return 0;
}
2. Write a function that takes two integers as parameters and returns their sum.
#include <iostream>
int add(int a, int b) {
return a + b;
}
int main() {
int result = add(5, 7);
std::cout << "The sum is: " << result << std::endl;
return 0;
}
3. Write a program that reads an integer from the user and prints whether it is even
or odd.
#include <iostream>
int main() {
int number;
std::cout << "Enter an integer: ";
std::cin >> number;
if (number % 2 == 0) {
std::cout << "The number is even." << std::endl;
} else {
std::cout << "The number is odd." << std::endl;
}
return 0;
}
Future University in Egypt
Faculty of Computer Information &
Technology
Fall 2024
4. Create a program that reads two floating-point numbers from the user and prints
their average.
#include <iostream>
int main() {
float num1, num2;
std::cout << "Enter two numbers: ";
std::cin >> num1 >> num2;
float average = (num1 + num2) / 2;
std::cout << "The average is: " << average << std::endl;
return 0;
}
5. Write a program that declares an array of 5 integers, assigns values to it, and
prints all the values.
#include <iostream>
int main() {
int arr[5] = {1, 2, 3, 4, 5};
for (int i = 0; i < 5; i++) {
std::cout << "Element " << i << ": " << arr[i] << std::endl;
}
return 0;
}
Future University in Egypt
Faculty of Computer Information &
Technology
Fall 2024
If Statements
6. Write a program that reads an integer from the user and prints whether it is
positive, negative, or zero.
#include <iostream>
int main() {
int number;
std::cout << "Enter an integer: ";
std::cin >> number;
if (number > 0) {
std::cout << "The number is positive." << std::endl;
} else if (number < 0) {
std::cout << "The number is negative." << std::endl;
} else {
std::cout << "The number is zero." << std::endl;
}
return 0;
}
7. Write a program that reads three integers from the user and prints the largest one.
#include <iostream>
int main() {
int num1, num2, num3;
std::cout << "Enter three integers: ";
std::cin >> num1 >> num2 >> num3;
if (num1 >= num2 && num1 >= num3) {
std::cout << "The largest number is: " << num1 << std::endl;
} else if (num2 >= num1 && num2 >= num3) {
std::cout << "The largest number is: " << num2 << std::endl;
} else {
std::cout << "The largest number is: " << num3 << std::endl;
}
return 0;
}
Future University in Egypt
Faculty of Computer Information &
Technology
Fall 2024
8. Create a program that reads a character from the user and checks if it is a vowel
(a, e, i, o, u) or a consonant.
#include <iostream>
int main() {
char ch;
std::cout << "Enter a character: ";
std::cin >> ch;
if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u' ||
ch == 'A' || ch == 'E' || ch == 'I' || ch == 'O' || ch == 'U') {
std::cout << "The character is a vowel." << std::endl;
} else {
std::cout << "The character is a consonant." << std::endl;
}
return 0;
}
#include <iostream>
int main() {
int year;
std::cout << "Enter a year: ";
std::cin >> year;
if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) {
std::cout << "The year is a leap year." << std::endl;
} else {
std::cout << "The year is not a leap year." << std::endl;
}
return 0;
}
Future University in Egypt
Faculty of Computer Information &
Technology
Fall 2024
10. Create a program that reads two integers and prints "True" if one of them is 10 or
if their sum is 10.
#include <iostream>
int main() {
int num1, num2;
std::cout << "Enter two integers: ";
std::cin >> num1 >> num2;
if (num1 == 10 || num2 == 10 || (num1 + num2) == 10) {
std::cout << "True" << std::endl;
} else {
std::cout << "False" << std::endl;
}
return 0;
}
Future University in Egypt
Faculty of Computer Information &
Technology
Fall 2024
Loops
11. Write a program that prints all numbers from 1 to 100.
#include <iostream>
int main() {
for (int i = 1; i <= 100; i++) {
std::cout << i << " ";
}
std::cout << std::endl;
return 0;
}
12. Create a program that prints the sum of all even numbers from 1 to 50.
#include <iostream>
int main() {
int sum = 0;
for (int i = 1; i <= 50; i++) {
if (i % 2 == 0) {
sum += i;
}
}
std::cout << "The sum of all even numbers from 1 to 50 is: " << sum << std::endl;
return 0;
}
Future University in Egypt
Faculty of Computer Information &
Technology
Fall 2024
13. Write a program that prints the multiplication table of a given number using a for
loop.
#include <iostream>
int main() {
int number;
std::cout << "Enter a number: ";
std::cin >> number;
for (int i = 1; i <= 10; i++) {
std::cout << number << " * " << i << " = " << number * i << std::endl;
}
return 0;
}
14. Write a program that reads a list of numbers (terminated by -1) and prints their
sum.
#include <iostream>
int main() {
int number, sum = 0;
std::cout << "Enter numbers (terminate with -1): ";
while (true) {
std::cin >> number;
if (number == -1) {
break;
}
sum += number;
}
std::cout << "The sum of the numbers is: " << sum << std::endl;
return 0;
}
Future University in Egypt
Faculty of Computer Information &
Technology
Fall 2024
15. Create a program that finds the factorial of a given number using a while loop.
#include <iostream>
int main() {
int number, factorial = 1;
std::cout << "Enter a number: ";
std::cin >> number;
int i = number;
while (i > 1) {
factorial *= i;
i--;
}
std::cout << "The factorial of " << number << " is: " << factorial << std::endl;
return 0;
}