0% found this document useful (0 votes)
13 views

Final Revision Sheet (Questions & Answers)

The document contains sample C++ code snippets for various basic programming concepts like variables, functions, arrays, if/else statements, loops. It includes problems and their solutions to demonstrate concepts like finding even/odd numbers, largest of three numbers, checking leap years.

Uploaded by

ifadyezzat
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

Final Revision Sheet (Questions & Answers)

The document contains sample C++ code snippets for various basic programming concepts like variables, functions, arrays, if/else statements, loops. It includes problems and their solutions to demonstrate concepts like finding even/odd numbers, largest of three numbers, checking leap years.

Uploaded by

ifadyezzat
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Future University in Egypt

Faculty of Computer Information &


Technology
Fall 2024

Sheet: Final Revision – Questions & Answers

Basics of Programming with C++


1. Write a program to declare an integer variable, assign it a value, and print the
value.

#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

Sheet: Final Revision – Questions & Answers

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

Sheet: Final Revision – Questions & Answers

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

Sheet: Final Revision – Questions & Answers

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;
}

9. Write a program that checks if a given year is a leap year.

#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

Sheet: Final Revision – Questions & Answers

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

Sheet: Final Revision – Questions & Answers

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

Sheet: Final Revision – Questions & Answers

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

Sheet: Final Revision – Questions & Answers

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;
}

You might also like