Estratto del documento

Programma che converte un numero decimale in un numero esadecimale.

#include <iostream>

#include <math.h>

using namespace std;

int main()

{ int num;

int n=0;

char resti[n];

int resto;

cout<<"Inserisci il numero decimale."<<endl;

cin>>num;

while (num>0)

{ resto=num%16;

switch (resto)

{

case 0:

resti[n]='0';

break;

case 1:

resti[n]='1';

break;

case 2:

resti[n]='2';

break;

case 3:

resti[n]='3';

break;

case 4:

resti[n]='4';

break;

case 5:

resti[n]='5';

break;

case 6:

resti[n]='6';

break;

case 7:

resti[n]='7';

break;

case 8:

resti[n]='8';

break;

case 9:

resti[n]='9';

break;

case 10:

resti[n]='A';

break;

case 11:

resti[n]='B';

break;

case 12:

resti[n]='C';

break;

case 13:

resti[n]='D';

break;

case 14:

resti[n]='E';

Anteprima
Vedrai una selezione di 1 pagina su 2
C++ - Conversione da decimale ad esadecimale Pag. 1
1 su 2
Sintesi

C++ - Conversione da decimale ad esadecimale

Codice C++ del programma:

//Programma che converte un numero decimale in un numero esadecimale. #include &lt;iostream&gt; #include &lt;math.h&gt; using namespace std; int main() { int num; int n=0; char resti[n]; int resto; cout&lt;&lt;"Inserisci il numero decimale."&lt;&lt;endl; cin&gt;&gt;num; while (num&gt;0) { resto=num%16; switch (resto) { case 0: resti[n]='0'; break; case 1: resti[n]='1'; break; case 2: resti[n]='2'; break; case 3: resti[n]='3'; break; case 4: resti[n]='4'; break; case 5: resti[n]='5'; break; case 6: resti[n]='6'; break; case 7: resti[n]='7'; break; case 8: resti[n]='8'; break; case 9: resti[n]='9'; break; case 10: resti[n]='A'; break; case 11: resti[n]='B'; break; case 12: resti[n]='C'; break; case 13: resti[n]='D'; break; case 14: resti[n]='E'; break; case 15: resti[n]='F'; break; default: break; } num=num/16; n++; } n--; cout&lt;&lt;"Il numero convertito e':"&lt;&lt;endl; while (n&gt;=0) { cout&lt;&lt;resti[n]; n--; } cout&lt;&lt;endl; return 0; }

D/illustrazione/soddisfatti o rimborsati
Acquista con carta o PayPal
Scarica i documenti tutte le volte che vuoi