Reemplace todo ‘0’ con ‘5’ en un entero de entrada

Dado un número entero como entrada y reemplace todos los ‘0’ con ‘5’ en el número entero. 

Ejemplos: 

C++

// C++ program to replace all ‘0’
// with ‘5’ in an input Integer
#include <iostream>
using namespace std;
 
// A iterative function to reverse a number
int reverseTheNumber(int temp)
{
    int ans = 0;
    while (temp > 0) {
        int rem = temp % 10;
        ans = ans * 10 + rem;
        temp = temp / 10;
    }
    return ans;
}
 
int convert0To5(int num)
{
    // if num is 0 return 5
    if (num == 0)
        return 5;
 
    // Extract the last digit and
    // change it if needed
    else {
        int temp = 0;
 
        while (num > 0) {
            int digit = num % 10;
            //if digit is 0, make it 5
            if (digit == 0)
                digit = 5;
 
            temp = temp * 10 + digit;
            num = num / 10;
        }
        // call the function reverseTheNumber by passing
        // temp
        return reverseTheNumber(temp);
    }
}
 
// Driver code
int main()
{
    int num = 10120;
    cout << convert0To5(num);
    return 0;
}
 
// This code is contributed by Vrashank Rao M.

Java

// Java program for the above approach
import java.io.*;
 
class GFG {
 
// A iterative function to reverse a number
static int reverseTheNumber(int temp)
{
    int ans = 0;
    while (temp > 0) {
        int rem = temp % 10;
        ans = ans * 10 + rem;
        temp = temp / 10;
    }
    return ans;
}
 
static int convert0To5(int num)
{
    // if num is 0 return 5
    if (num == 0)
        return 5;
 
    // Extract the last digit and
    // change it if needed
    else {
        int temp = 0;
 
        while (num > 0) {
            int digit = num % 10;
           
            //if digit is 0, make it 5
            if (digit == 0)
                digit = 5;
 
            temp = temp * 10 + digit;
            num = num / 10;
        }
       
        // call the function reverseTheNumber by passing
        // temp
        return reverseTheNumber(temp);
    }
}
 
    // Driver program
    public static void main(String args[])
    {
        int num = 10120;
        System.out.println(convert0To5(num));
    }
}
 
// This code is contributed by sanjoy_62.

Python3

# Python program for the above approach
 
# A iterative function to reverse a number
def reverseTheNumber(temp):
    ans = 0;
    while (temp > 0):
        rem = temp % 10;
        ans = ans * 10 + rem;
        temp = temp // 10;
     
    return ans;
 
def convert0To5(num):
    # if num is 0 return 5
    if (num == 0):
        return 5;
 
    # Extract the last digit and
    # change it if needed
    else:
        temp = 0;
 
        while (num > 0):
            digit = num % 10;
 
            # if digit is 0, make it 5
            if (digit == 0):
                digit = 5;
 
            temp = temp * 10 + digit;
            num = num // 10;
         
        # call the function reverseTheNumber by passing
        # temp
        return reverseTheNumber(temp);
 
# Driver program
if __name__ == '__main__':
    num = 10120;
    print(convert0To5(num));
 
# This code is contributed by umadevi9616

C#

// C# program for the above approach
using System;
public class GFG {
 
// A iterative function to reverse a number
static int reverseTheNumber(int temp)
{
    int ans = 0;
    while (temp > 0) {
        int rem = temp % 10;
        ans = ans * 10 + rem;
        temp = temp / 10;
    }
    return ans;
}
 
static int convert0To5(int num)
{
    // if num is 0 return 5
    if (num == 0)
        return 5;
 
    // Extract the last digit and
    // change it if needed
    else {
        int temp = 0;
 
        while (num > 0) {
            int digit = num % 10;
            //if digit is 0, make it 5
            if (digit == 0)
                digit = 5;
 
            temp = temp * 10 + digit;
            num = num / 10;
        }
       
        // call the function reverseTheNumber by passing
        // temp
        return reverseTheNumber(temp);
    }
}
 
