CPP
Operator
Fill in the blanks
1.Write a header file for Manipulators iomanip.h
2.Setw header file iomanip.h .
3.Logical NOT operator
symbol !
4.float a=8.94778899.setpricision(3)= 8.947 .
5. :: symbol means Scope resolution Operator
6. int a=8; a%3 = 2
7.int a=8;int b=3; (a>b)?
a: b; 8
8.Conditional Operator also called Ternery Operator
9. main function give first preference to global variable.[T/F]. False
10. int a=9;int b=4;( (++a)
+ ( b++))= 14
Answer the following Question
(4
marks)
1. Write a program for Arithmetic
operator
#include <iostream.h>
#include<conio.h>
void
main() {
int
a, b;
clrscr();
cout<<"Enter
a & b value ";
cin>>a>>b;
cout
<< "a + b = " << (a + b) << endl;
cout
<< "a - b = " << (a - b) << endl;
cout
<< "a * b = " << (a * b) << endl;
cout
<< "a / b = " << (a / b) << endl;
cout
<< "a % b = " << (a % b) << endl;
getch();
}
2. Write a program for Logical operator
#include <iostream.h>
#include<conio.h>
void
main() {
int
a, b;
clrscr();
cout<<"Enter
a & b value ";
cin>>a>>b;
cout
<< ((a==5)&&(b==3)) << endl;
cout
<< ((a==5)||(b==3)) << endl;
cout
<< (!a) << endl;
getch();
}
3. Write a program for Conditional
operator
#include <iostream.h>
#include
<conio.h>
void
main()
{
int a=10,b=5,c;
c=(a>b) ? a : b;
cout<<"Greatest number is "<<c;
getch();
}
4. Write a program for Bitwise operator
#include <iostream.h>
#include<conio.h>
void
main() {
int
a, b;
clrscr();
cout<<"Enter
a & b value ";
cin>>a>>b;
cout
<< "a & b = " << (a & b) << endl;
cout
<< "a | b = " << (a | b) << endl;
cout
<< "a ^ b = " << (a ^ b) << endl;
cout
<< "~a = " << (~a) << endl;
cout
<< "a <<2 = " << (a<<2 ) << endl;
cout
<< "a >>2 = " << (a>>2 ) << endl;
getch();
}
5. Write a program for Assignment
operator
#include
<iostream.h>
#include<conio.h>
void main() {
int
a, b;
clrscr();
cout<<"Enter
a & b value ";
cin>>a>>b;
cout
<< "a + = " << (a +=3 ) << endl;
cout
<< "a - = " << (a - =2) << endl;
cout
<< "a * = " << (a * =3) << endl;
cout
<< "a / = " << (a / =2) << endl;
getch();
}
6. Write a program for Unary operator
#include <iostream.h>
#include
<conio.h>
void
main()
{
int a=5,b=++a;
clrscr();
cout<<"b="<<b<<endl<<"a="<<a;
getch();
}
7. Write a program for Relational
operator
#include <iostream.h>
#include<conio.h>
void
main() {
int
a, b;
clrscr();
cout<<"Enter
a & b value ";
cin>>a>>b;
cout
<< "a < b = " << (a < b) << endl;
cout
<< "a <= b = " << (a <= b) << endl;
cout
<< "a > b = " << (a > b) << endl;
cout
<< "a >= b = " << (a >= b) << endl;
cout
<< "a == b = " << (a == b) << endl;
cout
<< "a != b = " << (a != b) << endl;
getch();
}
8. Write a program for Scope Resolution
Operator
#include <iostream.h>
#include<conio.h>
int a= 50;
void main ()
{
int a= 100;
cout << " The value of the local variable a: " << a;
cout << "\n The value of the global variable a: " << ::a;
getch();
}
Answer the following Question
(10
marks)
1. Explain 7 operator .Write a program
for 7 operator.
C++ Operators
- Arithmetic
Operators
- Relational
Operators
- Logical
Operators
- Bitwise
Operators
- Assignment
Operator
- Unary
operator
- Ternary or
Conditional Operator
- scope
resolution operator
o C++ Arithmetic Operators
Operator |
Operation |
+ |
Addition |
- |
Subtraction |
* |
Multiplication |
/ |
Division |
% |
Modulo Operation (Remainder after division) |
Arithmetic Operators
#include <iostream.h>
#include<conio.h>
void main() {
int a, b;
clrscr();
cout<<"Enter a
& b value ";
cin>>a>>b;
cout << "a + b =
" << (a + b) << endl;
cout << "a - b =
" << (a - b) << endl;
cout << "a * b =
" << (a * b) << endl;
cout << "a / b =
" << (a / b) << endl;
cout << "a % b =
" << (a % b) << endl;
getch();
}
Relational Operators
operator |
Meaning |
Example |
== |
Is Equal To |
3 == 5 gives
us false |
!= |
Not Equal To |
3 != 5 gives
us true |
> |
Greater Than |
3 > 5 gives
us false |
< |
Less Than |
3 < 5 gives
us true |
>= |
Greater Than or Equal To |
3 >= 5 give
us false |
<= |
Less Than or Equal To |
3 <= 5 gives
us true |
#include <iostream.h>
#include<conio.h>
void main() {
int a, b;
clrscr();
cout<<"Enter a
& b value ";
cin>>a>>b;
cout << "a <
b = " << (a < b) << endl;
cout << "a <=
b = " << (a <= b) << endl;
cout << "a >
b = " << (a > b) << endl;
cout << "a >=
b = " << (a >= b) << endl;
cout << "a == b
= " << (a == b) << endl;
cout << "a != b =
" << (a != b) << endl;
getch();
}
Unary Operator
PreIncrement
Post Increment
int
a=5
int a=5
int
b=++a
int b=a++
b=6
b=5
a=6
a=6
Predecrement
Post decrement
int
a=5
int a=5
int
b=--a
int b=a--
b=4
b=5
a=4
a=4
PreIncrement
#include <iostream.h>
#include <conio.h>
void main()
{
int
a=5,b=++a;
clrscr();
cout<<"b="<<b<<endl<<"a="<<a;
getch();
}
Post Increment
#include <iostream.h>
#include <conio.h>
void main()
{
int
a=5,b=a++;
clrscr();
cout<<"b="<<b<<endl<<"a="<<a;
getch();
}
Predecrement
#include <iostream.h>
#include <conio.h>
void main()
{
int
a=5,b=--a;
clrscr();
cout<<"b="<<b<<endl<<"a="<<a;
getch();
}
Post decrement
#include <iostream.h>
#include <conio.h>
void main()
{
int
a=5,b=a--;
clrscr();
cout<<"b="<<b<<endl<<"a="<<a;
getch();
}}
Conditional
Operator
(condition)
? true_value : false_value;
#include <iostream.h>
#include <conio.h>
void main()
{
int
a=10,b=5,c;
c=(a>b) ? a : b;
cout<<"Greatest number is "<<c;
getch();
}
C++ Assignment Operators
Operator |
Example |
Equivalent to |
+= |
a += 3; |
a = a + 3; |
-= |
a -= 2; |
a = a - 2; |
*= |
a *= 3; |
a = a * 3; |
/= |
a /= 2; |
a = a / 2; |
#include <iostream.h>
#include<conio.h>
void main() {
int a, b;
clrscr();
cout<<"Enter a
& b value ";
cin>>a>>b;
cout << "a + =
" << (a +=3 ) << endl;
cout << "a - =
" << (a - =2) << endl;
cout << "a * =
" << (a * =3) << endl;
cout << "a / =
" << (a / =2) << endl;
getch();
}
C++ Logical Operators
#include <iostream.h>
#include<conio.h>
void main() {
int a, b;
clrscr();
cout<<"Enter a
& b value ";
cin>>a>>b;
cout <<
((a==5)&&(b==3)) << endl;
cout <<
((a==5)||(b==3)) << endl;
cout << (!a) <<
endl;
getch();
}
C++ Bitwise Operators
Operator |
Description |
& |
Binary AND |
| |
Binary OR |
^ |
Binary XOR |
~ |
Binary One's Complement |
<< |
Binary Shift Left |
>> |
Binary Shift Right |
#include <iostream.h>
#include<conio.h>
void main() {
int a, b;
clrscr();
cout<<"Enter a
& b value ";
cin>>a>>b;
cout << "a &
b = " << (a & b) << endl;
cout << "a | b =
" << (a | b) << endl;
cout << "a ^ b =
" << (a ^ b) << endl;
cout << "~a =
" << (~a) << endl;
cout << "a
<<2 = " << (a<<2 ) << endl;
cout << "a
>>2 = " << (a>>2 ) << endl;
getch();
}
Scope Resolution Operator
#include <iostream.h>
#include<conio.h>
int a= 50;
void main ()
{
int a= 100;
cout << " The value of the local variable a: " << a;
cout << "\n The value of the global variable a: " << ::a;
getch();
}
No comments:
Post a Comment