You are on page 1of 26

C++ Lab Practicals

LAB PRACTICAL

Q.1. Write a program in c++ to accept 2 no’s from user, find out the sum and average of given input
number?
Program code:

#include<iostream.h>
#include<conio.h>
Void main()
{
int num1,num2,Sum=0,Avg=0;
float Per;
cout<<”Enter 2 number=”;
cin>>num1>>num2;

Sum=num1+num2
Avg=Sum/5;

cout<<”\nAggregate Marks=”<<Sum;
cout<<”\nPercentage Marks=”<<Avg;
getch()
}

Q.2. Write a program in c++ to accept marks by a student in five different subjects are input
through the keyword, find out the aggregate marks and percentage marks obtained by the student?
Assume that the maximum marks that can be obtained by a student in each subject is 100.
Program code:

#include<iostream.h>
#include<conio.h>

Void main()
{
int sub1,sub2,sub3,sub4,sub5,Sum;
float Per;
cout<<”Enter marks in 5 subject=”;
cin>>sub1>>sub2>>sub3>>sub4>>sub5;

Sum=sub1+sub2+sub3+sub4+sub5;
Per=sum/5;

cout<<”\nAggregate Marks=”<<Sum;
cout<<”\nPercentage Marks=”<<Per
getch()
}

Khan S. Alam 1 https://E-next.in

https://E-next.in
C++ Lab Practicals

Q.3. Ashok’s basic salary is input through the keyword. His dearness allowance is 30% of basic
salary, house rent allowance is 40% of basic salary and medical allowance is 10% of basic salary.
Write a program to calculate his gross salary.
Program Code:
#include<iostream.h>
#include<conio.h>
void main()
{
float B_salary, DA, HRA,G_salary;
clrscr();
cout<<” Enter the basic salary of Ashok =”;
cin>>B_salary;

DA=(B_salary*30)/100;
HRA=(B_salary*40)/100;
MA =(B_salary*10)/100;
G_salary=B_salary+DA+HRA+MA;

cout<<”\n Total Gross Salary of Ashok=”<<G_salary;


getch();
}

Q.4. Girish’s basic salary is input through the keyword. His dearness allowance is 30% of basic
salary, house rent allowance is 70% of basic salary, medical allowance is 10% of basic salary
and PF is 5% of basic salary. Write a program to calculate his gross salary and net salary.
Program Code:
#include<iostream.h>
#include<conio.h>
void main()
{
float B_Salary, DA, HRA,G_Salary,PF,Net_Salary;
clrscr();
cout<<” Enter the basic salary of Ashok =”;
cin>>B_Salary;

DA=(B_Salary*30)/100;
HRA=(B_Salary*40)/100;
MA =(B_Salary*10)/100;
G_Salary=B_Salary+DA+HRA+MA;
PF=(G_Salary*5)/100;
Net_Salary=G_Salary-PF;
cout<<”\n Total Gross Salary of Ashok=”<<G_Salary;
cout<<”\n And Net Salary =” <<Net_Salary
getch();
}

Khan S. Alam 2 https://E-next.in

https://E-next.in
C++ Lab Practicals

Q.5. If a five-digit number is input through the keyword, write a program to calculate the sum of its
digits.

Program Code:

