Wednesday, June 17, 2020

10931 - Parity (Convert Decimal to Binary )

#include <stdio.h>

int main(){

    int  n, i;
    int a[100];

    while(scanf("%d", &n))
    {

        if(n==0)break;
        else{
            int c=0;
            for(i=0; 0<n; i++){
                a[i] = n%2;
                n = n/2;

                if(a[i]==1)c++;
            }

            printf("The parity of ");
            for(i=i-1; i>=0; i--){
                printf("%d", a[i]);
            }
            printf(" is %d (mod 2).\n", c);
        }

    }
    return 0;
}

No comments:

Post a Comment