-1

I just started learning programming and I have been trying to write a very simple program that takes two integer numbers and divides them. I don't know what's wrong with my code. It works fine for some cases but other times it returns 0;

#include<stdio.h>

int main()
{
   int a,b;
   scanf("%d%d",&a,&b);
   double D= a/b;
   printf("%.4lf",D);
}

1 Answers1

0

Even though the variable D is of type double, since a and b are both integers, integer division will occur. This means that only the integer part of the division remains. For example 2/3 is 0.6 repeating, but will be truncated to 0.

marco
  • 341
  • 1
  • 7