#include<iostream.h>
#include<conio.h>
Void main()
{
int num,a,sum=0;
clrscr();
cout<<”Enter a five digit number (less than 32767)”;
cin>>num;

a=num%10;
sum=sum+a;
num=num/10;

a=num%10;
sum=sum+a;
num=num/10;

a=num%10;
sum=sum+a;
num=num/10;

a=num%10;
sum=sum+a;
num=num/10;

a=num%10;
sum=sum+a;
num=num/10;

cout<<”\n The sum of five-digit number=”<<sum;


getch();

Khan S. Alam 3 https://E-next.in

https://E-next.in
C++ Lab Practicals

LAB PRACTICAL ON DECISION CONTROL STRUCTURE

Q.1. Write a program in C++ to accept any integer value through the keyword, find out whether it is
an odd number or even number?

Program Code:

#include<iostream.h>
#include<conio.h>

void main()
{
int num;
clrscr();
cout<<”Enter any number=”;
cin>>num;

if(num%2==0)
{
cout<<”The number is even”;
}
else
{
cout<<”The number is odd”;
}
cout<<”\n press any key to exit…”;
getch();
}

Khan S. Alam 4 https://E-next.in

https://E-next.in
C++ Lab Practicals

Q.2. Accept year through the keyword. Write a program to determine whether the year is a leap
year or not.
Program Code:

#include<iostream.h>
#include<conio.h>
void main()
{
int year;
cout<<”Enter any year=”;
cin>>year;
if(year%4==0)
{
Cout<<”\nLeap year”;
}
else
{
Cout<<”\nThis is not a leap year”;
}
getch();
}

Khan S. Alam 5 https://E-next.in

https://E-next.in
C++ Lab Practicals

Q.3. Write a program to find the greatest of the three numbers entered through the keyboard
using conditional operators.

Program Code:
#include<iostream.h>
#include<conio.h>
void main()
{
int num1, num2, num3;
cout<<”Enter any three positive integer number=”;
cin>>num1>>num2>>num3;

if(num1>num2 && num1>num3)


{
cout<<”num1 is greater”;
}
else if(num2>num1 && num2>num3)
{
cout<<”num2 is greater”;
}
else if (num3>num1 && num3>num2)
{
cout<<”num3 is greater”;
}
else if(num1==num2 && num2==num3)
{
cout<<”Number is equal”;
}
else
{
cout<<”You entered wrong no”;
}
getch();
}

Khan S. Alam 6 https://E-next.in

https://E-next.in
C++ Lab Practicals

Q.4. Write a program in C++ to accept one number from user, find out whether input no is positive
or negative?

Program Code:
#include<iostream.h>
#include<conio.h>
void main()
{
int num;
cout<<”Enter no=”;
cin>>num;
if(num>0)
{cout<<”Value is positive”;}
else{ cout<<”Value is negative”;}
getch()}

Khan S. Alam 7 https://E-next.in

https://E-next.in
C++ Lab Practicals

Q.5. Write a program in C++ to accept 3 subject marks of a student, find sum and average of given
marks and find grade according to the following conditions.
Average Marks Grade
75 to 100 Distinction
60 to 74 First Division
50 to 59 Second Division
40 to 59 Third Division
0 to 39 Fail

Program Code:
#include<iostream.h>
#include<conio.h>
void main()
{
int sub1,sub2,sum3,sum=0,avg=0;
cout<<”Enter three subject marks=”;
cin>>sub1>>sub2>>sub3;
sum=sub1+sub2+sub3;
avg=sum/3;

if(avg>75)
{
cout<<”Distinction”;
}
else if(avg>=60 && avg<75)
{
cout<<”First Division”;
}
else if(avg>=50 && avg<60)
{
cout<<”Second Division”;
}
else if(avg>=40 && avg<50)
{
cout<<”Pass”;
}
else if(avg<40)
{
cout<<”Fail”;
}
else
{
cout<<”You typed wrong marks”;
}
getch();
}

Khan S. Alam 8 https://E-next.in

https://E-next.in
C++ Lab Practicals

Q.6. Write a program in C++ to accept two numbers from user and display their addition,
subtraction, multiplication and division through switch case.
Program Code:
#include<iostream.h>
#include<conio.h>
void main()
{
int choice,a,b,sum,div,sub,mul;
clrscr();
cout<<”\n press<1> for Addition program=”;
cout<<”\n press<2> for Subtraction program=”;
cout<<”\n press<3> for multiplication program=”;
cout<<”\n press<4> for division program =”;
cout<<”\n Select choice=”;
cin>>choice;

switch(choice)
{
case 1: cout<<”Enter two no=”;
cin>>a>>b;
sum=a+b;
cout<<”Sum of two no=”<<sum;
break;

case 2: cout<<”Enter two no=”;


cin>>a>>b;
sub=a-b;
cout<<”Sum of two no=”<<sub;
break;

case 3: cout<<”Enter two no=”;


cin>>a>>b;
mul=a*b;
cout<<”Sum of two no=”<<mul;
break;

case 4: cout<<”Enter two no=”;


cin>>a>>b;
sum=a/b;
cout<<”Sum of two no=”<<sum;
break;
default: cout<<”You select wrong choice”;
break;
}
getch();
}

Khan S. Alam 9 https://E-next.in

https://E-next.in
C++ Lab Practicals

LAB PRACTICAL ON LOOP

Q.1. Write a program in C++ to accept 10 number from user, find sum and average of given input
value through for loop.

Program Code:
#include<iostream.h>
#include<conio.h>
void main()
{
int num,i,Sum=0,Avg=0;
cout<<”Enter ten no=”;
for(i=0;i<10;i++)
{
cin>>num;
Sum=sum+num;
}
Avg=Sum/10;
cout<<”Sum of ten number=”<<sum;
cout<<”Avg of ten number=”<<Avg;
getch();
}

Khan S. Alam 10 https://E-next.in

https://E-next.in
C++ Lab Practicals

Q.2. Write a program in C++ to accept 10 number from user, find total number of even and odd of
given input value through for loop.
Program Code:
#include<iostream.h>
#include<conio.h>
void main()
{
int num,i,Even=0,Odd=0;
cout<<”Enter ten no=”;
for(i=0;i<10;i++)
{
cin>>num;
if(num%2==0)
{
Even=even+1;
}
else
{
Odd=odd+1;
}
}
cout<<”\n Total even no=”<<Even;
cout<<”\n Total odd no=”<<Odd;
getch();
}

Q.3. Write a program to find the factorial value of any number entered through the keyboard.

#include<iostream.h>
#include<conio.h>
void main()
{
int num,fact=1;
cout<<”Enter any number (less than 34)”;
cin>>num;
while(num>0)
{
fact=num*fact;
num--;
}
cout<<”Factorial Value=”<<fact;
getch();
}

Khan S. Alam 11 https://E-next.in

https://E-next.in
C++ Lab Practicals

Q.4. Write a program in C++ to accept one five-digit number from user, find out the reverse of the
given number.

#include<iostream.h>
#include<conio.h>
void main()
{
int num,rev=0;
cout<<”Enter any one 5 digit number”;
cin>>num;
while(num>0)
{
rev=num%10;
cout<<rev;
num=num/10;
}
getch();
}

Q.4. Write a program in C++ to accept one five-digit number from user, find out the sum of the
given digit number.

void main()
{
int num,sum=0,n;
cout<<”Enter any one 5 digit number”;
cin>>num;
while(num>0)
{
n=num%10;
sum=sum+n
num=num/10;
} cout<<”Sum of all digit no=”<<sum; getch();}

Khan S. Alam 12 https://E-next.in

https://E-next.in
C++ Lab Practicals

Q.4. Write a program in C++ to accept one five-digit number from user, find out the sum of alternet
digit number.

#include<iostream.h>
#include<conio.h>
void main()
{
int num,sum=0,n;
cout<<”Enter any one 5 digit number”;
cin>>num;
while(num>0)
{
n=num%10;
sum=sum+n;
num=num/100;
}
cout<<”Sum of alternet digit number=”<<sum;
getch();
}

Q.5. Write a program in C++ to accept one number from user. Find multiplication table of the given
number.

Program Code:

#include<iostream.h>
#include<conio.h>
void main()
{
int num,i;
cout<<”Enter no=”;
cin>>num;
cout<<”\n Multiplication table of “<< n;
cout<<”*****************************\n”;

for(i=1;i<=10;i++)
{
cout<<”\n”<<n<<” * “<<i<<” = “ <<n*I;
}
getch();
}

Khan S. Alam 13 https://E-next.in

https://E-next.in
C++ Lab Practicals

Q.6. Write a program in C++ to accept one number from user. Find out whether the input number
is Armstrong or not.

Program Code:

#include<iostream.h>
#include<conio.h>
void main()
{
int n,temp,rem,sum=0;
cout<<”Enter no=”;
cin>>n;
temp=n;

while(n>0)
{
rem=n%10;
sum=sum+(rem*rem*rem);
n=n/10;
}
if(temp==sum)
{
cout<<temp<<” is Armstrong number”;
}
else
{
cout<<temp<<” is not Armstrong number”;
}
getch();
}

Khan S. Alam 14 https://E-next.in

https://E-next.in
C++ Lab Practicals

Q.7. Write a C++ program for displaying the Fibonacci series.


Program Code:

#include<iostream.h>
#include<conio.h>
void main()
{
int a=0,b=1,temp,num;
cout<<”\n Input upper limit=”;
cin>>num;
cour<<a<<”\t”<<b;
for(i=3;i<=num;i++)
{ temp=a+b;
cout<<”\t”<<c;
a=b;
b=temp;
}
getch();
}

Q.8. Pattern Program.


(i) Program Code:

#include<iostream.h> Output
#include<conio.h>
Void main() *
{ **
Int I,j; ***
For(i=1;i<=5;i++) ****
{ *****
For(j=1;j<=I;j++)
{
cout(“*”);
}
Cout<<”\n”;
}

Khan S. Alam 15 https://E-next.in

https://E-next.in
C++ Lab Practicals

(ii) Program Code:

#include<iostream.h> Output #include<iostream.h> Output


#include<conio.h> #include<conio.h> 1
Void main() ***** Void main() 121
{ **** { 12321
Int I,j; *** Int I,j,k,c=1,m; 1234321
For(i=5;i>0;i--) ** For(i=1;i<=5;i++) 123454321
{ * { for(k=1;k<=5-I;k++)
For(j=1;j<=I;j++) { cout<<” “;}
{ For(j=1;j<=I;j++)
cout(“*”); { cout<<j; }
} For(m=j-2;m>=1;m--)
Cout<<”\n”; {
} Cout<<m;}
Cout<<”\n”;
Getch();
}

Khan S. Alam 16 https://E-next.in

https://E-next.in
C++ Lab Practicals

ARRAY PRACTICAL
Q.1. Write a program in C++ to accept 10 numbers from used, find out the sum and average of given input
number through array.
Program Code:

# include<iostream.h>
#include<conio.h>
void main ()
{
Int arr[10],I,sum=0,avg=0;
Clrscr();
Cout<<”Enter t ten number=\n”;
for(i=0;i<10;i++)
{
Cin>>arr[i];
Sum=sum+arr[i];
}
Avg=sum/10;
Cout<<” \nSum of array element=”<<sum;
Cout<<”\n Average of array element=”<<avg;
Getch();
}

Q.2. Write a program in C++ to accept from user, find out the total odd and even number present in an
array.

Program Code:
#include<iostream.h>
#include<conio.h>
Void main()
{
Int arr[10],I,even=0,odd=0;
Clrscr();
Cout<<”Enter ten no=”;
For(i=0;i<10;i++)
{
Cin>>arr[i];
If(arr[i]%2==0)
{
Even=even+1;
}
Else
{
Odd=odd+1;
}
}
Getch();
}

Khan S. Alam 17 https://E-next.in

https://E-next.in
C++ Lab Practicals

Q.3. Write a program in C++ to accept 10 numbers from user through array, display inputed data in
ascending order.

Program Code:

#include<iostream.h>
#include<conio.h>
Void main()
{
int arr[10],i,j,Temp;
cout<<”\n Enter ten numbers= “;
for(i=0;i<10;i++)
{
cin>>arr[i];
}
cout<<”Array element before sorting\n”;
for(i=0;i<10;i++)
{
cout<<arr[i] ;
}

for(i=0;i<9;i++)
{
for(j=i+1;j<10;j++)
{
if(arr[i]>arr[j])
{
Temp=arr[i];
arr[i]=arr[j];
arr[j]=Temp;
}
}
}

cout<<”\n Array elements after sorting\n”;


for(i=0;i<10;i++)
{
cout<<arr[i];
}
getch();
}

Khan S. Alam 18 https://E-next.in

https://E-next.in
C++ Lab Practicals

Q.4. Program to search an element from an array.


Program Code:

#include<iostream.h>
#include<conio.h>
void main()
{ int arr[10],i,num;
cout<<”\n Enter ten numbers :”;
for(i=0;i<10;i++)
{
cin>>arr[i];
}
cout<<”\n Enter number for search in an array=”;
cin>>num;

for(i=0;i<10;i++)
{
if(arr[i]==num)
{
cout<<”\n value found\n”;
cout<<”Index and value=”<<i<<”\t”<<arr[i];
}
}
getch();
}

Khan S. Alam 19 https://E-next.in

https://E-next.in
C++ Lab Practicals

Q.5. Write a program in C++ to accept 9 values in multidimensional array from user, print input data in the
format of 3*3.

Program Code:

#include<iostream.h>
#include<conio.h>
void main()
{
int arr[3][3],i,j;
cout<<”Enter array element=\n”;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
cin>>arr[i][j];
}
}
cout<<”\n Array element in matrix for:\n”;

for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
cout<<arr[i][j]<<”\t”;
}
cout<<”\n”;
}

getch();
}

