Logo 
Search:

C Programming Articles

Submit Article
Home » Articles » C Programming » Homework HelpRSS Feeds

PROGRAM TO PERFORM THE AND GATE

Posted By: Lois Schmidt     Category: C Programming     Views: 4774

WRITE A PROGRAM TO PERFORM THE AND GATE.

Code for PROGRAM TO PERFORM THE AND GATE in C Programming

#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
    char s1[20],s2[20];
    int int1[20],int2[20],fra1[20],fra2[20],t1[20],t2[20];
    int i,j,k,len1,len2,len3,len4,flag,ctr,ctr1,d1;
    int finalb[20],finalf[20];
    clrscr();
    i=j=len1=len2=len3=len4=flag=ctr=ctr1=0;

    printf("Enter the binary no 1 : ");
    scanf("%s",s1);

    printf("\n\n");

    printf("Enter the binary no 2 : ");
    scanf("%s",s2);

//Converting the two strings to their respective integers and decimal//parts.for(i=0,j=0,k=0;i<strlen(s1);i++)
    {
        if(s1[i]=='.')
        {
            flag=1;
        }
        elseif(flag==0)
            int1[j++]=s1[i] - 48;
        elseif(flag==1)
            fra1[k++]=s1[i] - 48;
    }
    len1 = j;
    len3 = k;
    flag = 0;

    for(i=0,j=0,k=0;i<strlen(s2);i++)
    {
        if(s2[i]=='.')
        {
            flag=1;
        }
        elseif(flag==0)
            int2[j++]=s2[i] - 48;
        elseif(flag==1)
            fra2[k++]=s2[i] - 48;
    }
    len2 = j;
    len4 = k;

//Checking the length of the two integer arrays.//If first integer is greater than second integer thanif(len1 > len2)
    {
        d1 = len1 - len2;

//append the zeros to the length of lesser integer.for(i=0;i<d1;i++)
        {
            t1[i] = 0;
        }

//inserting the original array of second integer.for(j=i,k=0;k<len2;j++,k++)
        {
            t1[j]=int2[k];
        }
        ctr=len1;

//performing the logical and.for(i=0;i<len1;i++)
        {
            finalb[i] = int1[i] & t1[i];
        }
    }
    elseif(len1 < len2)
    {
        d1 = len2 - len1;

//append the zeros to the length of lesser integer.for(i=0;i<d1;i++)
        {
            t1[i] = 0;
        }

//inserting the original array of first integer.for(j=i,k=0;k<len1;j++,k++)
        {
            t1[j]=int1[k];
        }
        ctr=len2;

//performing the logical and.for(i=0;i<len2;i++)
        {
            finalb[i] = int2[i] & t1[i];
        }
    }

//If the length of both integers are same.else
    {
         for(i=0;i<len2;i++)
        {
            finalb[i] = int1[i] & int2[i];
        }
        ctr=len1;
    }
//Checking the length of the fractional portions.//If the length of the first fractional part is greaterif(len3 > len4)
    {
        d1 = len3 - len4;
//inserting the original array of second integer.for(j=0;j<len4;j++)
        {
            t2[j]=fra2[j];
        }
        ctr1=len3;
//append the zeros to the length of lesser integer.for(i=0;i<d1;i++)
        {
            t2[j++] = 0;
        }
//performing the logical and.for(i=0;i<len3;i++)
        {
            finalf[i] = fra1[i] & t2[i];
        }
    }
    elseif(len3 < len4)
    {
        d1 = len4 - len3;
//inserting the original array of first integer.for(j=0;j<len3;j++)
        {
            t2[j]=fra1[j];
        }
        ctr1=len4;

//append the zeros to the length of lesser integer.for(i=0;i<d1;i++)
        {
            t2[j++] = 0;
        }
//performing the logical and.for(i=0;i<len4;i++)
        {
            finalf[i] = fra2[i] & t2[i];
        }
    }

//If the length of both integers are same.else
    {
         for(i=0;i<len3;i++)
         {
            finalf[i] = fra1[i] & fra2[i];
         }
        ctr1=len4;
    }

    printf("\n\nThe Logical And for the binary nos is :  ");

    for(i=0;i<ctr;i++)
    {
        printf("%d",finalb[i]);
    }
    printf(".");
    for(i=0;i<ctr1;i++)
    {
        printf("%d",finalf[i]);
    }
    getch();
}
  
Share: 


Didn't find what you were looking for? Find more on PROGRAM TO PERFORM THE AND GATE Or get search suggestion and latest updates.

Lois Schmidt
Lois Schmidt author of PROGRAM TO PERFORM THE AND GATE is from Frankfurt, Germany.
 
View All Articles

 
Please enter your Comment

  • Comment should be atleast 30 Characters.
  • Please put code inside [Code] your code [/Code].

 
No Comment Found, Be the First to post comment!