C Tips

Finding first and last digit of the number .
Eg. 52264  . FirstDigit = 5 and LastDigit=4.
 
int input = 52264; 
 
int firstDigit = input;
while (firstDigit >= 10)
{
    firstDigit /= 10;
}
 
int lastDigit = 52264 % 10; // 52264 % 10 = 4
int firstDigit = 52264;
firstDigit /= 10; // 5226
firstDigit /= 10; // 522
firstDigit /= 10; // 52
firstDigit /= 10; // 5 -- less than 10
 
===========================================================
Find number is even or odd.

int even(const int value)
{
       return((value & 1) == 0);
}

int even(const int value)
{
       return( ( value % 10) == 0);
}
=============================================================

 
 

No comments:

Post a Comment