📜  加K后检查号码是否可以回文

📅  最后修改于: 2021-04-29 09:46:33             🧑  作者: Mango

给定两个数字NK ,任务是检查在将给定的数字NK后是否可以将其设为回文。

例子:

方法:想法是先将K加到给定的数字上,然后检查所获得的数字是否是回文。

下面是上述方法的实现:

C++
// C++ program to check whether the number
// can be made palindrome number after adding K
#include 
using namespace std;
  
// Function to check whether a number
// is a palindrome or not
void checkPalindrome(int num)
{
  
    // Convert num to string
    string str = to_string(num);
  
    int l = 0, r = str.length() - 1;
  
    // Comparing kth character from the
    // beginning and N - kth character
    // from the end. If all the characters
    // match, then the number is a palindrome
    while (l < r) {
  
        if (str[l] != str[r]) {
            cout << "No";
            return;
        }
        l++;
        r--;
    }
  
    // If all the above conditions satisfy,
    // it means that the number is a palindrome
    cout << "Yes";
    return;
}
  
// Driver code
int main()
{
    int n = 19, k = 3;
  
    checkPalindrome(n + k);
  
    return 0;
}


Java
import java.util.*;
class GFG{
// Java program to check whether the number
// can be made palindrome number after adding K
  
// Function to check whether a number
// is a palindrome or not
static void checkPalindrome(int num)
{
  
    // Convert num to string
    String str = Integer.toString(num);
  
    int l = 0, r = str.length() - 1;
  
    // Comparing kth character from the
    // beginning and N - kth character
    // from the end. If all the characters
    // match, then the number is a palindrome
    while (l < r) {
  
        if (str.charAt(l) != str.charAt(r)) {
            System.out.print("No");
            return;
        }
        l++;
        r--;
    }
  
    // If all the above conditions satisfy,
    // it means that the number is a palindrome
    System.out.print("Yes");
    return;
}
  
// Driver code
public static void main(String args[])
{
    int n = 19, k = 3;
  
    checkPalindrome(n + k);
}
}
  
// This code is contributed by Surendra_Gangwar


Python3
# Python3 program to check whether the number
# can be made palindrome number after adding K
  
# Function to check whether a number
# is a palindrome or not
def checkPalindrome(num):
  
    # Convert num to stringing
    string = str(num)
  
    l = 0
    r = len(string) - 1;
  
    # Comparing kth character from the
    # beginning and N - kth character
    # from the end. If all the characters
    # match, then the number is a palindrome
    while (l < r):
  
        if (string[l] != string[r]) :
            print("No")
            return;
          
        l = l + 1;
        r = r - 1;
      
    # If all the above conditions satisfy,
    # it means that the number is a palindrome
    print("Yes")
    return;
  
# Driver code
if __name__=='__main__': 
  
    n = 19
    k = 3
  
    checkPalindrome(n + k);
  
# This code is contributed by Princi Singh


C#
using System;
  
class GFG{
// C# program to check whether the number
// can be made palindrome number after adding K
   
// Function to check whether a number
// is a palindrome or not
static void checkPalindrome(int num)
{
   
    // Convert num to string
    String str = num.ToString();
   
    int l = 0, r = str.Length - 1;
   
    // Comparing kth character from the
    // beginning and N - kth character
    // from the end. If all the characters
    // match, then the number is a palindrome
    while (l < r) {
   
        if (str[l] != str[r]) {
            Console.Write("No");
            return;
        }
        l++;
        r--;
    }
   
    // If all the above conditions satisfy,
    // it means that the number is a palindrome
    Console.Write("Yes");
    return;
}
   
// Driver code
public static void Main(String []args)
{
    int n = 19, k = 3;
   
    checkPalindrome(n + k);
}
}
  
// This code is contributed by Princi Singh


输出:
Yes