Program for replacing one digit with other
Last Updated :
16 Dec, 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++
#include <bits/stdc++.h>
using namespace std;
int replaceDigit( int x, int d1, int d2)
{
int result = 0, multiply = 1;
while (x / 10 > 0) {
int remainder = x % 10;
if (remainder == d1)
result = result + d2 * multiply;
else
result = result + remainder * multiply;
multiply *= 10;
x = x / 10;
}
if (x == d1)
result = result + d2 * multiply;
else
result = result + x * multiply;
return result;
}
int main()
{
int x = 645, d1 = 6, d2 = 5;
cout << replaceDigit(x, d1, d2) << endl;
return 0;
}
|
C
#include <stdio.h>
int replaceDigit( int x, int d1, int d2)
{
int result = 0, multiply = 1;
while (x / 10 > 0)
{
int remainder = x % 10;
if (remainder == d1)
result = result + d2 * multiply;
else
result = result + remainder * multiply;
multiply *= 10;
x = x / 10;
}
if (x == d1)
result = result + d2 * multiply;
else
result = result + x * multiply;
return result;
}
int main()
{
int x = 645, d1 = 6, d2 = 5;
printf ( "%d\n" , replaceDigit(x, d1, d2));
return 0;
}
|
Java
import java.io.*;
class GFG {
static int replaceDigit( int x, int d1, int d2)
{
int result = 0 , multiply = 1 ;
while (x / 10 > 0 ) {
int remainder = x % 10 ;
if (remainder == d1)
result = result + d2 * multiply;
else
result = result + remainder * multiply;
multiply *= 10 ;
x = x / 10 ;
}
if (x == d1)
result = result + d2 * multiply;
else
result = result + x * multiply;
return result;
}
public static void main(String[] args)
{
int x = 645 , d1 = 6 , d2 = 5 ;
System.out.println(replaceDigit(x, d1, d2));
}
}
|
Python3
def replaceDigit(x, d1, d2):
result = 0
multiply = 1
while (x / / 10 > 0 ):
remainder = x % 10
if (remainder = = d1):
result = (result + d2 *
multiply)
else :
result = (result + remainder *
multiply)
multiply * = 10
x = int (x / 10 )
if (x = = d1):
result = result + d2 * multiply
else :
result = result + x * multiply
return result
x = 645
d1 = 6
d2 = 5
print (replaceDigit(x, d1, d2))
|
C#
using System;
class GFG
{
static int replaceDigit( int x, int d1, int d2)
{
int result = 0, multiply = 1;
while (x / 10 > 0)
{
int remainder = x % 10;
if (remainder == d1)
result = result + d2 * multiply;
else
result = result + remainder * multiply;
multiply *= 10;
x = x / 10;
}
if (x == d1)
result = result + d2 * multiply;
else
result = result + x * multiply;
return result;
}
public static void Main()
{
int x = 645, d1 = 6, d2 = 5;
Console.WriteLine(replaceDigit(x, d1, d2));
}
}
|
PHP
<?php
function replaceDigit( $x , $d1 , $d2 )
{
$result = 0; $multiply = 1;
while ( $x / 10 > 0)
{
$remainder = $x % 10;
if ( $remainder == $d1 )
$result = $result + $d2 *
$multiply ;
else
$result = $result + $remainder *
$multiply ;
$multiply *= 10;
$x = $x / 10;
}
if ( $x == $d1 )
$result = $result + $d2 * $multiply ;
else
$result = $result + $x * $multiply ;
return $result ;
}
$x = 645; $d1 = 6; $d2 = 5;
echo replaceDigit( $x , $d1 , $d2 );
?>
|
Javascript
<script>
function replaceDigit(x, d1, d2)
{
let result = 0, multiply = 1;
while (parseInt(x / 10, 10) > 0)
{
let remainder = x % 10;
if (remainder == d1)
result = result + d2 * multiply;
else
result = result + remainder * multiply;
multiply *= 10;
x = parseInt(x / 10, 10);
}
if (x == d1)
result = result + d2 * multiply;
else
result = result + x * multiply;
return result;
}
let x = 645, d1 = 6, d2 = 5;
document.write(replaceDigit(x, d1, d2));
</script>
|
Time Complexity: O(logn), where n is the given number.
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...