-5

This program correctly prints whether a number is even or odd ...

#include <stdio.h>
int main(void)
{
    int n;
    printf("Please enter a number:");
    scanf("%d", &n);

    if(n % 2 == 0)
        printf("%d is even", n);
    else
        printf("%d is odd",n);

    return 0;
}

I don't understand how n % 2 can give a meaningful result when n is less than two. % is the remainder operation, right? If n is less than two, how can you divide it by two at all?

zwol
  • 135,547
  • 38
  • 252
  • 361
Asif Naeem
  • 61
  • 1
  • 7
  • 1
    What do you think happens if the input value is less than 2? – Stephen Canon Oct 02 '14 at 19:09
  • `n%2` returns the remainder of the division by two... so an even number is always divisible by two (i.e. remainder 0) whilst dividing an odd number always leaves a remainder of 1. – isedev Oct 02 '14 at 19:09
  • 4
    Please explain how you understand `3 % 2` but not `1 % 2`. – Bill Lynch Oct 02 '14 at 19:09
  • 2
    Do you know what does `%` means in C programming languages ? – haccks Oct 02 '14 at 19:10
  • if user put the value of n=0 or n=1. Then how that program will work? – Asif Naeem Oct 02 '14 at 19:11
  • I think this is actually a sensible, although poorly expressed, question, so I have taken the liberty of revising it to make it clearer what you are confused about. – zwol Oct 02 '14 at 19:18
  • 3
    The question seems to be "What is division?" – M.M Oct 02 '14 at 19:19
  • @MattMcNabb "How is integer division defined in this specific corner case that wasn't covered in the OP's grade-school math classes and which (to someone who doesn't know it *is* defined by the laws of arithmetic) might vary from language to language or CPU to CPU" is an on-topic question. – zwol Oct 02 '14 at 19:21
  • 2
    Given `(x % y)` if `x < y` then `(x % y) = x`. You shouldn't confuse `%` with `/` they are two completely different operations. So, `x % y` does not mean `x divided by y` – Adjit Oct 02 '14 at 19:21
  • duplicate of http://stackoverflow.com/questions/3602827/what-is-the-behavior-of-integer-division-in-c/ – M.M Oct 02 '14 at 19:27
  • @Adjit: Untrue (for negative numbers case), e.g. `-3 % 2` results into `-1`. According to C standard (C11 §6.5.5/6) there is "truncation towards zero", so `-3 / 2` results into `-1`, then `-1*2 + r = -3`, thus `r = -3 + 2 = -1` (according to `(a/b)*b + a%b = a` equation). – Grzegorz Szpetkowski Oct 02 '14 at 19:27
  • 1
    @GrzegorzSzpetkowski very true, probably should've specified when `x & y >= 0` – Adjit Oct 02 '14 at 19:29
  • @Adjit Any operation to find `/` also finds `%` in the same process. (CPUs use a single instruction to generate both). `x % y` is defined as `x - (x / y) * y`. Both are "division", just one of them says "take the quotient and discard the remainder" and the other says "take the remainder and discard the quotient" – M.M Oct 02 '14 at 19:30
  • 1
    Formally: find the unique solution to `x = Q * y + R` where if Q is negative then `Q < R` and `R <= 0`, or otherwise, `0 <= R` and `R < Q`. Then `x/y` is defined as `Q`, and `x%y` is defined as `R`. – M.M Oct 02 '14 at 19:32
  • 1
    @Adjit: I have made a mistake in calculations, now it should be correct. The whole my point was to show that your rule fails for negative numbers, at least using C standard semantics. – Grzegorz Szpetkowski Oct 02 '14 at 19:34
  • @MattMcNabb was talking about this more from a mathematical standpoint. (1 mod 2). Yes you are right that modulo is a variation of division but the two are not the same thing. – Adjit Oct 02 '14 at 19:34
  • In that code we are giving the condition n%2==0. I am asking that if value of n would be 0 or 1, that is less than two. How that program will find the number is even or odd. It is bit confusing for me. – Asif Naeem Oct 02 '14 at 19:36
  • @AsifNaeem work out `n / 2` and then apply the equation `n - (n / 2) * 2` . that gives you `n % 2` . – M.M Oct 02 '14 at 19:36
  • @Adjit "integer division" generates two outputs, called Quotient and Remainder. the quotient and the remainder are different, but both are an output of "division". – M.M Oct 02 '14 at 19:37
  • it is like 1%2 where quotient will be .5 and reminder will be 0. Same on 2%2. Quotient will be 1 and reminder will be 0. 3%2. Quotient will be 1 and reminder will be 1. I am clear now. Thanks for helping. – Asif Naeem Oct 02 '14 at 19:41
  • @MattMcNabb right. I guess I've always found it easier to just think of them as different entities stemming from the same operation – Adjit Oct 02 '14 at 19:41
  • 1
    @AsifNaeem in integer division, the quotient is always an integer. [See this post](http://stackoverflow.com/questions/3602827/what-is-the-behavior-of-integer-division-in-c/) if you are unsure about that – M.M Oct 02 '14 at 19:42
  • 1
    @AsifNaeem read to your hearts content : http://en.wikipedia.org/wiki/Modulo_operation – Adjit Oct 02 '14 at 19:44
  • @MattMcNabb Thank you for suggesting. – Asif Naeem Oct 02 '14 at 19:49

1 Answers1

1

I am unable to understand the logic of n%2==0. If user input a value less than 2. Then how it give us correct answer?

The operator % performs the modulus (or remainder) operation. The remainder of dividing a number by 2 (when that number is less than 2) is the number itself (with the quotient being 0). For example, one divided by two has a quotient of 0 and a remainder of 1 so 1%2 = 1.

zwol
  • 135,547
  • 38
  • 252
  • 361
Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249