Digit Toolbox Cheatsheet (C++)
1. Last Digit: last = no % 10;
→ Gets the last digit directly.
2. First Digit: while(no >= 10) no /= 10; first = no;
→ Keep dividing until only one digit remains.
3. Count Digits: count=0; while(no!=0){ no/=10; count++; }
→ Each chop counts one digit.
4. Sum of Digits: sum=0; while(no!=0){ sum+=no%10; no/=10; }
→ Add all digits together.
5. Product of Digits: product=1; while(no!=0){ product*=no%10; no/=10; }
→ Multiply all digits together.
6. Reverse Number: rev=0; while(no!=0){ rev=rev*10+(no%10); no/=10; }
→ Reverses the number digit by digit.
7. Palindrome Check: if(no == rev) cout << "Palindrome";
→ Number is same forward and backward.
8. Count Even/Odd Digits: if((no%10)%2==0) even++; else odd++;
→ Classify digits as even or odd.
Your quick reference for digit problems in C++