C/c

Advertisement


Question
Hey am kinda new @ this so..i kinda need some help understanding this part..

if (decimal < 0)
 {      
   decimal = -decimal;
   neg_flag = 1;
 }
 do
 {
   old_decimal = decimal;   // for test
   remain    = decimal % 2;

Answer
Hello Zizo

The first part is keeping the decimal positive. So that if it is less than 0, it is multiplied by -1 to make it positive. The neg_flag is used to remember that the original decimal was negative.

The remain is the remainder that you would have when dividing the decimal by 2. For example, if decimal is 2, 2/2 has a remainder of 0 so 2 % 2 is 0. The "%" operator is also known as the modulo operator and is used to get the remainder.  3/2 has a remainder of 1, 4/2 has a remainder of 0, 5/2 has a remainder of 1 and so on. In this case the remainder is 0 or 1, and might be used to determine if decimal is even or odd.

That is all I can say about this code. It is too short for me to know what the programmer is trying to accomplish.

Best regards
Zlatko

C

All Answers


Answers by Expert:


Ask Experts

Volunteer


Zlatko

Expertise

No longer taking questions.

Experience

No longer taking questions.

Education/Credentials
No longer taking questions.

©2012 About.com, a part of The New York Times Company. All rights reserved.