Answer to Question #259038 in C++ for Charmaine

Question #259038

Write a program to convert ASCII representation of Decimal number

(loaded from the keyboard using a loop calling the function getchar() )

and print its representation in Binary number system

(example:  13 » 00001101 )


1
Expert's answer
2021-11-01T08:28:16-0400
#include <stdio.h>
#include <math.h>
int main()  
{  
	int binary[100], dec, j = 7; 
    printf("Enter a character: ");  
    scanf("%d",&dec);
    printf("\nThe ascii value of the ch variable is : %d\n", dec);  
    while ( dec > 0 ) {
        binary[j] = dec % 2;
        dec /= 2;
        --j;
    }
 
    while ( j >= 0 ) { // Pad with leading 0s.
        binary[j] = 0;
        --j;
    }
	printf("\nBinary number is: ");  
    while ( ++j < 8 ) {
        printf("%d",binary[j]);
    }
	printf("\n");


	getchar();
	getchar();
    return 0;  
}  

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS