-->




Simple C++ program to convert binary decimal number

Leave a Comment
Cpp Tutorial Contains
  • Binary to Decimal Conversion Code in C++
  • Program Logic Explanation
  • Dry run the code with a simple input so the basic working of program can be understand easily
Concept Used
  • Know the basic math of converting one number to other number e.g BINARY to DECIMAL
  • functions 
  • while loop

Problem Statement
Write a C++ program which asks user to input a binary number then convert it into its equivalent decimal number and show the result. 
To improve readability of program make a separate function and function should return the resultant number.

C++ CODE
Compiler used: C++ codeBlocks Compiler

#include<iostream>
using namespace std;
long ConvertBinary2Decimal(unsigned long num);
int main()

{
    unsigned long binary_num, decimal = 0;
    cout <<"\n\tEnter a binary number to get 
    Equivalent Decimal : ";
    cin >> binary_num;
    decimal=ConvertBinary2Decimal(binary_num);
    cout <<"\n\tDecimal Value Of  
   " <<binary_num << " is = " << decimal << endl;
    return 0;

}

long ConvertBinary2Decimal(unsigned long bin_num)
{
    unsigned long decimal = 0;
    int remainder=0, base = 1;


    while (bin_num > 0)

    {
        remainder = bin_num % 10;
        decimal = decimal + remainder * base;
        bin_num = bin_num / 10;
        base = base * 2;
    }

return decimal;

}
Convert binary to decimal c++ code

Program Explanation
  • In main function user enters input number
  • Calls the function and pass the number 
  • Function calculates the required result and return
  • In main function program displays the final result

Dry Run of Code
Input = 101
--------------------------------
Iteration     = 1
 remainder  = 1
 decimal     = 1
 bin_num   = 10
 base          =  2
--------------------------------
Iteration     = 2
 remainder  = 0
 decimal     = 1
 bin_num   = 1
 base          =  4
--------------------------------
Iteration     = 3
 remainder  = 1 -> as 1 mod 10 =1
 decimal     = 5 -> previous value+remainder*base = 5 RESULT
 bin_num   =  0
 base          =  8
--------------------------------

Main logic of the program in while loop take another input and dry run it into a notebook. Best of Luck

See another example here
Read More...

C++ calculator code for beginner

Leave a Comment

Write a simple C++ program which have four different functions for basic operations like addition, subtraction, division and multiplication.
Program should be menu based asking user to enter option and if user enters a wrong option a proper message to enter right option should be displayed. 



Program explanation
  • Program has four different function for each calculator operation
  • do while loop is used to display menu options
  • if else statement is used to check option
  • Program has two variables on which all basic operations are applied 
  • Every functions receives two parameters and return result

    C++ source code compiler used Codeblocks


#include <iostream>
using namespace std;

int addition      (int num1, int num2);
int subtraction   (int num1, int num2);
int division      (int num1, int num2);
int multiplication(int num1, int num2);



int main()
{
    char option;
    int num1, num2, result;

    cout<<"\n\t\t\tSimple calculator"<<endl;
    do
    {

    cout<<"Enter + for Addtion\nEnter - for Subtraction\n
    Enter / for Division\nEnter * for Multiplication\n
    Enter E to EXIT\n";
    cout<<"\n\t\t\tEnter Option: ";
    cin>>option;

     if(option!='e'&&option!='E')
     {
     cout<<"\n\t\t\tEnter First Number: ";
     cin>>num1;
     cout<<"\n\t\t\tEnter Second Number: ";
     cin>>num2;
     }

    if(option!='+'&&option!='-'&&option!='/'
      &&option!='*'&&option!='e'&&option!='E')
    {
       cout<<"\n\t\t\tSELECT the VALID OPTION\n";
    }
    else if(option=='+')
    {
       result=addition(num1,num2);
       cout<<"\n\t\t\t"<<num1<<" + "<<num2<<" = "<<result<<endl;
    }
    else if(option=='-')
    {
       result=subtraction(num1,num2);
       cout<<"\n\t\t\t"<<num1<<" - "<<num2<<" = "<<result<<endl;
    }
    else if(option=='/')
    {
       result=division(num1,num2);
       cout<<"\n\t\t\t"<<num1<<" / "<<num2<<" = "<<result<<endl;

    }
    else if(option=='*')
    {
          result=multiplication(num1,num2);
       cout<<"\n\t\t\t"<<num1<<" X "<<num2<<" = "<<result<<endl;

    }



    }while(option!='e'&&option!='E');


 cout<<"\n\t\t\tProgram EXIT Successfully.......\n\t\t\t";
    return 0;
}
int addition(int num1, int num2)
{
    return (num1+num2);
}

int subtraction(int num1, int num2)
{

    return (num1-num2);
}

