C Program to check if given number is in binary form or not ,If Yes convert that Binary number to Decimal.

 

C-PROGRAM



#include<stdio.h>
#include<conio.h>
int main()
{
    int B,base,rem,num,number,flag=1,n,r;
    clrscr();
    printf("Enter a Binary number:");
    scanf("%d",&n);
    num=n;
    while(num>0)
    {
        r=num%10;
        if(r>1)
        {
            flag=0;
            break;
        }
        else
        {
           num=num/10;
        }
    }
    if(flag==1)
    {    
        B=0,base=1;
        number=n;
        while(number>0)
        {
            rem=number%10;
            B=B+rem*base;
            number=number/10;
            base=base*2;
        }
        printf("Binary equivalent of %d is:%d",n,B);
    }
    else
    {
        printf("**Sorry,enterd number is not Binary,
\nKindly enter correct number.**");
    }
    return 0;
}



INPUT && OUTPUT









Post a Comment

0 Comments