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++
// 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 } return result; } // Driver code int main() { int x = 645, d1 = 6, d2 = 5; cout << replaceDigit(x, d1, d2) << endl; return 0; } |
chevron_right
filter_none
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 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 } 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 |
chevron_right
filter_none
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 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 return result; # Driver code x = 645; d1 = 6; d2 = 5; print(replaceDigit(x, d1, d2)); # This Code is contributed # by mits |
chevron_right
filter_none
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 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 } 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 |
chevron_right
filter_none
PHP
<?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 } return $result; } // Driver code $x = 645; $d1 = 6; $d2 = 5; echo replaceDigit($x, $d1, $d2); // This Code is contributed // by inder_verma ?> |
chevron_right
filter_none
Output:
545
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Min steps to convert N-digit prime number into another by replacing a digit in each step
- Program to print the given digit in words
- Program to find the last digit of X in base Y
- Program to delete Nth digit of a Number
- Program to find last digit of n'th Fibonnaci Number
- C++ Program for Largest K digit number divisible by X
- C++ Program for Smallest K digit number divisible by X
- Java Program for Smallest K digit number divisible by X
- Java Program for Largest K digit number divisible by X
- Count of Numbers in Range where first digit is equal to last digit of the number
- Count numbers in a range with digit sum divisible by K having first and last digit different
- Find the remainder when First digit of a number is divided by its Last digit
- Count of pairs (A, B) in range 1 to N such that last digit of A is equal to the first digit of B
- Maximum possible GCD after replacing at most one element in the given array
- Array sum after replacing all occurrences of X by Y for Q queries
- Largest number less than N with digit sum greater than the digit sum of N
- Count 'd' digit positive integers with 0 as a digit
- Last digit of a number raised to last digit of N factorial
- Queries to find the XOR of an Array after replacing all occurrences of X by Y
- Array value by repeatedly replacing max 2 elements with their absolute difference
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