int division(int num1, int num2)
{
      if(num2==0)
      {
          cout<<"\n\t\t\tDivide by ZERO not allowed :"<<endl;
          return 0;
      }
    return (num1/num2);
}

int multiplication(int num1, int num2)
{
    return (num1*num2);
}


Program Output
simple calculator source code example

Find more examples here
C++ simple examples
Read More...

c++ program to find area of circle using functions

Leave a Comment
Cpp tutorial to calculate area of circle
  •  User enter radius or diameter of circle.
  •  Program should have two functions one which takes radius as parameter and second takes diameter as parameter.
  •  If user enters wrong option then tell users its valid selection and chose again the right option.
This Cpp Tutorial covers the concept of
  • if else if statement
  • for loop
  • function with parameter and return value
Cpp code compiler used: Codeblocks C++ Compiler

#include <iostream>
#define PI 3.14159
using namespace std;

float AreaOfCircle(float radius);
float AreaWithDiameter(float diameter);

int main()
{
    float radius,diameter,circleArea;
    char choice='0';
    cout<<"\n\t\t\tFind Area Of Circle:"<<endl;


    for(;choice!='1'&&choice!='2';)
        {
         cout<<"\nEnter 1 to Enter Radius OR
                 2 to Enter Diameter: ";
         cin>>choice;
         if(choice!='1'&&choice!='2')
         cout<<"\n\t\tEnter a VALID Option ";

        }

    if(choice=='1')
    {
    cout <<"\n\t\tEnter Radius To Find Area: ";
    cin>>radius;
    circleArea=AreaOfCircle(radius);
    }
    else if(choice=='2')
    {
    cout <<"\n\t\tEnter Diameter To Find Area: ";
    cin>>diameter;
    circleArea=AreaWithDiameter(diameter);

    }
    cout<<"\n\n\t\tArea of Circle is:->> "<<circleArea<<endl;
    return 0;
}

float AreaOfCircle(float radius)
{
    return (PI*(radius*radius));

}

float AreaWithDiameter(float diameter)
{

    return (AreaOfCircle(diameter/2));
}


output of program
calculate area of circle cpp tutorial

Program explanation
  • Program has two functions one for radius and one for diameter both called upon user choice
  • Both calculates the radius and diameter according to formula and return result value from where they have called
  • A for loop is used to prevent the wrong choice selection it will break only if user enters valid option 1 or 2 else it will continue to ask user to enter valid option
Read More...

c++ program to find prime numbers in a given range

Leave a Comment
Cpp tutorial to find prime numbers between given range
  • Write a c++ program in which user will enter start and end of range in integer 
  • Program should display every prime number between range and at the end total number of prime numbers found in range.
  • Do the same program with separate function which receives two parameters for range calculate result and display output like first code
  • Use any C++ compiler codeBlocks Recommended
Cpp Tutorial covers the following concepts

#include<iostream>
#include<math.h>
using namespace std;
int main()
{
    int startNum,endNum;
    int found=0,count=0;
    cout<<"Enter Number START of Range:  ";
    cin>>startNum;
    cout<<"Enter Number END of Range:  ";
    cin>>endNum;
    for(int i=startNum;i<=endNum;i++)
       {
           for(int j=2;j<=sqrt(i);j++)
               {
               if(i%j==0)
                  count++;
               }
               if(count==0&&i!=1)
               { found++;
                 cout<<"Prime Number -> "<<i<<endl;
                 count=0;
               }
               count=0;
       }

 cout<<"Total Prime Number Between Range "<<startNum<<" to
 "<<endNum<<" = "<<found<<endl;
 return 1;
}

Output : range is 1 to 10
 cpp tutorial to find prime numbers in a given range




same Code with a function



#include<iostream>
#include<math.h>
using namespace std;
void calculatePrime(int startNum, int endNum);

int main()
{
    int startNum,endNum;
    cout<<"Enter Number START of Range:  ";
    cin>>startNum;
    cout<<"Enter Number END of Range:  ";
    cin>>endNum;

    calculatePrime(startNum,endNum);
 return 1;
}

void calculatePrime(int startNum, int endNum)
{      int found=0,count=0;


    for(int i=startNum;i<=endNum;i++)
       {
           for(int j=2;j<=sqrt(i);j++)
               {
               if(i%j==0)
                  count++;
               }
               if(count==0&&i!=1)
               { found++;
                 cout<<"Prime Number -> "<<i<<endl;
                 count=0;
               }
               count=0;
       }

 cout<<"Total Prime Number Between Range "<<startNum<<" to
        "<<endNum<<" = "<<found<<endl;

}

Another cpp Tutorial here To Find Prime Number 
Find more Example Here Cpp Tutorials
Read More...