0
What is wrong with my code? I'm not getting my desired output. Answer for 1+x+x^2+x^3+.....
2 Answers
+ 2
try this:
#include <stdio.h>
#include<math.h>
int main() {
int n,i,x,sum=0;
printf ("enter x and n value");
scanf("%d %d",&x,&n);
if (n>0)
{
for(i=0;i<=n;i++)
{
sum=sum+pow(x,i);
}
printf("sum is %d",sum);
}
return 0;
}
0
Learning
Initialise x with 0 to avoid garbage value and start i with 0 till <= n