Vous êtes sur la page 1sur 2

Addition & Multiplication of Numbers

Problem
A program that will perform computation of 4 input numbers especially the addition of the first
two and multiplication of the last two input numbers.

Program
#include <iostream>
using namespace std;
int main ()
{
int num1,num2,num3,num4,sum,product;
cout<<"Enter First Number: ";
cin>>num1;
cout<<"Enter Second Number: ";
cin>>num2;
cout<<"Enter Third Number: ";
cin>>num3;
cout<<"Enter Fourth Number: ";
cin>>num4;
sum=num1+num2;
product=num3*num4;
cout<<"The sum of "<<num1<<" and "<<num2<<" is "<<sum<<endl;
cout<<"The product of "<<num3<<" and "<<num4<<" is "<<product<<endl;
return 0;
}

Output
Discussions
For creating a program and a source file and introduction of the main program, see
Discussion of Activity 1.
The main program starts with the declaration of the variable names which will be
used (See First Paragraph-Discussion of Chapter 2).For this time, num1 to num4 were
used to store the four numbers, and also sum and product, which will store the result of
adding the first two numbers and multiplying the 3rd and 4th number, respectively.
Use cout<< to display the phrase Enter First Number: . After this, the user is
already required to make an input. Therefore, cin>> is used followed by the designated
variable which will store the data (num1). Do these same procedures until the input of the
fourth number.
After placing the inputs, the process or computation shall take place. To add the first
two numbers, use the assigned variables, num1 & num2 and store the result in the
variable sum (i.e. sum=num1+num2). Same goes with the multiplication of the next two
numbers, but of different variables and using * instead, since multiplication shall be used
(product=num3*num4).
A final statement shall be displayed which tells the user the resulting computations:
The sum of <<num1<< and <<num2<< is <<sum. The variables in the statement will
now be substituted by the inputs entered by the user and the result of adding those inputs.
The characters enclosed with quotation marks would be the exact same words to be
displayed. Use cout<< for the display of the statement. Similar statement shall be used for
the multiplication of the next two numbers, using cout<<: The product of <<num3<< and
<<num4<< is <<product. Use <<endl; at the end of each line of programs to proceed to
the next line. Note that the presentation of the final output is done with style and always
depends upon your discretion. End the program with return 0 and unindented } after. Start
the program(See Discussion of Activity 1).

Vous aimerez peut-être aussi