problem with very basic code

This is a discussion on problem with very basic code within the C Programming forums, part of the General Programming Boards category; This function wont work for a factorial function. Trying to figure out why. Code: while(i<n) { n=n*i; i++; } i ...

  1. #1
    Registered User
    Join Date
    Aug 2011
    Posts
    3

    Exclamation problem with very basic code

    This function wont work for a factorial function. Trying to figure out why.

    Code:
    while(i<n)
          {
               n=n*i;
               i++;
          }
    i has been initialized to 1.

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    What has n been intialized to?
    If it's preset to 0, the loop will never run.

  3. #3
    Registered User
    Join Date
    Aug 2011
    Posts
    3
    It's taken as input from the user -

    Code:
    #include <stdio.h>
    
    /*to find factorial - considering user only enters +ve integers*/
    
    main()
    {
          int i=1, n;
          printf("Enter n\n");
          scanf("%d", &n); 
          
          /*need to figure out the problem with the loop */
          while(i<n)
          {
               n=n*i;
               i++;
          }
          printf("%d", n);
          getch();
    }

  4. #4
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,047
    Quote Originally Posted by phoenix.labs View Post
    Code:
          /*need to figure out the problem with the loop */
          while(i<n)
          {
               n=n*i;
               i++;
          }
    Will the terminal condition ever be reached when both i and n are being incremented?

  5. #5
    Registered User
    Join Date
    Aug 2011
    Posts
    3
    Oh.. That was silly. Thank You

    Code:
     
    #include <stdio.h>
    
    main()
    {
          int i=1, n, p;
          printf("Enter n\n");
          scanf("%d", &n); 
          
          p=n;
          
          while(i<n)
          {
               p=p*i;
               i++;
          }
          
          printf("%d", p);
          getch();
    }
    It works now!

  6. #6
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,866
    Additionally, read How to define main() FAQ

    Also, getch() is compiler specific. Read How to get my program to wait for a keypress.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Visual Basic Code Help
    By Slinger137 in forum Windows Programming
    Replies: 9
    Last Post: 01-04-2009, 10:26 AM
  2. Basic C-code problem
    By matanzalle in forum C Programming
    Replies: 2
    Last Post: 04-16-2008, 08:41 AM
  3. basic code help
    By matt37664 in forum C++ Programming
    Replies: 4
    Last Post: 05-20-2006, 02:40 PM
  4. Basic Code
    By MadCow257 in forum Game Programming
    Replies: 6
    Last Post: 03-01-2005, 05:28 PM
  5. Basic Networking Code
    By Nexus in forum Networking/Device Communication
    Replies: 3
    Last Post: 11-12-2004, 03:41 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21