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..
 
Thread 'Star maps using Blender'
Blender just recently dropped a new version, 4.5(with 5.0 on the horizon), and within it was a new feature for which I immediately thought of a use for. The new feature was a .csv importer for Geometry nodes. Geometry nodes are a method of modelling that uses a node tree to create 3D models which offers more flexibility than straight modeling does. The .csv importer node allows you to bring in a .csv file and use the data in it to control aspects of your model. So for example, if you...
I tried a web search "the loss of programming ", and found an article saying that all aspects of writing, developing, and testing software programs will one day all be handled through artificial intelligence. One must wonder then, who is responsible. WHO is responsible for any problems, bugs, deficiencies, or whatever malfunctions which the programs make their users endure? Things may work wrong however the "wrong" happens. AI needs to fix the problems for the users. Any way to...
Back
Top