Check whether the number can be made palindromic after adding K

Given two numbers N and K, the task is to check whether the given number N can be made a palindromic after adding K to it.

Example:

Input: N = 19, K = 3
Output: Yes
Explanation:
19 + 3 = 22 and 22 is a palindrome.

Input: N = 15, K = 3
Output: No
Explanation:
15 + 3 = 18 and 18 is not a palindrome.

Approach: The idea is to first add K to the given number and then check if the obtained number is a palindrome or not.



Below is the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to check whether the number
// can be made palindrome number after adding K
#include <bits/stdc++.h>
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;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:

Yes

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up

Recommended Posts:


Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.