Created
October 17, 2024 08:52
-
-
Save marsgpl/b00fa1e1f6d62a936c59350285857a01 to your computer and use it in GitHub Desktop.
Write a Program to Replace all 0’s with 1’s in a Number
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() { | |
int N = 102301; | |
printf("before:\t%d\n", N); | |
for (int power = 1; power <= N; power *= 10) { // 1, 10, 100, ... | |
int digit = N / power % 10; | |
if (digit == 0) { | |
N += power; // replace 0 with 1 means add 1 * power | |
} | |
} | |
printf("after:\t%d\n", N); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment