"The best way to cheer yourself up is to try to cheer somebody else up." Mark Twain
Showing posts with label Iteration (Palindrome). Show all posts
Showing posts with label Iteration (Palindrome). Show all posts

Monday, March 1, 2010

WAP to check a string is palindrome or not

eg.

MADAM is palindrome
MAIDEN is not a palindrome.


#include<iostream.h>
#include<conio.h>
#include<string.h>
#include<stdio.h>

void main()
{

char text[50] , rev[50];

clrscr();

cout<< "\n Enter a string : ";
gets(text);

/* calculate the length of the string */
int len  = strlen(text);

/* reverse the string */

for(int i = len - 1, k = 0; i>=0 ; i--, k++)
{
rev[k] = text[i];
}

rev[k] = '\0';

if(strcmp(text, rev) == 0)
cout<< "\n The string " << text << " is a palindrome. ";
else
cout<< "\n The string " << text << " is not a palindrome. ";



getch();
}

Sunday, February 28, 2010

WAP to check a number is palindrome or not

eg.  
10201 is a palindrome
10210 is not a palindrome


#include<iostream.h>
#include<conio.h>

void main()
{
int num, rev=0;

cout<< "\n Enter a number :";
cin>> num;

/* truncate each digit from number and store the reverse of the number in rev */
int digit;
for(int i = num; i >0 ; i = i/10)
{
digit = i % 10;  // get the last digit
rev = rev* 10 + digit;
}

if( rev == num )
cout<< "\n The " << num  << " is a Palindrome. ";
else
cout << " \n The " << num << " is not a Palindrome. ";

getch();

}

Thursday, February 25, 2010

WAP to print all prime numbers between 100 to 500 , that are palindrome.


#include<iostream.h>
#include<conio.h>
void main()
{
int flag,rev;

cout<< "\n The palindrome prime numbers between 100 to 500 are :\n";

for(int n = 100 ; n<=500 ; n ++ )
{
flag = 0;
/* Check Prime number for each n */ 
for(int j = 2; j < = n-1 ; j++)
{
if(n%j == 0)
{
flag = 1;
break;
}
}

rev = 0;

//reverse the number and check palindrome
if(flag == 0)
{
for(int k = n; k >=1 ; k = k/10)
{
rev = rev*10 + k%10;
}

if(rev == n)
cout<< n << " " ;
}

}

getch();

}
C makes it easy to shoot yourself in the foot; C++ makes it harder, but when you do it blows your whole leg off.
Now Playing: Ballade Pour Adeline

About Me

My photo
I m an IT lecturer of a college. I love social-work. I want to do something beneficial for society before dying , that can promote our society, to some extent.