
Logical Error
I have a logical problem that i want to divide any number by 2. i.e 4/2 the answer is 2 & the remaining is Zero. If i divide 5/2 then the remaining woule be 1.
My problem is that either i input 4 or 5 value in num the ELSE statement always execute. There is a logical error in my coding. I want that if i divide 4/2 then the remainder is Zero so the statement after if should be executed other wise the else statement but here always my else statement execute.
Please help how can i solve my Logical error. Thanks in Advance...
#include<stdio.h>
#include<conio.h>
void main()
{
int num,div;
clrscr();
printf("\n\tEnter any value = ");
scanf("%d",&num);
div=num/2;
if (div==0)
printf("\n\tRemaining is zero");
else
printf("\n\tRemaining is One");
getch();
}

Use the modulus operator % to find the remainder.

The only way that the statement will evaluate to 0 is if num==0. Scarlet7 was right, replace that line with
You might also want to change the name of "div" to something like "remainder." Or you could streamline the code a bit
Code:
if (num%2)
printf("\n\tRemaining is one");
else
printf("\n\tRemaining is zero");
That works because false is the same as 0 and true is anything else.