    // Driver Code
    public static void Main (string[] args) {
        int num = 10120;
        Console.Write(convert0To5(num));
 
    }
}
 
// This code is contributed by splevel62.

Javascript

<script>
// javascript program for the above approach
 
    // A iterative function to reverse a number
    function reverseTheNumber(temp) {
        var ans = 0;
        while (temp > 0) {
            var rem = temp % 10;
            ans = ans * 10 + rem;
            temp = parseInt(temp / 10);
        }
        return ans;
    }
 
    function convert0To5(num)
    {
     
        // if num is 0 return 5
        if (num == 0)
            return 5;
 
        // Extract the last digit and
        // change it if needed
        else {
            var temp = 0;
 
            while (num > 0) {
                var digit = num % 10;
 
                // if digit is 0, make it 5
                if (digit == 0)
                    digit = 5;
 
                temp = temp * 10 + digit;
                num = parseInt(num / 10);
            }
 
            // call the function reverseTheNumber by passing
            // temp
            return reverseTheNumber(temp);
        }
    }
 
    // Driver program
        var num = 10120;
        document.write(convert0To5(num));
 
// This code is contributed by umadevi9616
</script>

C++

#include<bits/stdc++.h>
using namespace std;
 
// Returns the number to be added to the
// input to replace all zeroes with five
int calculateAddedValue(int number)
{
     
    // Amount to be added
    int result = 0;
 
    // Unit decimal place
    int decimalPlace = 1;
     
    if (number == 0)
    {
        result += (5 * decimalPlace);
    }
 
    while (number > 0)
    {
        if (number % 10 == 0)
        {
             
            // A number divisible by 10, then
            // this is a zero occurrence in
            // the input
            result += (5 * decimalPlace);
 
        }
         
        // Move one decimal place
        number /= 10;
        decimalPlace *= 10;
    }
    return result;
}
 
int replace0with5(int number)
{
    return number += calculateAddedValue(number);
}
 
// Driver code
int main()
{
    cout << replace0with5(1020);
}
 
// This code is contributed by avanitrachhadiya2155

Java

public class ReplaceDigits {
    static int replace0with5(int number)
    {
        return number += calculateAddedValue(number);
    }
 
    // returns the number to be added to the
    // input to replace all zeroes with five
    private static int calculateAddedValue(int number)
    {
 
        // amount to be added
        int result = 0;
 
        // unit decimal place
        int decimalPlace = 1;
 
        if (number == 0) {
            result += (5 * decimalPlace);
        }
 
        while (number > 0) {
            if (number % 10 == 0)
                // a number divisible by 10, then
                // this is a zero occurrence in the input
                result += (5 * decimalPlace);
 
            // move one decimal place
            number /= 10;
            decimalPlace *= 10;
        }
        return result;
    }
 
    public static void main(String[] args)
    {
        System.out.print(replace0with5(1020));
    }
}

Python3

def replace0with5(number):
    number += calculateAddedValue(number)
    return number
     
# returns the number to be added to the
# input to replace all zeroes with five
def calculateAddedValue(number):
     
    # amount to be added
    result = 0
     
    # unit decimal place
    decimalPlace = 1
 
    if (number == 0):
        result += (5 * decimalPlace)
         
    while (number > 0):
        if (number % 10 == 0):
             
            # a number divisible by 10, then
            # this is a zero occurrence in the input
            result += (5 * decimalPlace)
             
        # move one decimal place
        number //= 10
        decimalPlace *= 10
     
    return result
     
# Driver code
print(replace0with5(1020))
     
# This code is contributed by shubhmasingh10

C#

using System;
 
