SlideShare une entreprise Scribd logo
1  sur  28
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
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
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
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
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
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
C++ PRACTICAL FILE


}
getch();
}




Output




PREPARED BY
BIJENDER KUMAR                        Page
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
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
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
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
C++ PRACTICAL FILE


}
getch();
}




Output




PREPARED BY
BIJENDER KUMAR                        Page
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
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
C++ PRACTICAL FILE


break;
}
}
if(temp==0)
{
cout<<"Prime Number";
}
else
{
cout<<"Not a prime number";
}
getch();
}
Output




PREPARED BY
BIJENDER KUMAR                            Page
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
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
C++ PRACTICAL FILE


fact=fact*i;
}
sum=sum+i/fact;
fact=1;
}
cout<<"Sum of the series is"<<sum;
getch();
}


Output




PREPARED BY
BIJENDER KUMAR                             Page
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
C++ PRACTICAL FILE


for(int j=1;j<=i;++j)
{
cout<<"*";
}
cout<<"n";
}
getch();
}


Output




PREPARED BY
BIJENDER KUMAR                               Page
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
C++ PRACTICAL FILE


{
for(int j=1;j<=i;++j)
{
cout<<j;
}
cout<<"n";
}
getch();
}


Output




PREPARED BY
BIJENDER KUMAR                               Page
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
C++ PRACTICAL FILE


avg=sum/10;
cout<<"Sum is"<<sum;
cout<<"nAverage is"<<avg;
getch();
}




Output




PREPARED BY
BIJENDER KUMAR                            Page
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
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
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
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();
}
Output




PREPARED BY
BIJENDER KUMAR                                    Page

Contenu connexe

Tendances

c++ program for Railway reservation
c++ program for Railway reservationc++ program for Railway reservation
c++ program for Railway reservation
Swarup Kumar Boro
 
81818088 isc-class-xii-computer-science-project-java-programs
81818088 isc-class-xii-computer-science-project-java-programs81818088 isc-class-xii-computer-science-project-java-programs
81818088 isc-class-xii-computer-science-project-java-programs
Abhishek Jena
 
Declaring friend function with inline code
Declaring friend function with inline codeDeclaring friend function with inline code
Declaring friend function with inline code
Rajeev Sharan
 
Assignment Examples Final 07 Oct
Assignment Examples Final 07 OctAssignment Examples Final 07 Oct
Assignment Examples Final 07 Oct
Sriram Raj
 
Rajeev oops 2nd march
Rajeev oops 2nd marchRajeev oops 2nd march
Rajeev oops 2nd march
Rajeev Sharan
 

Tendances (19)

project report in C++ programming and SQL
project report in C++ programming and SQLproject report in C++ programming and SQL
project report in C++ programming and SQL
 
C++ file
C++ fileC++ file
C++ file
 
54602399 c-examples-51-to-108-programe-ee01083101
54602399 c-examples-51-to-108-programe-ee0108310154602399 c-examples-51-to-108-programe-ee01083101
54602399 c-examples-51-to-108-programe-ee01083101
 
c++ program for Railway reservation
c++ program for Railway reservationc++ program for Railway reservation
c++ program for Railway reservation
 
High performance web programming with C++14
High performance web programming with C++14High performance web programming with C++14
High performance web programming with C++14
 
Pads lab manual final
Pads lab manual finalPads lab manual final
Pads lab manual final
 
COMPUTER SCIENCE CLASS 12 PRACTICAL FILE
COMPUTER SCIENCE CLASS 12 PRACTICAL FILECOMPUTER SCIENCE CLASS 12 PRACTICAL FILE
COMPUTER SCIENCE CLASS 12 PRACTICAL FILE
 
C++ project on police station software
C++ project on police station softwareC++ project on police station software
C++ project on police station software
 
81818088 isc-class-xii-computer-science-project-java-programs
81818088 isc-class-xii-computer-science-project-java-programs81818088 isc-class-xii-computer-science-project-java-programs
81818088 isc-class-xii-computer-science-project-java-programs
 
Cs pritical file
Cs pritical fileCs pritical file
Cs pritical file
 
c++ project on restaurant billing
c++ project on restaurant billing c++ project on restaurant billing
c++ project on restaurant billing
 
C++ project
C++ projectC++ project
C++ project
 
Basic Programs of C++
Basic Programs of C++Basic Programs of C++
Basic Programs of C++
 
Declaring friend function with inline code
Declaring friend function with inline codeDeclaring friend function with inline code
Declaring friend function with inline code
 
