Help Solving a Factorial Program with Infinite FOR Loop

In summary, The user is unable to get the output for a C program that calculates the factorial. They suspect that there is a problem in the for loop, possibly causing an infinite loop. The expert suggests that the error is in the for loop and advises to change the increment from "i+2" to "i=i+2" and to use "i+=2" instead of "count=count++" for better readability. The user confirms that the program is now working.
  • #1
swty todd
70
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
  • #2
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?).
 
  • #3
hey i its workin now...thnx a lot...
 
  • #4
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++
 
  • #5
okk...thnx a lot..
 

Related to Help Solving a Factorial Program with Infinite FOR Loop

1. What is a factorial program?

A factorial program is a mathematical algorithm used to calculate the factorial of a given number. The factorial of a number is the product of all the numbers from 1 to that number. For example, the factorial of 5 (written as 5!) is 1*2*3*4*5 = 120.

2. What is an infinite for loop?

An infinite for loop is a programming construct where a loop is executed repeatedly without an end condition. This means that the loop will continue to run indefinitely until it is explicitly stopped. This can lead to a program getting stuck in an infinite loop and not progressing any further.

3. Why is an infinite for loop a problem in a factorial program?

An infinite for loop can be a problem in a factorial program because the loop will continue to run without ever reaching an end condition. This means that the program will never be able to produce a result and will get stuck in an infinite loop. This can cause the program to crash or freeze.

4. How can an infinite for loop be solved in a factorial program?

An infinite for loop can be solved in a factorial program by adding an end condition to the loop. This can be done by setting a maximum number of iterations or by checking for a specific condition that will break the loop. It is also important to make sure that the loop is properly incrementing and not getting stuck in an endless cycle.

5. What are some best practices for writing a factorial program with a for loop?

Some best practices for writing a factorial program with a for loop include setting an end condition for the loop, properly incrementing the loop variable, and handling potential errors or edge cases. It is also important to use descriptive variable names and to comment the code to make it easier to understand and maintain. Additionally, using a recursive function instead of a for loop can also be a more efficient and elegant solution for calculating factorials.

Similar threads

  • Programming and Computer Science
Replies
9
Views
1K
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
4
Views
799
  • Programming and Computer Science
Replies
14
Views
2K
  • Programming and Computer Science
Replies
1
Views
952
  • Programming and Computer Science
Replies
12
Views
1K
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
4
Views
933
  • Programming and Computer Science
Replies
7
Views
1K
  • Programming and Computer Science
Replies
5
Views
22K
Back
Top