- 1C Hello World Program
- 2C Program to Print Your Own Name
- 3C Program to Print an Integer Entered by the User
- 4C Program to Add Two Numbers
- 5C Program to Multiply Two Floating‑Point Numbers
- 6C Program to Print the ASCII Value of a Character
- 7C Program to Swap Two Numbers
- 8C Program to Calculate Fahrenheit to Celsius
- 9C Program to Find the Size of int, float, double and char
- 10C Program - Add Two Complex Numbers
- 11C Program - Find Simple Interest
- 12C Program - Find Compound Interest
- 13C Program - Area And Perimeter Of Rectangle
- 14C Program - Check Whether a Number is Positive, Negative, or Zero
- 15C Program - Check Whether Number is Even or Odd
- 16C Program - Check Whether a Character is Vowel or Consonant
- 17C Program - Find Largest Number Among Three Numbers
- 18C Program - Calculate Sum of Natural Numbers
- 19C Program - Print Alphabets From A to Z Using Loop
- 20C Program - Make a Simple Calculator
- 21C Program - Generate Multiplication Table
- 22C Program - Reverse a Number
- 23C Program - Check whether the input number is a Neon Number
- 24C Program - Find All Factors of a Natural Number
C Program – Check Whether a Number is Positive, Negative, or Zero - If/Else & Ternary
Check Sign Using If-Else
#include <stdio.h>
int main() {
int num;
printf("Enter an integer: ");
scanf("%d", &num);
if (num > 0) {
printf("%d is positive\n", num);
} else if (num < 0) {
printf("%d is negative\n", num);
} else {
printf("The number is zero\n");
}
return 0;
}
Enter an integer: -7
-7 is negative
This approach compares the input with zero and prints a message depending on whether it is greater, less than or equal to zero.
Check Sign Using Ternary Operator
#include <stdio.h>
int main() {
int num;
const char *result;
printf("Enter an integer: ");
scanf("%d", &num);
result = (num > 0) ? "positive" : ((num < 0) ? "negative" : "zero");
printf("%d is %s\n", num, result);
return 0;
}
Enter an integer: 0
0 is zero
The ternary operator can compactly choose between multiple expressions. Here it selects one of three strings describing the number based on comparisons with zero.
⬅ Previous TopicC Program - Area And Perimeter Of Rectangle
Next Topic ⮕C Program - Check Whether Number is Even or Odd
Next Topic ⮕C Program - Check Whether Number is Even or Odd
Comments
Loading comments...