Division error

Posted on 2007-10-11
Last Modified: 2012-05-05
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??
Question by:adamport
    LVL 7

    Accepted Solution

    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;
    LVL 84

    Assisted Solution

    num1/num2 is an integer division
    you probably wanted

    result = (float)num1/num2;
    LVL 5

    Expert Comment

    result = num*1.0/num2;

    Featured Post

    Why You Should Analyze Threat Actor TTPs

    After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

    Join & Write a Comment

    This tutorial is posted by Aaron Wojnowski, administrator at  To view more iPhone tutorials, visit This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
    Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode ( They will have you believe that Unicode requires you to use…
    Video by: Grant
    The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.
    Video by: Grant
    The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.

    730 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now