0% found this document useful (0 votes)
261 views

C++ Lab Manual PDF

The document discusses various C++ standard library functions. It describes that the standard library can be categorized into the standard function library and object oriented class library. The standard function library consists of general purpose standalone functions inherited from C. The object oriented class library is a collection of classes and associated functions. It then provides examples of some common C++ programs like calculating even/odd numbers, swapping values, generating Fibonacci series, finding largest among 3 numbers, checking palindrome, finding LCM/GCD, square root, temperature conversion, calculating percentage, and simple interest.

Uploaded by

Bharath Hegdal
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
261 views

C++ Lab Manual PDF

The document discusses various C++ standard library functions. It describes that the standard library can be categorized into the standard function library and object oriented class library. The standard function library consists of general purpose standalone functions inherited from C. The object oriented class library is a collection of classes and associated functions. It then provides examples of some common C++ programs like calculating even/odd numbers, swapping values, generating Fibonacci series, finding largest among 3 numbers, checking palindrome, finding LCM/GCD, square root, temperature conversion, calculating percentage, and simple interest.

Uploaded by

Bharath Hegdal
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

Study of C++ Standard library functions

The C++ Standard Library can be categorized into two parts:

 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

 String and character handling

 Mathematical

 Time, date, and localization

 Dynamic allocation

 Miscellaneous

 Wide-character functions

The Object Oriented Class Library:


Standard C++ Object Oriented Library defines an extensive set of classes that provide support for a number of common activiti es, including I/O, strings, and
numeric processing. This library includes the following:

 The Standard C++ I/O Classes

 The String Class

 The Numeric Classes

 The STL Container Classes

 The STL Algorithms

 The STL Function Objects

 The STL Iterators

 The STL Allocators

 The Localization library

 Exception Handling Classes

 Miscellaneous Support Library

1. Even and Odd Program in C++

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:

Algorithm for Even and Odd Program in C++


 Step 1: Start
 Step 2: [ Take Input ] Read: Number
 Step 3: Check: If Number%2 == 0 Then
 Print: N is an Even Number.
 Else
 Print: N is an Odd Number.
 Step 4: Exit
Program:
#include<iostream>
using namespace std;
int main()
{
int no;
cout<<"Enter any num: ";
cin>>no;
if(no%2==0)
{
cout<<"Even num";
}
else
{
cout<<"Odd num";
}
return 0;
}
Output:
Enter any num: 5
Odd num

2. C++ program to swap two numbers

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

3. C++ Program to Generate Fibonacci Series

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

5. C++ program to Check Number is Palindrome or not

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.

Steps to write program:


Get the value from user.
Reverse it.
Compare it with the number entered by the user.
If both are same then print palindrome
Else print not a palindrome.
Program:
#include<iostream>
using namespace std;
int main()
{
int a,no,b,temp=0;

cout<<"Enter any num: ";


cin>>no;
b=no;
while(no>0)
{
a=no%10;
no=no/10;
temp=temp*10+a;
}
if(temp==b)
{
cout<<"Palindrome";
}
else
{
cout<<"Not Palindrome";
}
return 0;
}

Output:
Enter any num: 143
Not Palindrome

6. C++ program to find LCM of two numbers

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;

Suppose find LCM of 3 and 4


Multiple of 3 : 3, 6, 9, 12, 15, 18, 21, 24,.......
Multiple of 4 : 4, 8, 12, 16, 20, 24, 28,.....

So, common multiple of 3 and 4 is 12, 24


and Least Common Multiple is 12
LCM of 3 and 4 is: 12
Program:

#include <iostream>

using namespace std;


int lcm(int,int);
int main()
{
int a,b;

cout<<"Enter two numbers: ";


cin>>a;
cin>>b;
lcm(a,b);

}
//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

7. C++ program to find Square Root of a Number

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:

Enter any number: 4

Square root of 4 is 2

8. C++ program to Convert Celsius to Fahrenheit

Theory: Fahrenheit and Celsius are two unit for measure temperature. We have standard formula to Convert Celsius to Fahrenheit using this formula you can

change temperature Fahrenheit to Celsius.

Program:

#include<iostream>
using namespace std;

int main()
{
float cel, far;

cout<<"Enter temp. in Celsius: ";


cin>>cel;
far = cel * 9/5 + 32;
cout<<"Temp. in Fahrenheit: "<<far;
return 0;
}

Output:
Enter temp. in Celsius: 36
Temp. in Fahrenheit: 96.000

9. Calculate Percentage of Student Marks Program in C++

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).

Algorithm to Calculate Percentage of Students Marks

 First Receive Student Marks of each subject.


 Calculate sum of all subjects marks.
 divide total marks by number of subject (percentage=total marks / number of subject).
 Finally print percentage on screen.

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 %

10. C++ Program to Find the Simple Interest

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.

Simple Interest = (P * T * R)/100


 P = Principle amount
 T= Time Period
 R= Rate of interest

Program:
#include<iostream>
using namespace std;
int main()
{
float amount, rate, time, si;
cout<<"Enter Principal Amount: ";
cin>>amount;

cout<<"Enter Rate of Interest: ";


cin>>rate;

cout<<"Enter Period of Time: ";


cin>>time;

si = (amount * rate * time) / 100;


cout<<"Simple Interest: "<<si;

return 0;
}
Output:
Enter Principal Amount: 3000
Enter Rate of Interest: 2
Enter Period of Time: 3
Simple Interest: 180

11. C++ Program to Check Number is Positive or Negative

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

12. Find Area of Circle Program in C++

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;

area of circle = 3.14*(radius*radius);

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

13. Sort an Array Elements in Ascending Order in C++

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

14. C++ Program to find ncR and nPr of any Number

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); //function declaration


int main()
{
int n, r;
long int ncr, npr;
cout<<"Enter Any Value for n: ";
cin>>n;
cout<<"Enter Any Value for r: ";
cin>>r;
npr=fact(n)/fact(n-r); // function calling
ncr=npr/fact(r); //function calling
cout<<"NPR value = "<<npr<<"\n";
cout<<"NCR value = "<<ncr<<"\n";

}
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

15. C++ Program to Multiply Two Matrices

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

Multiplying two Matrices........


Multiplication of Two Matrices
23 23
31 21

16. Program in C++ to Convert Binary to Decimal

Theory: In this types of program we takes a binary number as input and converts it into decimal number.

Program:

 Take a binary number as input.


 Multiply each digits of the binary number starting from the last with the powers of 2 respectively.
 Add all the multiplied digits.
 Here total sum gives the decimal number.

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

17. C++ Program to Convert Decimal Number to Binary Number

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.

Decimal to Binary Conversion Algorithm

 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

18. C++ Program to Print Triangle of Stars

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:
*
***
*****
*******
*********

You might also like