diff --git a/algorithms/CPlusPlus/Maths/palindrome.cpp b/algorithms/CPlusPlus/Maths/palindrome.cpp new file mode 100644 index 00000000..3c580630 --- /dev/null +++ b/algorithms/CPlusPlus/Maths/palindrome.cpp @@ -0,0 +1,21 @@ +// Program to find whether a number and word is palindrome or not. + +#include //header file +using namespace std; +int main() //Main function +{ + string num_str = ""; //define variable + cin >> num_str; //taking input from user + string new_str = ""; //define a new variable + + for(int x = (num_str.size()-1); x >= 0; x--){ //for loop started + new_str += num_str[x]; //assigning the value input by user to new variable in reverse order + } //for loop end + + cout << (num_str == new_str ? "palindrome" : "Non-palindrome"); //checking whether the value assigned to both variables is equal or not using ternary operator and printing whether it's palindrome or non-palindrome + + return 0; //returning the main function +} + +//complexity of the program is O(n) +//test cases:- 101,pop,asdfgfdsa,123454321,obobo,nancyiycnan etc. \ No newline at end of file diff --git a/algorithms/CPlusPlus/README.md b/algorithms/CPlusPlus/README.md index 0cf38bcf..13ee1c3b 100644 --- a/algorithms/CPlusPlus/README.md +++ b/algorithms/CPlusPlus/README.md @@ -123,7 +123,8 @@ 3. [Prime Sieve](Maths/prime-sieve.cpp) 4. [Fibonacci Series](Maths/fibonaccci-series.cpp) 5. [Armstrong Number](Maths/armstrong.cpp) -6. [Reverse digit of a number](Maths/reverse-digits.cpp) +6. [Palindrome](Maths/palindrome.cpp) +7. [Reverse digit of a number](Maths/reverse-digits.cpp) # Recursion diff --git a/docs/en/Strings/Palindrome.md b/docs/en/Strings/Palindrome.md index 1b97db4b..0e68dbe3 100644 --- a/docs/en/Strings/Palindrome.md +++ b/docs/en/Strings/Palindrome.md @@ -22,6 +22,7 @@ A palindrome is a word, phrase, number or sequence of words that reads the same ## Implementation - [C](../../../algorithms/C/strings/palindrome.c) +- [C++](../../../algorithms/CPlusPlus/Maths/palindrome.cpp) - [C#](../../../algorithms/CSharp/src/Strings/palindrome.cs) - [Haskell](../../../algorithms/Haskell/strings/palindrome.hs) - [Java](../../../algorithms/Java/strings/palindrome.java) diff --git a/docs/ja/Strings/Palindrome.md b/docs/ja/Strings/Palindrome.md index e8e5bd11..bef37af3 100644 --- a/docs/ja/Strings/Palindrome.md +++ b/docs/ja/Strings/Palindrome.md @@ -21,6 +21,7 @@ ## 実装 - [C](../../../algorithms/C/strings/palindrome.c) +- [C++](../../../algorithms/CPlusPlus/Maths/palindrome.cpp) - [C#](../../../algorithms/CSharp/src/Strings/palindrome.cs) - [Haskell](../../../algorithms/Haskell/strings/palindrome.hs) - [Java](../../../algorithms/Java/strings/palindrome.java) diff --git a/docs/zh-tw/Strings/Palindrome.md b/docs/zh-tw/Strings/Palindrome.md index 4cb1f068..6117cb79 100644 --- a/docs/zh-tw/Strings/Palindrome.md +++ b/docs/zh-tw/Strings/Palindrome.md @@ -20,6 +20,7 @@ ## 程式碼 - [C](../../../algorithms/C/strings/palindrome.c) +- [C++](../../../algorithms/CPlusPlus/Maths/palindrome.cpp) - [C#](../../../algorithms/CSharp/src/Strings/palindrome.cs) - [Haskell](../../../algorithms/Haskell/strings/palindrome.hs) - [Java](../../../algorithms/Java/strings/palindrome.java)