Skip to main content

Arithmetic operators and program on them

In the previous post, i was discussed about rules for the naming the variable, constants, data types and tokens. Today i am going to discus about some programs on the five basic data types.

Before i start, let's me to introduce you with some  arithmetic operator.In the next post i will discus about operators of C++.

Arithmetic operator are:

1. Addition operator: '+' is known as addition operator  and it is a used to add to numbers. In C++ we can also add two string by overloading  '+' operator. For example : consider two  string "Kunal" and "Kumar" after '+' operator  it will become  "Kunal Kumar" i.e two strings are concatenated.
Latter, i will discus about operator overloading and concatenation of string.  
2. Subtraction operator: '-' is known as subtraction operator and this is used to subtract two number and splitting strings. For example consider two string "Kunal Kumar" and "Kumar", after '-' operator the 1st string will become "Kunal".
3. Multiplication operator: '*' is known as multiplication operator and used to multiply two numbers.
4. Division  operator: '/' is known as division operator and used to divide two number.
5. Modular division operator: '%' is known as modular division operator or modular divisor. This operator is used to calculate the reminder of two numbers when they are divided.
6. Assignment operator: '=' operator is known as assignment operator and this is used to assign the value to variables.

Let's start the programming on the five basic data types:-
1. Program to add two integer number.

 #include<iostream> // header file allows operate the input and                                                //output operation
using namespace std; // Standard namespace

int main(){
    int a=10,b=10,sum; // 'a' , 'b', 'sum' are three variable
    sum=a+b;
    cout<<"1st number="<<a<<"  2nd number="<<b:
    cout<<"\n Addition of two ="<<sum;
    return 0;
}

Output of the above program:


Now let's look what the program say:

The 1st line #include<iostream> , includes the contents of the 'iostream' header file in our program so that we can input and output the data and massages.
The 2nd line using namespace std;

Namespace is a new concept introduced by the ANSI C++ standards committee. This defines a scope for the identifiers that are used in  a program. For using the identifiers defined in the namespace scope we must include the using directive, like

     using namespace std;
Here, std is the namespace where ANSI C++ standard class libraries are defined. All ANSI C++ program must include this directive. This will bring all the identifies defines in std to current global scope.

The main() function:-
Like C, the C++ program is a collection of functions. The above example contains only one function, main(). The execution of every C and C++ program begins at main(). So, every C++ program must have a main() function. And after the main() a opening curly braces "{" indicate the start of the main() function body and at the last line a closing curly braces "}" indicate the ending of the main() function. Every function body start with "{" and end with "}".  

The line int a=10,b=10,sum;, defines three variable which are integer type means they can hold only integer numbers. The variable 'a' and 'b' are assigned the value by 10 means that 'a' and 'b' has value 10.
The line sum=a+b;, in this line 1st of all 'a' and 'b' are add their result is assigned to 'sum'.

The line cout<<"1st number="<<a<<"  2nd number="<<b:, cause the string in the quotation marks to be displayed on the screen and in that line '<<a' and '<<b' cause to be displayed on the screen the value of 'a' and 'b' which are 10.
When '<<' operator is used more than one then this is known as cascading of '<<' operator. We can also cascade '>>' operator.(Latter i will discus.) 
The '\n' in the line, cout<<"\n Addition of two ="<<sum;, is a new line character, in other word '\n' is 'enter' character that is present in 
the keyboard. This type characters can not be entered directly through the keyboard. Due to '\n' the string "Addition of two" is printed in the next line. 

At last the line return 0; will return 0 to the operating system.

Now, whenever we the the above program we will get the same output that means we are always adding 10 and 10 but we does not want that we want add different-different numbers by entering through the keyboard. Let's look how this can be achieved. I am going to modify  the above program to achieve this.


#include<iostream> // header file allows operate the input and                                            //output operation

using namespace std; // Standard namespace


int main(){
    int a,b,sum; // 'a' , 'b', 'sum' are three variable
    cout<<"\n Enter 1st number:";
    cin>>a; // taking value from keyboard
    cout<<"\n Enter 2nd number:";
    cin>>b; // taking value from keyboard
    sum=a+b; // adding 'a' and 'b'
    cout<<"1st number="<<a<<"  2nd number="<<b;
    cout<<"\n\n Addition of two ="<<sum;
    return 0;
}

Output of the above program:
1.Let's input first a=12 and b=56

