Professional Documents
Culture Documents
C++ Lab Practical (E-Next - In)
C++ Lab Practical (E-Next - In)
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()
}
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;
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();
}
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;
https://E-next.in
C++ Lab Practicals
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();
}
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();
}
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;
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()}
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();
}
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;
https://E-next.in
C++ Lab Practicals
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();
}
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();
}
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();}
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();
}
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();
}
https://E-next.in
C++ Lab Practicals
#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();
}
#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”;
}
https://E-next.in
C++ Lab Practicals
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();
}
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;
}
}
}
https://E-next.in
C++ Lab Practicals
#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();
}
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();
}
https://E-next.in
C++ Lab Practicals
void main()
{
int arr1[3][3],arr2[3][3],arr3[3][3],i,j;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
arr3[i][j]=arr1[i][j] +arr2[i][j];
}
}
for(i=0;i<3;i++)
{
cout(j=0;j<3;j++)
{
cout<<arr3[i][j];
}
}
getch();
}
https://E-next.in
C++ Lab Practicals
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 ();
}
https://E-next.in
C++ Lab Practicals
#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();
}
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”;
}
}
https://E-next.in
C++ Lab Practicals
#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();
}
https://E-next.in
C++ Lab Practicals
#include<iostream.h>
#include<conio.h>
void factorial( int &num);
void main()
{
int num;
cout<<”Enter no =”;
cin>>num;
factorial(num);
getch();
}
https://E-next.in