+ 1

I am getting correct answer only for multiples of the fiven number.Why is so???

int a,b; float c; cout<<"Enter wo numbers"; cin>>a>>b; c=a/b; cout<<c; /* The answer is coming true only for multiples of b. * Is it necessary to have a and b in float???? */

6th Aug 2019, 1:44 PM
Harsh Sharma
Harsh Sharma - avatar
4 Respuestas
+ 1
Harsh Sharma You are partly correct.. See, when you divide an integer with integer then it will return integer value so it doesn't matter if you are storing it in float variable or double.. For example, Dividing 7 with 2 will give answer 3.5 and if I store this in float variable it will be stored as 3.0 bcoz dividing integer with integer will give the part before decimal point.. So if you want to store whole decimal value then in this code declare either a or b as float or typecast a or b at formula like this.. c=(float)a/b; And last thing if you are using constant value like c=7/2 then write it like c=7/2.0 for storing whole decimal value..
11th Aug 2019, 2:56 PM
Alaska
Alaska - avatar
+ 6
Harsh Sharma code is returning the division, and it's working correctly. Ex:- 17, 8 will return 2 and 24, 8 will returns 3
6th Aug 2019, 1:48 PM
GAWEN STEASY
GAWEN STEASY - avatar
0
hi
6th Aug 2019, 4:13 PM
Nisar
Nisar - avatar
0
can u teach to use class in CSsS
6th Aug 2019, 4:14 PM
Nisar
Nisar - avatar