Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <math.h>
- float factorial(int n){ // x!
- if(n == 0) return 1;
- else if(n < 0) return -1;
- int i = 1;
- int x = 1;
- for(i = 1; i <= n; i++) x = (float) x*i;
- return x;
- }
- float etaylor(int n){ // e = Σ1/(x!)
- if(n < 0) return -1;
- float x = 0;
- int i = 1;
- for(i = 1; i <= n; i++) x = x+(1.0/factorial(i));
- return x;
- }
- float extaylor(int x, int n){ // e^x = Σx/n!
- if(x < 0 || n < 0) return -1;
- int i = 0;
- float y = 0;
- for (i = 0; i <= (n-1); i++) y = y + pow(x,i)/factorial(i);
- return y;
- }
- int main(){
- int n, s, x;
- printf("WELCOME TO MY TAYLOR SERIES DUNGEON!!!1!!\n");
- printf("PLEASE SELECT YOUR CHALLENGE:\n");
- printf("1. y = x!\n");
- printf("2. e = E1/x!\n");
- printf("3. e^x = E[(x^k)/k!]\n");
- printf("Your selection: "); scanf("%d", &s);
- switch(s){ // Choose selection
- case 1: {
- printf("\nx = "); scanf("%d", &x);
- printf("\ny = %f", factorial(x));
- return 0;
- }
- case 2: {
- printf("\n x = "); scanf("%d", &x);
- printf("\n e = %f", etaylor(x));
- return 0;
- }
- case 3: {
- printf("\n x = "); scanf("%d", &x);
- printf("\n Upper bound k = "); scanf("%d", &n);
- printf("\n e^%d = %f", x, extaylor(x, n));
- return 0;
- }
- default: {
- printf("Invalid selection %d", s);
- return -1;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement