forked from jainaman224/Algo_Ds_Notes
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
(jainaman224#1646) Implementation of palindrome in C & C++ GSSoC'20 (j…
…ainaman224#1825) Added code for implementation of palindrome in C/C++
- Loading branch information
1 parent
b15e142
commit 44f9260
Showing
3 changed files
with
135 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
/* | ||
This program checks whether the enterd number is palindrome or not and generates output according to input provided. | ||
*/ | ||
|
||
#include<stdio.h> | ||
int main() | ||
{ | ||
int n,rem,temp,rev = 0; | ||
printf("\nEnter a number: "); | ||
scanf("%d",&n); | ||
temp = n; | ||
while(temp != 0) | ||
{ | ||
rem = temp % 10; | ||
temp = temp / 10; | ||
rev = (rev * 10) + rem; | ||
} | ||
if(rev != n) | ||
{ | ||
printf("\nEntered number is not palindrome.\n"); | ||
} | ||
else | ||
{ | ||
printf("\nEntered number is palindrome.\n"); | ||
} | ||
return 0; | ||
} | ||
|
||
|
||
|
||
/* | ||
INPUT: | ||
n=12456 | ||
OUTPUT: | ||
Entered no. is not palindrome. | ||
INPUT: | ||
n=12821 | ||
OUTPUT: | ||
Entered no. is palindrome. | ||
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
/* | ||
This program checks whether the enterd number is palindrome or not and generates output according to input provided. | ||
*/ | ||
|
||
|
||
#include<iostream> | ||
using namespace std; | ||
int main() | ||
{ | ||
int n,rem,rev = 0; | ||
cout << "Enter a number: "; | ||
cin >> n; | ||
int temp = n; | ||
while(temp != 0) | ||
{ | ||
rem = temp % 10; | ||
temp = temp / 10; | ||
rev = (rev * 10) + rem; | ||
} | ||
if(rev != n) | ||
{ | ||
cout << "Entered number is not palindrome." << endl; | ||
} | ||
else | ||
{ | ||
cout << "Entered number is palindrome." << endl; | ||
} | ||
return 0; | ||
} | ||
|
||
|
||
|
||
/* | ||
INPUT: | ||
n=12456 | ||
OUTPUT: | ||
Entered no. is not palindrome. | ||
INPUT: | ||
n=12821 | ||
OUTPUT: | ||
Entered no. is palindrome. | ||
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
/* | ||
This program checks whether the enterd string is palindrome or not and generates output according to input provided. | ||
*/ | ||
|
||
#include<iostream> | ||
#include<string.h> // to use swap() | ||
using namespace std; | ||
|
||
void string_palindrome(string s) | ||
{ | ||
string reverse = s; | ||
int n = s.length(); | ||
for(int i=0;i<n/2;i++) | ||
{ | ||
swap(reverse[i], reverse[n-i-1]); | ||
} | ||
if(reverse == s) | ||
{ | ||
cout << s <<" is Plindrome." << endl; | ||
} | ||
else | ||
{ | ||
cout << s << " is not Palindrome." << endl; | ||
} | ||
} | ||
int main() | ||
{ | ||
string str; | ||
cout << "Enter a string: "; | ||
cin >> str; | ||
string_palindrome(str); | ||
return 0; | ||
} | ||
|
||
|
||
|
||
/* | ||
INPUT: | ||
Enter a string: abhnch | ||
OUTPUT: | ||
abhnch is not Palindrome. | ||
_______________________________________ | ||
INPUT: | ||
Enter a string: adbcbda | ||
OUTPUT: | ||
adbcbda is Palindrome. | ||
*/ |