Help Solving a Factorial Program with Infinite FOR Loop

Click For Summary

Discussion Overview

The discussion revolves around troubleshooting a C program intended to calculate factorials, specifically addressing an infinite loop issue within a for loop structure. Participants explore the implications of code syntax and operator behavior in the context of programming.

Discussion Character

  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant identifies that the for loop's increment statement "i+2" does not modify the value of i, leading to an infinite loop.
  • Another participant points out that the increment should be "i=i+2" to ensure proper functionality.
  • Concerns are raised about the expression "count=count++", with a participant explaining that its behavior is not straightforward and could lead to confusion.
  • A later reply suggests that using "i += 2" is preferred for brevity and readability, while also mentioning the use of "++count" as an alternative to "count++" for clarity on operator precedence.

Areas of Agreement / Disagreement

Participants generally agree on the issues with the for loop and the increment expressions, with some expressing satisfaction after the corrections were made. However, there is no formal consensus on the best practices for incrementing variables, as preferences for syntax are expressed.

Contextual Notes

Limitations include potential misunderstandings regarding operator precedence and the implications of different increment styles, which remain unresolved in the discussion.

Who May Find This Useful

Readers interested in programming, particularly in C language syntax and debugging practices, may find this discussion relevant.

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..
 

Similar threads

  • · Replies 9 ·
Replies
9
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 5 ·
Replies
5
Views
23K
Replies
14
Views
4K
  • · Replies 4 ·
Replies
4
Views
7K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 66 ·
3
Replies
66
Views
6K
  • · Replies 2 ·
Replies
2
Views
2K