how do you convert to binary.

i have to write a program that will prompt the user to enter an unlimited(user defined) number positive intergers, the program will convert each positive interger(base 10 value) into its equivalent binary representation and displat each conversation. to exit the program, the user is to ente a negative interger.

the largest enterger value to be used is 15, i also have to create an array of SIZE 4(all array position are to begin with the value '0' in each position).
prompt the user to enter an interger between 0 and 15;
convert the interger to its equivalent binary value;
display the new binary value.

can someone please help me with this and you show me an example.

>how do you convert to binary.
Try using printing 1 if val % 2 is not 0 and 1 otherwise, then divide val by 2 and repeat until val is 0. This should give you a good start toward the solution.

the program displays the binary code for integers up to 32,768 in

#include <stdio.h>
#include <stdlib.h>
void main( int argc, char *argv[] )
{
 int arg1, x;

 if( argc == 1 )
  {
  puts( "BINARY num = displays num in binary form." );
  exit( 1 );
  }

 arg1 = atoi( argv[1] );
 printf( "INTEGER: %i\n", arg1 );
 printf( "	HEX: %x\n", arg1 );
 printf( " BINARY:" );
 for( x = 15; x > 7; x-- )
  printf( " %i ", (arg1 & 1 << x ) > 0 ? 1 : 0 );

 printf( "  " );

 for( x = 7; x > -1; x-- )
  printf( " %i ", (arg1 & 1 << x ) > 0 ? 1 : 0 );

 puts( "" );
 puts( " 16 bit:15 14 13 12 11 10  9  8	7  6  5  4  3  2  1  0" );
 puts( "  8 bit: 7  6  5  4  3  2  1  0	7  6  5  4  3  2  1  0" );
 puts( "	Hex: |----4---|  |----3---|	|----2---|  |----1---|" );
 puts( "	Sum: 3  1											" );
 puts( "		 2  6  8  4  2  1								" );
 puts( "		 7  3  1  0  0  0  5  2	1					 " );
 puts( "		 6  8  9  9  4  2  1  5	2  6  3  1			" );
 puts( "		 8  4  2  6  8  4  2  6	8  4  2  6  8  4  2  1" );
 exit( 0 );
}

the program displays the binary code for integers up to 32,768 in

#include <stdio.h>
#include <stdlib.h>
void main( int argc, char *argv[] )
{
 int arg1, x;

 if( argc == 1 )
  {
  puts( "BINARY num = displays num in binary form." );
  exit( 1 );
  }

 arg1 = atoi( argv[1] );
 printf( "INTEGER: %i\n", arg1 );
 printf( "	HEX: %x\n", arg1 );
 printf( " BINARY:" );
 for( x = 15; x > 7; x-- )
  printf( " %i ", (arg1 & 1 << x ) > 0 ? 1 : 0 );

 printf( "  " );

 for( x = 7; x > -1; x-- )
  printf( " %i ", (arg1 & 1 << x ) > 0 ? 1 : 0 );

 puts( "" );
 puts( " 16 bit:15 14 13 12 11 10  9  8	7  6  5  4  3  2  1  0" );
 puts( "  8 bit: 7  6  5  4  3  2  1  0	7  6  5  4  3  2  1  0" );
 puts( "	Hex: |----4---|  |----3---|	|----2---|  |----1---|" );
 puts( "	Sum: 3  1											" );
 puts( "		 2  6  8  4  2  1								" );
 puts( "		 7  3  1  0  0  0  5  2	1					 " );
 puts( "		 6  8  9  9  4  2  1  5	2  6  3  1			" );
 puts( "		 8  4  2  6  8  4  2  6	8  4  2  6  8  4  2  1" );
 exit( 0 );
}

Yes, yes, do all of the work for the OP so that she doesn't need to think. Maybe the steady decline of the general public's IQ is your fault because of spoonfeeding. Do you think that writing bad code that does something similar to the homework problem makes you look any smarter? If so then you're wrong, it just makes you look gullible and ignorant.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.