0% found this document useful (0 votes)
27 views

Assignment # 02: Program # 01

Program #01 calculates the roots of a quadratic equation by taking in coefficients a, b, and c, calculating the discriminant, and outputting the two roots or an appropriate message if there are no real roots. Program #02 takes in two numbers and an operator, performs the calculation, and outputs the result. It uses a switch statement to handle the four basic operators. Program #03 generates and outputs the first n terms of the Fibonacci sequence by initializing the first two terms and then calculating each subsequent term as the sum of the previous two.

Uploaded by

Fariha Saeed
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views

Assignment # 02: Program # 01

Program #01 calculates the roots of a quadratic equation by taking in coefficients a, b, and c, calculating the discriminant, and outputting the two roots or an appropriate message if there are no real roots. Program #02 takes in two numbers and an operator, performs the calculation, and outputs the result. It uses a switch statement to handle the four basic operators. Program #03 generates and outputs the first n terms of the Fibonacci sequence by initializing the first two terms and then calculating each subsequent term as the sum of the previous two.

Uploaded by

Fariha Saeed
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

ASSIGNMENT # 02

Program # 01
#include <iostream>
#include <math.h>
using namespace std;

int main(int argc, char** argv) {


float a,b,c,d,disc,e;
cout<<"enter the values of a,b and c";
cin>>a>>b>>c;
disc=b*b-4*a*c;
cout<<"discriminant="<<disc<<endl;
if(disc>0)
{

d=(-b+sqrt(disc)/(2*a));
e=(-b-sqrt(disc)/(2*a));
cout<<"root 1 is"<<d<<endl;
cout<<"root 2 is"<<e<<endl;
}
else if(disc<0)
{
cout<<"roots are not real";
}
else if(disc==0){
cout<<"there is no solution";}
return 0; }

Result:

Program # 02
#include <iostream>
#include <cstdlib>
using namespace std;
int main()
{
int a,b;
char op;

cout<<"enter first number: \n";


cin>>a;
cout<<"enter operator from (+,-,*,/) \n";
cin>>op;
cout<<"enter second number: \n";
cin>>b;
switch(op)
{
case'+':
cout<<"sum is"<<a+b;
break;
case'-':
cout<<"difference is"<<a-b;
break;
case'*':
cout<<"multiplication of numbers is"<<a*b;
break;
case'/':
cout<<a/b;
break;
default:
cout<<"operator you enter is incorrect";
break;
}
system("pause");
}

Result:
PROGRAM # 03:
#include <iostream>
#include <cstdlib>
using namespace std;
int main()
{
int n,t1=0,t2=1,nextterm=0;
cout<<"enter number of terms:";
cin>> n;
cout<<"Fibn=Fibn-1+Fbin-2 \n";
cout<<"fibonacci series:";
for(int i=1;i<=n;++i)
{
if(i==1)
{
cout<<t1;
continue;
}
if(i==2)
{
cout<<t2;
continue;
}
nextterm=t1+t2;
t1=t2;
t2=nextterm;
cout<< nextterm; }

Result:

You might also like