program in C and C++ for full Adder

program in c and c++ for full Adder

Full Adder


Full Adder is a combinational logic circuit that perform the arithmetic sum of three inputs bits. There are three input and two output in Full adder.

Truth Table:

Circuit Diagram:

Sum = A XOR B XOR C_in
C_out = A.B + B.C_in + A.C_in

Program in C:




 #include <stdio.h>
 
 typedef char bit;
 bit Cout=0;
bit fulladd(bit A ,bit B,bit Cin){
 Cout=(A&B)||(Cin&(A^B));
 return (A^B)^Cin;
      }
 int main()
 {
  int i,j,k;
  int result;
 printf("A   B   Cin  |  S  Cout\n");
 for(i=0;i<2;i++)
 {
  for(j=0;j<2;j++)
  {
   for(k=0;k<2;k++)
   {
   result=fulladd(i,j,k);
  printf("%d   %d   %d    |  ",i,j,k);
  printf("%d    %d\n",result,Cout);
 }
  
      }
    }
 return 0;
}

Output:



A   B   Cin  |  S  Cout
0   0   0    |  0    0
0   0   1    |  1    0
0   1   0    |  1    0
0   1   1    |  0    1
1   0   0    |  1    0
1   0   1    |  0    1
1   1   0    |  0    1
1   1   1    |  1    1

--------------------------------
Process exited after 0.1661 seconds with return value 0
Press any key to continue . . .




Program in C++:




#include <iostream>
using namespace std;
 
 typedef char bit;

bit fulladd(bit A ,bit B,bit Cin,bit D){
 if(D==0)
   return (A&B)||(Cin&(A^B));
 else
  return (A^B)^Cin;
      }
 int main()
 {
  int i,j,k;
  int result,Cout;
  cout<<"A   B   Cin  |  S  Cout\n";
 for(i=0;i<2;i++)
 {
  for(j=0;j<2;j++)
  {
   for(k=0;k<2;k++)
   {
   Cout=fulladd(i,j,k,0);
   result=fulladd(i,j,k,1);
   cout<<i<<"   "<<j<<"   "<<k<<"    |  ";
   cout<<result<<"    "<<Cout<<"\n";
 }
  
      }
    }
 return 0;
}

Output:



A   B   Cin  |  S  Cout
0   0   0    |  0    0
0   0   1    |  1    0
0   1   0    |  1    0
0   1   1    |  0    1
1   0   0    |  1    0
1   0   1    |  0    1
1   1   0    |  0    1
1   1   1    |  1    1

--------------------------------
Process exited after 0.1661 seconds with return value 0
Press any key to continue . . .



Algorithm:

STEP 1: Start

STEP 2: Define 'fulladd' function

STEP 3: Perform the operation (A XOR B XOR Cin) on binary numbers and store it as Cout

STEP 4: Perform the operation (A.B + B.Cin + A.Cin) on binary numbers and store it as sum

STEP 5: Invoke main function

STEP 6: Initialize variables

STEP 7: Check for condition (i<2), if Condition is true, go to step 8 otherwise go to step 12

STEP 8: Check for condition (j<2), if Condition is true, go to further step otherwise go to step 7

STEP 9: Check for condition (k<2), if Condition is true, go to further steps otherwise go to step 8

STEP 10: Call 'fulladd' function for value of sum and carry

STEP 11: Print all values

STEP 12: Stop

Comments :

Post a Comment