Generate number with given operation and check if it is palindrome Last Updated : 16 Dec, 2022 Summarize Comments Improve Suggest changes Share Like Article Like Report Given an integer N the task is to create a string out of it by repeating the number such that the length of the resultant string is equal to the sum of the digits in the original number. For eg: If the number is 61 and the sum of the digits is 6 + 1 = 7 so the string generated after repeating 61 will be of 7 length i.e. 6161616. Examples: Input: N = 10101 Output: Yes Length of the string is given by sum of the digits i.e. 1 + 0 + 1 + 0 + 1 = 3. So the resultant string is "101" which is a palindrome. Input: N = 123 Output: No Length of the string will be 1 + 2 3 = 6. So the resultant string is "123123" which is not a palindrome. Recommended: Please solve it on “PRACTICE” first, before moving on to the solution. Approach: Find the sum of the digits of N and repeat the number till the length of the string becomes equal to this sum.Now check if the resultant string is a palindrome or not.If it is a palindrome then print Yes.Else print No. Below is the implementation of the above approach: C++ // CPP implementation of the approach #include<bits/stdc++.h> using namespace std; // Function that returns true if str is a palindrome bool isPalindrome(string str) { int len = str.length(); for (int i = 0; i < len / 2; i++) { if (str[i] != str[len - 1 - i]) return false; } return true; } // Function that returns true if the // generated string is a palindrome bool createStringAndCheckPalindrome(int N) { // sub contains N as a string ostringstream out; out << N; string result = out.str(); string sub = "" + result, res_str = ""; int sum = 0; // Calculate the sum of the digits while (N > 0) { int digit = N % 10; sum += digit; N = N / 10; } // Repeat the substring until the length // of the resultant string < sum while (res_str.length() < sum) res_str += sub; // If length of the resultant string exceeded sum // then take substring from 0 to sum - 1 if (res_str.length() > sum) res_str = res_str.substr(0, sum); // If the generated string is a palindrome if (isPalindrome(res_str)) return true; return false; } // Driver code int main() { int N = 10101; if (createStringAndCheckPalindrome(N)) cout << ("Yes"); else cout << ("No"); } // This code is contributed by // Shashank_Sharma Java // Java implementation of the approach import java.io.*; class GFG { // Function that returns true if str is a palindrome static boolean isPalindrome(String str) { int len = str.length(); for (int i = 0; i < len / 2; i++) { if (str.charAt(i) != str.charAt(len - 1 - i)) return false; } return true; } // Function that returns true if the // generated string is a palindrome static boolean createStringAndCheckPalindrome(int N) { // sub contains N as a string String sub = "" + N, res_str = ""; int sum = 0; // Calculate the sum of the digits while (N > 0) { int digit = N % 10; sum += digit; N = N / 10; } // Repeat the substring until the length // of the resultant string < sum while (res_str.length() < sum) res_str += sub; // If length of the resultant string exceeded sum // then take substring from 0 to sum - 1 if (res_str.length() > sum) res_str = res_str.substring(0, sum); // If the generated string is a palindrome if (isPalindrome(res_str)) return true; return false; } // Driver code public static void main(String args[]) { int N = 10101; if (createStringAndCheckPalindrome(N)) System.out.println("Yes"); else System.out.println("No"); } } Python 3 # Python 3 implementation of the approach # Function that returns true if # str is a palindrome def isPalindrome(s): l = len(s) for i in range(l // 2): if (s[i] != s[l - 1 - i]): return False return True # Function that returns true if the # generated string is a palindrome def createStringAndCheckPalindrome(N): # sub contains N as a string sub = "" + chr(N) res_str = "" sum = 0 # Calculate the sum of the digits while (N > 0) : digit = N % 10 sum += digit N = N // 10 # Repeat the substring until the length # of the resultant string < sum while (len(res_str) < sum): res_str += sub # If length of the resultant string exceeded # sum then take substring from 0 to sum - 1 if (len(res_str) > sum): res_str = res_str[0: sum] # If the generated string is a palindrome if (isPalindrome(res_str)): return True return False # Driver code if __name__ == "__main__": N = 10101 if (createStringAndCheckPalindrome(N)): print("Yes") else: print("No") # This code is contributed by ita_c C# // C# implementation of the approach using System ; class GFG { // Function that returns true if str is a palindrome static bool isPalindrome(string str) { int len = str.Length; for (int i = 0; i < len / 2; i++) { if (str[i] != str[len - 1 - i]) return false; } return true; } // Function that returns true if the // generated string is a palindrome static bool createStringAndCheckPalindrome(int N) { // sub contains N as a string string sub = "" + N, res_str = ""; int sum = 0; // Calculate the sum of the digits while (N > 0) { int digit = N % 10; sum += digit; N = N / 10; } // Repeat the substring until the length // of the resultant string < sum while (res_str.Length< sum) res_str += sub; // If length of the resultant string exceeded sum // then take substring from 0 to sum - 1 if (res_str.Length > sum) res_str = res_str.Substring(0, sum); // If the generated string is a palindrome if (isPalindrome(res_str)) return true; return false; } // Driver code public static void Main() { int N = 10101; if (createStringAndCheckPalindrome(N)) Console.WriteLine("Yes"); else Console.WriteLine("No"); } // This code is contributed by Ryuga } JavaScript <script> // JavaScript implementation of the approach // Function that returns true // if str is a palindrome function isPalindrome(str) { let len = str.length; for (let i = 0; i < len / 2; i++) { if (str[i] != str[len - 1 - i]) return false; } return true; } // Function that returns true if the // generated string is a palindrome function createStringAndCheckPalindrome(N) { // sub contains N as a string let sub = "" + N, res_str = ""; let sum = 0; // Calculate the sum of the digits while (N > 0) { let digit = N % 10; sum += digit; N = N / 10; } // Repeat the substring until the length // of the resultant string < sum while (res_str.length < sum) res_str += sub; // If length of the resultant string exceeded sum // then take substring from 0 to sum - 1 if (res_str.length > sum) res_str = res_str.substring(0, sum); // If the generated string is a palindrome if (isPalindrome(res_str)) return true; return false; } // Driver code let N = 10101; if (createStringAndCheckPalindrome(N)) document.write("Yes"); else document.write("No"); // This code is contributed by avanitrachhadiya2155 </script> OutputYes Comment More infoAdvertise with us Next Article Check if a number is palindrome or not without using any extra space | Set 2 R rachana soma Follow Improve Article Tags : Strings Technical Scripter DSA palindrome number-digits +1 More Practice Tags : palindromeStrings Similar Reads Check if a number with even number of digits is palindrome or not Given a number N containing an even number of digits. The task is to check whether that number is palindrome or not. Examples: Input: N = 123321 Output: Palindrome Input: 1234 Output: Not palindrome A Naive Approach is to traverse from the front and back of that number and stop where they do not mat 4 min read Check if a given string is Even-Odd Palindrome or not Given a string str, the task is to check if the given string is Even-Odd Palindrome or not. An Even-Odd Palindrome string is defined to be a string whose characters at even indices form a Palindrome while the characters at odd indices also form a Palindrome separately. Examples: Input: str="abzzab" 7 min read Check if a number is palindrome or not without using any extra space | Set 2 Given a number ânâ and our goal is to find out it is palindrome or not without using any extra space. We canât make a new copy of number. Examples: Input: n = 2332Output: Yes it is Palindrome.Explanation:original number = 2332reversed number = 2332Both are same hence the number is palindrome. Input: 5 min read Check if it is possible to create a palindrome string from given N Given a number N. The task is to create an alphabetical string in lower case from that number and tell whether the string is palindrome or not. a = 0, b = 1â¦.. and so on. For eg: If the number is 61 the substring âgbâ will be printed till 7 (6+1) characters i.e. âgbgbgbgâ and check if palindrome or 7 min read Check if number is palindrome or not in Octal Given a number which may be in octal or in decimal. If the number is not octal then convert it into octal then check if it is palindrome or not. If it is palindrome then print 1 otherwise print 0. If the number is already in octal then check if the number is palindrome or not.Examples : Input :n = 1 7 min read Check whether the given floating point number is a palindrome Given a floating-point number N, the task is to check whether it is palindrome or not. Input: N = 123.321 Output: YesInput: N = 122.1 Output: No Approach: First, convert the given floating-point number into a character array.Initialize the low to first index and high to the last index.While low < 4 min read Like