Hello to all coders out there.

I have a doubt regarding the format "long double" which is producing wierd results when used in the case of a factorial program.

When "long double" used with C lang. produces the output

input = 3
factorial = -2.000000

while when used with C++ gives

input = 3
factorial = 6

So does it imply that long double doesnt work or is not supported by C and only by C++.

Thanks in advance for your help.

The programs in both lang. are given below

// code in C lang
#include <stdio.h>

long double factorial (int input) {
    long double result = 1;
    while (input) {
        result *= input--;
    }
    return result;
}
 
int main() {
    int input = 1;
    long double result = 0;
    printf("\nEnter the number whose factorial you want :  ");
    scanf("%d", &input);
    result = factorial(input);
    printf("\nThe factorial of %d is %Lf", input, result);
    return 0;
}
// In C++ lang.
 
#include <iostream>
using namespace std;
long double factorial (int input) {
    long double result = 1;
    while (input) {
        result *= input--;
    }
    return result;
}
int main() {
    int input;
    long double result = 0;
    cout<<"\nyoEnter the number whose factorial you want :  ";
    cin>>input;
    result = factorial(input);
    cout<<"\nThe factorial of "<<input<<" is "<<result;
    return 0;
}

Recommended Answers

All 13 Replies

In code in C language try to use %lf instred %Lf.
Conversion for long double is %lf.

In code in C language try to use %lf instred %Lf.
Conversion for long double is %lf.

Have already tried all the scanf identifiers (including %lf, %d,&le ..) the program just gives a wierd output.

Please run the both the programs and you will urelf know wat i am trying to say.

Anyways thanks for the answer.

Member Avatar for iamthwee

Why would you wanna cast it as a long double anyway?

If you wanna find big factorials use a big number library.

The C code works for me. Output for input 3 is 6.0000

Why would you wanna cast it as a long double anyway?
If you wanna find big factorials use a big number library.

Any suggestions? Thanks for your reply.

The C code works for me. Output for input 3 is 6.0000

*Damn* Which compiler are u using anyways coz i am using GNU gcc complier.

Member Avatar for iamthwee

This should work in c for big factorials.

#include <stdio.h>

#define l11l 0xFFFF
#define ll1 for
#define ll111 if
#define l1l1 unsigned
#define l111 struct
#define lll11 short
#define ll11l long
#define ll1ll putchar
#define l1l1l(l) l=malloc(sizeof(l111 llll1));l->lll1l=1-1;l->ll1l1=1-1;
#define l1ll1 *lllll++=l1ll%10000;l1ll/=10000;
#define l1lll ll111(!l1->lll1l){l1l1l(l1->lll1l);l1->lll1l->ll1l1=l1;}\
lllll=(l1=l1->lll1l)->lll;ll=1-1;
#define llll 1000
#define stop getchar(); getchar();




                                                     l111 llll1 {
                                                     l111 llll1 *
      lll1l,*ll1l1        ;l1l1                      lll11 lll [
      llll];};main      (){l111 llll1                *ll11,*l1l,*
      l1, *ll1l, *    malloc ( ) ; l1l1              ll11l l1ll ;
      ll11l l11,ll  ,l;l1l1 lll11 *lll1,*            lllll; ll1(l
      =1-1 ;l< 14; ll1ll("\t\"8)>l\"9!.)>vl"         [l]^'L'),++l
      );scanf("%d",&l);l1l1l(l1l) l1l1l(ll11         ) (l1=l1l)->
      lll[l1l->lll[1-1]     =1]=l11l;ll1(l11         =1+1;l11<=l;
      ++l11){l1=ll11;         lll1 = (ll1l=(         ll11=l1l))->
      lll; lllll =(            l1l=l1)->lll;         ll=(l1ll=1-1
      );ll1(;ll1l->             lll1l||l11l!=        *lll1;){l1ll
      +=l11**lll1++             ;l1ll1 ll111         (++ll>llll){
      l1lll lll1=(              ll1l =ll1l->         lll1l)->lll;
      }}ll1(;l1ll;              ){l1ll1 ll111        (++ll>=llll)
      { l1lll} } *              lllll=l11l;}
      ll1(l=(ll=1-              1);(l<llll)&&
      (l1->lll[ l]              !=l11l);++l);        ll1 (;l1;l1=
      l1->ll1l1,l=              llll){ll1(--l        ;l>=1-1;--l,
      ++ll)printf(              (ll)?((ll%19)        ?"%04d":(ll=
      19,"\n%04d")              ):"%4d",l1->         lll[l] ) ; }
                                                     ll1ll(10); stop}

This should work in c for big factorials.
.......

LOLZ wat is this beast? I dont think i am that expert in C to figure out the plethora of #defines.

Anyways you were talkin of some libraries, do you know any?

Thanks for your reply.

@WolfPack

Can you please tell me which compiler are you using :cry: ?

Member Avatar for iamthwee

Try it, it should work.

If your sole interest to to compute large factorials then that should be fine and dandy. Like all big num libraries they don't rely on massive 64-bit+ data types.

It uses chars.

Try it, it should work.

If your sole interest to to compute large factorials then that should be fine and dandy. Like all big num libraries they don't rely on massive 64-bit+ data types.

It uses chars.

So do u imply that all the big shot math libraries use chars to execute the complex operataions with numbers?

Member Avatar for iamthwee

So do u imply that all the big shot math libraries use chars to execute the complex operataions with numbers?

Yup. They definitely don't use 64-bit + data types that's for sure.

For example if I wanted to multiply 3454534589384989347697547865478672934875 by 237485748375943794857937485739485793847582983478734873847837487485. It would be better to read both inputs in a chars. That's what I mean.

Yup. They definitely don't use 64-bit + data types that's for sure.

For example if I wanted to multiply 3454534589384989347697547865478672934875 by 237485748375943794857937485739485793847582983478734873847837487485. It would be better to read both inputs in a chars. That's what I mean.

Hmm.. sorry for asking this stupid question but how will you read both input as characters. Can you please explain in detail as my concepts are a bit fuzzy.

Also i had got stuck with a question whose answer i could not find out how it came. The question is

// find the output
 
main()
{
[LEFT]int i = 258;
int *iPtr = &i;
printf("%d %d", *((char*)iPtr), *((char*)iPtr+1) );
}[/LEFT]
 
[I][B]// Answer:
[/B][/I]
// 2   1
// ? can you explain how this output came.

Sorry if i have bothered you a lot and if this question is off the topic you can flame me and ask me to post in a seperate thread.

Thanks for all your valuable time.

Member Avatar for iamthwee

Hmm.. sorry for asking this stupid question but how will you read both input as characters. Can you please explain in detail as my concepts are a bit fuzzy.

Think about how you would do long multiplication on paper. Where you deal with two single digits at a time.

Also i had got stuck with a question whose answer i could not find out how it came. The question is

I don't know the language well enough to answer that.

@WolfPack
Can you please tell me which compiler are you using :cry: ?

Visual C++ 2003

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.