Khan S. Alam 20 https://E-next.in

https://E-next.in
C++ Lab Practicals

Q.6. Matrix addition and multiplication program.


Program Code:
#include <iostream.h>
#include<conio.h>

void main()
{
int arr1[3][3],arr2[3][3],arr3[3][3],i,j;

cout<<”Enter first array element=\n”;


for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
cin>>arr1[i][j];
}
cout<<”\n”;
}

cout<<”Enter second array element=\n”;


for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
cin>>arr2[i][j];
}
cout<<”\n”;
}

cout<<” \nAddition of two array element :”;

for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
arr3[i][j]=arr1[i][j] +arr2[i][j];
}
}

Cout<<” *****Output of two array element addition in third array*****\n”;

for(i=0;i<3;i++)
{
cout(j=0;j<3;j++)
{
cout<<arr3[i][j];
}
}
getch();
}

Khan S. Alam 21 https://E-next.in

https://E-next.in
C++ Lab Practicals

Function Lab Practical


Simple function program (function without parameter)

1.
#include<iostream.h>
#include<conio.h>
void sum ();
void sum ()
{
int a,b,sum;
cout<<”Enter two no=”;
cin>>a>>b;
sum=a+b;
cout<<”Sum of two no=”<<sum;
}
void main ()
{
clrscr ();
sum ();
getch ();
}

