Lab 1 Solutions
Question 1. ( 5 points )
Write a program that asks the user to enter two numbers obtains the two numbers from
the user and prints the sum, product, difference, quotient and remainder of the two
numbers.
Example of the output
Enter two numbers: 20
5
The sum is 25
The product is 100
The difference is 15
The quotient is 4
The modulus is 0
//
//
//
//
//
//
//
main.cpp
q1
Created by Rana on 9/29/13.
Copyright (c) 2013 Rana. All rights reserved.
#include <iostream>
using namespace std;
int main()
{
// Variables Declaraion
int num1, num2, sum = 0,product,difference,quotient,modulus;
// User's message and Inputs
cout << "Enter two numbers ";
cin >> num1 >> num2;
// Calculation
sum = num1 + num2;
product = num1 * num2;
difference = num1 - num2;
quotient = num1 / num2;
modulus = num1 % num2;
// Print the output
cout << "The sum is " << sum << endl;
cout << "The poduct is "<< product << endl;
cout << "The difference is "<< difference << endl;
cout << "The quotient is " << quotient << endl;
cout << "The modulus is " << modulus << endl;
return 0;
}
Question 2. ( 5 points )
Write a program that asks the user to enter two integers, obtains the numbers from the
user, and then prints the larger number followed by the words “is larger.” If the numbers
are equal, print the message “These numbers are equal.” Use only the single-selection
form of the if statement you learned in this chapter.
Example of the output
Enter two numbers:
5 20
20 is larger
Enter two numbers:
239 92
239 is larger
Enter two numbers:
17 17
These numbers are
equal
//
//
//
//
//
//
//
main.cpp
q2
Created by Rana on 9/29/13.
Copyright (c) 2013 Rana. All rights reserved.
#include <iostream>
using namespace std;
int main()
{
// Declarations
int num1,num2;
// Input two numbers
cout << "-----------------------------------------------\n";
cout << "****** Please enter two numbers to compare *******";
cout << "-----------------------------------------------\n";
cin >> num1 >> num2;
// Conditions to test which number is larger and print a message
if ( num1 > num2 )
cout << num1 << " is larger than " << num2 << endl;
if ( num1 < num2 )
cout << num2 << " is larger than " << num1 << endl;
if ( num1 == num2 )
cout << "These numbers are equal" << endl;
return 0;
}