Grade 9 · 15 algorithmsCitește în română
Digits of a numberCifrele unui număr
Every digits of a number algorithm from the Romanian high school curriculum, with the idea explained, its complexity, examples and code you can edit and run right here.
- 15algorithms
- 8chapters
- 1grade
15 algorithms
0%
Digits of a number
Cifrele unui număr · 15 algorithms
- Number of occurrences of digit kNumărul de apariții ale cifrei kWe go through the digits of n and count how many are equal to k. With do-while, for n = 0 and k = 0 the answer is correctly 1.
- Largest number formed from the digits of nCel mai mare număr format cu cifrele lui nWe count how many times each digit appears, in a frequency array with 10 positions (frecv[c] = how many times digit c appears). The largest number…
- Check digit (digital root)Cifra de controlThe check digit is obtained by repeatedly replacing the number with the sum of its digits until a single digit remains (for example 99999 -> 45 ->…
- Largest and smallest digitCifra maximă și cifra minimăWe start with the maximum at 0 (the smallest possible digit) and the minimum at 9 (the largest possible digit), then compare every digit with them.…
- Checking for distinct digitsVerificarea cifrelor distincteChecks whether all digits of n are different. We remember which digits we have already seen (vazut[c] becomes true on the first occurrence of c); if…
- Printing the digits in their order in the numberAfișarea cifrelor în ordinea din numărWith n % 10 the digits come out from right to left. To print them from left to right, we first compute p = the largest power of 10 not greater than…
- Doubling the odd digitsDublarea cifrelor impareEvery odd digit of n appears twice in the result. We build the result from right to left with rez = rez + digit * p; an odd digit is added twice, so…
- Removing the even digitsEliminarea cifrelor pareWe build a new number from the odd digits of n, keeping their order. The digits come from right to left, so every kept digit is placed in front of…
- Replacing one digit with anotherÎnlocuirea unei cifre cu altaReplaces every occurrence of digit x in n with digit y. We rebuild the number from right to left with rez = rez + digit * p, putting y in place of…
- Palindrome numberNumăr palindromA number is a palindrome if it reads the same from left to right and from right to left, i.e. it is equal to its reverse. We work on a copy, because…
- Number of digits of a numberNumărul de cifre ale unui numărCounts the digits of n by cutting off the last digit at every step (n / 10) until the number becomes 0. We use do-while so that the number 0 is…
- Reverse of a numberOglinditul unui numărBuilds the number made of the digits of n written in reverse order. At every step we take the last digit (n % 10), append it to the right of the…
- First digit of a numberPrima cifră a unui numărThe first (leftmost) digit is what remains after we cut off all the other digits: we divide by 10 while the number has at least two digits (n >= 10).
- Product of the digits of a numberProdusul cifrelor unui numărMultiplies the digits of n. The product starts from 1 (the identity element of multiplication), not from 0. With do-while, for n = 0 we correctly…
- Sum of the digits of a numberSuma cifrelor unui numărAdds up the digits of n. The last digit is obtained with n % 10, then removed with n / 10. We repeat until n becomes 0.