Home > Software engineering >  Code is returning zero from a mathematical expression
Code is returning zero from a mathematical expression

Time:01-26

#include <iostream> 
#include <cmath>

using namespace std;  

int Factorial_of(int n)
{
    int fact = 1, i;
    for(i=1; i<=n; i  )
    fact = fact * i;
    return fact;
}



int main()
{
    cout << Factorial_of(4) / (Factorial_of(10) * Factorial_of(abs(4-10))) << endl;
}

Does anyone know why this is printing 0?

And it is outputting "9.18577e-009" now, Does anyone know why?

CodePudding user response:

#include <iostream> 
#include <cmath>

using namespace std;  

double Factorial_of(int n)
{
    double fact = 1;
    int i;
    for(i=1; i<=n; i  )
    fact = fact * i;
    return fact;
}



int main()
{
    double res=Factorial_of(4) / (Factorial_of(10) * Factorial_of(abs(4-10)));
    cout<<res;
}

There are some errors/mistakes . First for division always go with double variable. And Your code is returning value in point so return type should be double. Also you miss << in cout statement before endl.

CodePudding user response:

I can explain the output you get after fixing things: its the right answer

enter image description here

  •  Tags:  
  • Related