+ 1
Number Validator
Can someone help me with this number validation? The 6 digit number is only valid when the Last digit is Equal to the Remainder when the Sum of the First five is divided by 10?!...... So 223355 is valid bc the sum of first 5 digits is 15, when divided by 10 the remainder equals the last digit, 5...... Can someone plz help me with this....
2 ответов
+ 1
You can use conversions (int, str) or you can use a combination of modulo 10 (to get the last digit) and //10 (integer division to take away the last digit). Knowing that the code shouldn't be so hard to figure out.
0
int n = 223355;
int lastDigit = n % 10; //5
n /= 10; //22335
int sum = 0;
Now you can use a while loop to get the sum of the rest of the digits:
while(n > 0){
//add last digit to sum
//remove last digit
}
compare sum % 10 with lastDigit