Friday, August 19, 2011

how to convert decimal to binary and binary to decimal


HOW TO CONVERT DECIMAL TO BINARY AND BINARY TO DECIMAL

For converting a number from decimal to binary we use repeated division by 2 method. We continue the repeated division by 2 till we get the number less than 2. For example:

Repeated division method:
Divide the decimal number by 2 and store remainder at one place. Continue this process till the decimal number is less than 2. Finally write the remainders in reverse order:

You will understand this concept by this example:
 If we want to convert decimal number 65 to binary, we can do this as following:

Divisor 2
Decimal Number and quotients
Remainder
2
65
2
32
1
2
16
0
2
8
0
2
4
0
2
2
0
2
1
0

0
1
   
Now writing the remainders in reverse order we get 1000001
So Decimal number 65 is 1000001 in binary
One more example:
Convert 45 from decimal to binary
 
Divisor 2
Decimal Number and quotients
Remainder
2
45

2
22
1
2
11
0
2
5
1
2
2
1
2
1
0
2
0
1

Writing the remainders in reverse order we get 101101 which is binary equivalent of 45.

Now if we want to convert vice versa i.e. from binary to decimal, we can use the following method:

Take each digit of binary number from right to left and multiply the digit with 2n in increasing order. Here n starts with 0 and increases by 1. Finally add the individual terms and get the decimal number. You will understand this concept by the following example:


Converting 101101 to decimal

1x25+0x24+1x23+1x22+0x21+1x20

=32+0+8+4+0+1
=45
So the decimal equivalent of 101101 is 45

I hope you have understood the process of converting decimal to binary and vice versa. Your comments are welcome

Thursday, August 18, 2011

Quick sort program in C++

//Quick sort program in C++
#include<conio.h>

#include<iostream.h>
#include<process.h>


void quickSort(int numbers[], int array_size);
void q_sort(int numbers[], int left, int right);


int numbers[5];


int main()
{
   clrscr();
   int i,n;
   cout<<"How many numbers you want to sort: ";
   cin>>n;
   cout<<"Enter "<<n<<" numbers.\n";
   for (i = 0; i<n; i++)
      cin>>numbers[i];
//perform quick sort on array
   q_sort(numbers,0,n-1);


   cout<<"Numbers are sorted\n";
   for (i = 0; i<n; i++)
      cout<<numbers[i]<<"   ";
   getch();
   return(0);
}


// Function to sort
void q_sort(int numbers[], int left, int right)
{
   int pivot, l_hold, r_hold;
   l_hold = left;
   r_hold = right;
   pivot = numbers[left];
   while (left < right)
   {
      while ((numbers[right] >= pivot) && (left < right))
      right--;
      if (left != right)
      {
numbers[left] = numbers[right];
left++;
      }
      while ((numbers[left] <= pivot) && (left < right))
left++;
      if (left != right)
      {
 numbers[right] = numbers[left];
 right--;
      }
   }
   numbers[left] = pivot;
   pivot = left;
   left = l_hold;
   right = r_hold;
   if (left < pivot)
      q_sort(numbers, left, pivot-1);
   if (right > pivot)
      q_sort(numbers, pivot+1, right);
}

C++ program to convert decimal to binary


//program to find binary equivalent of a decimal no.
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int arr[10];
int ctr=0;
long int n,quo=0,rem=0;
cout<<"Enter A No. "<<endl;
cin>>n;
for(quo=n;quo!=0;ctr++)
{
rem=quo%2;
arr[ctr]=rem;
quo=quo/2;
}
cout<<"Binary Equivalent Of "<<n<<" is-----> ";
for(ctr=ctr-1;ctr>=0;ctr--)
{
cout<<arr[ctr];
}
getch();
}//end main

C++ program to convert binary to decimal

C++ program to convert binary to decimal

//program to convert a binary no. into decimal no.
#include<iostream.h>
#include<conio.h>
#include<math.h>
void main()
{clrscr();
long int n,x;
cout<<"Please enter a binary no. "<<endl;
cin>>n;
x=n;
int rem=0,sum=0;
for(int ctr=0;n>0;ctr++)
{ rem=n%10;
  rem=rem*(pow(2,ctr));
  sum=sum+rem;
  n=n/10;
}
cout<<"Decimal Equivalent of "<<x<<" is "<<sum<<endl;
getch();
}//end main

Wednesday, August 17, 2011

C++ program to convert numbers to words

If you are looking for a C++ program to convert numbers to words, you can find a good C++ program here which can convert numbers to words up to 5 digits.

here is the program:

