Lab Manual
IV
Pre and Post Practice
#include<iostream.h>
#include<conio.h>
void main()
{
int a,b,c;
a=b=2;
++a;
a++;
a++;
cout<<a;
getch();
}
#include<iostream.h>
#include<conio.h>
void main()
{
int a,b,c;
a=2; b=2;
++b;
b++;
a++;
a++;
cout<<a<<b;
getch();
}
Output of
a
Output of
a
b
Pre and Post Practice
#include<iostream.h>
#include<conio.h>
void main()
{
int a,b,c;
a=b=8;
--a;
a--;
--a;
a--;
cout<<a;
getch();
}
#include<iostream.h>
#include<conio.h>
void main()
{
int a,b,c;
a=4; b=4;
--b;
b--;
a--;
a--;
cout<<a<<b;
getch();
}
Output of
a
Output of
a
b
Unary with cout
Use of Unary Operators with cout
Output as
4
5
Output as
4
5
Output as
4
5
Pre and Post Practice
#include<iostream.h>
#include<conio.h>
void main()
{
int a,b,c;
a=b=8;
cout<<--a;
cout<<--a;
cout<<a--;
cout<<a;
getch();
}
#include<iostream.h>
#include<conio.h>
void main()
{
int a,b,c;
a=4; b=4;
cout<<b--;
cout<<b--;
cout<<b--;
cout<<b;
getch();
}
Pre and Post Practice
#include<iostream.h>
#include<conio.h>
void main()
{
int a,b,c;
a=b=8;
cout<<--a;
cout<<a--;
cout<<a--;
cout<<a--;
getch();
}
#include<iostream.h>
#include<conio.h>
void main()
{
int a,b,c;
a=4; b=4;
cout<<b--;
cout<<b;
cout<<b--;
cout<<++b;
cout<<b;
getch();
}
Pre and Post Practice
#include<iostream.h>
#include<conio.h>
void main()
{
int a,b,c;
a=b=8;
--a;
a++;
cout<<a--;
cout<<--a;
getch();
}
#include<iostream.h>
#include<conio.h>
void main()
{
int a,b,c;
a=4; b=4;
b--;
b++;
b--;
cout<<++b;
cout<<b--;
cout<<b
getch();
}
Unary Practice with
Assignment statement
Use of Unary Operators into = statement
Output as
c=4
d=5
Output as
c=4
d=5
Output as
c=4
d=5
b=6
Use of Unary Operators in = statement
Output as
D=8
E=11
A=12
Output as
D=8
E=11
A=12
B=7
Pre and Post Practice
#include<iostream.h>
#include<conio.h>
void main()
{
int a,b,c;
a=b=8;
a = b++;
a = a+2;
++a;
b = a++;
cout<<a<<b;
getch();
}
#include<iostream.h>
#include<conio.h>
void main()
{
int a,b,c;
a=b=8;
b = ++b;
a = a++;
a--;
b = --a;
cout<<a<<b++;
getch();
}
Pre and Post into Expression
#include<iostream.h>
#include<conio.h>
void main()
{
int a,b,c;
a=b=8;
c = a + ++a + ++a;
cout<<a<<c;
getch();
}
#include<iostream.h>
#include<conio.h>
void main()
{
int a,b,c;
c = ++b + --b + b++;
cout<<b<<c;
getch();
}
Pre and Post Expression
#include<iostream.h>
#include<conio.h>
void main()
{
int a,b,c;
a=b=8;
a = b + ++a + ++b;
cout<<a<<b;
getch();
}
#include<iostream.h>
#include<conio.h>
void main()
{
int a,b,c; a=b=c=4;
a = ++a + --a + b++;
cout<<a<<b;
getch();
}
Pre and Post Expression
}
#include<iostream.h>
#include<conio.h>
void main()
{
int a,b,c; a=b=c=4;
a = ++a + --a + b++;
}
#include<iostream.h>
#include<conio.h>
void main()
{
int a,b,c;
a=2; b=1;
a = ++b + a++;
b= b + ++a - ++b;
cout<<a<<b;
getch();
b = a-- + a-- + ++b + b;
cout<<a++<<b++;
cout<<a<<b;
getch();
Transform Expression into C++ Format
Mathematics Operator in C++ Example
+ + 2+4 =6
- - 4-2 = 2
X * 2*3 =6
/ 4/2 = 2
% (Percentage) % (Remaining) 10%2 = 0
32 3*3 or 3^3 or Power (3,2) 2^3 = 8
Sqrt (9) Sqrt (9) = 3
Transform Expression into C++ Format
Mathematics C++ Format
2+3+4 2+3+4
2-6-5 2-6-5
2X3 2*3
4/2
4%2 4%2
32 3*3
Sqrt (9)
Transform Expression into C++ Format
Mathematics Exp C++ Exp
2*3+4*10/2
3X6-33+7 3*6-(3*3*3)+7
32 Power (3,2)
3*5-Sqrt (9) + (3^2)
Transform Expression into C++ Program
Mathematics C++
2*3+4*10/2
#include<iostream.h>
#include<conio.h>
void main ( ) {
int a,b,c,d,e, ans;
a=2; b=3; c=4; d=10; e=2;
ans= a*b+c*d/e;
cout<<“answer=“<<ans;
getch(); }
Transform Expression into C++ Program
Mathematics C++
3X6-33+7 3*6-(3*3*3)+7
#include<iostream.h>
#include<conio.h>
void main ( ) {
int a,b,c,d,e, ans;
a=3; b=6; c=7;
exp= a*b-(a*a*a)+c;
cout<<“answer=“<<exp;
getch(); }
Transform Expression into C++ Program
Mathematics C++
3*5-Sqrt (9) + (3^2)
#include<iostream.h>
#include<conio.h>
#include<math.h>
void main ( ) {
int a,b,c,d,e, ans;
a=3; b=5; c=9; d=3; e=2;
exp= a*b-sqrt(c)+ (d^2);
cout<<“answer=“<<exp;
getch(); }
Operator Precedence
Read expression from Left to Right
1. ( )
2 * , /, %
3 + , -
Solve Expression using Operator Precedence
Question Output
2 + 3 * 4 / 2+10 ?
( ( 2 + 3 ) * 4 / 2 + 10 ) ?
2 + 4 – 3 * 2 / 2 -10 % 2 + power(2,5) ?
( ( (5*(2+4) %2 )+10 )- sqrt (49) ) ?
(3 * 2 / 2 -10 % 2 + (5^2) ) ?
Solve Binary Operators in Expression
Output as
5 Output as
-3
Variable as a Constant
Output as
a=5
p=5
Output as
a=5
p=5
Constant Variable Value not Change
DEVC++
Output as
a=5
p=5
DEVC++
Output as
-15.6
Input Value at Run Time Screen
 If we want to input value at run time
cout<<a; cin>>a;
cout<<a<<b<<c; cin>>a>>b>>c;
Input at Runtime Screen
Using Turbo C++
a=5
Runtime screen
5 //input value & press
enter
a=5
Runtime screen
10 //input value &
press enter
a=10
Message before Input at Runtime Screen
More Input at Runtime Screen
More Input at Runtime Screen
Enter value at run time

Lab Manual IV (1).pdf on C++ Programming practice

  • 1.
  • 2.
    Pre and PostPractice #include<iostream.h> #include<conio.h> void main() { int a,b,c; a=b=2; ++a; a++; a++; cout<<a; getch(); } #include<iostream.h> #include<conio.h> void main() { int a,b,c; a=2; b=2; ++b; b++; a++; a++; cout<<a<<b; getch(); } Output of a Output of a b
  • 3.
    Pre and PostPractice #include<iostream.h> #include<conio.h> void main() { int a,b,c; a=b=8; --a; a--; --a; a--; cout<<a; getch(); } #include<iostream.h> #include<conio.h> void main() { int a,b,c; a=4; b=4; --b; b--; a--; a--; cout<<a<<b; getch(); } Output of a Output of a b
  • 4.
  • 5.
    Use of UnaryOperators with cout Output as 4 5 Output as 4 5 Output as 4 5
  • 6.
    Pre and PostPractice #include<iostream.h> #include<conio.h> void main() { int a,b,c; a=b=8; cout<<--a; cout<<--a; cout<<a--; cout<<a; getch(); } #include<iostream.h> #include<conio.h> void main() { int a,b,c; a=4; b=4; cout<<b--; cout<<b--; cout<<b--; cout<<b; getch(); }
  • 7.
    Pre and PostPractice #include<iostream.h> #include<conio.h> void main() { int a,b,c; a=b=8; cout<<--a; cout<<a--; cout<<a--; cout<<a--; getch(); } #include<iostream.h> #include<conio.h> void main() { int a,b,c; a=4; b=4; cout<<b--; cout<<b; cout<<b--; cout<<++b; cout<<b; getch(); }
  • 8.
    Pre and PostPractice #include<iostream.h> #include<conio.h> void main() { int a,b,c; a=b=8; --a; a++; cout<<a--; cout<<--a; getch(); } #include<iostream.h> #include<conio.h> void main() { int a,b,c; a=4; b=4; b--; b++; b--; cout<<++b; cout<<b--; cout<<b getch(); }
  • 9.
  • 10.
    Use of UnaryOperators into = statement Output as c=4 d=5 Output as c=4 d=5 Output as c=4 d=5 b=6
  • 11.
    Use of UnaryOperators in = statement Output as D=8 E=11 A=12 Output as D=8 E=11 A=12 B=7
  • 12.
    Pre and PostPractice #include<iostream.h> #include<conio.h> void main() { int a,b,c; a=b=8; a = b++; a = a+2; ++a; b = a++; cout<<a<<b; getch(); } #include<iostream.h> #include<conio.h> void main() { int a,b,c; a=b=8; b = ++b; a = a++; a--; b = --a; cout<<a<<b++; getch(); }
  • 13.
    Pre and Postinto Expression #include<iostream.h> #include<conio.h> void main() { int a,b,c; a=b=8; c = a + ++a + ++a; cout<<a<<c; getch(); } #include<iostream.h> #include<conio.h> void main() { int a,b,c; c = ++b + --b + b++; cout<<b<<c; getch(); }
  • 14.
    Pre and PostExpression #include<iostream.h> #include<conio.h> void main() { int a,b,c; a=b=8; a = b + ++a + ++b; cout<<a<<b; getch(); } #include<iostream.h> #include<conio.h> void main() { int a,b,c; a=b=c=4; a = ++a + --a + b++; cout<<a<<b; getch(); }
  • 15.
    Pre and PostExpression } #include<iostream.h> #include<conio.h> void main() { int a,b,c; a=b=c=4; a = ++a + --a + b++; } #include<iostream.h> #include<conio.h> void main() { int a,b,c; a=2; b=1; a = ++b + a++; b= b + ++a - ++b; cout<<a<<b; getch(); b = a-- + a-- + ++b + b; cout<<a++<<b++; cout<<a<<b; getch();
  • 16.
    Transform Expression intoC++ Format Mathematics Operator in C++ Example + + 2+4 =6 - - 4-2 = 2 X * 2*3 =6 / 4/2 = 2 % (Percentage) % (Remaining) 10%2 = 0 32 3*3 or 3^3 or Power (3,2) 2^3 = 8 Sqrt (9) Sqrt (9) = 3
  • 17.
    Transform Expression intoC++ Format Mathematics C++ Format 2+3+4 2+3+4 2-6-5 2-6-5 2X3 2*3 4/2 4%2 4%2 32 3*3 Sqrt (9)
  • 18.
    Transform Expression intoC++ Format Mathematics Exp C++ Exp 2*3+4*10/2 3X6-33+7 3*6-(3*3*3)+7 32 Power (3,2) 3*5-Sqrt (9) + (3^2)
  • 19.
    Transform Expression intoC++ Program Mathematics C++ 2*3+4*10/2 #include<iostream.h> #include<conio.h> void main ( ) { int a,b,c,d,e, ans; a=2; b=3; c=4; d=10; e=2; ans= a*b+c*d/e; cout<<“answer=“<<ans; getch(); }
  • 20.
    Transform Expression intoC++ Program Mathematics C++ 3X6-33+7 3*6-(3*3*3)+7 #include<iostream.h> #include<conio.h> void main ( ) { int a,b,c,d,e, ans; a=3; b=6; c=7; exp= a*b-(a*a*a)+c; cout<<“answer=“<<exp; getch(); }
  • 21.
    Transform Expression intoC++ Program Mathematics C++ 3*5-Sqrt (9) + (3^2) #include<iostream.h> #include<conio.h> #include<math.h> void main ( ) { int a,b,c,d,e, ans; a=3; b=5; c=9; d=3; e=2; exp= a*b-sqrt(c)+ (d^2); cout<<“answer=“<<exp; getch(); }
  • 22.
    Operator Precedence Read expressionfrom Left to Right 1. ( ) 2 * , /, % 3 + , -
  • 23.
    Solve Expression usingOperator Precedence Question Output 2 + 3 * 4 / 2+10 ? ( ( 2 + 3 ) * 4 / 2 + 10 ) ? 2 + 4 – 3 * 2 / 2 -10 % 2 + power(2,5) ? ( ( (5*(2+4) %2 )+10 )- sqrt (49) ) ? (3 * 2 / 2 -10 % 2 + (5^2) ) ?
  • 24.
    Solve Binary Operatorsin Expression Output as 5 Output as -3
  • 25.
    Variable as aConstant Output as a=5 p=5 Output as a=5 p=5
  • 26.
    Constant Variable Valuenot Change DEVC++ Output as a=5 p=5 DEVC++ Output as -15.6
  • 27.
    Input Value atRun Time Screen  If we want to input value at run time cout<<a; cin>>a; cout<<a<<b<<c; cin>>a>>b>>c;
  • 28.
    Input at RuntimeScreen Using Turbo C++ a=5 Runtime screen 5 //input value & press enter a=5 Runtime screen 10 //input value & press enter a=10
  • 29.
    Message before Inputat Runtime Screen
  • 30.
    More Input atRuntime Screen
  • 31.
    More Input atRuntime Screen
  • 32.