Assignment Examples Final 07 Oct
Assignment Examples Final 07 OctAssignment Examples Final 07 Oct
Assignment Examples Final 07 Oct
 
C++ manual Report Full
C++ manual Report FullC++ manual Report Full
C++ manual Report Full
 
Computer Science investigatory project class 12
Computer Science investigatory project class 12Computer Science investigatory project class 12
Computer Science investigatory project class 12
 
Rajeev oops 2nd march
Rajeev oops 2nd marchRajeev oops 2nd march
Rajeev oops 2nd march
 
c-programming-using-pointers
c-programming-using-pointersc-programming-using-pointers
c-programming-using-pointers
 

Similaire à Bijender (1)

1 borland c++ 5.02 by aramse
1   borland c++ 5.02 by aramse1   borland c++ 5.02 by aramse
1 borland c++ 5.02 by aramse
Aram SE
 
Assignement of programming & problem solving ass.(3)
Assignement of programming & problem solving ass.(3)Assignement of programming & problem solving ass.(3)
Assignement of programming & problem solving ass.(3)
Syed Umair
 
Lab Manual IV (1).pdf on C++ Programming practice
Lab Manual IV (1).pdf on C++ Programming practiceLab Manual IV (1).pdf on C++ Programming practice
Lab Manual IV (1).pdf on C++ Programming practice
ranaibrahim453
 

Similaire à Bijender (1) (20)

C++ assignment
C++ assignmentC++ assignment
C++ assignment
 
C++ file
C++ fileC++ file
C++ file
 
Final DAA_prints.pdf
Final DAA_prints.pdfFinal DAA_prints.pdf
Final DAA_prints.pdf
 
7720
77207720
7720
 
1 borland c++ 5.02 by aramse
1   borland c++ 5.02 by aramse1   borland c++ 5.02 by aramse
1 borland c++ 5.02 by aramse
 
C++ file
C++ fileC++ file
C++ file
 
Assignement of programming & problem solving ass.(3)
Assignement of programming & problem solving ass.(3)Assignement of programming & problem solving ass.(3)
Assignement of programming & problem solving ass.(3)
 
C++ TUTORIAL 8
C++ TUTORIAL 8C++ TUTORIAL 8
C++ TUTORIAL 8
 
Lab Manual IV (1).pdf on C++ Programming practice
Lab Manual IV (1).pdf on C++ Programming practiceLab Manual IV (1).pdf on C++ Programming practice
Lab Manual IV (1).pdf on C++ Programming practice
 
2014 computer science_question_paper
2014 computer science_question_paper2014 computer science_question_paper
2014 computer science_question_paper
 
Cpp programs
Cpp programsCpp programs
Cpp programs
 
C Programming
C ProgrammingC Programming
C Programming
 
operators.ppt
operators.pptoperators.ppt
operators.ppt
 
C++ practical
C++ practicalC++ practical
C++ practical
 
'C' language notes (a.p)
'C' language notes (a.p)'C' language notes (a.p)
'C' language notes (a.p)
 
A.P.S.E PRACTICAL FILE, NIT KURUKSHETRA
A.P.S.E PRACTICAL FILE, NIT KURUKSHETRA A.P.S.E PRACTICAL FILE, NIT KURUKSHETRA
A.P.S.E PRACTICAL FILE, NIT KURUKSHETRA
 
C++
C++C++
C++
 
Programming fundamentals
Programming fundamentalsProgramming fundamentals
Programming fundamentals
 
C Programming Example
C Programming ExampleC Programming Example
C Programming Example
 
pointer.ppt hfkogfhkigfvjjgdsyhhgfdfghjb
pointer.ppt hfkogfhkigfvjjgdsyhhgfdfghjbpointer.ppt hfkogfhkigfvjjgdsyhhgfdfghjb
pointer.ppt hfkogfhkigfvjjgdsyhhgfdfghjb
 

Dernier

Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
WSO2
 

Dernier (20)

Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...
Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...
Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...
 
ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challenges
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CV
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
 
Ransomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdfRansomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdf
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
 

Bijender (1)

  • 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
  • 15. C++ PRACTICAL FILE break; } } if(temp==0) { cout<<"Prime Number"; } else { cout<<"Not a prime number"; } getch(); } Output 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
  • 18. C++ PRACTICAL FILE fact=fact*i; } sum=sum+i/fact; fact=1; } cout<<"Sum of the series is"<<sum; getch(); } Output 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
  • 24. C++ PRACTICAL FILE avg=sum/10; cout<<"Sum is"<<sum; cout<<"nAverage is"<<avg; getch(); } Output 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
  • 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(); } Output PREPARED BY BIJENDER KUMAR Page