Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

You will write a program that converts binary numbers to decimal numbers, where

ID: 3670647 • Letter: Y

Question


You will write a program that converts binary numbers to decimal numbers, where the input binary number is a String. This program will first verify whether the input only contains '0's and '1's^2. If the utter enters letters or other invalid inputs, the program prints "The input is not binary!" When the input is valid, the program will then convert the binary number to a decimal number. Valid input -In order to check if the input Is valid your program should check if it is only composed of zeros and ones. More specifically, you should scan the input string using a for or while loop to make sure it only contains '0' or '1' characters. As soon as you find a character that is not '0' or '1', the process should stop. If the process reaches the end of the input string then we have a valid input. You may ignore the case where the input string is empty, as well.is any extra '0' s at the start of the string. Converting -At this point we assume that the input string is valid The binary conversion should work ns follows. For each digit in the binary number, if the digit is T you should add the corresponding decimal value (2^0 for the rightmost digit, 2^1 for the next digits to the left, 2^2 for the next one. and so on) to a variable that will hold the final result to be printed. This must be accomplished by using a loop.

Explanation / Answer

#include<stdio.h>

int main(){

    long int binaryNumber,decimalNumber=0,j=1,remainder;

    printf("Enter any number any binary number: ");

    scanf("%ld",&binaryNumber);

    while(binaryNumber!=0){

         remainder=binaryNumber%10;

        decimalNumber=decimalNumber+remainder*j;

        j=j*2;

        binaryNumber=binaryNumber/10;

    }

    printf("Equivalent decimal value: %ld",decimalNumber);

    return 0;

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote