Arithmetic and Relational operators
Arithmetic Operators
Following are the basic Arithmetic
operators used in C/C++:
i) + (Addition)
ii) - (Subtraction)
iii) * (Multiplication)
iv) / (Division)
Apart from the specified basic operators,
there are some other operators used in C/C++, and are
v) % (Remainder or Modulus)
vi) ++ (Increment)
vii) -- (Decrement)
viii) += (Increment Assignment)
ix) -= (Decrement Assignment)
x) *= (Multiplication Assignment)
xi) /= (Division Assignment)
xii) %= (Remainder Assignment)
- Increment and Decrement operators can be used in two ways, i.e.
i) Prefix
§ ++var, --var
ii) Postfix
§ Var++, var--
- Increment and Decrement operators can be used in two ways, i.e.
i) Prefix
§ ++var, --var
ii) Postfix
§ Var++, var--
Basic operators
#include <iostream.h>
#include <conio.h>
void main(void){
clrscr();
int a=5,b=2;
cout<<"A = 5 and B = 2"<<endl<<endl;
cout<<a<<" + "<<b<<" = "<<a+b<<endl;
cout<<a<<" - "<<b<<" = "<<a-b<<endl;
cout<<a<<" x "<<b<<" = "<<a*b<<endl;
cout<<a<<" / "<<b<<" = "<<a/b<<endl;
cout<<a<<" % "<<b<<" = "<<a%b;
getch();
}
Other operators
#include <iostream.h>
#include <conio.h>
void main(void)
{
clrscr();
int a=5,b=2;
a+=b;
cout<<"a += b means value of a is "<<a<<endl;
a=5,b=2;
a-=b;
cout<<"a -= b means value of a is "<<a<<endl;
a=5,b=2;
a*=b;
cout<<"a *= b means value of a is "<<a<<endl;
a=5,b=2;
a/=b;
cout<<"a /= b means value of a is "<<a<<endl;
a=5,b=2;
a%=b;
cout<<"a %= b means value of a is "<<a<<endl;
getch();
}
Prefix – Postfix
#include <iostream.h>
#include <conio.h>
void main(void)
{
clrscr();
int a=5;
cout<<"Value of A now is "<<a<<endl<<endl;
cout<<"Prefix operator ++a gives "<<++a<<endl;
cout<<"Value of a after Prefix is "<<a<<endl<<endl;
cout<<"Postfix operator a++ gives "<<a++<<endl;
cout<<"Value of a after Postfix "<<a<<endl<<endl;
getch();
}
Relational Operators
A relational operator compares two
values. Comparisons involved in relation operators can be
i) < Less than
ii) > Greater than
iii) == Equals to
iv) != Not equals
v) <= Less than or equals
vi) >= Greater than or equals
The result of comparison is either True
or False. If a comparison provides 1, it means True and if it provides 0, it
means False.
#include <iostream.h>
#include <conio.h>
void main(void)
{clrscr();
int number;
cout<<"Enter a Number ";
cin>>number;
cout<<"number < 10 = "<<(number<10)<<endl;
cout<<"number > 10 = "<<(number>10)<<endl;
cout<<"number == 10 = "<<(number==10)<<endl;
getch();
}
Using Library Functions
#include <iostream.h>#include <conio.h>#include <math.h>void main(void){
clrscr();int a;cout<<"Enter a value ";cin>>a;cout<<"Square Root of "<<a<<" is "<<sqrt(a);getch();}
No comments:
Post a Comment