Thread: Writing code for a program in C

  1. #1
    Registered User
    Join Date
    Jun 2005
    Posts
    24

    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 */
    Last edited by Sure; 06-08-2005 at 08:48 PM.

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Try searching the forum.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote 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

    Read and follow the above instructions, and try your post (in the same thread) again.

    Quzah.
    Hope is the first step on the road to disappointment.

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    24
    sorry forgot to read the faq's my bad

  5. #5
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    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. #6
    Registered User
    Join Date
    Jun 2005
    Posts
    24
    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. #7
    Registered User
    Join Date
    Jun 2005
    Posts
    4
    don't use goto, it seems well here but for big prog's it will be difficult to debug

  8. #8
    Handy Andy andyhunter's Avatar
    Join Date
    Dec 2004
    Posts
    540
    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.
    i don't think most standard compilers support programmers with more than 4 red boxes - Misplaced

    It is my sacred duity to stand in the path of the flood of ignorance and blatant stupidity... - quzah

    Such pointless tricks ceased to be interesting or useful when we came down from the trees and started using higher level languages. - Salem

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Writing a Unix Search Program, some questions
    By Acolyte in forum C Programming
    Replies: 3
    Last Post: 09-23-2008, 12:53 AM
  2. How to make a program that prints it's own source code???
    By chottachatri in forum C++ Programming
    Replies: 38
    Last Post: 03-28-2008, 07:06 PM
  3. Replies: 1
    Last Post: 12-10-2005, 11:25 AM
  4. True ASM vs. Fake ASM ????
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 04-02-2003, 04:28 AM
  5. Help with a file writing program
    By pritesh in forum C Programming
    Replies: 3
    Last Post: 11-11-2001, 01:56 AM