Replacing one digit with other in an integer

Here given code implementation process.

// C Program
// Replacing one digit with other in an integer
#include <stdio.h>

void replaceDigit(int number, int digit, int x)
{
	if (x < 0 || x > 9 || digit < 0 || digit > 9)
	{
		// When of given digit is not valid
		return;
	}
	int n = number;
	int result = 0;
	if (n < 0)
	{
		// Make positive
		n = -n;
	}
	if (digit == 0 && n == 0)
	{
		// When zero replace by x
		result = x;
	}
	else
	{
		// Auxiliary variable
		int temp = 0;
		int mul = 1;
		while (n > 0)
		{
			// Get last digit of n
			temp = n % 10;
			if (temp == digit)
			{
				// Include new digit
				result = (x *mul) + result;
			}
			else
			{
				result = (temp *mul) + result;
			}
			// Remove last digit of n
			n = n / 10;
			mul = mul *10;
		}
		if (number < 0)
		{
			result = -result;
		}
	}
	// Display given numbers
	printf("\n Given Number : %d", number);
	printf("\n Digit %d with replace by %d", digit, x);
	// Display calculated result
	printf("\n Result %d ", result);
}
int main()
{
	// Test A
	// Given number    : 1323  
	// Replacing digit : 3 
	// Replaced by     : 9
	// Result 1929
	replaceDigit(1323, 3, 9);
	// Test B
	// Given number    : -1161  
	// Replacing digit : 1 
	// Replaced by     : 3
	// Result -3363
	replaceDigit(-1161, 1, 3);
	// Test C
	// Given number    : 71761  
	// Replacing digit : 7 
	// Replaced by     : 0
	// Result 1061
	replaceDigit(71761, 7, 0);
	return 0;
}

Output

 Given Number : 1323
 Digit 3 with replace by 9
 Result 1929
 Given Number : -1161
 Digit 1 with replace by 3
 Result -3363
 Given Number : 71761
 Digit 7 with replace by 0
 Result 1061
/*
    Java program
    Replacing one digit with other in an integer
*/
public class Interchange
{
	public void replaceDigit(int number, int digit, int x)
	{
		if (x < 0 || x > 9 || digit < 0 || digit > 9)
		{
			// When of given digit is not valid
			return;
		}
		int n = number;
		int result = 0;
		if (n < 0)
		{
			// Make positive
			n = -n;
		}
		if (digit == 0 && n == 0)
		{
			// When zero replace by x
			result = x;
		}
		else
		{
			// Auxiliary variable
			int temp = 0;
			int mul = 1;
			while (n > 0)
			{
				// Get last digit of n
				temp = n % 10;
				if (temp == digit)
				{
					// Include new digit
					result = (x * mul) + result;
				}
				else
				{
					result = (temp * mul) + result;
				}
				// Remove last digit of n
				n = n / 10;
				mul = mul * 10;
			}
			if (number < 0)
			{
				result = -result;
			}
		}
		// Display given numbers
		System.out.print("\n Given Number : " + number );
		System.out.print("\n Digit " + digit + " with replace by " + x );
		// Display calculated result
		System.out.print("\n Result " + result );
	}
	public static void main(String[] args)
	{
		Interchange task = new Interchange();
		// Test A
		// Given number    : 1323  
		// Replacing digit : 3 
		// Replaced by     : 9
		// Result 1929
		task.replaceDigit(1323, 3, 9);
		// Test B
		// Given number    : -1161  
		// Replacing digit : 1 
		// Replaced by     : 3
		// Result -3363
		task.replaceDigit(-1161, 1, 3);
		// Test C
		// Given number    : 71761  
		// Replacing digit : 7 
		// Replaced by     : 0
		// Result 1061
		task.replaceDigit(71761, 7, 0);
	}
}

Output

 Given Number : 1323
 Digit 3 with replace by 9
 Result 1929
 Given Number : -1161
 Digit 1 with replace by 3
 Result -3363
 Given Number : 71761
 Digit 7 with replace by 0
 Result 1061
