Monday 14 January 2013

Let Us C Chapter 3 problem No: B-h


//Program to find octal equivalent of any number

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

using namespace std;

int convertor();
int main()
{
    cout<<"================================================================================\n";
    cout<<"                        Super Number Convertor (v3.7)";
    cout<<endl<<endl;
    cout<<"                                                Developed by: Azhar Ahmed";
    cout<<endl;
    cout<<"                                                            : Matthew Chan";
    cout<<endl<<endl;
    cout<<"                                                Designed by : Matthew Chan";
    cout<<endl;
    cout<<"================================================================================\n";
    cout<<endl;
    cout<<endl;
    system("color 33");

    return convertor();

}
void binary(long n) {
long converter;

if(n <= 1) {
cout << n;
return;
}

converter = n%2;
binary(n >> 1);
cout << converter;
}
int convertor(void)
 {
     long n,b,x,bina;
     char c;
     do
     {

     cout<<"Enter any decimal value:  ";

     if(cin>>n)
     {
         if(n<0)
         {
              cout<<"Error: Please enter valid decimal value"<<" TRY AGAIN\n";
            cin.clear(); /*-->  when some error encouner by wrong input from cin function of iostream the function cin became unstable so you need to clear this function's value */
            cin.ignore();//--> and than you need to ignore the previous state
            convertor();
         }
     }
     else
     {
             cout<<"Error: Please enter valid decimal value"<<" TRY AGAIN\n";
            cin.clear(); /*-->  when some error encouner by wrong input from cin function of iostream the function cin became unstable so you need to clear this function's value */
            cin.ignore();//--> and than you need to ignore the previous state
            convertor();
     }

        cout<<endl;
        cout<<"The Binary equivalent of "<<n<<" is:  ";
        binary(n);
        cout<<endl<<endl;

        cout<<"The Octal equivalent of "<<n<<" is:  ";
        printf("%o",n);
        cout<<endl<<endl;

        cout<<"The Hexadecimal equivalent of "<<n<<" is:  ";
        printf("%x",n);



     cout<<endl<<endl;
     cout<<"Do you want to convert another number?(y/n):  ";
     c=getche();
     cout<<endl<<endl;

     }

     while((c=='y') || (c=='Y'));
     cout<<"Thank you for using our application.";
     getch();
     exit(0);
 }

No comments:

Post a Comment

Comments please: