Skip to content

Instantly share code, notes, and snippets.

@marsgpl
Created May 29, 2025 06:59
Show Gist options
  • Save marsgpl/7afd32eaa56705de250c15c87823ddd0 to your computer and use it in GitHub Desktop.
Save marsgpl/7afd32eaa56705de250c15c87823ddd0 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>
#include <math.h>
int main() {
const int number = -102301;
printf("before: %d\n", number);
int input = number < 0 ? -number : number;
int output = 0;
int digits = 0;
while (input > 0) {
int last_digit = input % 10;
input /= 10;
if (last_digit == 0) {
last_digit = 1;
}
output += last_digit * pow(10, digits);
digits++;
}
if (number < 0) {
output = -output;
}
printf("after: %d\n", output);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment