Factorial Parsing

This is a discussion on Factorial Parsing within the C++ Programming forums, part of the General Programming Boards category; Hi I need help writing this program The factorial, n!, of an integer is defined to be: n! = ( ...

  1. #1
    Registered User
    Join Date
    Sep 2002
    Posts
    8

    Factorial Parsing

    Hi I need help writing this program

    The factorial, n!, of an integer is defined to be:

    n! = ( n * ( n - 1 ) * ( n - 2 ) * ….. * 2 * 1 )

    An application of this formula is as follows, 5! = 5 * 4 * 3 * 2 * 1 = 120. This month’s challenge is to find the rightmost non-zero digit of n!. From the above example this number is 2. The rightmost non-zero digit of 12! ( 12! = 479001600 ) is 6.

    Write a program that will input an integer between 1 and 14 inclusive and output the rightmost non-zero digit of the factorial. For an added challenge, handle inputs between 1 and 5000 inclusive.

    Here are a few test cases for you to try:

    Factorial Rightmost Non-Zero Digit
    5 - 2
    21 - 4
    789 - 4
    1000 - 2
    4013 - 8



    thanks for the help

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Show some code that you have written so far, and point out where you're having trouble, then someone will be able to help you better.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072

    Re: Factorial Parsing

    Originally posted by Joes108
    This month’s challenge is to find the rightmost non-zero digit of n!.
    Haha, this is clearly some kind of contest or assignment.

    But nevertheless:
    Code:
    cout << "Input n:";
    int n,curr=1;
    cin >> n;
    for (int c=1; c<=n; ++c) 
    { 
        long tmp = c; 
        while (tmp%5 == 0) 
        { 
            tmp/= 5; 
            curr /=2; 
        } 
        curr = (curr % 100000) * tmp; 
    } 
    curr%=10;
    cout << "Last number: " << curr;
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Segmentation fault using recursion to find factorial
    By kapok in forum C++ Programming
    Replies: 4
    Last Post: 02-23-2009, 11:10 AM
  2. Recursion
    By Lionmane in forum C Programming
    Replies: 11
    Last Post: 06-04-2005, 01:00 AM
  3. Basic Factorial from 1-10
    By AaA in forum C Programming
    Replies: 20
    Last Post: 05-28-2005, 08:39 AM
  4. I hate string parsing with a passion
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 03-19-2002, 07:30 PM
  5. factorial output
    By maloy in forum C Programming
    Replies: 1
    Last Post: 03-13-2002, 03:28 PM

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