2. Let's enter a=45 and b=359
This is that which we want.
Let's look at the program:
The 1st line as usual include the contents of the 'iostream' header file to use the 'cout', 'cin' ,'<<' and '>>' for input and output operation. 

The main code start from the line, int a,b,sum;, this line tells to the compiler to make three integer type variable namely 'a', 'b' and 'sum'.  

The line, cout<<"\n Enter 1st number:";, due to '\n' 1st a new line will print on the screen and the then the string "Enter 1st number:" will display on the screen. The main  aim of this line is that to send a message to the user to enter number.

The line,  cin>>a; , take a number from the keyboard and put it into the variable 'a'.

The line,  cout<<"\n Enter 2nd number:";, 1st print a new line due to '\n' present in double quotation and after that the string "Enter 2nd number:" will display. The aim of this line is same as the above line. This line also send a massage on the screen for the user to ask him/her a number.

The line,    cin>>b;, take a number from the keyboard and put it into the variable 'b'.

The line,  sum=a+b;, 1st add the value of variable 'a' with the value of the variable 'b' and then assign the result into the variable 'sum';

 The line, cout<<"1st number="<<a<<"  2nd number="<<b;, in this line we cascaded the operator '<<'. 1st of all the string on the quotation "1st number=" will display on the screen and then the value of the variable 'a' and again another string on the double quotation " 2nd number=" will display on the string following a space and after that the value of the variable 'b' will display on the screen. Te whole output of this line will look like:
1st number=10 2nd number=89

The 2nd last line,cout<<"\n\n Addition of two ="<<sum;, 1st output two newline and then the string "Addition of two=" will display on the screen. 

At the last line we return a 0 value to the operating system to indicate the successfully run of the program. 

Questions for practice:
1. Write a program to add two floating numbers. 
2. Write a program to add two double numbers.
3. Write a program to subtract two integer numbers.
4. Write a program to multiply a integer number with a floating number.

Comment if you have any doubt and comment your answers.

Comments

Popular posts from this blog

Advantage and disadvantages of Structure and object oriented programming

In my last post, i discussed about structured oriented programming, object oriented programming and language processors. Today i am going to discuss about :- 1. Advantages and Disadvantages of structured oriented programming. 2. Advantages and Disadvantages of object oriented programming. 3.Difference between them. And in the next post i will discuss about:- 1. 1st C++ program and explanation. 2. Structure of a C++ program. 3. Data types, Identifiers, Variable and Keywords. Let's start: Advantages of Structure oriented programming:- 1. It is easy to understand. 2. It is to test a program that is divided into modules(sub-problem) and sub-module(sub-sub-problem). 3. Modification is easier. 4. It results in speedy execution of the program. 5. Development of modules is easier. Disadvantages of Structured oriented programming:- 1. Modules occupy more space in the memory. 2.  Testing of every module is time consuming process. 3. Combination of modules becomes di

size and range of basic data types and modifing the basic types.

Size and range of basic data types:-   Modifying the basic types:-  Except for type void, the basic data types may have various modifiers preceding them.  You use a modifier to alter the meaning of the base type to fit various situations more  precisely. The list of modifiers is shown here: signed unsigned long short You can apply the modifiers signed, short, long, and unsigned to integer base  types. You can apply unsigned and signed to characters. You may also apply long to  double. The above  table shows all valid data type combinations, along with their minimal ranges and approximate bit widths. (These values also apply to a typical C++  implementation.) Remember, the table shows the minimum range that these types will  have as specified by Standard C/C++, not their typical range. For example, on  computers that use two's complement arithmetic (which is nearly all), an integer will have a range of at least 32,767 to –32,768. The use of signed on integers is al

C++ tokens, rule for naming variable and rule for constructing integer constant, real constant and character constant

 C++ character set :- The following image shows the valid character that are used in C++.  C++ data types:- 1st of all we have to know that What is a data? Data is a raw material for constructing a information. For example, the following list are the data:- Ram,17, XII C. And from this data we can construct the information as Ram Aged 17 years is in class XII C. Data may be any number,words, amount,quantity. Now, Let's come on the topic: Latter i will discus about 'structure', 'Union', 'Class', Array', 'function','pointer','reference' for now there are five basic data types that are: 1. int(integer)  2. char(Character) 3. float 4. double  5. void  Now look at variable:  Variable is a entity whose value may change and in programming language it is used to store data. A integer type variable can hold a integer number(number must be a whole number either in positive or in negative but not in fraction),  a ch