Navigation Bar

program to check entered character is small, capital, digit or a special character

C++ program which takes input a character and check it whether entered character is capital letter, small letter, Digit or Special character

All characters like small letterscapital lettersdigits or special character have ASCII codes when a key is pressed from keyboard there is always an ASCII value behind it like if small letter 'a' is pressed its ASCII is 97 if capital letter 'A' is pressed its ASCII value is 65 if a number digit '0' is pressed its ASCII value is 48.

ASCII value ranges 
 0 - 9      48 - 57
A - Z     65 - 90
a - z       97 - 122
Special Characters  0-47, 58-64, 91-96, 123-127

On the basis of ASCII values and using operators like and operator (&&) or Operator  (||) we can differentiate the letters.

Concept Used
C++ Code :
If you enter more that one character program will show the result on the basis of first charater



#include<iostream>
using namespace std;
int main()
{
  char character;
  cout<<"Enter a character =  ";
    cin>>character;

  int storeAscii=character;
  cout<<"The ASCII value  of "<<character<<
        " is "<<storeAscii;

  if (storeAscii>=65 && storeAscii<=90)
  {
    cout<<"\nYou have entered a capital letter";
  }

  else if (storeAscii>=97 && storeAscii<=122)
  {
    cout<<"\nYou have entered a small letter";
  }

  else if (storeAscii>=47 && storeAscii<=57)
  {
    cout<<"\nYou have entered a digit ";
  }

  else if (storeAscii>=&& storeAscii>=47
      || storeAscii>=54 && storeAscii<=64
      || storeAscii>=91 && storeAscii<=96 
      || storeAscii>=123 && storeAscii<=127)
  {
    cout<<"\nYou have entered a special character";
  }

return 0;
}

Sample Input:
C++ program to check the entered character is a capital letter, a small letter, a digit or a special character




This Program helps to understand the operators like OR operator(||) how if else works, and ASCII values of characters

Find Armstrong Number in C++ code with logic explanation and code dry run

What is Armstrong number?
 A number in which the sum of cube of its individual digits is equal to the number itself is called Armstrong number
For Example: 1^3 + 5^3 + 3^3 = 153
4 * 4 * 4 + 0 * 0 * 0 + 7 * 7 * 7 = 407 is an Armstrong number.


C++ program which takes input a number and check whether it is Armstrong Number or not



  #include<iostream>
  using namespace std;
  int main()
  {
  int armstrong=0,num=0,result=0,check;
  cout<<"Enter Number to find it is an Armstrong number?";
       cin>>num;
       check=num;
       for(int i=1;num!=0;i++){
           armstrong=num%10;
           num=num/10;
           armstrong=armstrong*armstrong*armstrong;
           result=result+armstrong;
       }
       if(result==check){
       cout<<check<<"  is an Armstrong Number";
       }
       else{
       cout<<check<<"  is NOT an Armstrong Number";
       }
       return 0;
    }



 Note: Program can be coded in more than one ways above program is very simple so any one can understand the logic of program.

Recommended: Change program logic and do experiment with it for fast learning.

Image view of the program click on image to view large
Armstrong number code in c++ programming
Find Armstrong number in c++ code


Logic Explanation:

Concept Used:  for loopif-else statement
  • To make logic firstly concept about Armstrong number should be veryclear
  • We took some variables in which we take input, make calculations and produce results
  • Program take input number in variable num and store it in checkvariable
  • In for loop we take mod of num with 10 and stores it in variable Armstrong then we divide it with 10(below dry running will make moreclear the working)
  • Then we take cubes of Armstrong variable and add it into result variable
  • When num=0; then for loop break after for loop using if else statement we test if our result equals to check variable then number is Armstrong else not.

Dry Running The Armstrong number Code:

Let input is equal to 153 

variable values before for loop
num=153; check=153; Armstrong=0; result=0;
variable values in for loop line by line
for i=1
Armstrong=3;
 num=15;
Armstrong=3*3*3=27 
result=0+27=27;

for loop condition check: num is not equal to zero loop will run again

for i=2
Armstrong=5;
num=1;
Armstrong=5*5*5=125 
result=27+125=152;

for loop condition: num is not equal to zero loop will run again

for i=3
Armstrong=1;
num=0;
Armstrong=1*1*1=1; 
result=152+1=153;


for loop condition: num is EQUAL TO ZERO loop will run again

Loop will break and if else condition will be checked as or result=153 and check=153 
if condition will true and program will show output
 153 is an Armstrong Number

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