Having to solve a fraction | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Having to solve a fraction

The fraction is n!/(4^(n^2)). For example, when I enter the value of n = 3, i get Factorial = 6, Numitor = 262144, but get Rezultat = 0. What should I do that Rezultat shows the actual value of the fraction. I thought about changing the data type of rezultat to something, but I'm not sure. Any help is appreciated. This is my code #include <iostream> #include <cmath> using namespace std; int nFactorial(unsigned int N) { unsigned long long Factorial=1; int i; for(i=1;i<=N;++i) { Factorial*=i; } return Factorial; } int numitor(unsigned int N) { float n_numitor=pow(4, pow(N, 2)); return n_numitor; } int main() { unsigned int n; cout<<"Introduceti n = ";cin>>n; cout<<"Factorial = "<<nFactorial(n)<<endl; cout<<"Numitor = "<<numitor(n); float rezultat=nFactorial(n)/numitor(n); cout<<endl<<"Rezultat = "<<rezultat; return 0; }

27th Mar 2020, 6:09 PM
Zadnipro Ion
2 Answers
+ 4
The rezultat = int / float. It makes to integer and after the decimal point everything is gone
27th Mar 2020, 6:33 PM
JaScript
JaScript - avatar
+ 3
You divide an integer by an integer, which in C++ results in another integer and therefore no floating point arithmetics. If you expect a floating point value, either of the operands has to be a floating point value itself. The easiest way would be to have the numitor function return a float instead of an int.
27th Mar 2020, 6:31 PM
Shadow
Shadow - avatar