Introduction
Operators are symbols used to perform operations on values and variables in C. They allow programs to calculate numbers, compare values, combine conditions, assign values, and perform other operations. In this chapter, you will practice the main C operator categories through simple programs, including arithmetic, relational, logical, assignment, increment, decrement, and conditional operators. The examples are arranged from basic calculations to practical problems so beginners can build a strong understanding of how operators work. C Operators Complete Practice questions with solutions to help you understand the concepts.
Q1. Perform Basic Arithmetic Operations
Problem Statement
Write a C program that takes two numbers and performs addition, subtraction, multiplication, division, and modulus operations.
C Program
#include <stdio.h>
int main()
{
int a = 20;
int b = 6;
printf("Addition = %d\n", a + b);
printf("Subtraction = %d\n", a - b);
printf("Multiplication = %d\n", a * b);
printf("Division = %d\n", a / b);
printf("Remainder = %d\n", a % b);
return 0;
}
Sample Output
Addition = 26
Subtraction = 14
Multiplication = 120
Division = 3
Remainder = 2
Concepts Covered
- Arithmetic operators
+-*/%- Integer division
Q2. Compare Two Numbers
Problem Statement
Write a C program to compare two numbers and display the results of different relational operations.
C Program
#include <stdio.h>
int main()
{
int a = 20;
int b = 15;
printf("a == b : %d\n", a == b);
printf("a != b : %d\n", a != b);
printf("a > b : %d\n", a > b);
printf("a < b : %d\n", a < b);
printf("a >= b : %d\n", a >= b);
printf("a <= b : %d\n", a <= b);
return 0;
}
Sample Output
a == b : 0
a != b : 1
a > b : 1
a < b : 0
a >= b : 1
a <= b : 0
In C, a comparison produces 1 when it is true and 0 when it is false.
Concepts Covered
- Relational operators
==!=><>=<=
Q3. Use Logical AND Operator
Problem Statement
A student passes only if their marks are at least 40 in both Mathematics and Science. Write a C program to check this condition using the logical AND operator.
C Program
#include <stdio.h>
int main()
{
int math = 65;
int science = 55;
int result = (math >= 40) && (science >= 40);
printf("Pass condition = %d", result);
return 0;
}
Sample Output
Pass condition = 1
Concepts Covered
- Logical operators
&&- Relational operators
- Boolean result
- Combining conditions
Q4. Use Logical OR Operator
Problem Statement
A student can participate in a special activity if they have either a score of at least 80 or an attendance percentage of at least 90. Write a C program using the logical OR operator.
C Program
#include <stdio.h>
int main()
{
int marks = 75;
int attendance = 92;
int eligible = (marks >= 80) || (attendance >= 90);
printf("Eligible = %d", eligible);
return 0;
}
Sample Output
Eligible = 1
Concepts Covered
- Logical OR
||- Relational operators
- Multiple conditions
Q5. Use the Logical NOT Operator
Problem Statement
Create a C program that checks whether a number is not zero using the logical NOT operator.
C Program
#include <stdio.h>
int main()
{
int number = 10;
int result = !(number == 0);
printf("Number is not zero = %d", result);
return 0;
}
Sample Output
Number is not zero = 1
Concepts Covered
- Logical NOT
!- Relational expression
- Logical result
Q6. Use Assignment Operators
Problem Statement
Start with a value of 20 and use compound assignment operators to modify it.
C Program
#include <stdio.h>
int main()
{
int number = 20;
number += 5;
printf("After += 5: %d\n", number);
number -= 3;
printf("After -= 3: %d\n", number);
number *= 2;
printf("After *= 2: %d\n", number);
number /= 2;
printf("After /= 2: %d\n", number);
number %= 4;
printf("After %%= 4: %d\n", number);
return 0;
}
Sample Output
After += 5: 25
After -= 3: 22
After *= 2: 44
After /= 2: 22
After %= 4: 2
Concepts Covered
- Assignment operators
=+=-=*=/=%=
Q7. Use Increment and Decrement Operators
Problem Statement
Write a C program to demonstrate pre-increment, post-increment, pre-decrement, and post-decrement.
C Program
#include <stdio.h>
int main()
{
int number = 10;
printf("Original value = %d\n", number);
printf("Post-increment = %d\n", number++);
printf("After post-increment = %d\n", number);
printf("Pre-increment = %d\n", ++number);
printf("Post-decrement = %d\n", number--);
printf("After post-decrement = %d\n", number);
printf("Pre-decrement = %d\n", --number);
return 0;
}
Sample Output
Original value = 10
Post-increment = 10
After post-increment = 11
Pre-increment = 12
Post-decrement = 12
After post-decrement = 11
Pre-decrement = 10
Concepts Covered
- Increment operator
++ - Decrement operator
-- - Pre-increment
- Post-increment
- Pre-decrement
- Post-decrement
Q8. Use the Conditional Operator
Problem Statement
Write a C program to find the larger of two numbers using the conditional operator.
C Program
#include <stdio.h>
int main()
{
int a = 45;
int b = 70;
int larger = (a > b) ? a : b;
printf("Larger number = %d", larger);
return 0;
}
Sample Output
Larger number = 70
Concepts Covered
- Conditional operator
?:- Relational operator
- Conditional expression
Q9. Calculate a Student’s Result Using Multiple Operators
Problem Statement
Write a C program that calculates a student’s total and average marks and checks whether the student passed all three subjects. A student must score at least 40 in every subject.
C Program
#include <stdio.h>
int main()
{
int math = 75;
int science = 68;
int computer = 82;
int total = math + science + computer;
float average = total / 3.0;
int passed = (math >= 40) &&
(science >= 40) &&
(computer >= 40);
printf("Total = %d\n", total);
printf("Average = %.2f\n", average);
printf("Passed = %d", passed);
return 0;
}
Sample Output
Total = 225
Average = 75.00
Passed = 1
Concepts Covered
- Arithmetic operators
- Relational operators
- Logical AND
- Assignment
- Floating-point division
- Multiple expressions
Q10. Build a Simple Operator-Based Calculator
Problem Statement
Write a C program that accepts two numbers from the user and displays their sum, difference, product, quotient, and remainder.
C Program
#include <stdio.h>
int main()
{
int a, b;
printf("Enter first number: ");
scanf("%d", &a);
printf("Enter second number: ");
scanf("%d", &b);
printf("\nAddition = %d\n", a + b);
printf("Subtraction = %d\n", a - b);
printf("Multiplication = %d\n", a * b);
if (b != 0)
{
printf("Division = %.2f\n", (float)a / b);
printf("Remainder = %d\n", a % b);
}
else
{
printf("Division and remainder are not possible with zero.");
}
return 0;
}
Sample Output
Enter first number: 25
Enter second number: 4
Addition = 29
Subtraction = 21
Multiplication = 100
Division = 6.25
Remainder = 1
Concepts Covered
- Arithmetic operators
- Relational operator
- Assignment
- Type casting
- Division
- Modulus
- User input
- Multiple operators
- Basic
if-else
Key Takeaways
- Operators perform operations on values and variables.
- Arithmetic operators are used for mathematical calculations.
- Relational operators compare two values and produce
1or0. - Logical operators are used to combine or reverse conditions.
- Assignment operators assign or update values.
++increases a value by one, while--decreases it by one.- Pre-increment changes the value before its expression is evaluated.
- Post-increment uses the current value before increasing it.
- The conditional operator
?:provides a compact way to choose between two expressions. - The modulus operator
%returns the remainder of integer division. - Integer division can discard the fractional part, so type conversion may be needed when a decimal result is required.
- Division and modulus by zero must be avoided.
FAQs
1. What are operators in C?
Operators are symbols that tell C to perform an operation on one or more operands.
For example:
int sum = a + b;
Here, + is an arithmetic operator.
2. What are the main types of operators in C?
Common operator categories include arithmetic, relational, logical, assignment, increment/decrement, conditional, and bitwise operators.
3. What is the difference between = and ==?
= is the assignment operator. It assigns a value.
x = 10;
== is the equality operator. It compares two values.
x == 10;
4. What does % do in C?
With integer operands, % returns the remainder after division.
int result = 17 % 5;
The value of result is 2.
5. What is the difference between ++x and x++?
++x is pre-increment: the value is increased before it is used in the surrounding expression.
x++ is post-increment: the current value is used first, and then the variable is increased.
6. What does the && operator do?
&& is the logical AND operator. An expression using && is true only when both of its operands are true.
(age >= 18) && (citizen == 1)
7. What is the conditional operator in C?
The conditional operator ?: is a three-part operator that selects one of two expressions based on a condition.
int max = (a > b) ? a : b;
If a > b is true, a is selected; otherwise, b is selected.
Written by Shubhranshu Shekhar, who has trained 20000+ students in coding.