class GFG{
     
static int replace0with5(int number)
{
    return number += calculateAddedValue(number);
}
 
// Returns the number to be added to the
// input to replace all zeroes with five
static int calculateAddedValue(int number)
{
     
    // Amount to be added
    int result = 0;
 
    // Unit decimal place
    int decimalPlace = 1;
 
    if (number == 0)
    {
        result += (5 * decimalPlace);
    }
 
    while (number > 0)
    {
        if (number % 10 == 0)
         
            // A number divisible by 10, then
            // this is a zero occurrence in the input
            result += (5 * decimalPlace);
 
        // Move one decimal place
        number /= 10;
        decimalPlace *= 10;
    }
    return result;
}
 
// Driver Code
static public void Main()
{
    Console.WriteLine(replace0with5(1020));
}
}
 
// This code is contributed by rag2127

Javascript

<script>
 
// Returns the number to be added to the
// input to replace all zeroes with five
function calculateAddedValue(number){
    // Amount to be added
    let result = 0;
    // Unit decimal place
    let decimalPlace = 1;
     
    if (number == 0) {
        result += (5 * decimalPlace);
    }
 
    while (number > 0){
        if (number % 10 == 0){
            // A number divisible by 10, then
            // this is a zero occurrence in
            // the input
            result += (5 * decimalPlace);
        }
        // Move one decimal place
        number = Math.floor(number/10);
        decimalPlace *= 10;
    }
    return result;
}
 
function replace0with5(number){
    return number += calculateAddedValue(number);
}
 
// Driver code
document.write(replace0with5(1020));
 
</script>

C++

// C++ program to replace all ‘0’
// with ‘5’ in an input Integer
#include <bits/stdc++.h>
using namespace std;
 
// A recursive function to replace all 0s
// with 5s in an input number It doesn't
// work if input number itself is 0.
int convert0To5Rec(int num)
{
    // Base case for recursion termination
    if (num == 0)
        return 0;
 
    // Extract the last digit and
    // change it if needed
    int digit = num % 10;
    if (digit == 0)
        digit = 5;
 
    // Convert remaining digits and
    // append the last digit
    return convert0To5Rec(num / 10) * 10 + digit;
}
 
// It handles 0 and calls convert0To5Rec()
// for other numbers
int convert0To5(int num)
{
    if (num == 0)
        return 5;
    else
        return convert0To5Rec(num);
}
 
// Driver code
int main()
{
    int num = 10120;
    cout << convert0To5(num);
    return 0;
}
 
// This code is contributed by Code_Mech.

C

// C program to replace all ‘0’
// with ‘5’ in an input Integer
#include <stdio.h>
 
// A recursive function to replace
// all 0s with 5s in an input number
// It doesn't work if input number itself is 0.
int convert0To5Rec(int num)
{
    // Base case for recursion termination
    if (num == 0)
        return 0;
 
    // Extract the last digit and change it if needed
    int digit = num % 10;
    if (digit == 0)
        digit = 5;
 
    // Convert remaining digits
    // and append the last digit
    return convert0To5Rec(num / 10) * 10 + digit;
}
 
// It handles 0 and calls
// convert0To5Rec() for other numbers
int convert0To5(int num)
{
    if (num == 0)
        return 5;
    else
        return convert0To5Rec(num);
}
 
// Driver program to test above function
int main()
{
    int num = 10120;
    printf("%d", convert0To5(num));
    return 0;
}

Java

// Java code for Replace all 0 with
// 5 in an input Integer
class GFG {
 
    // A recursive function to replace all 0s with 5s in
    // an input number. It doesn't work if input number
    // itself is 0.
    static int convert0To5Rec(int num)
    {
        // Base case
        if (num == 0)
            return 0;
 
        // Extract the last digit and change it if needed
        int digit = num % 10;
        if (digit == 0)
            digit = 5;
 
        // Convert remaining digits and append the
        // last digit
        return convert0To5Rec(num / 10) * 10 + digit;
    }
 
    // It handles 0 and calls convert0To5Rec() for
    // other numbers
    static int convert0To5(int num)
    {
        if (num == 0)
            return 5;
        else
            return convert0To5Rec(num);
    }
 
