Lab 3
Lab 3
Constants:
Constant is a quantity that cannot be changed during program
execution. Following are two types of constants in C++:
▪ Literal constant
▪ Symbolic constant Literal
Constant:
Write a program that inputs the radius of a circle and displays the
circumference by using formula 2πR. Store the value of π in a constant
by using DEFINE directive.
Algorithm:
1.Start
2.Save file and start coding for circumference of a circle.
3.compile and run code
5.Display result
6.End
Flow chart:
Coding:
#include #define PI
3.141 using
namespace std; void
main()
{
float r, circumference;
cout<<"Enter radius: "; cin>>r; circumference = 2.0 *PI * r; cout< 0 ");
}
Problem : 02
Write a program that performs all mathematical operations on two
variables.
Algorithm:
Start
Define variables
Assign values to the variable
Perform the required operations
Print the results
End
Flow chart:
Coding: #include
<iostream> using
namespace std; void
main()
{ int a , b; a = 10; b = 5;
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;
}
Operators:
Increment Operator:
▪ The increment operator is used to increase the value of a variable by
1.
▪ It is denoted by the symbol ++.
▪ It is a unary operator and works with single variable.
▪ Increment operator can be used in two forms: a)
Prefix Form
b) Postfix Form
a) Prefix Form:
▪ In prefix form, the increment operator is written before the variable as
follows:
++y;
▪ The above line increments the value of variable y by 1. b)
Postfix Form:
Problem: 03
Write a program that explains the difference of postfix increment
operator and prefix increment operator used as independent
expression.
Algorithm:
1.Start
2. define variables and assigns them values
3. apply postfix and prefix increment operator
4. display result
5. end
Flow chart:
Coding:
#include <iostream. using
namespace std; void
main()
int a, b, x, y; a=
b = x = y = 0; a+
+; b = a; ++x; y
= x;
Problem 04:
Void main()
Int a, b, x, y; A=
b = x = y = 0;
a--; b = a; --x;
Y = x;