![]() |
Power and Factorial
My program basically asks the users for a value (s) then calculates and displays the First, Middle, Last, and Final. The users have a choice to enter the value either in integer, real or character. If the user enters it in real or character, it will be converted into integer value before the required value is computed. I have written the power , factorial and the sigma functions which I will use in calculating the first, middle and the last. Can somebody assist in checking my codes to see if its correct and show how I will those functions to calculate the first and the last which a bit more complicated?
First = X^1/1! - X^3/3! + X^5/5! - X^7/7! + X^9/9! - ....X^n/n! Middle = n sigma i = 1 sqrt(i/x) last = 1- X^2/2! + X^4/4! - X^6/6! + X^8/8! - ....X^n/n! Final = first/last + middle Code:
#include<iostream> |
Re: Power and Factorial
check the functions again all are wrong.
hint: return is placed in wrong places in all functions. Code:
int factorial (int n){hint: (-1)^n ,n=0,2,4,6..... =1 (-1)^m,m=1,3,5,...=-1 X^1/1! - X^3/3! + X^5/5! - X^7/7! + X^9/9! - ....X^n/n!---->(-1)^0*X^1/1! +(-1)^1* X^3/3! +(-1)^2* X^5/5! +(-1)^3* X^7/7! + (-1)^4*X^9/9! +....X^n/n! |
Re: Power and Factorial
Can you Please put the hint in code so that I can have a good idea on how to solve the problem? I am grateful that you assist. I will be happy if you can do more
|
Re: Power and Factorial
Quote:
Code:
int n = 0,1,2,4,6.... =1;Code:
|
Re: Power and Factorial
1) C is case sensitive
first is NOT the same with First 2) for calculating First you need 2 parameters X and n you only pass one the correct would be Code:
int first (int x,int n){have you tried to fix errors? 4) you still put returns inside loops which terminates functions before even they start computing. now for the First function First = X^1/1! - X^3/3! + X^5/5! - X^7/7! + X^9/9! - ....X^n/n! you see that it is only a sum of numbers and you see also an increment from 1 to n ......> for(int i=1;i<=n;i++){.....} maybe? hint for the sign:....pow(-1,i-1)*X^i/i! fix them and send your new code again for the rest. |
Re: Power and Factorial
Quote:
the correct would be int counter=0; for(int i=1;i<=n;i=i+2){ ...pow(-1.0,counter++)*.....//for the sign changing ..... } |
| All times are GMT +5.5. The time now is 05:02. |