Division error

Hi, I have made a simple C program which asks for two input numbers, divides them and then outputs the result. The two input numbers must be integers and  the output must be as a float.

The program does not give the required output, e.g:
If, I enter in two numbers, 2 and 3 the program should divide those numbers and output the result which should be 0.66666666... instead it outputs 0.0000000

Here is the source code shown below:

#include <stdio.h>


                      int num1,num2;
      float result;

      printf("Enter two integers > ");
      scanf("%d %d",&num1,&num2);

      result = num1/num2;

      printf("%d divided by %d is %f",num1,num2,result);


Any ideas??
Who is Participating?
UrosVidojevicConnect With a Mentor Commented:
When applied to two integers operator / does a integer division.
That means:
2 / 3 = 0.
3 / 3 = 1;
5 / 3 = 1;
6 / 3 = 2;

Instead of

  result = num1/num2;

you should write:
  result = (float)num1 / num2;
ozoConnect With a Mentor Commented:
num1/num2 is an integer division
you probably wanted

result = (float)num1/num2;
result = num*1.0/num2;
All Courses

From novice to tech pro — start learning today.