# Program for replacing one digit with other

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
```

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

We traverse through all digits of x. For every digit, we check if it is d1, we update result accordingly.

## C++

 `// CPP program to replace a digit with other ` `// in a given number. ` `#include ` `using` `namespace` `std; ` ` `  `int` `replaceDigit(``int` `x, ``int` `d1, ``int` `d2) ` `{ ` `    ``int` `result = 0, multiply = 1; ` ` `  `    ``while` `(x % 10 > 0) { ` ` `  `        ``// Take reminder of number starting from ` `        ``// the unit place digit ` `        ``int` `reminder = x % 10; ` ` `  `        ``// check whether it is equal to the digit  ` `        ``// to be replaced.if yes then replace ` `        ``if` `(reminder == d1)  ` `            ``result = result + d2 * multiply;  ` `         `  `        ``else` `// else remain as such ` `            ``result = result + reminder * multiply;          ` ` `  `        ``// Update and move forward from unit place  ` `        ``// to hundred place and so on. ` `        ``multiply *= 10; ` `        ``x = x / 10; ``// update the value ` `    ``} ` `    ``return` `result; ` `} ` ` `  `// Driver code ` `int` `main() ` `{ ` `    ``int`  `x = 645, d1 = 6, d2 = 5; ` `    ``cout << replaceDigit(x, d1, d2) << endl; ` `    ``return` `0; ` `} `

## Java

 `// 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 reminder of number  ` `        ``// starting from the unit ` `        ``// place digit ` `        ``int` `reminder = x % ``10``; ` ` `  `        ``// check whether it is equal  ` `        ``// to the digit to be replaced. ` `        ``// if yes then replace ` `        ``if` `(reminder == d1)  ` `            ``result = result + d2 * multiply;  ` `         `  `        ``else` `// else remain as such ` `            ``result = result + reminder * multiply;      ` ` `  `        ``// Update and move forward  ` `        ``// from unit place to  ` `        ``// hundred place and so on. ` `        ``multiply *= ``10``; ` `        ``x = x / ``10``; ``// update the value ` `    ``} ` `    ``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 mits `

## Python3

 `# 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 reminder of number  ` `        ``# starting from the unit  ` `        ``# place digit ` `        ``reminder ``=` `x ``%` `10``; ` ` `  `        ``# check whether it is  ` `        ``# equal to the digit  ` `        ``# to be replaced.if yes ` `        ``# then replace ` `        ``if` `(reminder ``=``=` `d1):  ` `            ``result ``=` `(result ``+` `d2 ``*`  `                         ``multiply);  ` `         `  `        ``else``: ``# else remain as such ` `            ``result ``=` `(result ``+` `reminder ``*`  `                               ``multiply);  ` ` `  `        ``# Update and move forward  ` `        ``# from unit place to hundred  ` `        ``# place and so on. ` `        ``multiply ``*``=` `10``; ` `        ``x ``=` `int``(x ``/` `10``); ``# update the value ` ` `  `    ``return` `result; ` ` `  `# Driver code ` `x ``=` `645``;  ` `d1 ``=` `6``;  ` `d2 ``=` `5``; ` `print``(replaceDigit(x, d1, d2)); ` ` `  `# This Code is contributed  ` `# by mits `

## C#

 `// C# program to replace a digit  ` `// with other in a given number ` `using` `System; ` `class` `GFG ` `{ ` `static` `int` `replaceDigit(``int` `x, ``int` `d1,  ` `                        ``int` `d2) ` `{ ` `    ``int` `result = 0, multiply = 1; ` ` `  `    ``while` `(x % 10 > 0)  ` `    ``{ ` ` `  `        ``// Take reminder of number  ` `        ``// starting from the unit ` `        ``// place digit ` `        ``int` `reminder = x % 10; ` ` `  `        ``// check whether it is equal  ` `        ``// to the digit to be replaced. ` `        ``// if yes then replace ` `        ``if` `(reminder == d1)  ` `            ``result = result + d2 * ` `                         ``multiply;  ` `         `  `        ``else` `// else remain as such ` `            ``result = result + reminder *  ` `                               ``multiply;  ` ` `  `        ``// Update and move forward  ` `        ``// from unit place to  ` `        ``// hundred place and so on. ` `        ``multiply *= 10; ` `        ``x = x / 10; ``// update the value ` `    ``} ` `    ``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

 ` 0)  ` `    ``{ ` ` `  `        ``// Take reminder of number  ` `        ``// starting from the unit  ` `        ``// place digit ` `        ``\$reminder` `= ``\$x` `% 10; ` ` `  `        ``// check whether it is  ` `        ``// equal to the digit  ` `        ``// to be replaced.if yes ` `        ``// then replace ` `        ``if` `(``\$reminder` `== ``\$d1``)  ` `            ``\$result` `= ``\$result` `+ ``\$d2` `*  ` `                           ``\$multiply``;  ` `         `  `        ``else` `// else remain as such ` `            ``\$result` `= ``\$result` `+ ``\$reminder` `*  ` `                                ``\$multiply``;      ` ` `  `        ``// Update and move forward  ` `        ``// from unit place to hundred  ` `        ``// place and so on. ` `        ``\$multiply` `*= 10; ` `        ``\$x` `= ``\$x` `/ 10; ``// update the value ` `    ``} ` `    ``return` `\$result``; ` `} ` ` `  `// Driver code ` `\$x` `= 645; ``\$d1` `= 6; ``\$d2` `= 5; ` `echo` `replaceDigit(``\$x``, ``\$d1``, ``\$d2``); ` ` `  `// This Code is contributed  ` `// by inder_verma ` `?> `

Output:

```545
```

My Personal Notes arrow_drop_up

Article Tags :
Practice Tags :

Be the First to upvote.

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.