Tuesday 29 August 2017

Arithmetic Operations in C++(Addition,subtraction,multiplication,division and modulus) With The Help Of Global,Local And Default Variables.

Let understand arithmetic operations with the help of global and local variables.
The arithmetic operation is used for some computation or calculation.
Addition of two variables:-
#include<iostream>
using namespace std;
int x=6; //global variable

int main()
{
   int y=60,z; /* y is declared and initialized,y and z                 are local variable */
 
   z=x+y;
   cout<<"Addition:"<<z;
   return 0;
}


output:-
Addition:66
here x is the global variable(we can use the global variable outside the main function because it is available globally) y and z are local variables(we can use these variables inside the function in which they are declared). y is declared and initialized in the main function but you can give value to y  at the runtime by using cin.

Subtraction of two variables:-

#include<iostream>
using namespace std;
int main()
{
 int x,y,z;
 cout<<"Enter the value of x:";
 cin>>x; // gives value at runtime
 cout<<"Enter the value of y:";
 cin>>y;
 z=x-y;
 cout<<"Subtraction:"<<z;
 return 0;
}
output:-

Enter the value of x:53
Enter the value of y:30
subtraction:23
Multiplication of variables:-

#include<iostream>
using namespace std;
int main()
{
 float2 w=0,x,y,z; //default value for w
 cout<<"Enter the value of x:";
 cin>>x; // gives value at runtime
 cout<<"Enter the value of y:";
 cin>>y;
 z=w*x*y;
 cout<<"multiplication:"<<z;
 return 0;
}


output:

Enter the value of x:2
Enter the value of y:3
multiplication:0


here we have a default value to w that means if we forget to give a value to w it will automatically provide a value to w.
Take an example: if we assign a value to w on runtime say w=2, then answer will be 2*2*3=12 not zero.
do it yourself give the default value of w is zero then assign a value to w by using cin then see what will be the answer and also you can assign a value for w after default value through cin then latest value of w will be include in multiplication.

Division of two variables:-
#include<iostream>
using namespace std;
int main()
{
 float x,y,z; 
 cout<<"Enter the value of x:";
 cin>>x; 
 cout<<"Enter the value of y:";
 cin>>y;
 z=x/y;
 cout<<"Quotient:"<<z;
 return 0;
}

output:-
Enter the value of x:67
Enter the value of y:9
Quotient:7.44444
Modulus of two variables:-
#include<iostream>
using namespace std;
int main()
{
 float x,y,z; 
 cout<<"Enter the value of x:";
 cin>>x; 
 cout<<"Enter the value of y:";
 cin>>y;
 z=x%y;
 cout<<"Remainder:"<<z;
 return 0;
}

output:-
Enter the value of x:45
Enter the value of y:6
Remainder:3
when you divide two variables it gives you quotient where modulus give you the remainder.
if you use int variables is only provide either quotient or remainder, if you use float it will give you perfect value.
Example: divide 7 by 2 if you take 7 and 2 as the integer the quotient is 3 and modulus is 1 where if you take 7 and 2 as float it will give you 3.5 as answer.

                             HAPPY CODING

0 comments:

Post a Comment