#include <stdio.h> void bin (int num) //the bin commandturns every byte in the i
ID: 3614384 • Letter: #
Question
#include <stdio.h>
void bin (int num) //the bin commandturns every byte in the input into its binary representation
{
while (num != 0)
{
printf("%i", num % 2); //the modulusoperator returns the remainder of the number divided by base 2
num = num/2; //number divided by base2
}
printf(" ");
}
intmain(void){
intnum;
printf(" Please enter an integer number to be converted:");
scanf("%i", &num); //user entersnumber
printf(" ");
bin(num); //bin function call
printf(" ");
return 0;
}
Explanation / Answer
please rate - thanks to keep your algorithm arrays must be used since the digits aregenerated in reverse it can also be done with a mask and shifting I made the array 8 since your comment talks about bytes and 8 bitsper byte #include void bin (int num) //the bin command turns every byte in the inputinto its binary representation { int val[8],i,n=0; while (num != 0) { val[n++]=num%2; //printf("%i", num % 2); //the modulus operator returns theremainder of the number divided by base 2 num = num/2; //number divided by base 2 } for(i=n-1;i>=0;i--) printf("%d",val[i]); printf(" "); } int main(void){ int num; printf(" Please enter an integer number to be converted:"); scanf("%i", &num); //user enters number printf(" "); bin(num); //bin function call printf(" "); return 0; }Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.