שאלה 6
Back Up

 

פתרון

/*
  Module: tr2_q6.cpp
  Author: Yariv Amar
  What?
  This program converts integer numbers to binary.
  Input : Integer number 0-31.
  Output: Binari code in 5 bit.
  Assumptions on input : Valid
  How?
  Algorithm : calculating MSB first (from left to right)
    
*/
#include <iostream.h>
void main()
{
	int Number,Bit1,Bit2,Bit3,Bit4,Bit5; //declering variables
	cout<<"Please enter a number (0-31): ";
	cin>>Number;
	cout<<"-----------------------------\n";
	Bit1 = Number / 16;			//**************************
	Number = Number - Bit1 * 16;		//*   Converting process  *
	Bit2 = Number / 8;			//*              	    *
	Number = Number - Bit2 * 8;		//* starting from 2^4 and  *
	Bit3 = Number / 4;			//*                       	    *
	Number = Number - Bit3 * 4;		//* getting down to 2^0    *
	Bit4 = Number / 2;			//*                        	    *
	Number = Number - Bit4 * 2;		//*                        	    *
	Bit5 = Number / 1;			//*                        	    *
	Number = Number - Bit5 * 1;		//**************************
	cout<<"The binary Number is: "<<Bit1<<Bit2<<Bit3<<Bit4<<Bit5<<'\n';
}

פלטים מיצגים

פלט לדוגמה:

 

            Back Up

 

 

© Copyright YAProductions 2000