Program in C or C++ for T flip-flop

Program in C or C++ for T flip-flop

T Flip-Flop


If we join both J and K input terminal of JK flip flop, It converted into T flip-flop.So it means two input J & K wil convert into a singal input as T. It is also called Toggle flip-flop.

Circuit Diagram:

Truth Table:

Q(t+1)=T.Q(t)' + T'.Q(t)

OR

Q(t+1)=T⊕Q(t)

T Q(t) Q(t+1)
0 0 0
0 1 1
1 0 1
1 1 0
No change condition occures in T flip-flop when T=0. We can say that at T=0 AND gate does not come into play, only NOR gate play their role.

Program in C:




 #include <stdio.h>
  typedef char bit;

  bit t(bit T,bit Q)
  {
 
 return T&~(Q)|Q&~(T);
  
   }

  
int main()
{
 int i,j,a,res=0;
 int x,y;
 char b;
 printf("1.Truth Table of T flip flop");
 printf("\n2.I/O operation\n");
 printf("Enter your choice: ");
 scanf("%d",&a);
 switch(a) 
 {
  case 1:
  printf("T  Q(t) | Q(t+1)\n");
  for(i=0;i<2;i++)
  {
   for(j=0;j<2;j++)
   {
    res=t(i,j);
    printf("%d  %d    |",i,j);
    printf("  %d\n",res);
   
   }
  }
 break;
  
case 2:
 printf("Enter the value of T,Q(t):");
 scanf("%d%d",&x,&y);
 
  res=t(x,y);
  printf("Q(t+1)= %d\n\n",res);

  break;

 default:
  printf("invalid input");   
         
  }
 printf("do you want to continue(Y/N):");
 scanf(" %c",&b);
  if(b=='Y'||b=='y')
    {
     main();
 }
 else{
  return 0;
    }
}  
      

Output:



 1.Truth Table of T flip flop
 2.I/O operation
 Enter your choice: 1
 T  Q(t) | Q(t+1)
 0  0    |  0
 0  1    |  1
 1  0    |  1
 1  1    |  0
 do you want to continue(Y/N):y
 1.Truth Table of T flip flop
 2.I/O operation
 Enter your choice: 2
 Enter the value of T,Q(t):0 1
 Q(t+1)= 1

 do you want to continue(Y/N):n

 ...Program finished with exit code 0
Press ENTER to exit console.

Program in C++:




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

  bit t(bit T,bit Q)
  {
 
 return T&~(Q)|Q&~(T);
  
   }

  
int main()
{
 int i,j,a,res=0;
 char b;
 cout<<"1.Truth Table of T flip flop\n";
 cout<<"2.I/O operation\n";
 cout<<"Enter your choice: ";
 cin>>a;
 switch(a) 
  {
  case 1:
  cout<<"T  Q(t) | Q(t+1)"<<endl;
  for(i=0;i<2;i++)
  {
   for(j=0;j<2;j++)
   {
    res=t(i,j);
   cout<<i<<"  "<<j<<"  ";
   cout<<"  | "<<res<<endl;
   }
  }
 break;
  
 case 2:
  int j,k;
 cout<<"Enter the value of T & Q(t):";
 cin>>j>>k;
 
  res=t(j,k);
  
  cout<<"Q(t+1)= "<<res<<"\n\n";
  break;
  default:
  cout<<"invalid input";   
         
  }
  cout<<"do you want to continue(Y/N): ";
  cin>>b;
  if(b=='Y'||b=='y')
    {
     main();
 }
 else{
  return 0;
    }
}  
      

Output:



 1.Truth Table of T flip flop
 2.I/O operation
 Enter your choice: 1
 T  Q(t) | Q(t+1)
 0  0    |  0
 0  1    |  1
 1  0    |  1
 1  1    |  0
 do you want to continue(Y/N):y
 1.Truth Table of T flip flop
 2.I/O operation
 Enter your choice: 2
 Enter the value of T,Q(t):0 1
 Q(t+1)= 1

 do you want to continue(Y/N):n


...Program finished with exit code 0
Press ENTER to exit console.


Algorithm:

STEP 1: Start

STEP 2: Define 't' function

STEP 3: Perform the operation (T&~(Q)|Q&~(T)) where T and Q are one bit binary number

STEP 4: Invoke main function

STEP 5: Initialize variables

STEP 6: Print "Enter your choice"

STEP 7: Input choice 1 or 2

STEP 8: If the user enters 1 or 2 then follow the below steps else flow goes to default case & exit the program Switch(operator)

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

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

STEP 8.a.3: Call 't' function for value of Q(t+1)

STEP 8.a.4: Print i,j and Q(t+1) for truth table

STEP 8.b case 2:
STEP 8.b.1: Input three one bit binary number

STEP 8.b.2: Call 't' function for value of Q(t+1)

STEP 8.a.3: Print Q(t+1)

STEP 9: Stop

Comments :

Post a Comment