0% found this document useful (0 votes)
3 views9 pages

Lab 3

The document provides an overview of variables, constants, and operations in C++ programming. It includes examples of programs for calculating the circumference of a circle, performing mathematical operations, and demonstrating the differences between prefix and postfix increment and decrement operators. The conclusion emphasizes the understanding of basic C++ programming structures and operations.

Uploaded by

Aqsa Taqweem
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views9 pages

Lab 3

The document provides an overview of variables, constants, and operations in C++ programming. It includes examples of programs for calculating the circumference of a circle, performing mathematical operations, and demonstrating the differences between prefix and postfix increment and decrement operators. The conclusion emphasizes the understanding of basic C++ programming structures and operations.

Uploaded by

Aqsa Taqweem
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

Objective:

To be familiar with variables, constants and operations in C++ language.


Variables :
1. Name of variable - It refers to an identifier that represents a
memory location.
2. Address of variable - It refers to the memory location of the
variable.
3. Contents of variable - It refers to the value stored in memory
location referred by variable.
Syntax:
type_name variable = value ;

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:

A literal constant is a value that is typed directly in a program. It


appears in the program wherever it is needed.
Types of literal constant:
Different types of literal constants are as follows: a)
Integer constant
b) Floating point constant
c) Character constant
d) String constant Problem:

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:

▪ ln postfix form, the increment operator is written after the variable as


follows: y++;

▪ The above line also increments the value of variable y by 1.

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;

cout<<"a = "<<a<<endl<<" b = "<<b<<endl;


<<"x = "<<x<<endl<<" y = "<<y<<endl;

Problem 04:

Write a program illustrates on the difference of prefix and postfix


decrement.
Algorithm:
Start.
Define variables.
Assign values.
Calculate c=--a.
Calculate c=a--.
Display.
Print.
Stop.
Coding:
#include <iostream>

Using namespace std;

Void main()

Int a, b, x, y; A=
b = x = y = 0;
a--; b = a; --x;
Y = x;

Cout<<”a = “<<a<<endl<<” b = “<<b<<endl;

<<”x = “<<x<<endl<<” y = “<<y<<endl;

Discussion and Conclusion:


These programs are focused on the calculation of area of a sphere, the
difference of prefix and postfix operator assignments. We learned about
basic mathematical operations using C++. From this lab I understood the
basic structure of C++ programming including the meaning of header file
and steps of problem solving. Hence, area of sphere is calculated and
displayed.

You might also like