0
If a=5 b=25 n=4, why output is zero?
#include <stdio.h> int main() { int a,b,n; float result; printf("First value=\n"); scanf("%d",&a); printf("Second value=\n"); scanf("%d",&b); printf("Select\n\t1.Summaton\n\t2.Substraction\n\t3.Multiplication\n\t4.Division\nChoose an option=\n"); scanf("%d",&n); switch(n) { case 1: result=a+b; printf("Summation=%f\n",result); break; case 2: result=a-b; printf("Substraction=%f\n",result); break; case 3: result=a*b; printf("Multiplication=%f\n",result); break; case 4: result=a/b; printf("Division=%f\n",result); break; default : printf("Wrong option"); } return 0; }
3 Answers
+ 3
You should convert a and b into floats before division.
+ 2
because in case 4
result = a/b
since a and b are int data type.
and a < b so a /b = 0.
if a or b were float it would give the correct result with decimal places.
+ 1
Tanjir Ahmed here is your code modified ... change a and b to floats then as a bonus use %e instead of %d as you are assigning both as float pointers.
https://code.sololearn.com/cyk0Y254B1Fv/?ref=app