Module leader: Dr.
Sally Saad
Module TA:Eng Ameera Amer
Extra Sheet 1:
1- Write a program that asks the user to enter two integers, obtains the
numbers from the user, then prints the larger number followed by the words
"is larger." If the numbers are equal, print the message “These numbers are
equal.”
The screen dialogue should appear as follows:
Enter two integers: 22 8
22 is larger.
Ans:
int main() {
int num1, num2; // declaration
cout << "Enter two integers: "; // prompt
cin >> num1 >> num2; // input to numbers
if ( num1 == num2 )
cout << "These numbers are equal." << endl;
if ( num1 > num2 )
cout << num1 << " is larger." << endl;
if ( num2 > num1 )
cout << num2 << " is larger." << endl;
return 0; }
BY: Ameera Amgad Amer 1
2- Write a program that inputs three integers from the keyboard and prints the
sum, average, product, smallest and largest of these numbers.
The screen dialogue should appear as follows:
Input three different integers: 13 27 14
Sum is 54
Average is 18
Product is 4914
Smallest is 13
Largest is 27
ANS:
int main() {
int num1, num2, num3, smallest, largest; //declaration
cout << "Input three different integers: "; // prompt
cin >> num1 >> num2 >> num3; // input
largest = num1; // assume first number is largest
if ( num2 > largest ) // is num2 larger?
largest = num2;
if ( num3 > largest ) // is num3 larger?
largest = num3;
smallest = num1; // assume first number is smallest
if ( num2 < smallest )
smallest = num2;
if ( num3 < smallest )
smallest = num3;
BY: Ameera Amgad Amer 2
cout << "Sum is " << num1 + num2 + num3 32 << "\nAverage is " << (num1 +
num2 + num3) / << "\nProduct is " << num1 * num2 * num3 << "\nSmallest is " <<
smallest << "\nLargest is " << largest << endl;
return 0; }
3- Write a program that reads in two integers and determines and prints if the
first is a multiple of the second. (Hint: Use the modulus operator.)
Sample Output:
Input the first integer : Input the second integer:
9 is a multiple of 3.
Ans:
#include <iostream>
using namespace std;
int main()
{
int num1, num2;
cout << "Enter two integers: ";
cin >> num1 >> num2;
if ( num1 % num2 == 0 )
cout << num1 << " is a multiple of " << num2 << endl;
if ( num1 % num2 != 0 )
cout << num1 << " is not a multiple of " << num2 << endl;
return 0;
}
BY: Ameera Amgad Amer 3
4- **Write a program that inputs a five-digit number, separates the number
into its individual digits and prints the digits separated from one another by
three spaces each. (Hint: Use the integer division and modulus operators.)
For example, if the user types in 42339 the program should print 4 2 3 3
5- C++ Using nested if write a C++ program to ask the user about his age then
decide whether he is an adult or child.
6- if(age>14)
7- {
8- if(age>=18)
9- {
10- cout<<"Adult";
11- }
12- else
13- {
14- cout<<"Teenager";
15- }
16- }
17- else
18- {
19- if (age > 0)
20- {
21- cout<<"Child";
22- }
23- else
24- {
25- cout << "Something's wrong";
26- }
27- }
BY: Ameera Amgad Amer 4
6- Create a Calculator using the switch Statement by asking the user to input 2
numbers and the required operation.
Sample output:
Enter an operator (+, -, *,): *
Enter two operands: 1.5
4.5
1.5 * 4.5 = 6.8
Ans:
# include <iostream>
using namespace std;
int main() {
char op;
float num1, num2;
cout << "Enter operator: +, -, *, /: ";
cin >> op;
cout << "Enter two operands: ";
cin >> num1 >> num2;
switch(op) {
case '+':
cout << num1 << " + " << num2 << " = " << num1 + num2;
break;
case '-':
cout << num1 << " - " << num2 << " = " << num1 - num2;
break;
BY: Ameera Amgad Amer 5
case '*':
cout << num1 << " * " << num2 << " = " << num1 * num2;
break;
case '/':
cout << num1 << " / " << num2 << " = " << num1 / num2;
break;
default:
// If the operator is other than +, -, * or /, error message is shown
cout << "Error! operator is not correct";
break; }
return 0;
}
BY: Ameera Amgad Amer 6