Chapter - 4
Page - 79
Exercise - 4
Solution:
#include<iostream>
using namespace std;
int factorial(int n){
if(n==0){
return 1;
}
return n * factorial(n-1);
}
int main(){
float r;
cout<<"Enter value of 'r' "<<endl;
cin>>r;
float ans =0;
for(int i=1;i<=r;i++){
if((i&1)==0){
ans+=factorial(r)/factorial(i) ;
}else{
ans-=factorial(r)/factorial(i);
}
}
cout<<ans;
}
Solution:
#include<iostream>
#include<cmath>
using namespace std;
float rot(int n){
if(n==0){
return 0;
}
return sqrt(2+rot(n-1));
}
int main(){
int n;
cout<<"Enter number of iteration of n"<<endl;
cin>>n;
float ans =1;
for(int i=1;i<=n;i++){
ans*=rot(i)/2 ;
}
cout<<"from RHS 'solving eqaution' we get : "<<ans<<endl;
cout<<"from LHS '2/pi' we get : "<<2/M_PI<<endl;
}
Solution:
#include<iostream>
#include<cmath>
using namespace std;
float power(int a ,int b){
if(b==0){
return 1;
}
return a*power(a, b-1);
}
int factorial(int n){
if(n==0){
return 1;
}
return n * factorial(n-1);
}
int main(){
float x;
cout<<"Input value of 'x' : "<<endl;
cin>>x;
float ans=0;
for(int i=0;i<15;i++){
ans+=power(x,i)/factorial(i);
}
cout<<"by solving RHS equation : "<<ans<<endl;
cout<<"by solving LHS 'e^x' : "<<exp(x);
}
Solution:
#include<iostream>
using namespace std;
int power(int x , int i){
if(i==0){
return 1;
}
return x* power(x,i-1);
}
int main(){
cout<<"Enter number of coefficient : ";
int n;
cin>>n;
int coeff[n];
cout<<"input coeffecients in increasing order of power of x..."<<endl;
cout<<"For example: A1 X^n , A2 X^2....An X^n"<<endl;
for(int i=0;i<n;i++){
cin>>coeff[i];
}
cout<<"Input value of 'x' "<<endl;
int x;
cin>>x;
float ans=0;
for(int i=0 ; i<n;i++){
ans=ans+coeff[i]*power(x,i);
}
cout<<ans;
}
Solution:
#include<iostream>
using namespace std;
int main(){
cout<<"Enter number of coefficient : ";
int n;
cin>>n;
int coeff[n];
cout<<"input coeffecients in decreasing order of power of x..."<<endl;
cout<<"for exampal : An X^n , An-1 X^n-1...A1 X1"<<endl;
for(int i=0;i<n;i++){
cin>>coeff[i];
}
cout<<"Input value of 'x' "<<endl;
int x;
cin>>x;
float ans=0;
for(int i=0;i<n;i++){
if(i>1){
ans=ans*x+coeff[i];
}else{
ans+=x*coeff[i];}
}
cout<<"answer is : "<<ans;
}
Solution:
#include<iostream>
using namespace std;
int main(){
int d;
cout<<"input value of d : ";
cin>>d;
int n;
cout<<"input number of digit in nuber m : ";
cin>>n;
int ans =0;
cout<<"input number m (space between in digits of number m)"<<endl;
while(n--){
int m;
cin>>m;
if((d*m)>9 && ans>1){
ans=(ans+(d*m/10))*10+((d*m)%10);
}else{
ans=ans*10 + d*m ;
}
}
cout<<"your answer is "<<ans;
}
Comments
Post a Comment