## Breaking News

In this post I'm going to teach you how to write a simple program that will perform some calculation like summation and subtraction of two numbers. Through this post also we will apply the concept of Declaration and Initialization. Let us start.

Assume we want to write a program that will find out the summation and subtraction of two numbers entered by user.
To solve this kind of Question (Problem) we have to make some Analysis on it, and this is the most important thing for making Programming be easy.
On Analysis process, we have to ask ourselves, from this Question what is an Input? What is a Process if any? And what is an Output? After understanding these three things now you can solve any kind of Question. So lets make Analysis from the Question.

Input.
The Question itself say that it needs two numbers from the user, so here our input will be two numbers. These numbers can be of any data type (int, double or float).

Process.
In this point is mostly where the problem is solved and it takes those input and process them to provide new output. Here is the state where it describe the scope (goal) of the program. The Question itself says that it needed to solve (find) the Summation and Subtraction of two numbers where we will use plus( + ) and minus( - ) sign to perform that process of summation and subtraction.

Output.
In this point is where the output of your program is defined after some process to be performed. Often the output is coming from the process and the process is performed by the existing of an input. Our Question itself says that the program should find out the Summation (total) and Subtraction (difference). So, after all, your program should display the Summation and Subtraction of two numbers entered by user.

Implementation of the above analysis, type the following piece of codes.

#include <iostream>
using namespace std;

int main()
{
int sum, sub, num1, num2;
cin >> num1;
cin >> num2;
sum = num1 + num2;
sub = num1 - num2;
cout << "Summation is: " << sum << endl;
cout << "Subtraction is: " <<sub;
return 0;
}

Finally, save it and gives a name with extension of .cpp.

Description of the code.

Today, I'll tell you only the new lines of codes and I'll leave those that we have learned in the previous post.

int sum, sub, num1, num2;

This line has two things, first is Data type ( int ) and second is Variables ( sum, sub, num1, num2 ) and together this is what is called Variable Declaration. So, now we have reserve four(4) rooms (space) in Computer Memory where they will be used to store data of type Integer (int).

cin >> num1;

Again here we have two things, we have cin, this is called Built in Function, obtained from the library called iostream. As I told you in the previous post. iostream is concerned with  the Input and Output of data so this function is here for Input purpose and its syntax is must be followed by double greater than sign ( >> ). And we have num1, this is the variable that we have declared above, now we just use it to store some value.

sum = num1 + num2;

Here we have variables that we have declared above and now we use them. In this line we are applying what is called Initialization. On Initialization we use this sign ( = ) called Assignment Operator, its purpose is to assign a certain value to a variable sum, so the result that will be obtained from this expression ( num1 + num2 ) will be stored in variable sum. Example the result from that expression is 20, so it is equal to write sum = 20;

cout << "Summation is: " << sum << endl;

The new thing in this line is endl. This means the end of line, so anything that will come should be displayed in its new line and not on that line.

Now, our program is ready to run, so you have to compile first then run. When you run it, it will display a black screen and it will blink waiting for the input of your data through that cin line. Type your first input for the first cin line and hit Enter button to read the second input for the second cin line, and finally, hit Enter button to get the output of your Program.

My input was 25 for num1 and 8 for num2 and it produce the following output (see picture below).

We are done...!!!

I hope now you understand how to analyse any Question and write its program in C++. Feel free to ask any question if you didn't understand then I can reply as faster as I can.

Please comment and share with your friends. Don't miss the following post where we will continue to learn different things and application of new concepts.

Thank you for reading, be blessed....!!!