Created
May 24, 2023 00:30
-
-
Save condini-mastheus/ab1ad54c731c0f3109c617c843a9d017 to your computer and use it in GitHub Desktop.
Digit extraction algorithm (variant) implemented in C
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <stdio.h> | |
| int main() { | |
| // Define and initialize the variable 'n' with a large number | |
| long long n = 12345678901; | |
| // Define and initialize the variable 'counter' with 0 | |
| int counter = 0; | |
| // Loop until 'n' is less than 10 | |
| while (n >= 10) { | |
| // Define the variable 'jumps' based on the value of 'counter' | |
| // if jumps = 100 it will jump two number to the left; | |
| int jumps = 100; | |
| // same as counter == 0 ? 10 : 100; | |
| if(counter == 0) { | |
| // if jumps = 10 it will jump one number to the left; | |
| jumps = 10; | |
| } | |
| // Divide 'n' by 'jumps' and update the value of 'n' | |
| n = n / jumps; | |
| // Get the last digit of 'n' and store it in 'lastDigit' | |
| int lastDigit = n % 10; | |
| // Print the current 'counter', 'n', and 'lastDigit' | |
| printf("%d, %lld => %d\n", counter, n, lastDigit); | |
| // Increment the value of 'counter' by 1 | |
| counter += 1; | |
| } | |
| // End the main function and return 0 to indicate successful execution | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@GiovanniRafael look this awesome implementation of this algo. 🔥