ArtsAutosBooksBusinessEducationEntertainmentFamilyFashionFoodGamesGenderHealthHolidaysHomeHubPagesPersonal FinancePetsPoliticsReligionSportsTechnologyTravel

C++ Program: To Check Whether a Given Number is Palindrome or Not using Do-While loop

Updated on November 30, 2013
Source

What does this program do

This program checks whether a given number is a palindrome number or not. It works by storing a number in a variable, then reversing the number using a do while loop and checks whether the reverse of the number is equal to the given number using a if statement. if the both the reverse and the actual number are equal the program prints It's a palindrome number.

Example for a palindrome number is 2332

Source Code

#include <iostream.h>
#include <conio.h>
int main()
{
	clrscr();
	int n,rn=0,val,stval;
   	cout<<"Enter a number :";
    	cin>>val;
	stval=val;
	
		do
   
		{
        	n=val%10;
        	val=val/10;
        	rn=rn*10+n;
   
		}while(val>0);
	
		
		if(stval==rn)
			cout<<"\nIts a plaindrome number";
		else
			cout<<"\nIts not a palindrome number";	
	getch();
}

How it works

  • a number is stored in val (let it be 121)
  • stval is assigned to val
  • n = val%10 it n is assigned to 1 (1 is the reminder when 121 is divided by 10)
  • val=val/10 val is assigned to 12 (data type of val is int and 121 divided by 10 is 12.1 it only stores 12)
  • rn=rn*10+n rn is assigned to 1 (initially rn=0 , 0*10+1=1)
  • Now the do while loop checks the condition val>0
  • when it exits do while loop rn will have the value 121 (1: rn=0*10+1, 2: rn=1*10+2, 3: rn=12*10+1 = 121)
  • if statement checks whether stval=rn and displays the output


If you find this hub interesting do follow this hub and get updates on my posts

Was this program easy to understand ?

See results

Comments

    0 of 8192 characters used
    Post Comment

    • profile image

      Code Crazy 4 years ago

      Wonderfully explained. Loved it.

    • Atz profile image
      Author

      Athul M R 4 years ago from Calicut, Kerala, INDIA

      So happy to hear a positive feedback =)

    • profile image

      hetal 3 years ago

      very nice but, i like it's output.........

    • Atz profile image
      Author

      Athul M R 3 years ago from Calicut, Kerala, INDIA

      Hetal thanks for you're positive feedback =)

    • profile image

      Tiara 3 years ago

      What happens if we write val instead of stval in if checking?

    • profile image

      Tiara 3 years ago

      Can u pls answer this question fast?

    • Atz profile image
      Author

      Athul M R 3 years ago from Calicut, Kerala, INDIA

      stval is used to store the value inside "val", and the value inside the variable "val" is getting changed in the line 15. So if we use val instead of stval in line number 21 we'll get a wrong output

    • profile image

      omkar 3 years ago

      wat will happen if we put val as 122

    • Atz profile image
      Author

      Athul M R 3 years ago from Calicut, Kerala, INDIA

      @omkar If we put val as 122 the program will display "Its not a palindrome number"

    • profile image

      omkar 3 years ago

      thanxx a lot....

      n awesome xplaination sir

    • Atz profile image
      Author

      Athul M R 3 years ago from Calicut, Kerala, INDIA

      Its my pleasure =)

    • profile image

      Meenakshi 3 years ago

      very useful..:)

    • profile image

      suknya 3 years ago

      how the last iteration works??..ie when 1%10

    • Atz profile image
      Author

      Athul M R 3 years ago from Calicut, Kerala, INDIA

      Suknya ‎ 1 modulo 10 = 1

    • Atz profile image
      Author

      Athul M R 3 years ago from Calicut, Kerala, INDIA

      Meenakshi thank for your positive feedback

    • profile image

      shubham 3 years ago

      nice program.but it has some errors!!!

    • Atz profile image
      Author

      Athul M R 3 years ago from Calicut, Kerala, INDIA

      yep there was a semi-column missing after the "cin>>val" , thanks for mentioning. Now it will work fine =)

    • profile image

      ravi 3 years ago

      it's good , but how can i know that how many times the loop will be executed because in "rn=rn*10+n" the value that "rn" will store is 1. After that how will the rest of the digits be assigned?

    • Atz profile image
      Author

      Athul M R 3 years ago from Calicut, Kerala, INDIA

      Ravi the while loop will run till the variable "val" is equal to or less than "0"

      in this example when it exits do while loop "rn" will have the value 121 (in 1st iteration : rn=0*10+1,in 2nd iteration : rn=1*10+2 and in 3rd iteration : rn=12*10+1 = 121)

    • profile image

      ravi 3 years ago

      yeah , but why do only 3 iterations take place?

    • Atz profile image
      Author

      Athul M R 3 years ago from Calicut, Kerala, INDIA

      Ravi its depended on the value stored in the variable "val".

      in "How it works" paragraph I've used "121" as input (its mentioned inside brackets in the first line of the paragraph)

      So in the first iteration of the do while loop

      do{

      n=val%10; // n=121%10 = 1

      val=val/10; // val=121/10 =12 since val is of integer data type

      rn=rn*10+n; // rn=0*10+1 =1

      }while(val greater than 0); // val=12 which is greater than 0

      in second iteration val=12 which is and rn =1

      do{

      n=val%10; // n= 12%10 = 2

      val=val/10; //val= 12/10 = 1

      rn=rn*10+n; // rn=1*10+2 = 12

      }while(val greater than 0); // val=1 which is greater than 0

      in third iteration

      do{

      n=val%10; // n= 1%10 = 1

      val=val/10; //val= 1/10 = 0

      rn=rn*10+n; // rn=12*10+1 = 121

      }while(val greater than 0); // val=0 which is not greater than 0 hence exit the loop

      since i chose a three digit number as input only 3 iterations take place

    • profile image

      ravi 3 years ago

      2nd line from the last!!!! that's what i was looking for!!!! thank you very much man!!!!

    • Atz profile image
      Author

      Athul M R 3 years ago from Calicut, Kerala, INDIA

      Ah its my pleasure, Glad I could help you out :)

    • profile image

      bodhit 3 years ago

      Superb..helped a lot..

    • profile image

      rounak 3 years ago

      it is vry useful

    • profile image

      amir naveed 2 years ago

      Its very helpfull

    • profile image

      phillip 2 years ago

      thanks so much wat about a fibbonacci serries program using c#

    • profile image

      Hrishikesh 2 years ago

      Wish you have used much simpler variables

    Click to Rate This Article