2. Write a C program that reads a decimal number in and then calls a function de
ID: 672202 • Letter: 2
Question
2. Write a C program that reads a decimal number in and then calls a function dec2bin to find the binary equivalent the decimal integer and display it. The binary equivalent of a number is obtained by continuously dividing a decimal number (and then the successive results from 2, retaining the remainders until the end of division) and then writing the remainders backwards, as shown in the examples. The output of your program should look something like the below text.
This program converts the decimal number supplied by you to its binary equivalent! Enter the decimal number for conversion: 156 The binary equivalent of 156 is 1 0 0 1 1 1 0 0 Would you like to run the program again? (enter Y for yes, any other letter for no): Y Enter the decimal number for conversion: 26 The binary equivalent of 26 is 1 1 0 1 0 Would you like to run the program again? (enter Y for yes, any other letter for no): q Thanks for using the program. Bye!
Explanation / Answer
#include<stdio.h>
int main()
{
int dec;
char c;
printf(" Enter the decimal number for conversion: ");
scanf("%d",&dec);
// This is the function which converti decimal to binary.
decimal_to_binary(dec);
printf("Would you like to run the program again? (enter Y for yes, any other letter for no)");
// Please note that there is a space before '%c' because when you enter number for conversion and then press enter or space. In that case this scanf statement will read this enter or space in char variable 'c'.This is the reason I give space so that leading space or newline will cover in this given space.
scanf(" %c",&c);
// Logic here is untill user enter Y we will promt him/her to enter decimal number to convert in to binary.
while(c=='y' || c=='Y')
{
printf(" Enter the decimal number for conversion: ");
scanf("%d",&dec);
decimal_to_binary(dec);
printf("Would you like to run the program again? (enter Y for yes, any other letter for no):");
scanf(" %c",&c);
}
if(c!='y'||c!='Y')
{
printf("Thanks for using the program. Bye!");
return 0;
}
return 0;
}
// Logic here used is we will divide decimal number by 2 untill quotient becoms 0. For example. Suppose number is 8.
Then 8/2--> quotient 4 reminder 0
4/2--> quotient 2 reminder 0
2/2--> quotient 1 reminder 0
1/2--> quotient 0 reminder 1
now we need to print these reminders in reverse order from below to up.
So what we used here is instead of printing in reverse order we are adding them. Like
123 is made from 1,2,3. equal to 3*1+2*10+1*100
Same case is here we get reminders in above division are 1,0,0,0 which can be written as
1*1000+0*100+0*10+0*1=1000
Same logic is applies in below function. Evertime when we are getting reminder we are adding in increasing power of 10.
void decimal_to_binary(int decimal)
{
int res=0,i=1,rem;
while(decimal!=0)
{
// intially i=0 ,res=0 and decimal%2 is giving reminder so we multiply reminder with i and adding them in res. Next time we increase the power of i and multiply it with new reminder and adding them again in same varriable res.
res=res+(i * (decimal%2));
decimal=decimal/2;
i=i*10;
}
printf(" The Binary value is %d ",res);
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.