Program for replacing one digit with other

26 May, 2022
Given a number x and two digits d1 and d2, replace d1 with d2 in x.Examples: 
Input : x = 645, d1 = 6, d2 = 5
Output : 545
We replace digit 6 with 5 in number 645.

Input  : x = 746, d1 = 7, d2 = 8
Output : 846
We traverse through all digits of x. For every digit, we check if it is d1, we update result accordingly.  
C++
C
Java
Python3
C#
PHP
Javascript
// CPP program to replace a digit with other// in a given number.#include <bits/stdc++.h>using namespace std; int replaceDigit(int x, int d1, int d2){    int result = 0, multiply = 1;    while (x / 10 > 0) {        // Take remainder of number starting from the unit        // place digit        int remainder = x % 10;        // check whether it is equal to the digit to be        // replaced.if yes then replace        if (remainder == d1)            result = result + d2 * multiply;        else // else remain as such            result = result + remainder * multiply;        // Update and move forward from unit place to        // hundred place and so on.        multiply *= 10;        x = x / 10; // update the value    }    // check whether it is equal to the digit to be    // replaced.if yes then replace    if (x == d1)        result = result + d2 * multiply;    else // else remain as such        result = result + x * multiply;    return result;} // Driver codeint main(){    int x = 645, d1 = 6, d2 = 5;    cout << replaceDigit(x, d1, d2) << endl;    return 0;} // This code is contributed by Sania Kumari Gupta
// C program to replace a digit with other// in a given number.#include <stdio.h> int replaceDigit(int x, int d1, int d2){    int result = 0, multiply = 1;    while (x / 10 > 0)    {               // Take remainder of number starting from the unit        // place digit        int remainder = x % 10;               // check whether it is equal to the digit to be        // replaced.if yes then replace        if (remainder == d1)            result = result + d2 * multiply;        else // else remain as such            result = result + remainder * multiply;               // Update and move forward from unit place to        // hundred place and so on.        multiply *= 10;        x = x / 10; // update the value    }       // check whether it is equal to the digit to be    // replaced.if yes then replace    if (x == d1)        result = result + d2 * multiply;    else // else remain as such        result = result + x * multiply;    return result;} // Driver codeint main(){    int x = 645, d1 = 6, d2 = 5;    printf("%d\n", replaceDigit(x, d1, d2));    return 0;} // This code is contributed by Sania Kumari Gupta
// Java program to replace a digit// with other in a given number.class GFG {    static int replaceDigit(int x, int d1, int d2)    {        int result = 0, multiply = 1;        while (x / 10 > 0) {            // Take remainder of number starting from the            // unit place digit            int remainder = x % 10;            // check whether it is equal to the digit to be            // replaced. if yes then replace            if (remainder == d1)                result = result + d2 * multiply;            else // else remain as such                result = result + remainder * multiply;            // Update and move forward from unit place to            // hundred place and so on.            multiply *= 10;            x = x / 10; // update the value        }        // check whether it is equal to the digit to be        // replaced.if yes then replace        if (x == d1)            result = result + d2 * multiply;        else // else remain as such            result = result + x * multiply;        return result;    }     // Driver code    public static void main(String[] args)    {        int x = 645, d1 = 6, d2 = 5;        System.out.println(replaceDigit(x, d1, d2));    }} // This code is contributed by Sania Kumari Gupta
# Python3 program to replace# a digit with other# in a given number.  def replaceDigit(x, d1, d2):    result = 0    multiply = 1     while (x // 10 > 0):         # Take remainder of number        # starting from the unit        # place digit        remainder = x % 10         # check whether it is        # equal to the digit        # to be replaced.if yes        # then replace        if (remainder == d1):            result = (result + d2 *                      multiply)         else:  # else remain as such            result = (result + remainder *                      multiply)         # Update and move forward        # from unit place to hundred        # place and so on.        multiply *= 10        x = int(x / 10)  # update the value     # check whether it is equal to the digit    # to be replaced.if yes then replace    if (x == d1):        result = result + d2 * multiply     else:  # else remain as such        result = result + x * multiply    return result  # Driver codex = 645d1 = 6d2 = 5print(replaceDigit(x, d1, d2)) # This Code is contributed# by mits
// C# program to replace a digit// with other in a given numberusing System;class GFG{    static int replaceDigit(int x, int d1, int d2)    {        int result = 0, multiply = 1;         while (x / 10 > 0)        {             // Take remainder of number            // starting from the unit            // place digit            int remainder = x % 10;             // check whether it is equal            // to the digit to be replaced.            // if yes then replace            if (remainder == d1)                result = result + d2 * multiply;             else // else remain as such                result = result + remainder * multiply;             // Update and move forward            // from unit place to            // hundred place and so on.            multiply *= 10;            x = x / 10; // update the value        }         // check whether it is equal to the digit        // to be replaced.if yes then replace        if (x == d1)            result = result + d2 * multiply;         else // else remain as such            result = result + x * multiply;        return result;    }     // Driver code    public static void Main()    {        int x = 645, d1 = 6, d2 = 5;        Console.WriteLine(replaceDigit(x, d1, d2));    }} // This Code is contributed// by inder_verma
<?php// PHP program to replace// a digit with other// in a given number. function replaceDigit($x, $d1, $d2){    $result = 0; $multiply = 1;     while ($x / 10 > 0)    {         // Take remainder of number        // starting from the unit        // place digit        $remainder = $x % 10;         // check whether it is        // equal to the digit        // to be replaced.if yes        // then replace        if ($remainder == $d1)            $result = $result + $d2 *                           $multiply;                 else // else remain as such            $result = $result + $remainder *                                $multiply;             // Update and move forward        // from unit place to hundred        // place and so on.        $multiply *= 10;        $x = $x / 10; // update the value    }       // check whether it is equal to the digit    // to be replaced.if yes then replace    if ($x == $d1)        $result = $result + $d2 * $multiply;             else // else remain as such        $result = $result + $x * $multiply;    return $result;} // Driver code$x = 645; $d1 = 6; $d2 = 5;echo replaceDigit($x, $d1, $d2); // This Code is contributed// by inder_verma?>
<script>    // Javascript program to replace a digit    // with other in a given number         function replaceDigit(x, d1, d2)    {        let result = 0, multiply = 1;          while (parseInt(x / 10, 10) > 0)        {              // Take remainder of number            // starting from the unit            // place digit            let remainder = x % 10;              // check whether it is equal            // to the digit to be replaced.            // if yes then replace            if (remainder == d1)                result = result + d2 * multiply;              else // else remain as such                result = result + remainder * multiply;              // Update and move forward            // from unit place to            // hundred place and so on.            multiply *= 10;            x = parseInt(x / 10, 10); // update the value        }          // check whether it is equal to the digit        // to be replaced.if yes then replace        if (x == d1)            result = result + d2 * multiply;          else // else remain as such            result = result + x * multiply;        return result;    }         let x = 645, d1 = 6, d2 = 5;      document.write(replaceDigit(x, d1, d2)); // This code is contributed by rameshtravel07.</script>
545
 
Time Complexity: O(logn), where n is the given number.
Auxiliary Space: O(1)
Mithun Kumar
inderDuMCA
Akanksha_Rai
kirankadam633
rameshtravel07
krisania804
rohitmishra051000
Accenture
number-digits
Mathematical
Accenture
Mathematical
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Algorithm to solve Rubik's Cube
Sum of the series (1*2) + (2*3) + (3*4) + ...... upto n terms
Merge two sorted arrays with O(1) extra space
Program to print prime numbers from 1 to N.
Count ways to reach the n'th stair
Segment Tree | Set 1 (Sum of given range)
Check if a number is Palindrome
Median of two sorted arrays of same size
Count all possible paths from top left to bottom right of a mXn matrix
Maximize difference between the Sum of the two halves of the Array after removal of N elements