// Include header file
#include <iostream>
using namespace std;
/*
    C++ program
    Replacing one digit with other in an integer
*/
class Interchange
{
	public: void replaceDigit(int number, int digit, int x)
	{
		if (x < 0 || x > 9 || digit < 0 || digit > 9)
		{
			// When of given digit is not valid
			return;
		}
		int n = number;
		int result = 0;
		if (n < 0)
		{
			// Make positive
			n = -n;
		}
		if (digit == 0 && n == 0)
		{
			// When zero replace by x
			result = x;
		}
		else
		{
			// Auxiliary variable
			int temp = 0;
			int mul = 1;
			while (n > 0)
			{
				// Get last digit of n
				temp = n % 10;
				if (temp == digit)
				{
					// Include new digit
					result = (x *mul) + result;
				}
				else
				{
					result = (temp *mul) + result;
				}
				// Remove last digit of n
				n = n / 10;
				mul = mul *10;
			}
			if (number < 0)
			{
				result = -result;
			}
		}
		// Display given numbers
		cout << "\n Given Number : " << number;
		cout << "\n Digit " << digit 
      		 << " with replace by " << x;
		// Display calculated result
		cout << "\n Result " << result;
	}
};
int main()
{
	Interchange *task = new Interchange();
	// Test A
	// Given number    : 1323  
	// Replacing digit : 3 
	// Replaced by     : 9
	// Result 1929
	task->replaceDigit(1323, 3, 9);
	// Test B
	// Given number    : -1161  
	// Replacing digit : 1 
	// Replaced by     : 3
	// Result -3363
	task->replaceDigit(-1161, 1, 3);
	// Test C
	// Given number    : 71761  
	// Replacing digit : 7 
	// Replaced by     : 0
	// Result 1061
	task->replaceDigit(71761, 7, 0);
	return 0;
}

Output

 Given Number : 1323
 Digit 3 with replace by 9
 Result 1929
 Given Number : -1161
 Digit 1 with replace by 3
 Result -3363
 Given Number : 71761
 Digit 7 with replace by 0
 Result 1061
// Include namespace system
using System;
/*
    Csharp program
    Replacing one digit with other in an integer
*/
public class Interchange
{
	public void replaceDigit(int number, int digit, int x)
	{
		if (x < 0 || x > 9 || digit < 0 || digit > 9)
		{
			// When of given digit is not valid
			return;
		}
		int n = number;
		int result = 0;
		if (n < 0)
		{
			// Make positive
			n = -n;
		}
		if (digit == 0 && n == 0)
		{
			// When zero replace by x
			result = x;
		}
		else
		{
			// Auxiliary variable
			int temp = 0;
			int mul = 1;
			while (n > 0)
			{
				// Get last digit of n
				temp = n % 10;
				if (temp == digit)
				{
					// Include new digit
					result = (x * mul) + result;
				}
				else
				{
					result = (temp * mul) + result;
				}
				// Remove last digit of n
				n = n / 10;
				mul = mul * 10;
			}
			if (number < 0)
			{
				result = -result;
			}
		}
		// Display given numbers
		Console.Write("\n Given Number : " + number);
		Console.Write("\n Digit " + digit + " with replace by " + x);
		// Display calculated result
		Console.Write("\n Result " + result);
	}
	public static void Main(String[] args)
	{
		Interchange task = new Interchange();
		// Test A
		// Given number    : 1323  
		// Replacing digit : 3 
		// Replaced by     : 9
		// Result 1929
		task.replaceDigit(1323, 3, 9);
		// Test B
		// Given number    : -1161  
		// Replacing digit : 1 
		// Replaced by     : 3
		// Result -3363
		task.replaceDigit(-1161, 1, 3);
		// Test C
		// Given number    : 71761  
		// Replacing digit : 7 
		// Replaced by     : 0
		// Result 1061
		task.replaceDigit(71761, 7, 0);
	}
}

Output

 Given Number : 1323
 Digit 3 with replace by 9
 Result 1929
 Given Number : -1161
 Digit 1 with replace by 3
 Result -3363
 Given Number : 71761
 Digit 7 with replace by 0
 Result 1061
