-3

i've recently started Learning C. and after understanding the basics I started implementing what I've learnt, so I am trying to perform basic mathematical operation like add, substract etc, but the dividion operator doesn't work. every time I give it two different values the answer is 0.00000. please help me understand the error.Here is my code.

#include<stdio.h>
void main()
{
    int a,b,c,d,e;
    float f = 0;
    clrscr();
    printf("enter the values of a, b\n");
    scanf("%d %d", &a, &b);
    c = a + b;
    d = a - b;
    e = a * b;
    f = a / b;
    printf("%d\n %d\n  %d\n  %f\n", c, d, e, f);
    getch();
}
lost_in_the_source
  • 10,998
  • 9
  • 46
  • 75
  • 5
    Please don't use this title, **Division operator does not work**, then how do we write programs? – Iharob Al Asimi Aug 26 '16 at 15:52
  • Input? Actual output? Expected output? – Support Ukraine Aug 26 '16 at 15:53
  • 2
    Get [a good book on C](http://stackoverflow.com/q/562303/253056) and read up about **integer division**. – Paul R Aug 26 '16 at 15:54
  • What did you type for `a` and `b`? Remember, since these are `int`, it will perform integer division, which rounds down. Then it will convert that to float in the assignment. – Barmar Aug 26 '16 at 15:54
  • Are you aware that code indentation is considered a good thing? – Support Ukraine Aug 26 '16 at 15:55
  • @4386427 My sister had an instructor that told her, that it was wasted space. – Iharob Al Asimi Aug 26 '16 at 15:55
  • 2
    "every time I give it two different values the answer is 0.00000." [Couldn't reproduce](http://melpon.org/wandbox/permlink/bwJTVHATdXXWke7z). Correctly `2.000000` is printed for input `5 2` after doing some hack to make it compile. – MikeCAT Aug 26 '16 at 15:56
  • Of topic: Always check the value returned by `scanf`, i.e. `if (2 != scanf("%d %d", &a, &b)) {.... error handling ....}` – Support Ukraine Aug 26 '16 at 15:58
  • @MikeCAT Right, but what about `2 5`, isn't the output `0.00000` then? – Iharob Al Asimi Aug 26 '16 at 15:58
  • @4386427 True except that `if (2 != scanf(...))` is so horrible that I wish I could delete the comment. Comment this by, annoyed not you're hope I. – Iharob Al Asimi Aug 26 '16 at 15:59
  • @iharob - feel free to flag the comment – Support Ukraine Aug 26 '16 at 16:01
  • @4386427 There is no need to. I just said it that way because Yoda conditions are really discouraged and they make the code hard. And they are not safer anymore because compilers can warn now if you accidentally write `if (x = 2)`. But in the case of `scanf()` it doesn't even make any sense at all because it's impossible to do `if (scanf(...) == 2)` wrong. – Iharob Al Asimi Aug 26 '16 at 16:02
  • 2
    @iharob she could tell her instructor that she wants to ensure the disk sectors are fully used and not left half empty - to get her money's worth. – Weather Vane Aug 26 '16 at 16:03

1 Answers1

0

You can't divide integers and expect a floating point value as a result, just do this

f = (float) a / (float) b;

and of course check b != 0.

See since a and b are integers you can only expect an integer as a result, declaring f as a float will not automagically help. For instance

1 / 2

is equal to 0, whereas

1.0 / 2 == 1 / 2.0 == 1.0 / 2.0 != 0
Iharob Al Asimi
  • 52,653
  • 6
  • 59
  • 97