    // Driver function
    public static void main(String[] args)
    {
        System.out.println(convert0To5(10120));
    }
}
 
// This code is contributed by Kamal Rawal

Python3

# Python program to replace all
# 0 with 5 in given integer
 
# A recursive function to replace all 0s
# with 5s in an integer
# Does'nt work if the given number is 0 itself
def convert0to5rec(num):
 
    # Base case for recursion termination
    if num == 0:
        return 0
 
    # Extract the last digit and change it if needed
    digit = num % 10
 
    if digit == 0:
        digit = 5
 
    # Convert remaining digits and append the last digit
    return convert0to5rec(num // 10) * 10 + digit
 
# It handles 0 to 5 calls convert0to5rec()
# for other numbers
def convert0to5(num):
    if num == 0:
        return 5
    else:
        return convert0to5rec(num)
 
 
# Driver Program
num = 10120
print (convert0to5(num))
 
# Contributed by Harshit Agrawal

C#

// C# code for Replace all 0
// with 5 in an input Integer
using System;
 
class GFG {
 
    // A recursive function to replace
    // all 0s with 5s in an input number.
    // It doesn't work if input number
    // itself is 0.
    static int convert0To5Rec(int num)
    {
        // Base case
        if (num == 0)
            return 0;
 
        // Extract the last digit and
        // change it if needed
        int digit = num % 10;
        if (digit == 0)
            digit = 5;
 
        // Convert remaining digits
        // and append the last digit
        return convert0To5Rec(num / 10) * 10 + digit;
    }
 
    // It handles 0 and calls
    // convert0To5Rec() for other numbers
    static int convert0To5(int num)
    {
        if (num == 0)
            return 5;
        else
            return convert0To5Rec(num);
    }
 
    // Driver Code
    static public void Main()
    {
        Console.Write(convert0To5(10120));
    }
}
 
// This code is contributed by Raj

PHP

<?php
// PHP program to replace all 0 with 5
// in given integer
 
// A recursive function to replace all 0s
// with 5s in an integer. Does'nt work if
// the given number is 0 itself
function convert0to5rec($num)
{
 
    // Base case for recursion termination
    if ($num == 0)
        return 0;
 
    // Extract the last digit and
    // change it if needed
    $digit = ($num % 10);
 
    if ($digit == 0)
        $digit = 5;
 
    // Convert remaining digits and append
    // the last digit
    return convert0to5rec((int)($num / 10)) *
                                10 + $digit;
}
 
// It handles 0 to 5 calls convert0to5rec()
// for other numbers
function convert0to5($num)
{
    if ($num == 0)
        return 5;
    else
        return convert0to5rec($num);
}
 
// Driver Code
$num = 10120;
print(convert0to5($num));
 
// This code is contributed by mits
?>

Javascript

<script>
// javascript code for Replace all 0 with
// 5 in an input Integer   
// A recursive function to replace all 0s with 5s in
    // an input number. It doesn't work if input number
    // itself is 0.
    function convert0To5Rec(num) {
        // Base case
        if (num == 0)
            return 0;
 
        // Extract the last digit and change it if needed
        var digit = num % 10;
        if (digit == 0)
            digit = 5;
 
        // Convert remaining digits and append the
        // last digit
        return convert0To5Rec(parseInt(num / 10)) * 10 + digit;
    }
 
    // It handles 0 and calls convert0To5Rec() for
    // other numbers
    function convert0To5(num) {
        if (num == 0)
            return 5;
        else
            return convert0To5Rec(num);
    }
 
    // Driver function
     
        document.write(convert0To5(10120));
 
// This code contributed by gauravrajput1
</script>

Python3

# Python program for the above approach
 
# Function to replace all 0s with 5s
def change(num):
    s = str(num)
    s = s.replace('0', '5')
    return s
 
# Driver code
if __name__ == '__main__':
    num = 10120
    print(change(num))

Publicación traducida automáticamente

Artículo escrito por GeeksforGeeks-1 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *