Solutions:-
1) W.A.P. to display roots of a quadratic equation where coefficients a,b,c are given ?
#include<iostream>
#include<cmath>
using namespace std;
int main()
{
int a,b,c;
float D,r1,r2;
cout<<"Enter the Coefficients of the quadratic equation-";
cin>>a>>b>>c;
D=pow(b,2) - (4*a*c);
if(D<0)
{cout<<"\nNo Real Roots";
return 0;}
else
{if(D>0)
{
cout<<"\nDistinct Real Roots\n";
r1=(-b + sqrt(D))/(2*a);
r2=(-b - sqrt(D))/(2*a);
cout<<"Roots ="<<r1<<" "<<r2;}
else{
cout<<"\nEqual Roots\n";r1=-b/(2*a);
cout<<"Root ="<<r1; }}
return 0;}
2) W.A.P. to check whether a given number is prime or not ?
#include<iostream>
using namespace std;
int main()
{
int n;
char p='Y';
cout<<"Enter a number=";
cin>>n;
if(n==1)
p='N';
for(int i=2;i<=n/2;i++)
if(n%i==0)
{p='N';
break; }
if(p=='Y')
cout<<"\n"<<n<<" is a prime number.";
else
cout<<"\n"<<n<<" is not a prime number.";
return 0;
}
3) W.A.P. to display all the factors of a given number along with the sum of the factors?
#include<iostream>
using namespace std;
int main()
{ int n,sum=0;
cout<<"Enter a number=";
cin>>n;
cout<<"\nFactors of "<<n<<" are \n";
for(int i=1;i<=n/2;i++)
{
if(n%i==0){
cout<<i<<'\n';
sum+=i;}
}
cout<<n<<'\n';
sum+=n;cout<<"\nSum of Factors="<<sum;
return 0;
}
We will try more of different problems in the upcoming blogs.
Please Subscribe to my blog by clicking the link next to Subscribe to, to get Regular Updates.Be Happy and Keep Coding.
1) W.A.P. to display roots of a quadratic equation where coefficients a,b,c are given ?
#include<iostream>
#include<cmath>
using namespace std;
int main()
{
int a,b,c;
float D,r1,r2;
cout<<"Enter the Coefficients of the quadratic equation-";
cin>>a>>b>>c;
D=pow(b,2) - (4*a*c);
if(D<0)
{cout<<"\nNo Real Roots";
return 0;}
else
{if(D>0)
{
cout<<"\nDistinct Real Roots\n";
r1=(-b + sqrt(D))/(2*a);
r2=(-b - sqrt(D))/(2*a);
cout<<"Roots ="<<r1<<" "<<r2;}
else{
cout<<"\nEqual Roots\n";r1=-b/(2*a);
cout<<"Root ="<<r1; }}
return 0;}
2) W.A.P. to check whether a given number is prime or not ?
#include<iostream>
using namespace std;
int main()
{
int n;
char p='Y';
cout<<"Enter a number=";
cin>>n;
if(n==1)
p='N';
for(int i=2;i<=n/2;i++)
if(n%i==0)
{p='N';
break; }
if(p=='Y')
cout<<"\n"<<n<<" is a prime number.";
else
cout<<"\n"<<n<<" is not a prime number.";
return 0;
}
3) W.A.P. to display all the factors of a given number along with the sum of the factors?
#include<iostream>
using namespace std;
int main()
{ int n,sum=0;
cout<<"Enter a number=";
cin>>n;
cout<<"\nFactors of "<<n<<" are \n";
for(int i=1;i<=n/2;i++)
{
if(n%i==0){
cout<<i<<'\n';
sum+=i;}
}
cout<<n<<'\n';
sum+=n;cout<<"\nSum of Factors="<<sum;
return 0;
}
We will try more of different problems in the upcoming blogs.
Please Subscribe to my blog by clicking the link next to Subscribe to, to get Regular Updates.Be Happy and Keep Coding.
Comments
Post a Comment