Course Code: 23ADS1101 Course Name: C.
Programming Lab
Name of Students: Dhanshree Gaur Semester/ Section: 1st Semester
Roll No: M-05 Enroll No: 24070403
Problem Definition:
rite a programme In C programming using conditional operators to evaluate the
W
following expression and print the value of y :
1. y = 2.4x + 3, for x <= 2
2. y = 3x - 5, for x > 2
Source Code in C:
#include <stdio.h>
int main() {
float x, y;
printf("Enter the value of x : ");
scanf("%f", &x);
y = (x <= 2) ? (2.4 * x + 3) : (3 * x - 5);
printf("The value of y : %f\n", y);
return 0;
}
OR
#include <stdio.h>
int main() {
float y, x;
printf("Enter the value of x : ");
scanf ("%f", &x);
if (x <= 2) {
y = 2.4*x + 3;
printf("Value of y : %f\n", y);
}
else {
y = 3*x - 5;
printf("Value of y : %f\n", y);
}
return 0;
}
Output :
Conclusion:
I n this practical we learnt about the use of conditional operators. conditional or ternary
operators are a concise way to express a condition in many programming languages. Here
we used if and if-else statements to evaluate the expression and get the desired output
according to it.