Sample Program 2

 

This is a sample program that uses the arithmetic operators. It displays the result of arithmetic operations on the screen. To make the result more visible we have used end line character (endl) with cout. So the statement:

cout << endl;

Simply ends the current line on the screen and the next cout statement displayed at the next line on the screen.

 Code of The Program

// A Sample Program that uses Arithmetic Operators.

 

#include <iostream.h>

 

main ()

{

            //declaration of variables

            int a;

            int b;

            int c;    

 

            float x;

            float y;

            float z;

           

            // assigning values to variables

            a = 23;

            b = 5;

            x = 12.5;

            y = 2.25;

           

            // processing and display with integer values

            cout << "Operations with integers" << endl;

            cout << "a = " << a << endl;

            cout << "b = " << b << endl;

           

            //addition

            c = a + b;

            cout << "a + b = " << c << endl;

           

            //subtraction

            c = a - b;

            cout << "a - b = " << c << endl;

           

            //multiplication

            c = a * b;

            cout << "a * b = " << c << endl;

           

            // division

            c = a / b;

            cout << "a / b = " << c << endl;

           

            //modulus

            c = a % b;

            cout << "a % b = " << c << endl;

           

            // processing and display with decimal values

            cout << "Operations with decimal numbers" << endl;

            cout << "x = " << x << endl;

            cout << "y = " << y << endl;

           

            //addition

            z = x + y;

            cout << "x + y = " << z << endl;

           

            //subtraction

            z = x - y;

            cout << "x - y = " << z << endl;

           

            //multiplication

            z = x * y;

            cout << "x * y = " << z << endl;

           

            //division

            z = x / y;

            cout << "x / y = " << z << endl;

 

}

Output of the program

Operations with integers

a = 23

b = 5

a + b = 28

a - b = 18

a * b = 115

a / b = 4

a % b = 3

Operations with decimal numbers

x = 12.5

y = 2.25

x + y = 14.75

x - y = 10.25

x * y = 28.125

x / y = 5.55556

 

 

 

Previous

 

 

TOC

 

 

Next