0% found this document useful (0 votes)
5 views15 pages

C Programming: Conditional Operators Explained

Uploaded by

9b.amankumar
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views15 pages

C Programming: Conditional Operators Explained

Uploaded by

9b.amankumar
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

C PROGRAMING FILE

ARYAN KUMR

IIOTB2

131251005873
EXPERIMENT-3 DATE-22/9/2025

AIM :- Understanding conditional operators in C

Theory:- In C programming, operators play a vital role in performing different types of


computations and decision-making processes. Among them, the conditional operator is one of
the most important tools used for implementing simple decisions in a concise manner. Along
with it, operator associativity and library functions (like those in math.h) are essential
concepts to master for effective programming.

1. Conditional (Ternary) Operator

The conditional operator (?:) is the only ternary operator in the C language, meaning it
works on three operands. It is a compact substitute for if...else statements when the
decision is simple.

Syntax:

condition ? expression1 : expression2;

• If the condition is true, the value of expression1 is returned.


• If the condition is false, the value of expression2 is returned.

For example:

int a = 10, b = 20;


int max = (a > b) ? a : b;

Here, if a > b is true, max will be assigned the value of a, otherwise the value of b.

The conditional operator is right-to-left associative, meaning when multiple conditional


operators are used in a statement, evaluation starts from the rightmost operator.

2. Operator Associativity in C

In C, operators are evaluated based on two rules:

1. Precedence → Which operator is executed first.


2. Associativity → The direction of evaluation if two operators have the same
precedence.
• Example of left-to-right associativity:
x + y - z is evaluated as (x + y) - z.
• Example of right-to-left associativity:
a = b = c = 5; is evaluated as a = (b = (c = 5));.

The conditional operator (?:) has right-to-left associativity. This property allows nesting
multiple conditions, such as:

int result = (marks >= 50) ? ((marks >= 90) ? 'A' : 'B') : 'F';

3. The Math.h Library

The math.h header file in C provides standard mathematical functions that are frequently
used in programs. It includes functions for square roots, powers, trigonometric calculations,
logarithms, and absolute values.

Some important functions are:

• sqrt(x) → square root of x


• pow(x, y) → x raised to power y
• fabs(x) → absolute value of x
• log(x) → natural logarithm of x
• sin(x), cos(x), tan(x) → trigonometric functions

For example, in solving quadratic equations, the discriminant D = b² – 4ac must be


calculated, and the roots are found using the square root function (sqrt) from math.h.

4. Application in Programs

To understand conditional operators and decision-making clearly, four different programs are
implemented in this experiment:

(a) Checking Positive, Negative, or Zero

• Demonstrates the use of if...else if...else to classify a number.


• Though this can also be written using the conditional operator, the structured form
improves clarity for beginners.

(b) Generating the Fibonacci Sequence

• Uses loops to generate the first n terms.


• Demonstrates iterative addition and updating variables.
• Highlights how conditions inside loops control program flow.

(c) Finding Roots of a Quadratic Equation


• Uses the discriminant formula (D = b² – 4ac) to decide root type.
• Demonstrates conditional checks and the use of math.h library for computing square
roots.
• Shows three possible cases: distinct real roots, equal real roots, and complex roots.

(d) Generating Prime Numbers up to n

• Demonstrates nested loops and conditions for checking divisibility.


• Prints all prime numbers within the given range.
• Reinforces the concept of decision-making in loops.

5. Importance of the Concepts

• Conditional operators simplify small decisions into one line, reducing code length.
• Operator associativity ensures correctness in evaluating expressions, especially in
assignments and conditional chains.
• The math.h library provides ready-to-use mathematical functions, making
calculations easier and more reliable than writing custom code.
• Together, these concepts strengthen the foundation of problem-solving in C and
prepare students to write efficient, structured programs.
3.1 To write a C program to find if a number is negative,
positive or zero using if ... else if ... else statement.
FLOWCHART:-

Start

Input
n

Check yes positive


n>0

no
Check yes negative
n<0

End
Zero
CODE:-
#include <stdio.h>
int main() {
int n;
printf("Enter a number: ");
scanf("%d", &n);

if (n > 0)
printf("The number is Positive");
else if (n < 0)
printf("The number is Negative");
else
printf("The number is Zero");

return 0;
}

RESULT:-
Enter a number: -5
The number is Negative.
=== Code Execution Successful ===

3.2 Write a C program to generate the


first n terms of the Fibonacci sequence.

FLOWCHART:-

Start

Input n

Initialize a=0, b=1

print

loop

Generate
next term

Update

Stop
CODE:-

#include <stdio.h>
int main() {
int n, i, a = 0, b = 1, c;
printf("Enter number of terms: ");
scanf("%d", &n);

printf("Fibonacci Sequence: %d %d ", a, b);

for(i = 2; i < n; i++) {


c = a + b;
printf("%d ", c);
a = b;
b = c;
}
return 0;
}

RESULT:-
Enter the number of terms: 5
Fibonacci Series: 0 1 1 2 3

=== Code Execution Successful ===

3.3 Write a C program to find the roots of a


quadratic equation.

FLOWCHART:-
Start

Imput a,b,c

Compute D

D>0?

Real roots Equal roots

D==0? Complex
roots

End
CODE:-
#include <stdio.h>
#include <math.h>

int main() {
float a, b, c, D, root1, root2, real, imag;

printf("Enter coefficients a, b and c: ");


scanf("%f %f %f", &a, &b, &c);

D = b*b - 4*a*c;

if (D > 0) {
root1 = (-b + sqrt(D)) / (2*a);
root2 = (-b - sqrt(D)) / (2*a);
printf("Roots are real and distinct: %.2f and %.2f", root1, root2);
}
else if (D == 0) {
root1 = root2 = -b / (2*a);
printf("Roots are real and equal: %.2f and %.2f", root1, root2);
}
else {
real = -b / (2*a);
imag = sqrt(-D) / (2*a);
printf("Roots are complex: %.2f+%.2fi and %.2f-%.2fi", real, imag, real,
imag);
}

return 0;
}

RESULT:-
Enter coefficients a, b and c: 1 -3 2
Roots are real and distinct: 2.00 and 1.00

=== Code Execution Successful ===


3.4 Write a C program to generate all the prime
numbers between 1 and n is a value supplied by
the user
FLOWCHART:-
CODE:-
#include <stdio.h>
int main() {
int n, i, j, flag;
printf("Enter value of n: ");
scanf("%d", &n);

printf("Prime numbers between 1 and %d are: ", n);


for (i = 2; i <= n; i++) {
flag = 1;
for (j = 2; j <= i/2; j++) {
if (i % j == 0) {
flag = 0;
break;
}
}
if (flag == 1)
printf("%d ", i);
}
return 0;
}
RESULT:-
Enter the value of n: 10
Prime numbers between 1 and 10 are: 2 3 5 7

=== Code Execution Successful ===

CONCLUSION:-
• The conditional operator (?:) helps in decision-making using
concise expressions.
• Associativity in operators decides evaluation order; the
conditional operator has right-to-left associativity.
• The math.h library is essential for mathematical computations
like square root and power.
• We successfully implemented:
1. Checking if a number is positive, negative, or zero.
2. Generating Fibonacci sequence.
3. Finding roots of a quadratic equation.
4. Printing all prime numbers up to n.

These programs demonstrate the use of decision-making, loops,


mathematical functions, and conditions in C programming.

ARYAN KUMAR IIOT B2


131251005873

You might also like