Please help me with this :- | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
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.

17th Sep 2020, 8:25 AM
V1rtual*_*boi
V1rtual*_*boi - avatar
3 Answers
+ 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; }
17th Sep 2020, 8:31 AM
Hatsy Rei
Hatsy Rei - avatar
+ 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; }
17th Sep 2020, 9:15 AM
A S Raghuvanshi
A S Raghuvanshi - avatar
0
Thanks for the help
17th Sep 2020, 6:54 PM
V1rtual*_*boi
V1rtual*_*boi - avatar