0
Please help me with this :-
#include <iostream> using namespace std; int main( ) { int a , b; int divide; cout << "enter a number = "; cin >> a; cout << "enter another number = "; cin >> b; divide = a / b; cout << "divide is: " << divide << endl; return 0; } But the output is : enter a number =45 enter another number = 46 divide is:0 Why? Whenever the value of a < b the output shows 0 why ? Please help me with this.
3 ответов
+ 7
Integer division takes place when you divide two integer variables. If you want to obtain floating point results, cast at least one of the operands to float.
#include <iostream>
int main()
{
int a = 39, b = 42;
//std::cout << a/b;
std::cout << float(a)/b;
}
+ 2
Yes your will give zero in numerator is smaller than denominator . If you want value in decimal form you need to do type cast or you can use float data type you saying when you comparing both Number it giving zero . It wont give zero it will return true or 1
Becoz 45<46 which is true so result will be true .
#include <iostream>
using namespace std;
int main( )
{
float a , b;
float divide;
cout << "enter a number ";
cin >> a;
cout<<"\n entered no is="<<a;
cout << "\nenter another number ";
cin >> b;
cout<<"entered no is "<<b;
divide = a / b;
cout << "\ndivide is: " << divide << endl;
return 0;
}
0
Thanks for the help