2.
#include<iostream.h>
#include<conio.h>
void reverse();
void reverse()
{
int num,rev;
cout<<”Enter one five-digit number for reverse=”;
cin>>num;
while(num>0)
{
rev=num%10;
cout<<rev;
num=num/10;
}
}
void main ()
{
clrscr ();
reverse ();
getch ();
}

Khan S. Alam 22 https://E-next.in

https://E-next.in
C++ Lab Practicals

3. Function Call by value with parameter program:-

#include<iostream.h>
#include<conio.h>
void swap(int a,int b);

void main()
{
int a,b;
clrscr();
cout<<”Enter two no=”;
com>>a>>b;
cout<<”\n Value of a and b before swap=”<<a <<” “ <<b;
swap(a,b);
getch();
}

Void swap(int a,int b)


{
Int temp;
Temp=a;
A =b;
B=temp;
Cout<<”Value of a and b after swap=”<<a<<” “<<b;
}

Khan S. Alam 23 https://E-next.in

https://E-next.in
C++ Lab Practicals

4.
#include<iostream.h>
#include<conio.h>
void Armstrong(int num);
void main()
{ int a;
cout<<”Enter no=”;
cin>>a;
Armstrong(a);
getch();
}
void Armstrong (int num)
{
int temp,rem,sum=0;
temp=num;
while(num>0)
{ rem=num%10;
sum=sum+(rem*rem*rem);
num=num/10;
}
if(temp==sum)
{
cout<<”\n Armstong no”;
}
else
{
cout<<”\n This is not a Armstrong no”;
}
}

Khan S. Alam 24 https://E-next.in

https://E-next.in
C++ Lab Practicals

5. Function Call by reference program :

#include<iostream.h>
#include<conio.h>
void swap(int &a,int &b);

void main()
{
int a,b;
clrscr();
cout<<”Enter two no=”;
com>>a>>b;
cout<<”\n Value of a and b before swap=”<<a <<” “ <<b;
swap(a,b);
getch();
}

void swap(int &a,int &b)


{
int temp;
temp=a;
a =b;
b=temp;
cout<<”Value of a and b after swap=”<<a<<” “<<b;
}

Khan S. Alam 25 https://E-next.in

https://E-next.in
C++ Lab Practicals

6. Reverse program through function call by reference.

#include<iostream.h>
#include<conio.h>
void factorial( int &num);
void main()
{
int num;
cout<<”Enter no =”;
cin>>num;
factorial(num);
getch();
}

void factorial( int &num)


{
int fact=1;
while(num>0)
{
fact=fact*num;
num--;
}
cout<<”Factorial Value of given input number=”<<fact;
}

Khan S. Alam 26 https://E-next.in

https://E-next.in

You might also like