Created
May 29, 2025 06:59
-
-
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.
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> | |
#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