Binary Calculator

#include<stdio.h>

int a[8],b[8];

void sub()
{
    int bor=0,i,acc[4];
    for(i=0;i<4;i++)
    {
        if(a[i]==0&&b[i]==0&&bor==1)
        {
            acc[i]=1;
            bor=1;
        }
        else if(a[i]==0&&b[i]==1&&bor==0)
        {
            acc[i]=1;
            bor=1;
        }
        else if(a[i]==0&&b[i]==1&&bor==1)
        {
            acc[i]=0;
            bor=1;
        }
        else if(a[i]==1&&b[i]==1&&bor==1)
        {
            acc[i]=1;
            bor=1;
        }
        else
        {
            acc[i]=a[i]-b[i]-bor;
        }
    }
    printf("\nSubstraction : ");
    for(i=3;i>=0;i--)
        printf("%d",acc[i]);
    printf("\n");
}

void add()
{
    int carry=0,i,acc[4];
    for(i=0;i<4;i++)
    {
        acc[i]=a[i]+b[i]+carry;
        carry=0;
        if(acc[i]==2)
        {
            acc[i]=0;
            carry=1;
        }
        if(acc[i]==3)
        {
            acc[i]=1;
            carry=1;
        }
    }
    printf("\nAdditition : ");
    for(i=3;i>=0;i--)
        printf("%d",acc[i]);
    printf("\n");
}

void mult()
{
    int i,j,mult[8],carry=0,acc[8];
    for(i=0;i<8;i++)
        acc[i]=0;
    for(i=0;i<4;i++)
    {
        if(i!=0)
        {
            for(j=6;j>=0;j--)
            {
                a[j+1]=a[j];
            }
            a[0]=0;
        }
        if(b[i]==1)
        {
            for(j=0;j<8;j++)
            {
                acc[j]=acc[j]+a[j]+carry;
                carry=0;
                if(acc[j]==2)
                {
                    carry=1;
                    acc[j]=0;
                }
                else if(acc[j]==3)
                {
                    carry=1;
                    acc[j]=1;
                }
            }
        }
    }
    printf("\nMultiplication : ");
    for(i=7;i>=0;i--)
        printf("%d",acc[i]);
    printf("\n");
}

void main()
{
    int c;
    printf("Enter first 4 bit binary no. ") ;
    scanf("%d%d%d%d",&a[3],&a[2],&a[1],&a[0]);
    printf("Enter second 4 bit binary no. ") ;
    scanf("%d%d%d%d",&b[3],&b[2],&b[1],&b[0]);
    do
    {
        printf("\nChoice:\n1.Addition\n2.Substraction\n3.Multiplication\n4.Exit\n");
        scanf("%d",&c);
        switch(c)
        {
            case 1:
                add();
                break;
            case 2:
                sub();
                break;
            case 3:
                mult();
                break;
            case 4:
                printf("\nExit !!");
                break;
            default:
                printf("\nInvalid choice !!\n");
        }
    }while(c!=4);
}

/*Output
Enter first 4 bit binary no. 1 0 1 1
Enter second 4 bit binary no. 1 1 0 0

Choice:
1.Addition
2.Substraction
3.Multiplication
4.Exit
1

Additition : 0111

Choice:
1.Addition
2.Substraction
3.Multiplication
4.Exit
2

Substraction : 1111

Choice:
1.Addition
2.Substraction
3.Multiplication
4.Exit
3

Multiplication : 10000100

Choice:
1.Addition
2.Substraction
3.Multiplication
4.Exit
4

Exit !!
*/

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s