Navigation Bar

Find Palindrome Number in C++

What is a palindrome number?
If the digits of a numbers reversed and number remain the same then it is called palindrome number

For example:
Digits from 0 to 9 are palindrome numbers and
22 ,33, 44, 121, 12321, 131 etc..
for more information click here palindrome number concept by Wikipedia

Simple c++ code to find number is palindrome or not:


  1. #include<iostream>
  2. using namespace std;
  3. int main()
  4. {
  5.     int palindrome, reverse=0;
  6.     cout<<"Enter number:  ";
  7.     cin>>palindrome;
  8.     int num=0,key=palindrome;
  9. for(int i=1;palindrome!=0;i++){

  10.     num=palindrome%10;
  11.     palindrome=palindrome/10;
  12.     reverse=num+(reverse*10);
  13.               }

  14.    if(reverse==key){
  15.    cout<<key<<" is a Palindrome Number";
  16.             }
  17.             else{
  18.    cout<<key<<"is NOT a Palindrome Number";
  19.             }
  20. return 0;
}
 Dry Running The Code with respect to variable values and iteration
Let input is 121
  1. Before for loop variable values are
  2. palindrome=121;
  3. key=121;
  4. reverse=0;
  5. num=0;

After for loop


1st iterationwhen   i = 1

  1. num=1;
  2. palindrome=12;
  3. reverse=1;
2nd iterationwhen   i = 2

  1. num=2;
  2. palindrome=1;
  3. reverse=12;
3rd iteration
when   i = 3

  1. num=1;
  2. palindrome=0;
  3. reverse=121;
So palindrome=0; loop will break and program control will transfer to if else statement
Image View Of Code: click on image to view large

c++ program to find palindrome number
Palindrome number in c++

Logic Explanation:
  • We already known that if reverse of a number is equal to the same number than it is palindrome number.
  • Keeping in mind this certain variables and a loop use to get the reverse of a number which stores in variable 'reverse'.
  • After that using if else we check if reverse is equal to original number than it is a palindrome number otherwise it is not a palindrome number.

C++ Program to find Fibonacci series with simple logic

Fibonacci Series or Sequence
 The Fibonacci numbers or Fibonacci series or Fibonacci sequence has first two numbers equal to 1 and 0 and the further each number is consist of the addition of previous two numbers
  1st number = 0
  2nd number = 1
  3rd number = 0+1= 1
  4th number = 1+1= 2
  5th number = 1+2= 3
  And so on.  

 The Fibonacci Sequence can be written as  0,1,1,2,3,5,8,13........



 C++ program to find Fibonacci Series upto given range.This C++ programuse simple logic to get the concept of writing code in C++ for Fibonacci Series
  1.     #include<iostream>
  2.     using namespace std;
  3.     int main()
  4.     {
  5.        int range, first = 0, second = 1, fibonicci=0;
  6.        cout << "Enter Range for Terms of Fibonacci Sequence: ";
  7.        cin >> range;
  8.        cout << "Fibonicci Series upto " << range << " Terms "<< endl;
  9.        for ( int c = 0 ; c < range ; c++ )
  10.        {
  11.           if ( c <= 1 )
  12.              fibonicci = c;
  13.           else
  14.           {
  15.              fibonicci = first + second;
  16.              first = second;
  17.              second = fibonicci;
  18.           }
  19.           cout << fibonicci <<" ";
  20.        }
  21.        return 0;
  22.     }

  Fibonacci Series program logic explanation
  • Before writing C++ code we understand that what is Fibonacci Seriesand in what sequence numbers are occurs
  • After understanding mathematically Fibonacci series completely we thought it in the form of C++ code
  • The Fibonacci series is infinite but we can't make a program that display an infinite output
  • In above program we take input the range in integer upto which Fibonacci series will be displayed
  • For this purpose a for loop has been taken which starts from 0 and terminates less than range for example if Input is 5 then for loop will run from 0 to 4.
  • In for loop if  variable 'c' is  less or equal than 1 in this case if statement  will be executed  and if  'c' is greater than 1 else part will be executed for greater than 1
   e.g if range=2 than only
   if part will run.   let input is equal to 5
   Before loop variables values


   first = 0, second = 1, Fibonacci=0;
   After input range=5
   Values are changing in the following sequence
   in below table  ' c '  representing the for loop iterations
   c    first    second   Fibonacci    Output
   

   0     0         1                 0           0 

   1     0         1                 1           0 1
  
   2     0         1           0+1=1         0 1 1
        
          1          1                1           

   3     1         1           1+1=2         0 1 1 2
     
          1         2                  2

   4    1         2             1+2=3         0 1 1 2 3  its final output
   
         2         3                  3       


     

Image View Of  Program (Click on Image To Enlarge)
Fibonacci series in C++ image Code

Fibonacci Series In C++ Programming
Fibonacci series C++ Code

C++ Program to find the area and perimeter of rectangle

What is meant by quadrilateral?
     A shape which has four straight sides is called quadrilateral shape

What is meant by Rectangle or Parallelogram?

    Such a quadrilateral which has four right angles (angle 90 Degree) triangle is called rectangle

Formula: Area of rectangle:  height*width

 Perimeter of rectangle:  2*(height+width)


C++ program to find the Area and Perimeter of a Rectangle:


#include<iostream>

using namespace std;

int main()

{

    int width,height,area,perimeter;

    cout<<"Enter  Width of Rectangle = ";

    cin>>width;

    cout<<"Enter  Height of Rectangle = ";

    cin>>height;

    area=height*width;

    cout<<"Area of Rectangle ="<<area<<endl;

    perimeter=2*(height+width);

cout<<" Perimeter of rectangle are = "<<perimeter<<endl;

return 0;

}
 