package main
import "fmt"
/*
    Go program
    Replacing one digit with other in an integer
*/
func replaceDigit(number, digit, x int) {
	if x < 0 || x > 9 || digit < 0 || digit > 9 {
		// When of given digit is not valid
		return
	}
	var n int = number
	var result int = 0
	if n < 0 {
		// Make positive
		n = -n
	}
	if digit == 0 && n == 0 {
		// When zero replace by x
		result = x
	} else {
		// Auxiliary variable
		var temp int = 0
		var mul int = 1
		for (n > 0) {
			// Get last digit of n
			temp = n % 10
			if temp == digit {
				// Include new digit
				result = (x * mul) + result
			} else {
				result = (temp * mul) + result
			}
			// Remove last digit of n
			n = n / 10
			mul = mul * 10
		}
		if number < 0 {
			result = -result
		}
	}
	// Display given numbers
	fmt.Print("\n Given Number : ", number)
	fmt.Print("\n Digit ", digit, " with replace by ", x)
	// Display calculated result
	fmt.Print("\n Result ", result)
}
func main() {
	
	// Test A
	// Given number    : 1323  
	// Replacing digit : 3 
	// Replaced by     : 9
	// Result 1929
	replaceDigit(1323, 3, 9)
	// Test B
	// Given number    : -1161  
	// Replacing digit : 1 
	// Replaced by     : 3
	// Result -3363
	replaceDigit(-1161, 1, 3)
	// Test C
	// Given number    : 71761  
	// Replacing digit : 7 
	// Replaced by     : 0
	// Result 1061
	replaceDigit(71761, 7, 0)
}

Output

 Given Number : 1323
 Digit 3 with replace by 9
 Result 1929
 Given Number : -1161
 Digit 1 with replace by 3
 Result -3363
 Given Number : 71761
 Digit 7 with replace by 0
 Result 1061
<?php
/*
    Php program
    Replacing one digit with other in an integer
*/
class Interchange
{
	public	function replaceDigit($number, $digit, $x)
	{
		if ($x < 0 || $x > 9 || $digit < 0 || $digit > 9)
		{
			// When of given digit is not valid
			return;
		}
		$n = $number;
		$result = 0;
		if ($n < 0)
		{
			// Make positive
			$n = -$n;
		}
		if ($digit == 0 && $n == 0)
		{
			// When zero replace by x
			$result = $x;
		}
		else
		{
			// Auxiliary variable
			$temp = 0;
			$mul = 1;
			while ($n > 0)
			{
				// Get last digit of n
				$temp = $n % 10;
				if ($temp == $digit)
				{
					// Include new digit
					$result = ($x * $mul) + $result;
				}
				else
				{
					$result = ($temp * $mul) + $result;
				}
				// Remove last digit of n
				$n = (int)($n / 10);
				$mul = $mul * 10;
			}
			if ($number < 0)
			{
				$result = -$result;
			}
		}
		// Display given numbers
		echo("\n Given Number : ".$number);
		echo("\n Digit ".$digit.
			" with replace by ".$x);
		// Display calculated result
		echo("\n Result ".$result);
	}
}

function main()
{
	$task = new Interchange();
	// Test A
	// Given number    : 1323  
	// Replacing digit : 3 
	// Replaced by     : 9
	// Result 1929
	$task->replaceDigit(1323, 3, 9);
	// Test B
	// Given number    : -1161  
	// Replacing digit : 1 
	// Replaced by     : 3
	// Result -3363
	$task->replaceDigit(-1161, 1, 3);
	// Test C
	// Given number    : 71761  
	// Replacing digit : 7 
	// Replaced by     : 0
	// Result 1061
	$task->replaceDigit(71761, 7, 0);
}
main();

Output

 Given Number : 1323
 Digit 3 with replace by 9
 Result 1929
 Given Number : -1161
 Digit 1 with replace by 3
 Result -3363
 Given Number : 71761
 Digit 7 with replace by 0
 Result 1061
