جایگزینی ارقام در اعداد صحیح ورودی — به زبان ساده

در این مطلب، روش نوشتن برنامه‌ای که یک عدد صحیح را از ورودی دریافت و رقم خاصی از آن را با رقم دیگری جایگزین کند، آموزش داده خواهد شد. در اینجا، هدف جایگزینی ارقام صفر (۰) با پنج (۵) است. برای درک بهتر مسأله، مثال زیر قابل توجه است.

۱۰۲ -> 152
۱۰۲۰ -> 1525

فرض می‌شود که امکان استفاده از آرایه نیز برای ذخیره‌سازی همه ارقام وجود ندارد. این پرسش، یکی از سوالات مصاحبه‌های استخدامی آمازون بوده است. این مسأله، راه حل ساده‌ای دارد. آخرین رقم عدد با استفاده از عملگر باقی‌مانده (Mod) محاسبه می‌شود. سپس، بازگشت برای سایر ارقام صورت می‌پذیرد. در ادامه، پیاده‌سازی راهکار بالا در زبان‌های برنامه‌نویسی گوناگون شامل ++C، «جاوا» (Java)، «پایتون» (Python) و #C انجام شده است.

جایگزینی ارقام ۰ با ۵ در ++C

// C++ program to replace all ‘۰’ 
// with ‘۵’ 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; 
  
    // Extraxt 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) *  
                       ۱۰ + 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 ‘۰’ with ‘۵’ 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; 
  
    // Extraxt 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 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; 
          
        // Extraxt 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

جایگزینی ارقام ۰ با ۵ در پایتون

# 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 recurssion 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;  
      
    // Extraxt 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 recurssion 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)) *  
                                ۱۰ + $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 
?>

اگر نوشته بالا برای شما مفید بوده است، آموزش‌های زیر نیز به شما پیشنهاد می‌شوند:

منبع [+]

پاسخی بگذارید

نشانی ایمیل شما منتشر نخواهد شد. بخش‌های موردنیاز علامت‌گذاری شده‌اند *