Number Validator | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 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....

26th Sep 2019, 3:59 PM
Jacques Cozart
Jacques Cozart - avatar
2 Answers
+ 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.
26th Sep 2019, 5:42 PM
Thoq!
Thoq! - avatar
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
26th Sep 2019, 11:44 PM
Denise Roßberg
Denise Roßberg - avatar