1. C++ PRACTICAL FILE
Q1. (a) Program to display the following using single cout
MATHS=90
PHYSICS=77
CHEMISTRY=69
Solution code
//Program to display the program using single cout
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
cout<<"Maths=90nPhysics=77nChemistry=69";
getch();
}
Output
PREPARED BY
BIJENDER KUMAR Page
2. C++ PRACTICAL FILE
Q1. (b) Program to display the following using multiple
cout
MATHS=90
PHYSICS=77
CHEMISTRY=69
Solution code
//Program to display the program using single cout
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
cout<<"Maths=90";
cout<<"nPhysics=77";
cout<<"nChemistry=69";
getch();
}
Output
PREPARED BY
BIJENDER KUMAR Page
3. C++ PRACTICAL FILE
Q13. Program to find that entered year is leap year or not
Solution Code
//Program to find entered year is leap or not
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int year;
cout<<"Enter the yearn";
cin>>year;
if(year%4==0)
{
cout<<"Entered year is a leap yearn";
}
else
{
cout<<"Entered year is not a leap year";
}
getch();
}
Output
PREPARED BY
BIJENDER KUMAR Page
4. C++ PRACTICAL FILE
Q14. Program to solve quadratic equation ax2+bx+c=0
Solution Code
//Program to solve quadratic equation
#include<iostream.h>
#include<conio.h>
#include<math.h>
void main()
{
clrscr();
float a,b,c,d,r1,r2;
cout<<"Enter the value of a,b,cn";
cin>>a>>b>>c;
if(a==0)
{
cout<<"cannot be solve";
}
else
{
d=(b*b)-(4*a*c);
if(d>0)
{
r1=(-b+sqrt(d))/(2*a);
PREPARED BY
BIJENDER KUMAR Page
5. C++ PRACTICAL FILE
r2=(-b-sqrt(d))/(2*a);
cout<<"Roots are equal and
differentnroot1="<<r1<<"nroot2="<<r2;
}
else if(d==0)
{
r1=r2=-b/(2*a);
cout<<"Roots are real but samenroot1=root2="<<r1;
}
else
{
cout<<"Roots are imaginary";
}
}
getch();
}
Output
PREPARED BY
BIJENDER KUMAR Page
6. C++ PRACTICAL FILE
Q15. Program to find whether given number is even or
odd
Solution Code
//Program to check number is even or odd
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
intnum,res;
cout<<"Enter the numbern";
cin>>num;
res=num%2;
switch(res)
{
case 0:cout<<"Number is even";
break;
case 1:cout<<"Number is odd";
break;
default:cout<<"Invalid entry";
PREPARED BY
BIJENDER KUMAR Page
8. C++ PRACTICAL FILE
Q17. Program to display arithmetic operations using
switch case
Solution Code
//Program for arithmetic operators using switch
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
inta,b,res;
charch;
cout<<"Enter the two numbersn";
cin>>a>>b;
cout<<"Enter the operation to perform +,-,*,/,%n";
cin>>ch;
switch(ch)
{
case '+':res=a+b;
cout<<res;
break;
PREPARED BY
BIJENDER KUMAR Page
9. C++ PRACTICAL FILE
case '-':res=a-b;
cout<<res;
break;
case '*':res=a*b;
cout<<res;
break;
case '/':res=a/b;
cout<<res;
break;
case '%':res=a%b;
cout<<res;
break;
default:cout<<"Wrong choice";
}
getch();
}
Output
PREPARED BY
BIJENDER KUMAR Page
10. C++ PRACTICAL FILE
Q18. To display first 10 natural no and their sum
Solution code
//Program to display first 10 natural numbers and their
sum
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int sum=0;
cout<<"First 10 natural number aren";
for(int i=1;i<=10;++i)
{
cout<<i<<"n";
sum=sum+i;
}
cout<<"Sum is"<<sum;
getch();
}
PREPARED BY
BIJENDER KUMAR Page
11. C++ PRACTICAL FILE
Output
Q19. Program to display the fibonacci series
Solution code
//Program to display the fibonacci series
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int a=0,b=1,sum=0,limit;
cout<<"Enter the limit";
cin>>limit;
cout<<a<<"n"<<b<<"n";
for(int i=0;i<=limit-2;++i)
{
sum=a+b;
cout<<sum<<"n";
a=b;
b=sum;
PREPARED BY
BIJENDER KUMAR Page
13. C++ PRACTICAL FILE
Q20. Program to find the factorial of a number
Solution code
//Program to display the factorial of a number
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
inta,fact=1;
cout<<"Enter the value of a";
cin>>a;
for(int i=1;i<=a;++i)
{
fact=fact*i;
}
cout<<"The factorial of numberis"<<fact;
getch();
PREPARED BY
BIJENDER KUMAR Page
14. C++ PRACTICAL FILE
}
Output
Q21. Program to find whether given number is prime or
not
Solution Code
//Program to check number is prime or not
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
intnum,temp=0;
cout<<"Enter the numbern";
cin>>num;
for(int i=2;i<=num-1;++i)
{
if(num%i==0)
{
temp=1;
PREPARED BY
BIJENDER KUMAR Page
16. C++ PRACTICAL FILE
Q21. (a) Program to find the sum of the series 1+2+3….n
Solution Code
//Program to find the sum of 1+2+3+..+n
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
intlimit,sum=0;
cout<<"Enter the limitn";
cin>>limit;
for(int i=1;i<=limit;++i)
{
sum=sum+i;
}
cout<<"Answer is"<<sum;
PREPARED BY
BIJENDER KUMAR Page
17. C++ PRACTICAL FILE
getch();
}
Output
Q21. (b) Program to find the sum of the series
1+1/1!+2/2!+3/3!....n/n!.
Solution Code
//Program to solve the series 1+1/1!+2/2!+3/3!+..+n/n!
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
floatlimit,fact=1,sum=1;
cout<<"Enter the limitn";
cin>>limit;
for(int i=1;i<=limit;++i)
{
for(int j=1;j<=limit;++j)
{
PREPARED BY
BIJENDER KUMAR Page
19. C++ PRACTICAL FILE
Q22. (a) Program to generate the following pattern
*
**
***…
Solution Code
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
intch;
cout<<"Enter the number of rowsn";
cin>>ch;
for(int i=1;i<=ch;++i)
{
PREPARED BY
BIJENDER KUMAR Page
20. C++ PRACTICAL FILE
for(int j=1;j<=i;++j)
{
cout<<"*";
}
cout<<"n";
}
getch();
}
Output
PREPARED BY
BIJENDER KUMAR Page
21. C++ PRACTICAL FILE
Q24. (b) Program to generate the following pattern
1
12
123….
Solution Code
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
intch;
cout<<"Enter the number of rowsn";
cin>>ch;
for(int i=1;i<=ch;++i)
PREPARED BY
BIJENDER KUMAR Page
22. C++ PRACTICAL FILE
{
for(int j=1;j<=i;++j)
{
cout<<j;
}
cout<<"n";
}
getch();
}
Output
PREPARED BY
BIJENDER KUMAR Page
23. C++ PRACTICAL FILE
Q25. Program to show sum of 10 elements of array and
show the average
Solution Code
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
float a[10],sum=0,avg;
cout<<"Enter 10 elements of arrayn";
for(int i=0;i<10;++i)
{
cin>>a[i];
sum=sum+a[i];
}
PREPARED BY
BIJENDER KUMAR Page
25. C++ PRACTICAL FILE
Q26.Program to perform linear search
Solution code
//Program to perform linear search
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
float a[10],limit,n,f=0,pos;
cout<<"Enter the limit";
cin>>limit;
for(int i=0;i<limit;++i)
{
cin>>a[i];
}
PREPARED BY
BIJENDER KUMAR Page
26. C++ PRACTICAL FILE
cout<<"Enter number you want to search";
cin>>n;
for(i=0;i<limit;++i)
{
if(n==a[i])
{
pos=i+1;
f=1;
}
break;
}
if(f==1)
{
cout<<"The number is at pos"<<pos;
}
else
{
cout<<"Number is not found";
}
getch();
}
Output
PREPARED BY
BIJENDER KUMAR Page
27. C++ PRACTICAL FILE
Q28. Program to find the maximum number in an array
Solution code
//Program to find the maximum number in an array
#include<iostream.h>
#include<conio.h>
void main()
{
int a[10],limit,largest;
cout<<"Enter the limit";
cin>>limit;
cout<<"Enter elements";
for(int i=0;i<limit;++i)
PREPARED BY
BIJENDER KUMAR Page