3.
Program to illustrate arithmetic and logical operators
C Arithmetic Operators
Aim:
To write a C program to perform basic arithmetic operations (addition, subtraction,
multiplication, division, and modulus) and logic operator (&&,|| , ==) on two numbers
entered by the user.
ALGORITHM
1. Start
2. Declare variables: `num1`, `num2`, `sum`, `difference`, `product`, `quotient`,
`remainder`.
3. Prompt the user to enter two numbers.
4. Read the input values.
5. Perform the following operations:
* Addition: `sum = num1 + num2`
* Subtraction: `difference = num1 - num2`
* Multiplication: `product = num1 * num2`
* Division: `quotient = num1 / num2`
* Modulus: `remainder = num1 % num2`
6. Display the results of all operations.
7. Stop
PROGRAM:
// Online C compiler to run C program online
#include <stdio.h>
int main()
{
int a = 9,b = 4, c;
c = a+b;
printf("a+b = %d \n",c);
c = a-b;
printf("a-b = %d \n",c);
c = a*b;
printf("a*b = %d \n",c);
c = a/b;
printf("a/b = %d \n",c);
c = a%b;
printf("Remainder when a divided by b = %d \n",c);
return 0;
}
OUTPUT
a+b = 13
a-b = 5
a*b = 36
a/b = 2
Remainder when a divided by b = 1
RESULT:
The program successfully performs all basic arithmetic operations (addition,
subtraction, multiplication, division, and modulus) on two user-input numbers.
MANUAL CALCULATION:
Let’s assume two numbers:
A = 20
B=5
num1 + num2 = 20 + 5 = 25
num1 - num2 = 20 - 5 = 15
num1 * num2 = 20 * 5 = 100
num1 / num2 = 20 / 5 = 4.00
num1 % num2 = 20 % 5 = 0 Modulus (Remainder)
LOGIC OPERATOR
PROGRAME
// Online C compiler to run C program online
#include <stdio.h>
int main() {
int a = 5, b = 10;
// Logical AND
printf("a > 0 && b > 0 = %d \n", (a > 0 && b > 0)); // true && true = 1
// Logical OR
printf("a > 0 || b < 0 = %d \n", (a > 0 || b < 0)); // true || false = 1
// Logical NOT
printf("!(a == b) = %d \n", !(a == b)); // !(false) = 1
// Another example
printf("a < 0 && b > 0 = %d \n", (a < 0 && b > 0)); // false && true = 0
printf("a < 0 || b < 0 = %d \n", (a < 0 || b < 0)); // false || false = 0
return 0;
}
OUTPUT:
a > 0 && b > 0 = 1
a > 0 || b < 0 = 1
!(a == b) = 1
a < 0 && b > 0 = 0
a < 0 || b < 0 = 0
MANUAL CALCULATION:
OUTPUT: a > 0 && b > 0 = 1
OUTPUT: a > 0 || b < 0 = 1
OUTPUT: !(a == b) = 1
OUTPUT: a < 0 && b > 0 = 0
OUTPUT: a < 0 || b < 0 = 0