Dividing two integers to produce a float result [duplicate] - c++

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Why can't I return a double from two ints being divided
My C++ program is truncating the output of my integer devision even when I try and place the output into a float. How can I prevent this whilst keeping those to variables (a & b) as integers?
user#box:~/c/precision$ cat precision.cpp
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
int a = 10, b = 3;
float ans = (a/b);
cout<<fixed<<setprecision(3);
cout << (a/b) << endl;
cout << ans << endl;
return 0;
}
user#box:~/c/precision$ g++ -o precision precision.cpp
user#box:~/c/precision$ ./precision
3
3.000

Cast the operands to floats:
float ans = (float)a / (float)b;

Related

How can I print a a number upto 6 decimal digits in c++? [duplicate]

This question already has answers here:
Fixed Decimal Precision [duplicate]
(4 answers)
Closed 2 years ago.
I am trying a problem. I need to print the ans in 6 decimal digits. For example if the ans is 64, I want to print 64.000000
I tried the following way.
what I did wrong?
#include <iostream>
#include<bits/stdc++.h>
using namespace std;
int main() {
long long t;
cin>>t;
float n;
while(t--)
{
cin>>n;
float s=(n-2)*180.000000;
float w=(s/n)*1.000000;
cout<<w*1.000000<<setprecision(6)<<endl;
}
return 0;
}
You can make use of std::fixed:
#include <iostream>
#include <iomanip>
void MyPrint(int i)
{
double d = static_cast<double>(i);
std::cout << std::setprecision(6) << std::fixed << d << std::endl;
}
int main() {
MyPrint(64);
MyPrint(100);
return 0;
}
Running the above code online results in the following output:
64.000000
100.000000

Function pow doesn't show the right answer [duplicate]

This question already has answers here:
Why does pow(n,2) return 24 when n=5, with my compiler and OS?
(4 answers)
Why pow(10,5) = 9,999 in C++
(8 answers)
Closed 3 years ago.
I have this code and the function pow() is behaving differently in two cases.
For "p" I'm using the number 3, in the first output is showing 124 but the second shows 125.
#include <iostream>
#include <math.h>
using namespace std;
int main(){
long long int p, ans;
cin >> p; //3
ans = pow(5,p);
cout << ans << endl; //124
cout << pow(5,p) << endl; //125
return 0;
}

Variables declared in header cant be used in main file? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Why can't I return a double from two ints being divided
My C++ program is truncating the output of my integer devision even when I try and place the output into a float. How can I prevent this whilst keeping those to variables (a & b) as integers?
user#box:~/c/precision$ cat precision.cpp
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
int a = 10, b = 3;
float ans = (a/b);
cout<<fixed<<setprecision(3);
cout << (a/b) << endl;
cout << ans << endl;
return 0;
}
user#box:~/c/precision$ g++ -o precision precision.cpp
user#box:~/c/precision$ ./precision
3
3.000
Cast the operands to floats:
float ans = (float)a / (float)b;

trying to create factorial calculator but fact dips below 0 [duplicate]

This question already has answers here:
Factorial does not work for all values
(2 answers)
Closed 4 years ago.
I'm new at c++, I'm trying to create a factorial calculator, but I'm having trouble calculating n! when n > 15.
Here's the Code:
#include <iostream>
using namespace std;
int calculate_factorial(int num)
{
int fact = num;
for (int i = fact - 1; i > 0; i--)
{
cout << i << " " << fact << endl;
fact *= i;
}
return fact;
}
int main()
{
int num = 16;
cout << calculate_factorial(num) << endl;
system("pause");
}
Apart from the fact that calling it a sum is just... no. Anyway, int can only go up to 2^31 - 1, and that's only good for 12!. Even changing it to unsigned int won't change that, and even using unsigned long long int would only allow you to go up to 20!.
Your result is overflowing. You might wanna look into the GNU Multiple Precision library.

How to calculate a/b in C++? [duplicate]

This question already has answers here:
Why does dividing two int not yield the right value when assigned to double?
(10 answers)
Closed 6 years ago.
I am trying to get value for a/b, but I always get '0' for a/b.
Why do I get a/b=0 ?
#include <iostream>
using namespace std;
int main()
{
int a,b;
cout << "Give a and b" << endl;
cin >> a >> b;
double q=a/b;
cout << "a/b=" << q;
return 0;
}
You are using integer arithmetic, so the division result will be an integer. A floating point value that is between 0 and 1 will get truncated to 0 when interpretted as an integer, before it is assigned to your q variable. So either:
change your a and b variables to double instead of int
typecast a and/or b to double during the division.
Either way, you will then be performing floating-point division instead of integer division, so the result will be a floating-point value.
Can do double q=double(a)/b; instead.