VEF Blog

Titre du blog : programmeC
Auteur : jamal
Date de création : 24-04-2008
 
posté le 24-04-2008 à 02:19:13

convertir un nombr ENTIER en BINAIRE

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<conio.h>
#include<math.h>

//debut fonction
             int convertir(int n)
              {
                          /*
                            nb= nombre de bits
                            nf= nombre final ou résultat final
                            *tabbits est le tableau de   bits                                               
                          */
                             int nb, i, nf, p, j, d;
                             int *tabbits;
             
                             nb=0;
                             p=1;
                             while(n >= p)
                                     {
                                     p=p*2;
                                     nb=nb+1;
                                     }
             
                             tabbits=(int*)calloc(nb,sizeof(int)); 
                             for (i=0 ; i<nb ; i++) 
                                 {
                                 tabbits[i] = (int)i ;
                                 }
             
                             for (i=nb-1 ; i>=0 ; i--)
                                 { 
                                     d=1;
                                     for (j=1 ; j<=i ; j++)
                                     {
                                     d=d*2;
                                     }
                      
                                     if(n>=d)
                                     {
                                     tabbits[i]=1;  
                                     n=n-d;  
                                     }
                                     else tabbits[i]=0;              
                                  }
                nf=0;
               for (i=0 ; i<nb ; i++)
                   {
                         d=1;
                       for (j=1 ; j<=i ; j++)
                           {
                           d=d*10;
                           }
                    nf=nf+ tabbits[i] * d;
                   }
               return (nf);
               free(tabbits);
               }
// fin fonction


  main(void)
 
{
            int a, b;
           
           
         printf("ENTRER UN NOMBRE   \n");
         scanf("%d",&a);
        b= convertir(a);
       
        printf("CE NOMBRE EN BINAIRE EST  %d ", b);
       
        
          getch();
             
 
  }

 

Commentaires

dana ray le 25-07-2011 à 20:54:11
onvertir un nombr ENTIER en BINAIRE


#include<stdio>

#include<stdlib>

#include<string>

#include<conio>

#include<math>


//debut fonction


Buy Articles</math></conio></string></stdlib></stdio>