Skip to content

Instantly share code, notes, and snippets.

@marsgpl
Created October 17, 2024 08:52
Show Gist options
  • Save marsgpl/b00fa1e1f6d62a936c59350285857a01 to your computer and use it in GitHub Desktop.
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
#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