Bijender (1)

547 views

Published on

Published in: Technology
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
547
On SlideShare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
17
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Bijender (1)

  1. 1. C++ PRACTICAL FILEQ1. (a) Program to display the following using single coutMATHS=90PHYSICS=77CHEMISTRY=69Solution code//Program to display the program using single cout#include<iostream.h>#include<conio.h>void main(){clrscr();cout<<"Maths=90nPhysics=77nChemistry=69";getch();}OutputPREPARED BYBIJENDER KUMAR Page
  2. 2. C++ PRACTICAL FILEQ1. (b) Program to display the following using multiplecoutMATHS=90PHYSICS=77CHEMISTRY=69Solution 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();}OutputPREPARED BYBIJENDER KUMAR Page
  3. 3. C++ PRACTICAL FILEQ13. Program to find that entered year is leap year or notSolution 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();}OutputPREPARED BYBIJENDER KUMAR Page
  4. 4. C++ PRACTICAL FILEQ14. Program to solve quadratic equation ax2+bx+c=0Solution 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 BYBIJENDER KUMAR Page
  5. 5. C++ PRACTICAL FILEr2=(-b-sqrt(d))/(2*a);cout<<"Roots are equal anddifferentnroot1="<<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();}OutputPREPARED BYBIJENDER KUMAR Page
  6. 6. C++ PRACTICAL FILEQ15. Program to find whether given number is even oroddSolution 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 BYBIJENDER KUMAR Page
  7. 7. C++ PRACTICAL FILE}getch();}OutputPREPARED BYBIJENDER KUMAR Page
  8. 8. C++ PRACTICAL FILEQ17. Program to display arithmetic operations usingswitch caseSolution 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 BYBIJENDER KUMAR Page
  9. 9. C++ PRACTICAL FILEcase -: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();}OutputPREPARED BYBIJENDER KUMAR Page
  10. 10. C++ PRACTICAL FILEQ18. To display first 10 natural no and their sumSolution code//Program to display first 10 natural numbers and theirsum#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 BYBIJENDER KUMAR Page
  11. 11. C++ PRACTICAL FILEOutputQ19. Program to display the fibonacci seriesSolution 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 BYBIJENDER KUMAR Page
  12. 12. C++ PRACTICAL FILE}getch();}OutputPREPARED BYBIJENDER KUMAR Page
  13. 13. C++ PRACTICAL FILEQ20. Program to find the factorial of a numberSolution 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 BYBIJENDER KUMAR Page
  14. 14. C++ PRACTICAL FILE}OutputQ21. Program to find whether given number is prime ornotSolution 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 BYBIJENDER KUMAR Page
  15. 15. C++ PRACTICAL FILEbreak;}}if(temp==0){cout<<"Prime Number";}else{cout<<"Not a prime number";}getch();}OutputPREPARED BYBIJENDER KUMAR Page
  16. 16. C++ PRACTICAL FILEQ21. (a) Program to find the sum of the series 1+2+3….nSolution 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 BYBIJENDER KUMAR Page
  17. 17. C++ PRACTICAL FILEgetch();}OutputQ21. (b) Program to find the sum of the series1+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 BYBIJENDER KUMAR Page
  18. 18. C++ PRACTICAL FILEfact=fact*i;}sum=sum+i/fact;fact=1;}cout<<"Sum of the series is"<<sum;getch();}OutputPREPARED BYBIJENDER KUMAR Page
  19. 19. C++ PRACTICAL FILEQ22. (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 BYBIJENDER KUMAR Page
  20. 20. C++ PRACTICAL FILEfor(int j=1;j<=i;++j){cout<<"*";}cout<<"n";}getch();}OutputPREPARED BYBIJENDER KUMAR Page
  21. 21. C++ PRACTICAL FILEQ24. (b) Program to generate the following pattern112123….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 BYBIJENDER KUMAR Page
  22. 22. C++ PRACTICAL FILE{for(int j=1;j<=i;++j){cout<<j;}cout<<"n";}getch();}OutputPREPARED BYBIJENDER KUMAR Page
  23. 23. C++ PRACTICAL FILEQ25. Program to show sum of 10 elements of array andshow the averageSolution 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 BYBIJENDER KUMAR Page
  24. 24. C++ PRACTICAL FILEavg=sum/10;cout<<"Sum is"<<sum;cout<<"nAverage is"<<avg;getch();}OutputPREPARED BYBIJENDER KUMAR Page
  25. 25. C++ PRACTICAL FILEQ26.Program to perform linear searchSolution 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 BYBIJENDER KUMAR Page
  26. 26. C++ PRACTICAL FILEcout<<"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();}OutputPREPARED BYBIJENDER KUMAR Page
  27. 27. C++ PRACTICAL FILEQ28. Program to find the maximum number in an arraySolution 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 BYBIJENDER KUMAR Page
  28. 28. C++ PRACTICAL FILE{cin>>a[i];}largest=a[0];for(i=1;i<limit;++i){if(largest<a[i]){largest=a[i];}cout<<"Largest number in an array is"<<largest;}getch();}OutputPREPARED BYBIJENDER KUMAR Page

×