Thread: ITS ASSIGNMENT :c program to generate truth table for n input logic gates

  1. #1
    Registered User
    Join Date
    Aug 2015
    Posts
    3

    ITS ASSIGNMENT :c program to generate truth table for n input logic gates

    Either it should generate result of all logical gates or ask the user which logical gate result you want and then return the result of that gate along with the 2^n generated input.
    for example : If the user inputs value of n as 3,then it should generate 8 combinations of numbers from 0 to 7 in binary(000,001,010,011,100,101,110,111) and show the result of AND, OR, NAND...etc,.
    Last edited by mueezmas; 08-29-2015 at 08:12 AM. Reason: spelling mistake

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    Nope, sorry.
    Devoted my life to programming...

  3. #3
    Registered User
    Join Date
    Aug 2015
    Posts
    3
    Any code for atleast the basic gates??

  4. #4
    Registered User
    Join Date
    Aug 2015
    Posts
    3

    Solution

    Code:
    #include<stdio.h>
    int fun(int a)
    ..{
    ..int r=1;
    ..while(a--)
    ..{
    .. r*= 2;
    ..}
    return r;
    ..}
    ..
    main()
    {
    int i,ch,j,n,or=0,mask,and,xor,xnor,nand;
    printf("enter the value of n\n");
    scanf("%d",&n);
    printf("enter ur choice 1 for Or gate 2 for and gate 3 for xor 4 for xnor 5 for nand\n");
    scanf("%d",&ch);
    switch(ch)
    .. {
    ...... case 1: for(i=0;i<=fun(n)-1;i++)
    .......................... {
    ................................ for(j=n-1;j>=0;j--)
    .................................... {....
    ........................................ mask=i>>j;
    ........................................ printf("%d\t", mask & 1);
    ........................................ or=or |(mask & 1);
    .................................... }..
    ........................................ printf("%d\n",or);
    .......................... }
    .......................... break;
    .... case 2 : for(i=0;i<=fun(n)-1;i++)
    ........................ {and=1;
    ............................ for(j=n-1;j>=0;j--)
    ................................ {
    .................................... mask=i>>j;
    .................................... printf("%d\t",mask & 1);
    ......................................
    .................................... and=and &(mask & 1);
    ................................ }
    
    .............................. printf("%d\n", and);
    ........................ }
    .......................... break;
    .. case 3: for(i=0;i<=fun(n)-1;i++)
    ...................... {xor=0;
    .................... for(j=n-1;j>=0;j--)
    ............................ {
    .................................. mask=i>>j;
    .............................. printf("%d\t",mask & 1);
    .............................. xor=xor ^(mask & 1);
    ............................ }
    ............................ printf("%d\n",xor);
    ...................... }
    .................... break;
    ..case 4:for(i=0;i<=fun(n)-1;i++)
    ...................... {xnor=1;
    .................... for(j=n-1;j>=0;j--)
    ............................ {
    .................................. mask=i>>j;
    .............................. printf("%d\t",mask & 1);
    .............................. xnor=xnor ^(mask & 1);
    ............................ }
    ............................ printf("%d\n",xnor);
    ...................... }
    .................... break;..
    case 5:for(i=0;i<=fun(n)-1;i++)
    ...................... {nand=1;
    .................... for(j=n-1;j>=0;j--)
    ............................ {
    .................................. mask=i>>j;
    .............................. printf("%d\t",mask & 1);
    .............................. nand=nand &(mask & 1);
    ............................ }nand=!nand;
    ............................ printf("%d\n",nand);
    ...................... }
    .................... break;..
    
    default:printf("wrong input\n");break;
    
    .. }
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 07-24-2014, 11:57 AM
  2. C program related to logic gates!
    By yellowmania in forum C Programming
    Replies: 4
    Last Post: 02-14-2012, 07:38 AM
  3. program to construct a truth table
    By ashok varma in forum C Programming
    Replies: 2
    Last Post: 08-04-2005, 10:53 AM
  4. logic circuit truth table
    By botakis in forum C Programming
    Replies: 3
    Last Post: 11-18-2004, 11:02 AM
  5. Logic Gates
    By JFK in forum C Programming
    Replies: 0
    Last Post: 10-21-2001, 05:56 AM

Tags for this Thread