C Program for replacing one digit with other

Given a number n, we have to replace a digit x from that number with another given number m. We have to look for the number whether the digit is present in the given number or not, if it is present in the given number then replace that particular digit x with another digit m.

Like we are given with a number "123" and m as 5 and the digit to be replaced i.e. x as "2", so the result should be "153".

Syntax

int digitReplace(int n, int digit, int replace);

Algorithm

The approach used below is as follows −

  • We will look up for the digit by starting from the unit place
  • When we find the digit which we want to replace then add result to the (replace * d) where d should be equal to 1
  • If we don't find the digit just simply keep the digit as it is
  • Continue this process until all digits are processed

Example 1: Replacing Existing Digit

This example shows how to replace a digit that exists in the number −

#include <stdio.h>

int digitReplace(int n, int digit, int replace) {
    int res = 0, d = 1;
    int rem;
    
    while(n) {
        // Finding the remainder from the back
        rem = n % 10;
        
        // Checking whether the remainder equals to the
        // digit we want to replace. If yes then replace.
        if(rem == digit)
            res = res + replace * d;
        // Else don't replace just store the same in res.
        else
            res = res + rem * d;
            
        d *= 10;
        n /= 10;
    }
    return res;
}

int main() {
    int n = 983;
    int digit = 9;
    int replace = 7;
    
    printf("Original number: %d<br>", n);
    printf("Digit to replace: %d<br>", digit);
    printf("Replace with: %d<br>", replace);
    
    int result = digitReplace(n, digit, replace);
    printf("Result: %d<br>", result);
    
    return 0;
}
Original number: 983
Digit to replace: 9
Replace with: 7
Result: 783

Example 2: Digit Not Found

This example demonstrates the case where the digit to be replaced is not present in the number −

#include <stdio.h>

int digitReplace(int n, int digit, int replace) {
    int res = 0, d = 1;
    int rem;
    
    while(n) {
        rem = n % 10;
        
        if(rem == digit)
            res = res + replace * d;
        else
            res = res + rem * d;
            
        d *= 10;
        n /= 10;
    }
    return res;
}

int main() {
    int n = 123;
    int digit = 5;
    int replace = 4;
    
    printf("Original number: %d<br>", n);
    printf("Digit to replace: %d<br>", digit);
    printf("Replace with: %d<br>", replace);
    
    int result = digitReplace(n, digit, replace);
    printf("Result: %d<br>", result);
    
    return 0;
}
Original number: 123
Digit to replace: 5
Replace with: 4
Result: 123

How It Works

The algorithm works by processing digits from right to left:

  1. Extract the last digit using modulo operator (%)
  2. Check if this digit matches the digit to be replaced
  3. If it matches, add the replacement digit to result; otherwise add the original digit
  4. Multiply the position multiplier (d) by 10 for next iteration
  5. Remove the last digit from the number using integer division
  6. Repeat until all digits are processed

Conclusion

The digit replacement program effectively processes numbers digit by digit from right to left, replacing all occurrences of a specified digit with a new digit. If the target digit is not found, the original number remains unchanged.

Updated on: 2026-03-15T12:18:54+05:30

4K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements