Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find nearest even number for given int? (given 11 return 12)

Tags:

c++

c

int

So how to create a function to return nearest up so nearest to 9 9+ 1 to given int leaving no remainder when divided by 2 int?

like image 443
Rella Avatar asked Dec 05 '10 18:12

Rella


People also ask

How do I find the nearest even number?

To round a number down to the nearest even number:floor() method passing it the number divided by 2 . Multiply the result by 2 . The result of the calculation is the number rounded down to the nearest even integer.

How do you round to even numbers in C++?

If you assume FE_TONEAREST, the result is simply std:nearbyint(x). FE_TONEAREAT is round to even.

How do you check if a number is even?

If a number is evenly divisible by 2 with no remainder, then it is even. You can calculate the remainder with the modulo operator % like this num % 2 == 0 .

What is the best way to check if an integer is even or odd?

A number is even if, when divided by two, the remainder is 0. A number is odd if, when divided by 2, the remainder is 1. Methods are great!


1 Answers

To round to the nearest int:

number+=(number & 1)
like image 140
Sean Avatar answered Oct 12 '22 21:10

Sean