/*
    Node JS program
    Replacing one digit with other in an integer
*/
class Interchange
{
	replaceDigit(number, digit, x)
	{
		if (x < 0 || x > 9 || digit < 0 || digit > 9)
		{
			// When of given digit is not valid
			return;
		}
		var n = number;
		var result = 0;
		if (n < 0)
		{
			// Make positive
			n = -n;
		}
		if (digit == 0 && n == 0)
		{
			// When zero replace by x
			result = x;
		}
		else
		{
			// Auxiliary variable
			var temp = 0;
			var mul = 1;
			while (n > 0)
			{
				// Get last digit of n
				temp = n % 10;
				if (temp == digit)
				{
					// Include new digit
					result = (x * mul) + result;
				}
				else
				{
					result = (temp * mul) + result;
				}
				// Remove last digit of n
				n = parseInt(n / 10);
				mul = mul * 10;
			}
			if (number < 0)
			{
				result = -result;
			}
		}
		// Display given numbers
		process.stdout.write("\n Given Number : " + number);
		process.stdout.write("\n Digit " + digit + " with replace by " + x);
		// Display calculated result
		process.stdout.write("\n Result " + result);
	}
}

function main()
{
	var task = new Interchange();
	// Test A
	// Given number    : 1323  
	// Replacing digit : 3 
	// Replaced by     : 9
	// Result 1929
	task.replaceDigit(1323, 3, 9);
	// Test B
	// Given number    : -1161  
	// Replacing digit : 1 
	// Replaced by     : 3
	// Result -3363
	task.replaceDigit(-1161, 1, 3);
	// Test C
	// Given number    : 71761  
	// Replacing digit : 7 
	// Replaced by     : 0
	// Result 1061
	task.replaceDigit(71761, 7, 0);
}
main();

Output

 Given Number : 1323
 Digit 3 with replace by 9
 Result 1929
 Given Number : -1161
 Digit 1 with replace by 3
 Result -3363
 Given Number : 71761
 Digit 7 with replace by 0
 Result 1061
#    Python 3 program
#    Replacing one digit with other in an integer
class Interchange :
	def replaceDigit(self, number, digit, x) :
		if (x < 0 or x > 9 or digit < 0 or digit > 9) :
			#  When of given digit is not valid
			return
		
		n = number
		result = 0
		if (n < 0) :
			#  Make positive
			n = -n
		
		if (digit == 0 and n == 0) :
			#  When zero replace by x
			result = x
		else :
			#  Auxiliary variable
			temp = 0
			mul = 1
			while (n > 0) :
				#  Get last digit of n
				temp = n % 10
				if (temp == digit) :
					#  Include new digit
					result = (x * mul) + result
				else :
					result = (temp * mul) + result
				
				#  Remove last digit of n
				n = int(n / 10)
				mul = mul * 10
			
			if (number < 0) :
				result = -result
			
		
		#  Display given numbers
		print("\n Given Number : ", number, end = "")
		print("\n Digit ", digit ," with replace by ", x, end = "")
		#  Display calculated result
		print("\n Result ", result, end = "")
	

def main() :
	task = Interchange()
	#  Test A
	#  Given number    : 1323  
	#  Replacing digit : 3 
	#  Replaced by     : 9
	#  Result 1929
	task.replaceDigit(1323, 3, 9)
	#  Test B
	#  Given number    : -1161  
	#  Replacing digit : 1 
	#  Replaced by     : 3
	#  Result -3363
	task.replaceDigit(-1161, 1, 3)
	#  Test C
	#  Given number    : 71761  
	#  Replacing digit : 7 
	#  Replaced by     : 0
	#  Result 1061
	task.replaceDigit(71761, 7, 0)

if __name__ == "__main__": main()

Output

 Given Number :  1323
 Digit  3  with replace by  9
 Result  1929
 Given Number :  -1161
 Digit  1  with replace by  3
 Result  -3363
 Given Number :  71761
 Digit  7  with replace by  0
 Result  1061