#include<iostream.h>
#include<conio.h>
void single(int);
void ten2ninety(int);
void elev2nine(int);
void dig2word(int);
void main()
{
clrscr();
int a;
cout<<"\n Enter a number\n";
cin>>a;
dig2word(a);
getch();
}
void dig2word(int y)
{
int s=y,ctr=0,ldig,sdig,l2dig,l3dig,l3rddig;
while(y>0)
{
y=y/10;
++ctr;
}
if(ctr==1)
{
single(s);
return;
}
else if(ctr==2)
{
ldig=s%10;
s=s/10;
sdig=s%10;
 if(ldig==0)
 {
   ten2ninety(sdig);
   return;
 }
 if(sdig==1)
 {
 elev2nine(ldig);
 return;
 }
ten2ninety(sdig);
single(ldig);
return;
}
       else if(ctr==3)
       {
l2dig=s%100;
s=s/100;
sdig=s%10;
if(l2dig==0)
{
single(sdig);
cout<<" Hundred";
return;
}
else
{
single(sdig);
cout<<" Hundred";
dig2word(l2dig);
return;
}
       }
       else if(ctr==4)
       {
l3dig=s%1000;
s=s/1000;
sdig=s%10;
if(l3dig==0)
{
single(sdig);
cout<<" Thousand";
return;
}
else
{
single(sdig);
cout<<" Thousand";
dig2word(l3dig);
return;
}
       }
      else if(ctr==5)
       {
l2dig=s%100;
l3dig=s%1000;
s=s/100;
l3rddig=s%10;
s=s/10;
if(l2dig==0&&l3rddig==0)
{
dig2word(s);
cout<<"Thousand";
return;
}
dig2word(s);
cout<<"Thousand";
dig2word(l3dig);
return;
       }
}
void single(int x)
{
 switch(x)
 {
  case 0:cout<<" Zero ";break;
  case 1:cout<<" One ";break;
  case 2:cout<<" Two ";break;
  case 3:cout<<" Three ";break;
  case 4:cout<<" Four ";break;
  case 5:cout<<" Five ";break;
  case 6:cout<<" Six ";break;
  case 7:cout<<" Seven ";break;
  case 8:cout<<" Eight ";break;
  case 9:cout<<" Nine ";break;
 }
}
void elev2nine(int x)
{
 switch(x)
 {
  case 1:cout<<" Eleven ";break;
  case 2:cout<<" Tweleve ";break;
  case 3:cout<<" Thirteen ";break;
  case 4:cout<<" Fourteen ";break;
  case 5:cout<<" Fifteen ";break;
  case 6:cout<<" Sixteen ";break;
  case 7:cout<<" Seventeen ";break;
  case 8:cout<<" Eighteen ";break;
  case 9:cout<<" Nineteen ";break;
 }
}
void ten2ninety(int x)
{
 switch(x)
 {
  case 1:cout<<" Ten ";break;
  case 2:cout<<" Twenty ";break;
  case 3:cout<<" Thirty ";break;
  case 4:cout<<" Forty ";break;
  case 5:cout<<" Fifty ";break;
  case 6:cout<<" Sixty ";break;
  case 7:cout<<" Seventy ";break;
  case 8:cout<<" Eighty ";break;
  case 9:cout<<" Ninety ";break;
 }
}
 
The above program works best for integer limit that means up to 32767 but if you want up to 99999. convert all "int" to "long int"

C++ program to print armstrong numbers between 1 to 1000


Are you looking for the armstrong numbers program in C++? if yes, read up on to know and learn the armstrong numbers program in C++.

What is armstrog number?
An armstrong number is that number whose digits when raised to power 3 and added produce the number itself for example:

153 = 13 + 53 + 33

The following program prints armstrong numbers between 1 to 1000.


#include<iostream.h>
#include<conio.h>
void arm(int);

void main()
{
                for(int i=1;i<=1000;++i)
                {
                 arm(i);
                }
}
void arm(int a)
{
int b,rem,sum=0;
b=a;
     while(a>0)
     {
      rem=a%10;
      sum=sum+(rem * rem * rem);
      a=a/10;
     }
if(sum==b)
cout<<"Armstrong Num "<<b<<"\n";
}

C++ program to find lcm and hcf


If you are looking for a C++ program to find lcm and hcf of 2 numbers, you will find the program here. The following program accepts 2 numbers and finds lcm and hcf of those numbers. 


Here is the program:

#include<iostream.h>
#include<conio.h>
int lcm(int,int);
int hcf(int,int);
void main()
{
clrscr();
int a,b,l,h;
cout<<"\n Enter 2 numbers\n";
cin>>a>>b;
l=lcm(a,b);
h=hcf(a,b);
cout<<"LCM:"<<l<<"\t"<<"HCF:"<<h;
getch();
}
int lcm(int x,int y)
{
  int i,big;
   big=(x>y)?x:y;


for(i=big;i<=x*y;++i)
{
if(i%x==0&&i%y==0)
{
return i;
}
}
return 0;
}
int hcf(int x,int y)
{
  int i,small,max=1;
  small=(x<y)?x:y;


for(i=1;i<=small;++i)
{
if(x%i==0&&y%i==0)
{
max=i;
}
}
return max;
}



C++ program to print pythagoran triplets between 1 to 1000


If you are looking for a C++ program which prints pythagoran triplets between 1 to 1000, you will find the program here.

What is pythagoran triplet?
A group of 3 numbers a,b,c where c2=a2+b2.


Here is the program: 

#include<iostream.h>
#include<conio.h>
void pytha(long int,long int,long int);
void main()
{   clrscr();
    long int i,j,k;
for(i=1;i<=100;++i)
{
for(j=1;j<i;++j)
{
   for(k=1;k<j;++k)
   {
pytha(i,j,k);
   }
}
}
getch();
}
void pytha(long int a,long int b,long int c)
{
if((a*a)==(b*b)+(c*c))
cout<<" Pythagoran triplets-->"<<a<<" "<<b<<" "<<c<<"\n";
}