locked
Write a C program for the following question RRS feed

  • Question

  •  

    Hi friends,

     

    Write a C program for the following question.

     

    The program should return the last non-zero digit of n! (i.e n factorial).  The program should work for n ranging from 0 to atleast upto n=1000

     

    Sample input and output:

     

    Input    :  6

    Output :  2

     

    Explanation : 6! 720. Therefore the last non-zero digit in 720 is 2...

    Sunday, September 30, 2007 4:39 AM

All replies

  • Code Block

    #include "stdlib.h"

    using namespace std;

    int _l_non_zero_fact(int val)

    {

    long res=1;

    int i;

    for (i=1;i<=val;i++)

    res *= i;

    char* buf = new char;

    itoa(res, buf, 10);

    int ret = -1;

    for (i=0;i<strlen(buf);i++)

    ret = buf[i] == '0'? ret : (int)buf[i]-48;

    return ret;

    }

     

     

    Just something quick I cooked up. The code sucks but it will do the job anyway.

     

    Sunday, September 30, 2007 6:43 AM
  •  

    Above program may not work because of not able to store factorial of number upto 1000

     

    Here is an optimise and working sol---

     

    #include <iostream>
    #include <sstream>
    using namespace std;
    int lnondig(int num)
    {
        stringstream s;
        s<<num;
        string s1=s.str();
        int l;
        for(l=(s1.size()-1);l>=0;l--)
        {
              if (s1[l]!='0')
              break;
              }
        return (s1[l]-48);

    int _l_non_zero_fact(int val)

    {

    long res=lnondig(val);

    int i;

    for (i=val-1;i>=1;i--)
    {
        res=i*res;
        res=lnondig(res);
    }
    return res;

    }
    int main()
    {
         int l=_l_non_zero_fact(99);
         cout<<l<<endl;
         system("pause");
    }

     

    It work good in Dev-cpp

     

    Plz let me know if there r any bug in this program.

    Sunday, September 30, 2007 10:06 AM
  • Hi Imran,

     

    Great effort.. But the code fails for n>=15... The correct o/p for n=15 is 8 whereas ur prog shows 6..

    Thursday, October 11, 2007 5:04 PM
  • Here's my code ---

    But have the same problem - "THE CODE IS UNABLE TO WORK FOR BIG NUMBERS." 

    I've used "long" as the datatype. This problem could be solved by using "long double", but it won't work for my code.

    This is because - i used the "mod" operator (%) that doesn't work with the floating point types.

     

    #include<stdio.h>

     

    long fact(int x)
    {

    int i;
    long f=1;
    if(x<0 || x>1000)

    f=0;

    else

    for(i=1;i<=x;i++)

    f=f*i;

    return(f);

    }

     

    void main()
    {

    int a=12,n;
    long b;
    b=fact(a);
    if(b>=1)
    {

    printf("\n\nNumber : ",a);
    printf("\n\nFactorial : ",b);
    while(1)
    {

    n=b%10;
    if(n!=0)

    break;

    b=b/10;

    }
    printf("\n\nThe last non-zero : ",n);

    }
    else

    printf("\nOverflow error..");

    }

     

     

     

    Well, I hope to get a better solution..

    Friday, October 12, 2007 6:59 AM

  • Super answers.......
    Friday, October 12, 2007 2:14 PM