Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

100/0 = 100 ????

[b][red]This message was edited by rahulkothari at 2002-9-16 11:29:39[/red][/b][hr]
[code]
#include
void main()
{
printf("%d",100/0);
}
[/code]

the output of the above prg when compiled in [italic]Borland C++ compiler ver 3 (dos based)[/italic] turns out to be 100 ! same for 99/0=99 and so on.

100/0 should be infinity or not defined. then aint the compiler wrong ?







Comments

  • Geeth AsokanGeeth Asokan Member Posts: 127
    Hi,

    I am using the Dev C++ 4.9.5.0 compiler, which I downloaded from the Bloodshed software website (www.bloodshed.net). I don't think the compiler is the problem though. Try the following piece of code, which calculates 100/0, it should not compile:

    [code]
    #include
    #include
    #include

    int main(void)
    {
    printf("%d
    ",100/0);

    system("PAUSE");
    return 0;
    }
    [/code]

    Now, try out the following piece of code, which calculates 100/1. This should compile and give the answer, 100:

    [code]
    #include
    #include
    #include

    int main(void)
    {
    printf("%d
    ",100/1);

    system("PAUSE");
    return 0;
    }
    [/code]
    Hope this helps,

  • rahulkotharirahulkothari Member Posts: 65
    thanks for replying.

    asokan, both of the above codes compiled fine in my [italic]Turbo C++ Borland (Ver 3-dos based) compiler[/italic] .... and the answer of both was shown as 100 !

    i just dont understand that why does the compiler not follow the basic math rule i.e. 100/0 (or anything/0) is undefined.

  • Geeth AsokanGeeth Asokan Member Posts: 127
    Hi,

    I don't understand. In my compiler Dev C++ 4.9.5.0, both programs work.

    For the program below:
    [code]
    #include
    #include
    #include

    int main(void)
    {
    printf("%d
    ",100/0);

    system("PAUSE");
    return 0;
    }
    [/code]
    This message should appear [b]7 C:My DocumentsC++ Programming WorkPractice.cpp
    [Warning] division by zero in `100 / 0'[/b]

    But if you compile the following program:
    [code]
    #include
    #include
    #include

    int main(void)
    {
    printf("%d
    ",100/1);

    system("PAUSE");
    return 0;
    }
    [/code]
    The answer is 100.

    I think you should download the Dev C++ 4.9.5.0 from the Bloodshed software website (www.bloodshed.net).

    Let me know if it works with this compiler.

    Hope this helps,


  • Geeth AsokanGeeth Asokan Member Posts: 127
    Hi again,

    Try this program on your compiler (Turbo C++ Borland (Ver 3-dos based) compiler), and check if 100/0 still gives 100. This should return [b]Division by zero error![/b]

    [code]
    #include
    #include

    void main(void)
    {
    /* Input numbers */
    int number1, number2;

    /* Answer to the calculation */
    int answer = 0;

    /* Operation to perform */
    char operation;

    /* Enter the first number */
    printf("Enter the first number: ");
    scanf("%d", &number1);

    /* Enter the operation to perform */
    printf("Enter the operation to perform (+, -, *, /): ");
    scanf("
    %c", &operation);

    /* Enter the second number */
    printf("Enter the second number: ");
    scanf("%d", &number2);

    /* Do the calculation */
    switch(operation) {
    case '+':
    answer = number1 + number2;
    break;

    case '-':
    answer = number1 - number2;
    break;

    case '*':
    answer = number1 * number2;
    break;

    case '/':
    if (number2 != 0)
    answer = number1 / number2;
    else
    printf("Division by zero error!
    ");
    break;

    default:
    printf("Invalid operation!
    ");
    break;
    }

    /* Print out the answer to the calculation */
    printf("The answer to the calculation is %d
    ", answer);

    }
    [/code]

    But if you try to calculate 100/1, it should give 100 as the answer.

    Let me know if this works on your compiler (Turbo C++ Borland (Ver 3-dos based) compiler)

    Hope this helps,
  • ignatandrignatandr Member Posts: 10
    try this:
    [code]
    #include
    void main()
    {
    printf("%[red]lf[/red]",[red]100.0[/red]/0);
    }
    [/code]
    May be you will get what you want.

  • rahulkotharirahulkothari Member Posts: 65
    thx a lot everyone for replying ...i m sorry i couldnt keep track of this thread ...

    i will get back to you guys after my exams are done in jan ...

    thx again , bye.

  • Andre YoungAndre Young USAMember Posts: 0

    ________ < http://forcoder.org /> free video tutorials and ebooks about [ C, Objective-C, PL/SQL, Visual Basic .NET, Assembly, R, JavaScript, C++, Go, MATLAB, Delphi, PHP, Perl, Java, Swift, C#, Scratch, Visual Basic, Ruby, Python Scala, Lua, SAS, VBScript, Lisp, Alice, Erlang, Julia, ABAP, LabVIEW, Hack, Scheme, Crystal, Apex, Rust, Clojure, Fortran, Kotlin, FoxPro, D, Transact-SQL, COBOL, ML, Logo, Prolog, Dart, Ada, Bash, F#, Awk ] _______

Sign In or Register to comment.