C++ Lab Manual PDF
C++ Lab Manual PDF
The Standard Function Library: This library consists of general-purpose, stand-alone functions that are not part of any class. The function library is
inherited from C.
The Object Oriented Class Library: This is a collection of classes and associated functions.
Standard C++ Library incorporates all the Standard C libraries also, with small additions and changes to support type safety.
The Standard Function Library: The standard function library is divided into the following categories:
I/O
Mathematical
Dynamic allocation
Miscellaneous
Wide-character functions
Theory: In general Even numbers are that which are divisible by 2, and which numbers are not divisible 2 is called Odd number.
But in term of programming for find even number we check remainder of number is zero or not, If remainder is equal to zero that means number is divisible
by 2. To find remainder of any number we use modulo (%) operator in C language which return remainder as result.
Flow Chart:
Theory: To write swap two numbers program in C++ is very simple and easy just you need 3 variable and = operator. Swap numbers means exchange the
values of two variables with each other. For example variable num1 contains 200 and num2 contains 400 after swap their values num1 contains 400 and num2
contains 200.
Flowchart:
Program:
#include<iostream>
using namespace std;
int main()
{
int a,b,c;
cout<<"Enter value of a: ";
cin>>a;
cout<<"Enter value of b: ";
cin>>b;
c=a;
a=b;
b=c;
cout<<"After swap a: "<<a<<"b: "<<b;
return 0;
}
Output:
Enter value of a: 10
Enter value of b: 20
After swap a: 20 b: 10
Theory: Fibonacci Series is in the form of 0, 1, 1, 2, 3, 5, 8, 13, 21 ... To find this series we add two previous terms/digits and get next term/number.
Program:
#include<iostream>
using namespace std;
int main()
{
int i, no, first=0, second=1, next;
first=0;
second=1;
cout<<"Enter number of terms for Series: ";
cin>>no;
cout<<"Fibonacci series are: \n";
for(i=0; i<no; i++)
{
cout<<"\n"<<first;
next = first + second;
first = second;
second = next;
}
return 0;
}
Output:
Enter number of terms for Series: 7
Fibonacci series are:
0
1
1
2
3
5
8
4. C++ program to Find Largest Number among three numbers
Program:
#include<iostream>
using namespace std;
int main()
{
int a, b, c;
cout <<"Enter any three numbers: ";
cin>>a;
cin>>b
cin>>c;
if(a>=b && a>=c)
{
cout<<"Largest number: "<<a;
}
if(b>=a && b>=c)
{
cout<<"Largest number: "<<b;
}
if(c>=a && c>=b)
{
cout<<"Largest number: "<<c;
}
return 0;
}
Output:
Enter any three numbers:
15
30
20
Largest number: 30
Theory: A Palindrome number is a number that remains the same when its digits are reversed. Like 16461, for example: we take 121 and reverse it, after
revers it is same as original.
Output:
Enter any num: 143
Not Palindrome
Theory: To find the LCM (Least Common Multiple) of two or more numbers, make multiple of numbers and choose common multiple. Then take lowest
common multiple, this lowest common multiple is LCM of numbers. For example;
#include <iostream>
}
//function to calculate l.c.m
int lcm(int a, int b)
{
int m,n;
m=a;
n=b;
while(m!=n)
{
if(m < n)
{
m=m+a;
}
else
{
n=n+b;
}
}
cout<<"\nL.C.M of "<<a<<" and "<<b<<" is "<<m;
return 0;
}
Output:
Enter any two number:
3
15
L.C.M of 3 & 15 is 15
Theory: For calculate Square Root of a number we multiply number by 0.5 because square root of any number means power of 1/2 and 1/2=0.5. And one
another method for this program is use sqrt() function it is pre-defined in math.h header file.
Program:
#include <iostream>
#include<math.h>
using namespace std;
int main()
{
float sq,n;
cout<<"Enter any number:";
cin>>n;
sq=sqrt(n);
cout<<"Square root of "<<n<<" is "<<sq;
return 0;
}
Output:
Square root of 4 is 2
Theory: Fahrenheit and Celsius are two unit for measure temperature. We have standard formula to Convert Celsius to Fahrenheit using this formula you can
Program:
#include<iostream>
using namespace std;
int main()
{
float cel, far;
Output:
Enter temp. in Celsius: 36
Temp. in Fahrenheit: 96.000
Theory: Write code for calculate percentage of marks program in C++. For calculate percentage of marks obtained by student, divide total marks by number
of subject (percentage=total marks / number of subject).
Program:
#include<iostream>
using namespace std;
int main()
{
int no, i;
float marks[10], per=0, total=0;
cout<<"Enter number of subject: ";
cin>>no;
cout<<"Enter marks of "<<no<<" subjects";
for(i=0; i<no; i++)
{
cin>>marks[i];
}
for(i=0; i<no; i++)
{
total=total+marks[i];
}
per=total/no;
cout<<"Percentage: "<<per;
return 0;
}
Output:
Enter number of subject: 4
Enter marks of 4 subject:
80
93
87
95
Percentage: 88.75 %
Theory: There are three values involve in simple interest, Principal, Rate and Time. Simple interest is simple and easiest way to calculate interest charge on a
loan. This type of interest usually applies for short-term loans.
Program:
#include<iostream>
using namespace std;
int main()
{
float amount, rate, time, si;
cout<<"Enter Principal Amount: ";
cin>>amount;
return 0;
}
Output:
Enter Principal Amount: 3000
Enter Rate of Interest: 2
Enter Period of Time: 3
Simple Interest: 180
Theory: In this porgram we check given number is +ve or -ve number if number is greater than 0 it will positive if number is less than 0 i will negative
number. To write this code we need only if....else conditional statement.
Program:
#include<iostream>
using namespace std;
int main()
{
int num;
cout<<"Enter any number: ";
cin>>num;
if (num > 0)
{
cout<<num<<" is a Positive number \n";
}
else if (num < 0)
{
cout<<num<<" is a Negative number \n";
}
else
{
cout<<"0 is Neither Positive nor Negative";
}
return 0;
}
Output:
Enter any Number: 15
15 is a Positive number
Enter any Number: -40
-40 is a Negative number
Enter any Number: 0
0 is neither Positive nor Negative
Theory: In C++ programming you can easily write any program, for print message on screen use cout<<" " and for receive input use cin>>. For calculate area
of circle you need radius of circle and apply below formula;
Program:
#include<iostream>
using namespace std;
int main()
{
int radius;
float area_circle=0;
cout<<"Enter radius of circle: ";
cin>>radius;
area_circle=3.14*(radius*radius);
cout<<"Arial of circle: "<<area_circle;
return 0;
}
Output:
Enter radius of circle: 5
Area of Circle: 78.5
Theory: Sort an array elements means arrange elements of array in Ascending Order and Descending Order. You can easily sort all elements using bubble
sort.
Different Ways to Sort Array Elements
There are literally hundreds of different ways to sort arrays. The basic goal of each of these methods is the same: to compare each array element to another
array element and swap them if they are in the wrong position.
Bubble Sort
Exchange Sort
Selection Sort
Insertion Sort
Shell Sort
Quick Sort
Merge Sort
Program:
#include<iostream>
using namespace std;
int main()
{
int i,a[10],temp,j;
clrscr();
cout<<"Enter any 10 num in array: \n";
for(i=0;i<=10;i++)
{
cin>>a[i];
}
cout<<"\nData before sorting: ";
for(j=0;j<10;j++)
{
cout<<a[j];
}
for(i=0;i<=10;i++)
{
for(j=0;j<=10-i;j++)
{
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
cout<<"\nData after sorting: ";
for(j=0;j<10;j++)
{
cout<<a[j];
}
return 0;
}
Output:
Enter any 10 num in array:
2 5 1 7 5 3 8 9 11 4
Data After Sorting: 1 2 3 4 5 7 8 9 11
Theory: Basically in NCR, AB is a single combination. But in NPR, AB and BA are two different permutations of a combination of A and B. The difference
is quite clear and is easy for us to note that permutations are always equal to or greater in most cases than the combinations of elements. Also note that any
number raised to the power of zero is one. This is a situation encountered during calculation of NCR and NPR.
Program:
#include<iostream>
using namespace std;
}
long int fact(int x) //defining the function
{
int i, f=1;
for(i=2; i<=x; i++)
{
f=f*i;
}
return f;
}
Output:
Enter Any Value for n: 3
Enter Any Value for r: 2
NPR Value: 6
NCR Value: 3
Theory: A matrix is a rectangular array of numbers that is arranged in the form of rows and columns. A 2*2 matrix has 2 rows and 2 columns; a 3*3 matrix
has 3 rows and 3 columns.
To write matrices program in C++ we need receive two matrices value from user after this process we start multiplying the two matrices and store the
multiplication result inside any variable and finally store the value of sum in the third matrix say mat3[ ][ ].
Program:
#include<iostream>
using namespace std;
int main()
{
int mat1[2][2], mat2[2][2], mat3[2][2], sum=0, i, j, k;
cout<<"Enter First Matrix Element (2*2): ";
for(i=0; i<2; i++)
{
for(j=0; j<2; j++)
{
cin>>mat1[i][j];
}
}
cout<<"Enter Second Matrix Element (2*2): ";
for(i=0; i<2; i++)
{
for(j=0; j<2; j++)
{
cin>>mat2[i][j];
}
}
cout<<"Multiplying two Matrices........\n";
for(i=0; i<2; i++)
{
for(j=0; j<2; j++)
{
sum=0;
for(k=0; k<2; k++)
{
sum = sum + mat1[i][k] * mat2[k][j];
}
mat3[i][j] = sum;
}
}
cout<<"\nMultiplication of Two Matrices : \n";
for(i=0; i<2; i++)
{
for(j=0; j<2; j++)
{
cout<<mat3[i][j]<<" ";
}
cout<<"\n";
}
return 0;
}
Output:
Enter First Matrix Element (2*2): 2
3
4
1
Enter Second Matrix Element (2*2): 7
4
3
5
Theory: In this types of program we takes a binary number as input and converts it into decimal number.
Program:
Program:
#include<iostream>
using namespace std;
int main()
{
long int bin_number, dec_number=0, i=1, rem;
cout<<"Please Enter any Binary Number: ";
cin>>bin_number;
while(bin_number!=0)
{
rem=bin_number%10;
dec_number=dec_number+rem*i;
i=i*2;
bin_number=bin_number/10;
}
cout<<"Equivalent Decimal value:"<<dec_number;
return 0;
}
Output:
Please Enter any Binary Number: 1001
Equivalent Decimal value: 9
Theory: Decimal number is a base 10 number because it ranges from 0 to 9, there are total 10 digits between 0 to 9. Any combination of digits is decimal
number such as 213, 385, 592, 2, 0, 8 etc.
Binary number is a base 2 number because it is either 0 or 1. Any combination of 0 and 1 is binary number such as 10101, 1001 , 1111, 10010 etc.
With the help of C++ Language you can convert any number from decimal to binary and binary to decimal, binary to octal, binar y to hexadecimal, decimal to
octal and etc.
Step 1: Divide the number by 2 through % (modulus operator) and store the remainder in array
Step 2: Divide the number by 2 through / (division operator)
Step 3: Repeat the step 2 until the number is greater than zero
Program:
#include<iostream>
using namespace std;
int main()
{
int no,rem[20],i=0,j;
cout<<"Enter any Decimal Number: ";
cin>>no;
while(no>0)
{
rem[i]=no%2;
i++;
no=no/2;
}
cout<<"Equivalent Binary Number is: ";
for(j=i-1;j>=0;j--)
{
cout<<rem[j];
}
return 0;
}
Output:
Enter any Decimal Number: 12
Equivalent Binary Number is: 1100
Theory: In C++ language you can print any star patter, here you need nested loop first loop for print star and inner loop is used for line break.
Program:
#include<iostream>
using namespace std;
int main()
{
int i,j,k;
clrscr();
for(i=1; i<=5; i++)
{
for(j=4; j>=i; j--)
{
cout<<" ";
}
for(k=1; k<=(2*i-1); k++)
{
cout<<"*";
}
cout<<"\n";
}
return 0;
}
Output:
*
***
*****
*******
*********