Sample Output:


Image view of Code:

Program to find greatest number between 3 number using if-else-if statement

Program description 
This program shows the greatest number between three numbers using if-else-if statement. It takes three numbers as input from user and output the greatest number.

code:



  1. #include<iostream>
  2. using namespace std;
  3. int main()
  4. {
  5. int num1,num2,num3;
  6. cout<<" Enter value for first number";
  7. cin>>num1;

  8. cout<<" Enter value for second number";
  9. cin>>num2;

  10. cout<<" Enter value for third number";
  11. cin>>num3;
  12. if(num1>num2&&num1>num3)
  13. {
  14. cout<<" First number is greatest:"<<endl<<"whick is= "<<num1;
  15. }
  16. else if(num2>num1&&num2>num3)
  17. {
  18. cout<<" Second number is greatest"<<endl<<"whick is= "<<num2;
  19. }
  20. else
  21. {
  22. cout<<" Third number is greatest"<<endl<<"whick is= "<<num3;
  23. }
  24. return 0;
  25. }

Note : If you have any query or idea about program 
Comment below.

Program Take Hours, Minutes, Seconds And Print It In 24 Hours & 12 Hours Format

C++ program to display hours minutes and seconds in both  12 and 24 hours format:
24 Hours format : 23:30:12
Standard format : 11:30:12 pm
C++ CODE using Turbo C++ IDE.



  1. #include<iostream.h>
  2. #include<conio.h>
  3. void main()
  4. {
  5.    clrscr();
  6.    int hours,mins,seconds,x;                         
  7.    cout<<"Enter hours=";
  8.    cin>>hours;
  9.    cout<<"\nEnter minutes=";
  10.    cin>>mins;
  11.    cout<<"\nEnter seconds=";
  12.    cin>>seconds;
  13.    if(hours > 24)
  14.     {
  15.      cout<<"Invalid Entery";
  16.    }
  17.    else
  18.    {
  19.        cout<<"\n24 Hours Format\n";
  20. cout<<"Hours  :  Mins  :  Seconds\n"<<"  "<<hours<<"  :     "<<mins<<"   :     "<<seconds<<"\n";
  21.   if(hours > 12)
  22. {
  23.   hours=hours-12;
  24.   cout<<"12 Hours Format\n";
  25. cout<<"Hours  :  Mins  :  Seconds\n"<<"  "<<hours<<"  :     "<<mins<<"   :     "<<seconds;
  26.   }
  27.        else
  28.   {

  29.  cout<<"12 Hours Format\n";

  30.  cout<<"Hours  :  Mins  :  Seconds\n"<<" "<<hours<<": "<<mins<<"   :        "<<seconds;
  31.   }
  32.    }
  33. }                            // end of main

Find GCD of two numbers

What is GCD of two numbers?
 It means a greatest number which divides both numbers
For example: Two numbers are 18 and 24
Numbers which divides both are 1, 2, 3 and  6 in which greatest number is 6 
So 6 is the GCD of 18 and 24 

Concept used: for loop, if statement
Post contains: 
  • C++ code
  • Program logic explanation
  • Variable values after each iteration (Dry run the code)
  • image view of code
  • sample output




#include<iostream>
using namespace std;

int main() {

int first_number;
cout<<"Enter First Number : ";cin>>first_number;

int  second_number;
cout<<"Enter Second Number: ";cin>>second_number;

int  gcd;
for(int i=1;i<=first_number&&i<=second_number;i++){


     if(first_number%i==&& second_number%i == ){

                     gcd=i;

   }

}

cout<<"Greatest Common Divison (GCD):"<<gcd<<endl;
return 0;
}






Logic Explanation:

  • Program takes input two numbers
  • A for loop which terminates if loop variable which is 'i'
    is greater than one of two numbers and operator is used to
    maintain this condition
  • Within for loop an if condition is used to check if variable 'i'divides both of them it will be saved in  GCD variable as value of 'i' is changing during eachiteration in increasing order if another number divides both of them it will replace the previous value of GCD
  • After the loop termination our program will show the GCD of two number as the result


 Image view of code:

find GCD of two numbers c++ program
find GCD of two numbers c++ program

Sample output image view:





Sample input with dry run of code:
Let our sample input is 9 and 24

Values before loop
first_number=9 second_number=24

for i=1 

   i<=9&&i<=24    condition is true

          9%1==0 && 24%1==0 true
  
                     GCD=1

 
for i=2 

   i<=9&&i<=24    condition is true

          9%2!=0 && 24%2==0   false
  
                     GCD=1
         
for i=3 

   i<=9&&i<=24    condition is true

          9%3==0 && 24%8==0 true

                     GCD=3
for i=4 

   i<=9&&i<=24    condition is true

          9%4!=0 && 24%4==0 false

                     GCD=3

for i=5 

   i<=9&&i<=24    condition is true

          9%5!=0 && 24%5!=0 false

                     GCD=3


for i=6 

   i<=9&&i<=24    condition is true

          9%6!=0 && 24%6==0 false

                     GCD=3


for i=7 

   i<=9&&i<=24    condition is true

          9%7!=0 && 24%7!=0 false

                     GCD=3


for i=8 

   i<=9&&i<=24    condition is true

          9%8!=0 && 24%8==0 false

                     GCD=3

for i=9 

   i<=9&&i<=24    condition is true

          9%9==0 && 24%!9=0 true

                     GCD=3


for i=10

   i<=9&&i<=24    CONDITION IS FALSE

Final output  :

Greatest Common Divison (GCD): 3

Recommended: Change the logic and do experiment with code 
for better understanding.