Help Solving a Factorial Program with Infinite FOR Loop

AI Thread Summary
The discussion centers on troubleshooting a C program intended to calculate a series using factorials. The primary issue identified is an infinite loop caused by an incorrect increment in the for loop. The original increment statement "i+2" does not update the value of i, leading to the infinite loop. The correct increment should be "i=i+2" or the shorthand "i+=2". Additionally, there is a problem with the statement "count=count++", which does not function as intended due to its ambiguous behavior; it should simply be replaced with "count++". After making these corrections, the program functions correctly, and the participants discuss preferences for increment syntax, noting that "i+=2" is more concise and easier to read. The conversation emphasizes clarity in code and the importance of understanding operator behavior in C programming.
swty todd
Messages
69
Reaction score
0
i m unable 2 get the output for the followin c program.
The factorial program is correct
i think there is problem in the FOR loop..guess its goin infinite...pls help

#include<stdio.h>
#include<math.h>
factorial(int);
main()
{
int n,count,i;
float number,x,series;
printf("nter number and no of terms");
scanf("%f %d",&number,&n);
x=3.14*number/180;
count=0;
series=0;
for(i=1;i<=(2*n);i+2)
{
series=series+pow(x,i)*pow(-1,count)/factorial(i);
count=count++;
}
printf("%f",series);
return 0;
}

int factorial(int m)
{int fact;
if(m==1)
return (1);
else
fact=m*factorial(m-1);
return(fact);
}
 
Technology news on Phys.org
Your error is in your for loop. Your increment is "i+2" where it should be "i=i+2". "i+2" doesn't change the value of i, and gives you an infinite loop.

Also, "count=count++" is problematic; "count++" has the same meaning as "count=count+1". The behavior of the "count=count++" is not obvious and probably unspecified (does the left-assignment come before or after the increment?).
 
hey i its workin now...thnx a lot...
 
i+=2;
i=i+2;
same
the first statement is preferred because it has 4 keystrokes instead of 5 keystrokes

also, it is easier for me to read like this:
i += 2;
i = i + 2;

If you have doubt about operator precedence, you can use ++count instead of count++
 
okk...thnx a lot..
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I have a quick questions. I am going through a book on C programming on my own. Afterwards, I plan to go through something call data structures and algorithms on my own also in C. I also need to learn C++, Matlab and for personal interest Haskell. For the two topic of data structures and algorithms, I understand there are standard ones across all programming languages. After learning it through C, what would be the biggest issue when trying to implement the same data...
Back
Top