Thread: Looping Problem

  1. #1
    Registered User usmankhan's Avatar
    Join Date
    Jul 2007
    Posts
    2

    Looping Problem

    Guys!

    I have the code it calculates the factorial only once i want it to calculate it again n again until "0" is entered i think it involves the use of sentinel loops but how its used don know

    The code that i have is here:


    Code:
    #include<stdio.h>
    void main()
    {
    long int fact , n;
    printf ("Enter the integer");
    scanf ("&#37;ld", &n);
    fact = 1;
    while (n>=1)
    {
    fact = fact * n;
    n = n - 1;
    }
    printf ("Factorial = %ld", fact);
    }
    Last edited by usmankhan; 07-30-2007 at 07:43 AM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    main returns int, see the FAQ.

    So use another while loop around the code which asks the question, reads the response and prints the result.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User linuxdude's Avatar
    Join Date
    Mar 2003
    Location
    Louisiana
    Posts
    926
    think about what you are asking. you want it to keep working until 0 is entered doesn't that sound like another loop to you?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Words and lines count problem
    By emo in forum C Programming
    Replies: 1
    Last Post: 07-12-2005, 03:36 PM
  2. looping problem
    By chris285 in forum C++ Programming
    Replies: 4
    Last Post: 04-22-2005, 11:03 AM
  3. half ADT (nested struct) problem...
    By CyC|OpS in forum C Programming
    Replies: 1
    Last Post: 10-26-2002, 08:37 AM
  4. binary tree problem - help needed
    By sanju in forum C Programming
    Replies: 4
    Last Post: 10-16-2002, 05:18 AM
  5. Looping problem
    By sketchit in forum C Programming
    Replies: 2
    Last Post: 10-01-2001, 02:19 PM