Creating a simple boolean logic calculator in C

  • Context:
  • Thread starter Thread starter Logical Dog
  • Start date Start date
  • Tags Tags
    Calculator Logic
Join the discussion
Ask a follow-up here, or get your own question answered by working scientists, mathematicians and engineers — people, not an autocomplete.
Real named experts · corrections over time · the nuance an AI answer skips
6 replies · 6K views
Logical Dog
Messages
362
Reaction score
97
Code:
#define _CRT_SECURE_NO_DEPRECATE
#include <stdio.h>

int main()
{
    int A; int B;
    printf("This is a simple boolean truth value calculator.\n\n");
    printf("Please input the boolean value for statement A\n\n\n");
    scanf("%d", &A); printf("Press enter to continue\n\n\n");
    getchar();
    printf("Please input the boolean value of the statement B\n\n\n");
    scanf("%d", &B);
    int C = A + B;
    getchar();
    printf("Press enter to calculate the boolean value of their disjunction\n\n\n");
    getchar();
    printf("A AND B: %d", A*B);
    printf("A OR B: %d", C);
    getchar();
    printf("Press enter to exit program");
    return 0;
}
-------------------------------------------------------------------------------------------------------------------------------------------
I need to make the variable C to be only 0 or 1...not TWO when A and B are 1 and their disjunction gives 2.

Please use simple language as much as you can, I am very new!

thank you!: D
 
Last edited by a moderator:
on Phys.org
jtbell said:
Look up "logical operators in C" or "boolean operators in C". The '+' and '*' operators are not the ones that you need.
oh alright
thank you.
 
I'm confused, neither of your representations are correct. You don't multiple values to do a logical AND and you don't add them to do a logical OR.

C also has build in logical operators: || (OR), && (AND), ! (NOT). Just be careful when you google because & and && both mean AND, but in very different ways so make sure you know the difference between bitwise operator and plain operators.
 
  • Like
Likes   Reactions: Logical Dog
newjerseyrunner said:
C also has build in logical operators: || (OR), && (AND), ! (NOT). Just be careful when you google because & and && both mean AND, but in very different ways so make sure you know the difference between bitwise operator and plain operators.
Elaborating on what NJrunner said, && and || are, respectively, logical AND and logical OR. & and | are, respectively, bitwise AND and bitwise OR.
 
  • Like
Likes   Reactions: Logical Dog
I want to make the user only able to input two values, 0 or 1 (as they should)...how do I go about this? sorry for late reply, I was busy.

nd thanks for the answers..dont know what bitwise means? is it like exclusive and inclusive AND ?
 
Bipolar Demon said:
I want to make the user only able to input two values, 0 or 1 (as they should)...how do I go about this? sorry for late reply, I was busy.
Inside a while loop, prompt the user to input either a 0 or a 1. Use scanf(), as you did in the code above, to read the input. If the input value is a 0 or 1, exit the loop. If it's any other value, start another iteration of the while loop.
Bipolar Demon said:
nd thanks for the answers..dont know what bitwise means? is it like exclusive and inclusive AND ?
Bitwise means that the operation is performed on the respective bits of two different numbers. There is no such thing as exclusive AND and inclusive AND - but there are exclusive and inclusive OR.

Here's how some of the bitwise operations work. Suppose A is set to 01011001 and that B is set to 00001111.
01011001 (A)
00001111 (B)
-------------
00001001 (A & B) If both of the bits in a particular position of A and B are 1, then the bit in that position of the result will be 1; otherwise it is 0.
01011111 (A | B) If either bit or both bits in a particular position of A and B are 1, then the bit in that position of the result will be 1; otherwise it is 0.