# Thread: Writing code for a program in C

1. ## Writing code for a program in C

it works but need to refiine it a bit

What i need to do:

The factorial of a non-negative integer n is written n! (pronounced "n factorial") and is defined as follows:
n! = n * (n-1) * (n-2) * ... * 1 (for values of n greater than or equal to 1)
and
0! = 1
For example 5! = 5*4*3*2*1, which is equal to 120.

Write a program that inputs a non-negative integer and computes and prints its factorial. Use a "while loop" as part of your program implementation. only between 0 and 12 .

Code:
```#include <stdio.h>

int main()                                                /* Function main begins program execution */
{

int integern;                                         /* Integer n to be evaluated */

int counter;                                          /* Integer entered */

int result;                                           /* Factorial of integer */

int start;                                            /* beginning of execution */

start:
printf( "Input value to compute factorial for:\n" );   /* Prompt input value for n */
scanf( "%d", &integern );                              /* Obtained integer value */

/* Initialization phase */

result = 1;                                           /* Intitialize result */

counter = integern;                                   /* Initialize loop counter */

/* Processing phase */

while ( counter > 1 ) {                               /* Loop until counter equals 1 */
result = result * counter;                    /* Multiply counter to result */
--counter;                                    /* decrement counter */
}

/* If integer greater or 0, print "factorial is equal to: %d" */

if ( integern >= 0 )
printf( "\nFactorial is equal to:%d\n", result );

/* otherwise, print "Input value not valid." */

else{
printf( "\nError: Input value not valid.\n\n");
goto start;
}

return 0;                                             /* Indicate program ended successfully */

}                                                         /* Ends function main */```

2. Try searching the forum.

3. Originally Posted by kermi3
Welcome to the boards. If you haven't already done so then please take some time to familiarise yourself with the faq:
http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

Make sure you have a look at the the posting guidelines:
http://cboard.cprogramming.com/annou...ouncementid=51
Following the rules will ensure you get a prompt answer to your question.

Remember, too, that the board has a search facility, a link is at the top of your screen:
http://cboard.cprogramming.com/search.php
It will often get you a quicker answer to your questions than waiting for a response to one you have posted.

It appears that you are posting a homework assignment or other project.

Please don't ask people to do all your work for you, See the announcement on Homework at the top of all forums to see what is acceptable or PM me. Basically people are happy to help, but they're not going to do it all for you.

Show us what you've got, show your code (using code tags), or where you're confused and someone will be happy to help you I'm sure. If it's something that you absolutely don't understand how it works, like you have no clue how qsort works, then ask a general question about the function and I'm sure someone will explain it. Though they may not give you all of the code for it, but someone will explain the concept.

On obivous homework questions especially, I like to remind people of the board's ninth guildeline, while this board is very helpful to people, make sure you have your instructor's permission before seeking help on assignments. While people on these boards are more than happy to help, we discourage people from asking for help on graded work without the instructor's permission, and we claim no repsonsibilty for any cheating or honor violations.

Feel free to PM me with any questions.

Good Luck,

Kermi3

Quzah.

5. Details are also very important on this board. Are you getting error messages? Is the program behaving differently than you thought it would? If so, how? The questions give us some idea of where to look to help you.

I notice you're using goto - this is considered very bad practice in C. It's almost always better to use constructs such as loops, etc..

6. Thx bro i changed it to this seems to bw working fine:

Code:
```#include <stdio.h>

int main()

{
long factorial;
long num;
long counter;

factorial = 1;
num = 0;
counter = num;

do {
printf( "Enter a positive integer less than 13: " );
scanf( "%d",&num );
}	while( num < 0 || num > 12 );
counter = num;

while ( counter > 1 ) {
factorial = factorial * counter;
counter = counter - 1;
}

if( num >= 0 )
printf( "\n%d! is %d\n\n", num , factorial);

return 0;
}```

7. don't use goto, it seems well here but for big prog's it will be difficult to debug

8. This is a classic recursion problem. Just do a search for recursion and factorials.

The key to this is that you have a predefined endpoint, aka 0! = 1.