Need help Thank you 10 points The following bash script and

Need help. Thank you
(10 points) The following bash script and C program can accomplish the following task. Read a positive decimal number from user and convert it to the equivalent binary representation A sample of the output is like below: Please enter a decimal: 12 The binary number is 1100

Solution

Here is the shell script for you:

#!/bin/bash
echo \'Please enter a decimal: \'
read dec
bin=0
while [ $dec -ne 0 ]
do
   d=`expr $dec % 2`
   bin= `expr $bin * 10 + $d`
   dec=`expr $dec / 2`
done
echo \'The binary number is \'$bin  

And the C code is:

#include <stdio.h>
#include <math.h>
int main()
{
int dec;
printf(\"Please enter a decimal number: \");
scanf(\"%d\", &dec);
  
int i = 1;
int bin = 0;
while(dec != 0)
{
int d = dec % 2;
bin = bin * 10 + d;
dec = dec / 2;
i++;
}
printf(\"The binary number is \");
for(int j = 0; j < i-1; j++)
{
printf(\"%d\", bin % 10);
bin /= 10;
}
printf(\"\ \");
}

Need help. Thank you (10 points) The following bash script and C program can accomplish the following task. Read a positive decimal number from user and convert

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site