#    Ruby program
#    Replacing one digit with other in an integer
class Interchange 
	def replaceDigit(number, digit, x) 
		if (x < 0 || x > 9 || digit < 0 || digit > 9) 
			#  When of given digit is not valid
			return
		end

		n = number
		result = 0
		if (n < 0) 
			#  Make positive
			n = -n
		end

		if (digit == 0 && n == 0) 
			#  When zero replace by x
			result = x
		else
 
			#  Auxiliary variable
			temp = 0
			mul = 1
			while (n > 0) 
				#  Get last digit of n
				temp = n % 10
				if (temp == digit) 
					#  Include new digit
					result = (x * mul) + result
				else
 
					result = (temp * mul) + result
				end

				#  Remove last digit of n
				n = n / 10
				mul = mul * 10
			end

			if (number < 0) 
				result = -result
			end

		end

		#  Display given numbers
		print("\n Given Number : ", number)
		print("\n Digit ", digit ," with replace by ", x)
		#  Display calculated result
		print("\n Result ", result)
	end

end

def main() 
	task = Interchange.new()
	#  Test A
	#  Given number    : 1323  
	#  Replacing digit : 3 
	#  Replaced by     : 9
	#  Result 1929
	task.replaceDigit(1323, 3, 9)
	#  Test B
	#  Given number    : -1161  
	#  Replacing digit : 1 
	#  Replaced by     : 3
	#  Result -3363
	task.replaceDigit(-1161, 1, 3)
	#  Test C
	#  Given number    : 71761  
	#  Replacing digit : 7 
	#  Replaced by     : 0
	#  Result 1061
	task.replaceDigit(71761, 7, 0)
end

main()

Output

 Given Number : 1323
 Digit 3 with replace by 9
 Result 1929
 Given Number : -1161
 Digit 1 with replace by 3
 Result -3363
 Given Number : 71761
 Digit 7 with replace by 0
 Result 1061
/*
    Scala program
    Replacing one digit with other in an integer
*/
class Interchange()
{
	def replaceDigit(number: Int, digit: Int, x: Int): Unit = {
		if (x < 0 || x > 9 || digit < 0 || digit > 9)
		{
			// When of given digit is not valid
			return;
		}
		var n: Int = number;
		var result: Int = 0;
		if (n < 0)
		{
			// Make positive
			n = -n;
		}
		if (digit == 0 && n == 0)
		{
			// When zero replace by x
			result = x;
		}
		else
		{
			// Auxiliary variable
			var temp: Int = 0;
			var mul: Int = 1;
			while (n > 0)
			{
				// Get last digit of n
				temp = n % 10;
				if (temp == digit)
				{
					// Include new digit
					result = (x * mul) + result;
				}
				else
				{
					result = (temp * mul) + result;
				}
				// Remove last digit of n
				n = n / 10;
				mul = mul * 10;
			}
			if (number < 0)
			{
				result = -result;
			}
		}
		// Display given numbers
		print("\n Given Number : " + number);
		print("\n Digit " + digit + " with replace by " + x);
		// Display calculated result
		print("\n Result " + result);
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var task: Interchange = new Interchange();
		// Test A
		// Given number    : 1323  
		// Replacing digit : 3 
		// Replaced by     : 9
		// Result 1929
		task.replaceDigit(1323, 3, 9);
		// Test B
		// Given number    : -1161  
		// Replacing digit : 1 
		// Replaced by     : 3
		// Result -3363
		task.replaceDigit(-1161, 1, 3);
		// Test C
		// Given number    : 71761  
		// Replacing digit : 7 
		// Replaced by     : 0
		// Result 1061
		task.replaceDigit(71761, 7, 0);
	}
}

Output

 Given Number : 1323
 Digit 3 with replace by 9
 Result 1929
 Given Number : -1161
 Digit 1 with replace by 3
 Result -3363
 Given Number : 71761
 Digit 7 with replace by 0
 Result 1061
