In: Computer Science
C CODE PLZ! All instructions for what to do in code #include <stdio.h> int main(int argc, char **argv) { int n, k, l, r, t, d, i; char str[65]; /* Make the user enter a non-negative integer */ printf("Please enter a non-negative integer: "); scanf("%d", &n); while (n < 0) { printf("Sorry, your input is incorrect.\n"); printf("Please enter a non-negative integer: "); scanf("%d", &n); } /* Convert the integer to reversed binary: e.g. 6 gets converted to 011 */ if (n == 0) { /* Special case for n = 0 */ str[0] = '0'; str[1] = '\0'; } else { /* Common case */ k = n; /* TODO */ } /* The conversion made the string come out reversed, so reverse the string again. DO NOT USE A COPY OF THE STRING. DO NOT USE ANY LIBRARY FUNCTION (no strlen or other !) */ /* TODO */ /* Display the number and the string */ printf("The decimal number %d is %s in binary.\n", n, str); return 0; }
Code
#include <stdio.h>
int main(int argc, char **argv) {
int n, k, l, r, t, d, i;
char str[65],c;
/* Make the user enter a non-negative integer */
printf("Please enter a non-negative integer: ");
scanf("%d", &n);
while (n < 0) {
printf("Sorry, your input is incorrect.\n");
printf("Please enter a non-negative integer: ");
scanf("%d", &n);
}
/* Convert the integer to reversed binary: e.g. 6 gets converted to 011 */
if (n == 0) {
/* Special case for n = 0 */
str[0] = '0';
str[1] = '\0';
} else {
/* Common case */
k = n;
i=0;
while(k>0)
{
if(k%2==0)
str[i]='0';
else
str[i]='1';
k=k/2;
i++;
}
str[i]='\0';
}
/* TODO */
l = 0;
k =i-1;
while (l < k) {
c = str[l];
str[l] = str[k];
str[k] = c;
l++;
k--;
}
/* Display the number and the string */
printf("The decimal number %d is %s in binary.\n", n, str);
return 0;
}
output
If you have any query regarding the code please ask me in the comment i am here for help you. Please do not direct thumbs down just ask if you have any query. And if you like my work then please appreciates with up vote. Thank You.