Programming Using C++: Muhammad Saleem Raza
Programming Using C++: Muhammad Saleem Raza
JHANG 2020
Department of Computer Science
PROGRAMMING IN C++
(It is chapter No. 03,
IT Series book:- Object Oriented
Programming using C++ by Prof. Tasleem Mustafa )
1. Identifiers and their types.
2. Keywords.
3. Data types.
i. Integer Data Type (int) and its qualifiers.
ii. Real Data Type and its qualifiers.
iii. Character Data Type.
4. Integer Underflow and Overflow.
5. Variable, its declaration and initialization.
i. Rules for naming variables.
6. Constants and their types.
7. Operators and their types.
----------------------------------------------------------------------
Note:
Prepare this chapters. If there is any difficulty, contact
me.
Muhammad Saleem Raza
Department of Computer Sciences
Government Postgraduate College, Jhang
0345-4707500,0313-7035400
0
GOVERNMENT POSTGRADUATE COLLEGE 1
JHANG
Department of Computer Science
++
PROGRAMMING USING C
QUESTION NO.01:- What is an Identifier? Discuss the two types of
Identifier in C++ Language.
identifier
The unique names used in the program to represent variables,
constants, functions and labels etc. are called Identifiers. We can use number
of characters for the name of an Identifier but only the first 31 characters
are significant to C++-compiler.
Following are the rules for writing a identifier name in C++ language.
First character must be an alphabetic character or underscore.
The identifier name must consist of only alphabet characters, digits or underscore.
The reserved word/keywords cannot be used as identifier name.
TYPES OF IDENTIFIER
There are two types of Identifier in C++-language. These are:
Standard Identifier User defined Identifier
STANDARD IDENTIFIERS
Like keywords, there are many predefined identifiers in C++-language.
The predefined identifier of C++-language used for special purposes in the
source program are called Standard Identifiers.
The names used for standard library functions are standard
Identifiers. In C++-language, cout, cin are objects that are mostly used for
input and output operations. Thus, the objects cin and cout are examples of
Standard Identifiers.
USER DEFINED IDENTIFIERS
The Programmer can define its own identifiers in C++-language
program such as variables, user defined functions and labels etc. The
identifiers defined by the programmer are called user defined identifiers.
Generally, these are the names that a programmer assigns to functions,
variables, data types etc. in the program.
QUESTION NO.02:-What are keywords? Give a list of keywords used
in C++ language.
KEYWORDS C++ -LANGUAGE KEYWORDS
Keywords are the words auto do goto signed Unsigned
break double if sizeof Void
whose meanings have already case else int static Volatile
been explained to the compiler. char enum long struct While
const extern register switch
The keywords are not used for continue float return typedef
default for short union
GOVERNMENT POSTGRADUATE COLLEGE 2
JHANG
Department of Computer Science
variable names in the program. The keywords are also called reserved words.
There are only 32 keywords available in C++ language. Following is the list of
keywords.
JHANG
Department of Computer Science
JHANG
Department of Computer Science
SOLUTION
#include<conio.h>
#include<iostream.h>
void main(void){ clrscr( );
Value of x is: = 10
int x,y,z; Value of y is: = 15
x=10; Value of z is: = 20
y=15;
z=20;
cout<<" Value of x is:= "<< x<<endl;
cout<<"Value of y is:= "<<y <<endl;
cout<<"Value of z is:= "<<z <<endl;
getch ( ); }
PROGRAM-02
Write a program to perform arithmetic operations on integer data values by using
all arithmetic operators.
SOLUTION
#include<conio.h>
#include<iostream.h>
void main(void){ clrscr( );
int a, b, c, d , r ;
a=5+2; Addition is: = 7
Subtraction is: = 3
b=5-2; Multiplication is: =10
c=5*3; Division is: =2
Remainder is:=1
d=5/2 ;
r=5%2;
cout<<" Addition is :="<< a<<endl;
cout<<" Subtraction is := "<<b<<endl;
cout<<" Multiplication is :="<<c<<endl;
cout<<" Division is :="<<d<<endl;
cout<<" Remainder is :="<<r<<endl;
getch ( ); }
PROGRAM -03
Write a program in C++ language to display Maximum and Minimum Ranges
for integer variable.
SOLUTION Minimum value for int variable is: = -32768
#include<conio.h> Maximum value for int variable is: = 32767
#include<iostream.h>
#include<limits.h>
void main(void) { clrscr( );
GOVERNMENT POSTGRADUATE COLLEGE 5
JHANG
Department of Computer Science
PROGRAM -04
Write a program in C++ language to display Maximum and Minimum Ranges
for short integer variable.
SOLUTION
#include<conio.h>
#include<iostream.h> Minimum value for short int is: = -32768
JHANG
Department of Computer Science
LONG INT
Long int data type is used to store large integer values. Long integer
data type occupies Four Bytes space in memory and range of values stored
in memory is from (-2147483648 to2147483647). Minimum long integer
value is defined by LONG_MIN and Maximum long integer Value is defined
by LONG_MAX.
PROGRAM-06
Write a program to display Maximum and Minimum values for long integer.
SOLUTION
#include<conio.h> Minimum value for long int is: = -2147483648
#include<iostream.h> Maximum value for long int is: = 2147483647
#include<limits.h>
void main(void ) { clrscr( );
long int min, max;
min= LONG_MIN;
max =LONG_MAX;
cout<<"Minimum value for long int is:="<<min<<endl;
cout<<"Maximum value for long int is:="<< max<<endl;
getch( );}
UNSIGNED INT
Unsigned integer can store only positive whole numbers. Unsigned
integer data type occupies two Bytes space in memory. It can store integer
value from 0 to 65,535. Minimum unsigned integer value is 0 and Maximum
unsigned integer value is defined by UINT_MAX.
PROGRAM-07
Write a program to display Maximum value for unsigned integer.
SOLUTION
#include<conio.h>
Maximum value for unsigned int is= 65535
#include<iostream.h>
#include<limits.h>
void main(void) { clrscr( );
unsigned int max;
max =UINT_MAX;
cout<<"Maximum value for unsigned int is:="<< max<<endl;
GOVERNMENT POSTGRADUATE COLLEGE 7
JHANG
Department of Computer Science
getch( ); }
Unsigned long
Unsigned long can store only positive whole numbers. It can store integer
value from 0 to 4294967295. It takes 4 bytes space in the memory. The
unsigned long int is also called unsigned long int.
ULONG_MAX represent the largest value that can be stored in unsigned
long int. it is found in limits.h header file.
PROGRAM-08
Write a program to display Maximum value for unsigned long integer.
SOLUTION Maximum value for unsigned int is= 4294967295
#include<conio.h>
#include<iostream.h>
#include<limits.h>
void main(void) { clrscr( );
unsigned long int max;
max =ULONG_MAX;
cout<<"Maximum value for unsigned long int is:="<< max<<endl;
getch( ); }
JHANG
Department of Computer Science
PROGRAM-09
Write a program in C++ language to show the sum and product of two floating
point numbers.
SOLUTION Sum is: = 11.7
#include<conio.h> Product is: =14.8
#include<iostream.h>
void main(void) { clrscr( );
float a, b;
a=1.45;
b=10.25;
cout<<" Sum is :=" <<(a+b )<<endl;
cout<<" Product is := " <<(a-b)<<endl;
getch ( ); }
PROGRAM-10
Write a program to display Maximum and Minimum values for float variable.
SOLUTION
#include<conio.h>
#include<iostream.h>
#include<float.h>
void main(void) { clrscr( ); Minimum value for float is: = 1.175*10 -38
+38
Maximum value for float is: = 3.40*10
float min, max;
min= FLT_MIN;
max = FLT_MAX;
cout<<"Minimum value for float is:="<<min<<endl;
cout<<"Maximum value for float is:="<< max<<endl;
getch( ); }
DOUBLE TYPE DATA
Double data type is also called a Real data type. It is used to store large
Real values. A double type variable takes 8 Bytes space in memory and can
store Real values from 1.7×10-308 to 1.7×10+308. It is an accurate up to 12
decimal places.
Minimum double value is defined by DBL_MIN and Maximum double
value is defined DBL_MAX. Minimum and Maximum values for double data
type are specified in float.h header file.
PROGRAM-11
Write a program to display Maximum and Minimum values for double type
variable.
GOVERNMENT POSTGRADUATE COLLEGE 9
JHANG
Department of Computer Science
SOLUTION
#include<conio.h>
#include<iostream.h>
Minimum value for double is: = 2.2*10-308
#include<float.h>
Maximum value for double is: = 1.7*10308
void main(void) { clrscr( );
double min, max;
min= DBL_MIN;
max = DBL_MAX;
cout<<"Minimum value for double is:=" <<min <<endl;
cout<<"Maximum value for double is:="<< max <<endl;
getch( ); }
LONG DOUBLE TYPE DATA
Long double type variable is used to store very large Real data values.
Storage capacity for long double variable is 10 bytes. Range of stored long
double type variable values in memory is from (1.1×10-4932 to 1.1×10+4932).
Minimum value for long double variable is defined by LDBL_MIN and
Maximum value for long double variable is defined by LDBL_MAX. Minimum
and Maximum values for long double data type are specified in float.h header
file.
PROGRAM-12
Write a program to display Maximum and Minimum values for long double
variable.
SOLUTION
#include<conio.h>
#include<iostream.h>
#include<float.h>
Minimum value for long double is:= 1.1*10-4932
void main(void) { clrscr( ); Maximum value for long double is: = 1.1*10+4932
long double min, max;
min= LDBL_MIN;
max = LDBL_MAX;
cout<<"Minimum value for long double is:=" <<min <<endl;
cout<<"Maximum value for long double is:="<< max<<endl;
getch( ); }
JHANG
Department of Computer Science
Maximum and Minimum values for chartered data types are specified
in limits header file. Minimum char values are defined by CHAR_MIN and
Maximum value for char value is CHAR_MAX.
PROGRAM-13
Write a program in C++-language to display character value on the computer
screen.
SOLUTION Character constant is= A
#include<conio.h>
#include<iostream.h>
void main(void) { clrscr( );
char num;
num='A' ;
cout<<" Character constant is= " << num<<endl;
getch( ); }
PROGRAM-14
Write a program that displays a single character value on the screen.
SOLUTION
#include<conio.h>
#include<iostream.h>
void main(void) { clrscr( );
char num; Character constant is= A
num= 'AA';
cout<<" Character constant is=" <<num<<endl;
getch( ); }
PROGRAM-15
Write a program that convert character value into integer value and show the
result on the screen.
SOLUTION Character code in integer format is=65
#include<conio.h>
#include<iostream.h>
void main(void) { clrscr( );
char num;
num= 'A';
int num1;
num1=num;
cout<<"Character code in integer format is = "<< num1<<endl;
getch( ); }
GOVERNMENT POSTGRADUATE COLLEGE 11
JHANG
Department of Computer Science
PROGRAM-16
Write a program to display Maximum and Minimum values for char variable.
SOLUTION
#include<conio.h>
#include<iostream.h>
#include<limits.h>
Minimum value for char variable is: = -128
void main(void ) { clrscr( ); Maximum value for char variable is: = 127
int min, max;
min= CHAR_MIN;
max = CHAR_MAX;
cout<<"Minimum value for char variable is:=" <<min <<endl;
cout<<" Maximum value for char variable is:="<< max<<endl;
getch( ); }
QUESTION NO.09:-What is variable? Illustrate with examples.
VARIABLE
A quantity whose value may change during the execution of the program
is called variable. It may be numeric/ non-numeric quantity. It is represented
by a symbol called variable name.
A Variable represents a storage or memory location inside Computer
memory. Data is stored in memory location. Name of the Memory location
i.e. Variable name remains fixed during the program execution but data
stored in that location may changes from time to time.
A variable is also known as object in C++ language. In C++, a variable
name consists of alphabets and digits.
The variables are created in RAM that is temporary memory. That is
why the data stored in RAM is also temporary. It can only be used and
processed during the execution of the program.
The data stored in a variable is automatically removed when program
ends.
JHANG
Department of Computer Science
JHANG
Department of Computer Science
PROGRAM-17
Write a program in C++ language to show declaration and initialization of
variables.
solution Resultant value of b is:=6.28318
#include<conio.h> Value of integer variable m is=75
#include<iostream.h>
void main(void) { clrscr( );
float p= 3.14159;
int m=75;
float b;
b=2*p;
cout<<"Resultant value of b is:= "<<b<<endl;
cout<<"Value of integer variable m is="<<m<<endl;
getch ( ); }
PROGRAM-18
Write a program in C++ language to assign values to different variables at the
time of declaration. Print the assigned values on the Computer screen.
solution
#include<conio.h> Value of integer variable abc is:=4
#include<iostream.h> Value of integer variable b is=1957;
void main(void) { clrscr( ); Value of floating point variable b is=3.14
JHANG
Department of Computer Science
A variable declared for one data type cannot be used for another data type.
C is a case sensitive language. Variable names with same spelling but in different
cases are treated as different variable names. For example rem , Rem and REM are
three different variables.
JHANG
Department of Computer Science
JHANG
Department of Computer Science
PROGRAM-19
Write a program C++-language to define the procedure of Define Directive.
SOLUTION
#include<conio.h> Resultant value of b is: =6.28318
#include<iostream.h>
#define PI 3.14159
void main(void) { clrscr( );
float b;
b=2*PI;
cout<<"Resultant value of bis:="<<b<<endl;
getch ( ); }
PROGRAM-20
Write a program in C++-language using Define Directive to find the area of a
circle. Where area=R2
SOLUTION
#include<conio.h> Area of the circle is:=314.159
#include<iostream.h>
#define PI 3.14159
void main(void) { clrscr( );
float area, R;
R=10;
area=PI*R*R;
cout<<"Area of the circle is :="<<area<<endl;
getch ( ); }
JHANG
Department of Computer Science
#include<iostream.h>
void main(void) { clrscr( ); Resultant value of b is: =6.28318
const float p= 3.14159;
float b;
b=2*p;
cout<<"Resultant value of b is:= "<<b<<endl;
getch ( ); }
PROGRAM-22
Write a program in C++-language to assign three values to three integer type
variables x ,y , z. add variables x ,y and multiply their sum to the variable z.
SOLUTION
#include<conio.h>
#include<iostream.h>
void main( ) { clrscr( );
int x ,y, z, sum;
cout<<" Enter value for X:= ";
Enter value for x:=5
cin>>x; Enter value for y:=10
cout<<" Enter value for Y:= "; Enter value for z:=15
Resultant value is :=225
cin>>y;
cout<<" Enter value for Z:= ";
cin>>z;
sum= x+y;
z*=sum;
cout<<" Resultant value is := "<<z<<endl;
getch( ); }
JHANG
Department of Computer Science
JHANG
Department of Computer Science
SOLUTION
#include<conio.h>
#include<iostream.h> Enter value for a:=10
void main(void) { clrscr( ); Enter value for b:=5
Sum is:=15
int a, b, add, sub, mul , div, rem; Subtraction is:=5
cout<<" Enter value for a:= "; Multiplication is:=50
Division is:= 2
cin>>a; Remainder is:=0
cout<<" Enter value for b:= " ;
cin>>b;
add= a+b;
sub= a-b;
mul=a*b;
div= a/b;
rem= a%b;
cout<<" Sum is:="<< add<<endl;
cout<<" Subtraction is:="<< sub<<endl;
cout<<" Multiplication is:="<< mul<<endl;
cout<<" Division is:="<< div<<endl;
cout<<" Remainder is:="<< rem<<endl;
getch( );}
PROGRAM-24
Write a program which reads two integer values and prints their Sum,
Difference, Product, Division and Remainder as shown in the output on Computer
screen.
SOLUTION
#include<conio.h>
#include<iostream.h> Enter First Integer:=4
void main(void) {clrscr( ); Enter Second integer:= 3
4+3=7
int a,b ; 4 -3=1
cout<<" Enter First Integer:= "; 4*3=12
4/3=1
cin>>a; 4%3=1
cout<<" Enter Second Integer:= ";
cin>>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( );}
GOVERNMENT POSTGRADUATE COLLEGE 20
JHANG
Department of Computer Science
PROGRAM-25
Write a program in C++-language to interchange the values of two variables
using assignment statement. Print the original and exchanged values on the Computer
screen.
SOLUTION
#include<conio.h>
#include<iostream.h> Values before interchange
Value of a:=10
void main(void) { clrscr( ); Value of b:=20
int a, b, temp ; Values after interchange
Value of a:=20
a=10; Value of b:=10
b=20;
cout<<"Values before interchange "<<endl;
cout<<"Value of a:="<< a<<endl;
cout<<" Value of b:="<< b<<endl;
temp=b;
b=a;
a=temp;
cout<<"Values after interchange “<<endl;
cout<<"Value of a:="<< a<<endl;
cout<<" Value of b:="<< b<<endl;
getch( ) ; }
PROGRAM-26
Write a program in C++ language to assign a numeric value to a variable year.
Calculate the number of months and print the result on Computer screen.
SOLUTION
#include<conio.h>
#include<iostream.h>
void main(void) { clrscr( );
Enter No. of years: = 4
int years, month; Number of months:=48
cout<<"Enter No. of years: = ";
cin>>years;
month=years*12;
cout<<" Number of months:="<<month<<endl;
getch( ); }
UNARY OPERATORS
The operators that need only one data item to operate on are called
unary operators. Minus(-) operator is an example of unary operator. This
operator is used to inverse the sign of the value. Furthermore ++(increment
GOVERNMENT POSTGRADUATE COLLEGE 21
JHANG
Department of Computer Science
JHANG
Department of Computer Science
PROGRAM-27
Write a program in C++ language that uses arithmetic assignment operators.
SOLUTION
#include<conio.h>
#include<iostream.h> Enter any No.:=5
Resultant value is := 20
void main(void) { clrscr( ); Resultant value is :=10
int ans; Resultant value is :=250
Resultant value is := 14
cout<<" Enter any No. := "; Resultant value is :=4
cin>>ans;
ans+=15;
cout<<" Resultant value is :="<<ans<<endl;
ans-=10;
cout<<" Resultant value is := "<<ans<<endl;
ans*=25;
cout<<" Resultant value is := "<<ans<<endl;
ans/=17;
cout<<" Resultant value is := "<<ans<<endl;
ans%=5;
cout<<" Resultant value is := "<<ans<<endl;
getch( ); }
JHANG
Department of Computer Science
JHANG
Department of Computer Science
a=5;
b=10;
Values before evolution
cout<<"Values before evolution "<<endl; Value of a:=5
cout<<"Value of a:="<< a<<endl; Value of b:=10
Resultant value:=15
cout<<" Value of b:="<< b<<endl; Values after evolution
res=a+b++; Value of a:=5
Value of b:=11
cout<<"Values after evolution "<<endl; Resultant value is:=15
cout<<"Value for a:="<< a<<endl;
cout<<" Value for b:="<< b<<endl;
out<<" Resultant value is:="<<res<<endl;
getch( ) ; }
PROGRAM-30
Write a program in C-language to evaluate an expression “++a+b++”.
Suppose the value of a=5 and value of b=10. Print the resultant value and values of
a and b before and after execution of expression.
SOLUTION
Values before evolution
#include<conio.h>
Value of a:=5
#include<iostream.h> Value of b:=10
void main(void) { clrscr( ); Resultant value:=16
Values after evolution
int a,b, res; Value of a:=6
a=5; Value of b:=11
Resultant value is:=15
b=10;
cout<<"Values before evolution "<<endl;
cout<<"Value of a:="<< a<<endl;
cout<<" Value of a:="<< a<<endl;
res=++a+b++;
cout<<"Values after evolution "<<endl;
cout<<"Value for a:="<< a<<endl;
cout<<" Value for b:="<< b<<endl;
cout<<" Resultant value is:="<<res<<endl;
getch( ) ; }
QUESTION NO.28:-What is Decrement operator? Differentiate
between Prefix and Postfix Decrement operators.
DECREMENT OPERATOR
Decrement operator is represented by double Minus (- -) sign. This
operator is used to subtract 1 from the value of the variable. This operator
can be used before and after the variable name.
GOVERNMENT POSTGRADUATE COLLEGE 25
JHANG
Department of Computer Science
PROGRAM-31
Write a program in C++-language to evaluate an expression “- -a+b- -”.
Suppose the value of a=5 and value of b=10. Print the resultant value and values of
a and b before and after execution of expression.
SOLUTION Values before evolution
# include<conio.h> Value of a:=5
#include<iostream.h> Value of b:=10
Resultant value:=14
void main(void) { clrscr( );
Values after evolution
int a,b, res; Value of a:=4
Value of b:=9
a=5;
Resultant value is:=14
b=10;
cout<<"Values before evolution "<<endl;
cout<<"Value of a:="<< a<<endl;
cout<<" Value of b:="<< b<<endl;
res= - -a+b- -;
cout<<"Values after evolution "<<endl;
cout<<"Value for a:="<< a<<endl;
cout<<" Value for b:="<< b<<endl;
cout<<" Resultant value is:="<<res<<endl;
getch( ) ; }
QUESTION NO.29:-What
is Compound Assignment Operator?
COMPOUND ASSIGNMENT OPERATOR
An assignment operator used to assign one value to more variables is
called Compound Assignment Operator. Compound assignment operators
are used to perform mathematical operations more easily.
PROGRAM-32
Write a program that uses the compound assignment statement.
GOVERNMENT POSTGRADUATE COLLEGE 26
JHANG
Department of Computer Science
SOLUTION
# include<conio.h>
#include<iostream.h>
void main(void) { clrscr( );
int a, b , c ,d; Result of multiplication is :=1000
a=b=c=10;
d=a*b*c;
cout<<"Result of multiplication is :="<<d<<endl;
getch( ); }
PROGRAM-33
Suggest the output of the following program.
SOLUTION
#include<conio.h> Resultant value of c is :=65
#include<iostream.h> Resultant value of j is :=250
PROGRAM-34
Evaluate following expressions and display result on computer screen.
solution
#include<conio.h>
#include<iostream.h>
Resultant value of a is: =13
void main(void) { clrscr( );
Resultant value of b is :=12
int a=1,b=2,c=3; Resultant value of c is :=10
a+=b+=c+=7;
cout<<"Resultant value of a is :="<<a<<endl;
cout<<"Resultant value of b is :="<< b<<endl;
cout<<"Resultant value of c is :="<<c<<endl;
getch( ); }
QUESTION NO.30:-What is the Order of Precedence? Explain the
Order of Precedence of Arithmetic Operators.
ORDER OF PRECEDENCE
C++ language treats arithmetic operators with different importance,
known as order of precedence of arithmetic operators. Furthermore, order
GOVERNMENT POSTGRADUATE COLLEGE 27
JHANG
Department of Computer Science
EXAMPLE:-In order to calculate the volume of a box with sides x, y, z, We use the
following formula. Volume=x*y*z Where x ,y , z are variables and known as
operands and multiplication sign is called operator.
JHANG
Department of Computer Science
RELATINAL EXPRESSIONS
Relational Expressions are used to compare between two or more
values and show the result which value is greater or least. In these
expressions, Relational operators are used. Relational operators are
<,>,<=,>=, != etc.
LOGICAL EXPRESSIONS
Logical Expressions are used to compare between two or more
Relational Expressions. Three operators AND (&&), OR (||) and NOT (!) are
used in Logical Expressions.
QUESTION NO.33:-What are Relational Operators?
RELATIONAL OPERATORS
Operators used to specify a Operator Symbol Example
relation between two Expressions or Grater than > a>b
Greater than or equal to >= a>=b
two variables are called Relational Less than < a<b
Operators. Relational Operators are Less than or equal to <= a<=b
Equal to == a==b
used to compare two expressions. Not equal to != a!=b
The expressions used with Relational
Operators may be constants, variables or arithmetic operators. Following is
list of Relational operators commonly used in C++ Language.
JHANG
Department of Computer Science
c=a%b;
d=a/++b;
e=(++a)/(b--);
a/=b+=2;
b+= -a;
cout<<" Resultant value of c is:="<<c<<endl;
cout<<" Resultant value of d is:="<<d<<endl;
cout<<" Resultant value of e is:="<<e<<endl;
cout<<" Resultant value of a is:="<<a<<endl;
cout<<" Resultant value of b is:="<<b<<endl;
getch( ); }
JHANG
Department of Computer Science
L-value :-
L-value is an operand that can be written on the left side of assignment
operator (=). It must be a variable or an expression.
QUESTION NO.36:-What are comments? Write in detail.
COMMENTS
Comments are non-executable statements. These are used to add
remarks in the program. These are usually written to explain the logic of the
program. Compiler ignores comments written in the program. Compiler does
not take any action on the comment statement.
TYPES OF COMMENTS
Comments are of two types. These types are:
Single line commentsMultiple line comments
SINGLE LINE COMMENTS
Single line Comments consist of a single line non-executable statement.
These Comments are also called Double Slash Comments. In order to make
a single line non-executable comment, we use double slash (//).
PROGRAM-36
Write a program in C++ language to explain single line comments.
SOLUTION
#include<conio.h> Government Islamia College, Chiniot
#include<iostream.h>
void main(void) { clrscr( ); // This is a simple program.
cout<<" Government Islamia College, Chiniot "<<endl;
getch( ); }
EXPLANATION
In the above program, a single line comment is added. When we will
compile program, it will print " Government Islamia College, Chiniot " on the
monitor screen. The statement starting with double slash "This is a simple
program." Will not be executed.
MULTIPLE LINE COMMENTS
Multiple line comments consist of more than one line non-executable
statements. These comments help us to write a paragraph or set of
statements to explain the working of the program. We can write multiple line
comments using following structure.
/*-------------------------------
……………………………..
………………………………
………………………………
---------------------------------- */
GOVERNMENT POSTGRADUATE COLLEGE 31
JHANG
Department of Computer Science
PROGRAM-38
Write a program in C++-language to find the volume of a cylinder by using
const qualifier. Where volume=R2H.
SOLUTION
#include<conio.h>
#include<iostream.h>
void main(void) { clrscr( );
float R, H, Vol;
const float PI=3.1415; Volume of Cylinder is: =741.237
R=5.5;
H=7.8;
Vol=PI*R*R*H;
cout<<"Volume of Cylinder is: ="<< Vol<<endl;
getch(); }