/*
    Swift 4 program
    Replacing one digit with other in an integer
*/
class Interchange
{
	func replaceDigit(_ number: Int, _ digit: Int, _ x: Int)
	{
		if (x < 0 || x > 9 || digit < 0 || digit > 9)
		{
			// When of given digit is not valid
			return;
		}
		var n: Int = number;
		var result: Int = 0;
		if (n < 0)
		{
			// Make positive
			n = -n;
		}
		if (digit == 0 && n == 0)
		{
			// When zero replace by x
			result = x;
		}
		else
		{
			// Auxiliary variable
			var temp: Int = 0;
			var mul: Int = 1;
			while (n > 0)
			{
				// Get last digit of n
				temp = n % 10;
				if (temp == digit)
				{
					// Include new digit
					result = (x * mul) + result;
				}
				else
				{
					result = (temp * mul) + result;
				}
				// Remove last digit of n
				n = n / 10;
				mul = mul * 10;
			}
			if (number < 0)
			{
				result = -result;
			}
		}
		// Display given numbers
		print("\n Given Number : ", number, terminator: "");
		print("\n Digit ", digit ," with replace by ", x, terminator: "");
		// Display calculated result
		print("\n Result ", result, terminator: "");
	}
}
func main()
{
	let task: Interchange = Interchange();
	// Test A
	// Given number    : 1323  
	// Replacing digit : 3 
	// Replaced by     : 9
	// Result 1929
	task.replaceDigit(1323, 3, 9);
	// Test B
	// Given number    : -1161  
	// Replacing digit : 1 
	// Replaced by     : 3
	// Result -3363
	task.replaceDigit(-1161, 1, 3);
	// Test C
	// Given number    : 71761  
	// Replacing digit : 7 
	// Replaced by     : 0
	// Result 1061
	task.replaceDigit(71761, 7, 0);
}
main();

Output

 Given Number :  1323
 Digit  3  with replace by  9
 Result  1929
 Given Number :  -1161
 Digit  1  with replace by  3
 Result  -3363
 Given Number :  71761
 Digit  7  with replace by  0
 Result  1061
/*
    Kotlin program
    Replacing one digit with other in an integer
*/
class Interchange
{
	fun replaceDigit(number: Int, digit: Int, x: Int): Unit
	{
		if (x < 0 || x > 9 || digit < 0 || digit > 9)
		{
			// When of given digit is not valid
			return;
		}
		var n: Int = number;
		var result: Int = 0;
		if (n < 0)
		{
			// Make positive
			n = -n;
		}
		if (digit == 0 && n == 0)
		{
			// When zero replace by x
			result = x;
		}
		else
		{
			// Auxiliary variable
			var temp: Int ;
			var mul: Int = 1;
			while (n > 0)
			{
				// Get last digit of n
				temp = n % 10;
				if (temp == digit)
				{
					// Include new digit
					result = (x * mul) + result;
				}
				else
				{
					result = (temp * mul) + result;
				}
				// Remove last digit of n
				n = n / 10;
				mul = mul * 10;
			}
			if (number < 0)
			{
				result = -result;
			}
		}
		// Display given numbers
		print("\n Given Number : " + number);
		print("\n Digit " + digit + " with replace by " + x);
		// Display calculated result
		print("\n Result " + result);
	}
}
fun main(args: Array < String > ): Unit
{
	val task: Interchange = Interchange();
	// Test A
	// Given number    : 1323  
	// Replacing digit : 3 
	// Replaced by     : 9
	// Result 1929
	task.replaceDigit(1323, 3, 9);
	// Test B
	// Given number    : -1161  
	// Replacing digit : 1 
	// Replaced by     : 3
	// Result -3363
	task.replaceDigit(-1161, 1, 3);
	// Test C
	// Given number    : 71761  
	// Replacing digit : 7 
	// Replaced by     : 0
	// Result 1061
	task.replaceDigit(71761, 7, 0);
}

Output

 Given Number : 1323
 Digit 3 with replace by 9
 Result 1929
 Given Number : -1161
 Digit 1 with replace by 3
 Result -3363
 Given Number : 71761
 Digit 7 with replace by 0
 Result 1061


Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment







© 2021, kalkicode.com, All rights reserved