Sunday, 16 March 2014

Convert Binary to Decimal or Decimal to Binary

C++ Program to convert Binary number to Decimal number or vice versa.This program converts either binary number entered by user to decimal number or decimal number entered by user to binary number in accordance with the character entered by user.This program asks user to enter alphabet 'b' to convert
decimal number to binary and alphabet 'd' to convert binary number to decimal.
In accordance with the character entered, user is asked to enter either binary value to convert to decimal or decimal value to convert to binary.To perform conversion, two functions are made decimal_binary(); to convert decimal to binary and binary_decimal(); to convert binary to decimal. Decimal number entered by user is passed to decimal_binary() and this function computes the binary value of that number and returns it main() function. Similarly, binary number is passed to function binary_decimal() and this function computes decimal value of that number and returns it to main() function.

Source Code:-

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

int binary_decimal(int n);
int decimal_binary(int n);
int main()
{  clrscr();
   int n;
   char c;
   cout<<"Convet Decimal to Binary & vice versa by-Tarun Rawat\n\n";
   cout<<"1. Enter alphabet 'd' to convert binary to decimal.\n";
   cout<<"2. Enter alphabet 'b' to convert decimal to binary.\n";
   cin>>c;
   if (c =='d' || c == 'D')
   {   cout<<"\nEnter a binary number : ";
       cin>>n;
       cout<<n<<" in binary = "<<binary_decimal(n)<<" in decimal";
   }
   if (c =='b' || c == 'B')
   {   cout<<"\nEnter a decimal number : ";
       cin>>n;
       cout<<n<<" in decimal = "<<decimal_binary(n)<<" in binary";
   }
getch();
}

int decimal_binary(int n)  /* Function to convert decimal to binary.*/
{   int rem, i=1, binary=0;
    while (n!=0)
    {   rem=n%2;
    n/=2;
    binary+=rem*i;
    i*=10;
    }
    return binary;
}

int binary_decimal(int n) /* Function to convert binary to decimal.*/
{  int decimal=0, i=0, rem;
    while (n!=0)
    {   rem = n%10;
    n/=10;
    decimal += rem*pow(2,i);
    ++i;
    }
    return decimal;
}

Saturday, 15 March 2014

Check number is Even or Odd

C++ Program to check the number is Even or Odd:Numbers perfectly divisible by 2 are known even numbers and numbers which are not divisible by 2 are called odd numbers.This program takes an integer from user and checks whether that number is even or odd and displays the result.In this program, user is asked to enter an integer which is stored in variable a. Then, the remainder is found when that number is divided by 2 and checked whether remainder is 0 or not.
If remainder is 0 then, that number is even otherwise that number is odd. This task is performed using if...else statement in C programming and the result is displayed accordingly. 


Source Code:-

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

void main()
{clrscr();
 int a;
 cout<<"Check Number is Even or Odd by-Tarun Rawar\n";
 cout<<"\nEnter number to  check : ";
 cin>>a;
 if(a%2==0)
  cout<<"\n"<<a<<" is a Even number ";
 else cout<<"\n"<<a<<" is a Odd number ";
getch();
}


check Armstrong number

 C++ Program to check the input number is Armstrong number or not.
Armstrong number c++ program:  A number is armstrong if the sum of cubes of individual digits of a number is equal to the number itself. For example 371 is an armstrong number as 33 + 73 + 13 = 371. Some other armstrong numbers are: 0, 1, 153, 370, 407. 


Source Code:-


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

void main()
{ clrscr();
  int temp,num,sum=0,r;
  cout<<"Program to check Armstrong condition by-Tarun Rawat\n\n";
  cout<<"Enter a number : ";
  cin>>num;
  temp=num;
  while(num!=0)
       {r=num%10;
       num=num/10;
       sum=sum+(r*r*r);
       }
  if(sum==temp)
    cout<<endl<<temp<<" is a Armstrong number. \n";
  else cout<<endl<<temp<<" is not a Armstrong number./n";
getch();
}