Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums
Old 10-29-2006, 11:50 PM   #1 (permalink)
js2006
Recruit
 
Join Date: Oct 2006
Posts: 19
js2006 is on a distinguished road
Decimal - Binary

how can we write a program that takes a decimal number input from the user and displays its binary number equivalent in 12 bits on the screen in the c++.

Code:
Output

Enter a decimal number: 32
Its Binary Equivalent is :   000000100000
js2006 is offline   Reply With Quote
Old 10-30-2006, 12:14 AM   #2 (permalink)
js2006
Recruit
 
Join Date: Oct 2006
Posts: 19
js2006 is on a distinguished road
Java script for Decimal to Binary:
Code:
x = 75 ;	// x is the decimal number
y = “” ;	// y is the binary equivalent
while ( x > 0) {
	remainder = x % 2 ;
	quotient = Math.floor( x / 2 ) ;
	y = remainder + y ;
	x = quotient ;
}
document.write(“y = ” + y) ;
how could i do in C++ with displays its binary number equivalent in 12 bits on the screen in the c++
js2006 is offline   Reply With Quote
Old 10-30-2006, 12:46 PM   #3 (permalink)
toe_cutter
Code Monkey
 
Join Date: Aug 2002
Location: Boston, MA
Posts: 79
toe_cutter is on a distinguished road
Send a message via ICQ to toe_cutter Send a message via AIM to toe_cutter Send a message via Yahoo to toe_cutter
Code:
void printBin( long long var ) {
  int i;

  for (i=11; i>=0; i--)
    printf("%d", (var >> i) & 1);
  putchar('\n');
}
Opps this is in C, but should work similar in c++.
__________________
toe_cutter is offline   Reply With Quote
Old 10-30-2006, 07:34 PM   #4 (permalink)
js2006
Recruit
 
Join Date: Oct 2006
Posts: 19
js2006 is on a distinguished road
something for it

done something with it ,

but i need improvement and like to see my prog working with above output i mean 12 bits on screen. how to do it ?

Like to see C++ code plz

Code:
#include <iostream>
using namespace std;

int main() {
    int n;  // number to convert to binary

    while (cin >> n) {
        if (n > 0) {
            cout << n << " (decimal) = ";
    
            while (n > 0) {
                cout << n%2;
                n = n/2;
            }
    
            cout << " (binary) in reverse order" << endl;
        } else {
            cout << "Please enter a number greater than zero." << endl;
        }
    }
  return 0;
}//end main
js2006 is offline   Reply With Quote
Old 10-31-2006, 07:22 AM   #5 (permalink)
toe_cutter
Code Monkey
 
Join Date: Aug 2002
Location: Boston, MA
Posts: 79
toe_cutter is on a distinguished road
Send a message via ICQ to toe_cutter Send a message via AIM to toe_cutter Send a message via Yahoo to toe_cutter
My code works fine for c++. You just need to include library stdio.h.

Code:
#include <stdio.h>
#include <iostream>
using namespace std;

void printBin( int var ) {
  int i;

  for (i=11; i>=0; i--)
    printf("%d", (var >> i) & 1);
  putchar('\n');
}

int main() {
  int n;  // number to convert to binary

    while (cin >> n) {
        if (n > 0) {
            cout << n << " (decimal) = ";
            printBin(n);
            cout << " (binary) in reverse order" << endl;
        } else {
            cout << "Please enter a number greater than zero." << endl;
        }
    }
  return 0;
}
__________________
toe_cutter is offline   Reply With Quote
Old 10-31-2006, 08:58 AM   #6 (permalink)
redhead
Newbie
 
redhead's Avatar
 
Join Date: Jun 2002
Location: Denmark
Posts: 1,709
redhead is on a distinguished road
Quote:
You just need to include library stdio.h.
Or change printBin() to:
Code:
void printBin( int var ) {
  int i;
  for (i=11; i>=0; i--)
    cout << ((var >> i) & 1);
  cout << endl;
}
__________________
Don't worry Ma'am, We're university students, We know what We're doing.
-----
If you pull the pin, Mr.Grenade would no longer be your friend.
-----
01000111 01101111 00100000 01000011 00100000 00100001
redhead is offline   Reply With Quote
Old 10-31-2006, 09:40 AM   #7 (permalink)
toe_cutter
Code Monkey
 
Join Date: Aug 2002
Location: Boston, MA
Posts: 79
toe_cutter is on a distinguished road
Send a message via ICQ to toe_cutter Send a message via AIM to toe_cutter Send a message via Yahoo to toe_cutter
Quote:
Originally Posted by redhead View Post
Or change printBin() to:
Code:
void printBin( int var ) {
  int i;
  for (i=11; i>=0; i--)
    cout << ((var >> i) & 1);
  cout << endl;
}
Good call. I've been programming in C for so long now I have forgotten a bunch of c++ stuff.
__________________
toe_cutter is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
C#...Converting Binary Data to ASCII wvufreelancer MS Technologies ( ASP, VB, C#, .NET ) 2 05-28-2006 10:04 AM
Detecting binary image types with PHP . . possible? metazai PHP 6 02-16-2005 10:35 AM
a way to set decimal points limit? cracksevi Standard C, C++ 5 10-25-2004 01:44 PM
Binary I/O file reading (0x1a trouble) Danish Standard C, C++ 2 05-26-2003 10:02 AM


All times are GMT -8. The time now is 01:31 PM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting