Advertisement
Dr4noel

Transformari din zecimal in binar,octa si hexa

Jan 10th, 2018
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.43 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <conio.h>
  3. #include <stdlib.h>
  4.  
  5. void inHexa(long int n) {
  6.     int i = 1, j, temp, r, q;
  7.     char numarHexadecimal[100];
  8.  
  9.     q = n;
  10.     while (q != 0) {
  11.         temp = q % 16;
  12.  
  13.         //Convertim numarul intreg in caracter
  14.  
  15.         if (temp < 10) {
  16.             temp = temp + 48;
  17.         }
  18.         else {
  19.             temp = temp + 55;
  20.         }
  21.         numarHexadecimal[i++] = temp;
  22.         q = q / 16;
  23.     }
  24.     printf("\nNumarul %d in hexadecimal este ", n);
  25.     for (j = i - 1; j > 0; j--)
  26.         printf("%c.", numarHexadecimal[j]);
  27.  
  28. }
  29.  
  30. void inBinar(int n) {           // prin recursivitate, convertim numarul in baza 2
  31.     if (n / 2 != 0) {
  32.         inBinar(n / 2);
  33.     }
  34.     printf("%d", n % 2);
  35. }
  36.  
  37. void main() {
  38. AGAIN:
  39.  
  40.     long int n;
  41.     char conversie, continuare;
  42.     int i = 1, j, temp;
  43.     char numarHexadecimal[100];
  44.  
  45.     printf("Introduceti un numar: \n");
  46.     scanf_s("%ld", &n);
  47.  
  48.     printf("Introduceti tasta: \n~\"o\" - octal \n~\"h\" - hexadecimal:\n~\"b\" - binar\n");
  49.  
  50.     conversie = _getch();
  51.  
  52.     if (conversie == 'o') {
  53.         printf("\nNumarul %d in octal este %o .", n, n);
  54.     }
  55.     else
  56.         if (conversie == 'h') {
  57.             inHexa(n);
  58.         }
  59.         else if (conversie == 'b') {
  60.             printf("\nNumarul %d in binar este: ", n);
  61.             inBinar(n);
  62.         }
  63.         else {
  64.             printf("\nVa rog sa reintroduceti tasta!");
  65.  
  66.             conversie = getchar();
  67.         }
  68.  
  69.         printf("\n\nDoriti sa reluati programul?(y/any key)");
  70.         continuare = _getch();
  71.         if (continuare == 'y') {
  72.             system("CLS");
  73.             goto AGAIN;
  74.         }
  75.         else {
  76.             exit(0);
  77.         }
  78.    
  79. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement