C program for sum of exponential numbers

X and n numbers will be entered
1+x^1+x^2+x^n =?

Here is the program to solve this,

#include <stdio.h>
#include <math.h>

int main()
{
    int x, n;
    int sum = 0;
    int loop;

printf("Enter the value of x: ");
scanf("%d", &x);

printf("Enter the value of n: ");
scanf("%d", &n);

sum += 1; //add 1 to the sum which is first term
for (loop = 1; loop <= n; loop++) {

    sum += pow(x, loop); //pow(x,loop) = x^1, x^2, ... x^n
}

printf("sum = %d", sum);

return 0;
}

Output:
Enter the value of x: 2
Enter the value of n: 3
sum = 15

1 Like

But why dont u use pow(X, n)?I don’t understand for 5 hours.And function has error.If n<0 result is
5^-1=1.And another problem 0^1 = 1.

  • must be 0^1=0
  • must be 5^-1=6/5
    How can we solve this problem?
  1. use both header files in the code
  2. n can not be used - if we use then in each case x will raised to the power of n, while - we need it 1, 2,… n. This, we used “loop” instead of “n”.
  3. if n=0, then sum = 1 (it’s correct - no issue in the code)
    If still find any issue share the output