Determining whether a number is positive, or negative is a fundamental task in programming. In this article, we will cover various C Program to Determine if a Number is Positive or Negative. We will explore multiple examples, each demonstrating a different approach to to check number is positive or negative.
Prerequisites
Before we delve into the examples, ensure you have the following prerequisites:
- A C compiler (such as GCC)
- A text editor or IDE for writing your C code
- Basic understanding of C programming concepts, especially conditionals and functions
1. Checking if a Number is Positive or Negative
In this section, we will look at several methods to determine whether a number is positive or negative in C.
1.1 Using Simple if-else Statements
Example 1: Check Using Simple if-else
Statements
This method uses basic if-else
statements to check if the number is positive or negative.
Code
#include <stdio.h>
int main() {
int num;
printf("Enter a number: ");
scanf("%d", &num);
if (num > 0) {
printf("%d is a positive number.\n", num);
} else if (num < 0) {
printf("%d is a negative number.\n", num);
} else {
printf("You entered zero.\n");
}
return 0;
}
Explanation
- Include necessary header:
#include <stdio.h>
for input/output functions. - Input the number:
scanf
reads an integer from the user. - Check the number:
if-else
statements determine if the number is positive, negative, or zero. - Output the result:
printf
displays whether the number is positive, negative, or zero.
Output
Enter a number: 5
5 is a positive number.
1.2 Using Ternary Operator
Example 2: Check Using Ternary Operator
This method uses the ternary operator to check if the number is positive or negative.
Code
#include <stdio.h>
int main() {
int num;
const char* result;
printf("Enter a number: ");
scanf("%d", &num);
result = (num > 0) ? "positive" : ((num < 0) ? "negative" : "zero");
printf("%d is a %s number.\n", num, result);
return 0;
}
Explanation
- Include necessary header:
#include <stdio.h>
for input/output functions. - Input the number:
scanf
reads an integer from the user. - Check the number using the ternary operator: A nested ternary operator determines if the number is positive, negative, or zero.
- Output the result:
printf
displays whether the number is positive, negative, or zero.
Output
Enter a number: -3
-3 is a negative number.
1.3 Using Functions
Example 3: Check Using a Function
This method encapsulates the logic to check if the number is positive or negative in a separate function for better modularity.
Code
#include <stdio.h>
void checkNumber(int num);
int main() {
int num;
printf("Enter a number: ");
scanf("%d", &num);
checkNumber(num);
return 0;
}
void checkNumber(int num) {
if (num > 0) {
printf("%d is a positive number.\n", num);
} else if (num < 0) {
printf("%d is a negative number.\n", num);
} else {
printf("You entered zero.\n");
}
}
Explanation
- Include necessary header:
#include <stdio.h>
for input/output functions. - Declare a function:
void checkNumber(int num)
to encapsulate the logic for checking the number. - Input the number:
scanf
reads an integer from the user. - Call the function:
checkNumber(num)
checks if the number is positive, negative, or zero and displays the result.
Output
Enter a number: 0
You entered zero.
1.4 Using a Struct
Example 4: Check Using a Struct
This method uses a struct to encapsulate the number and its sign status.
Code
#include <stdio.h>
typedef struct {
int num;
const char* status;
} Number;
Number checkNumber(int num);
int main() {
int num;
printf("Enter a number: ");
scanf("%d", &num);
Number result = checkNumber(num);
printf("%d is a %s number.\n", result.num, result.status);
return 0;
}
Number checkNumber(int num) {
Number result;
result.num = num;
if (num > 0) {
result.status = "positive";
} else if (num < 0) {
result.status = "negative";
} else {
result.status = "zero";
}
return result;
}
Explanation
- Include necessary header:
#include <stdio.h>
for input/output functions. - Declare a struct:
typedef struct { ... } Number
to encapsulate the number and its status. - Declare a function:
Number checkNumber(int num)
to check the number and return the struct. - Input the number:
scanf
reads an integer from the user. - Call the function:
checkNumber(num)
determines the status of the number and returns it as a struct. - Output the result:
printf
displays whether the number is positive, negative, or zero.
Output
Enter a number: 7
7 is a positive number.
1.5 Using Arrays
Example 5: Check Using Arrays
This method uses an array to store and check multiple numbers for their sign status.
Code
#include <stdio.h>
void checkNumbers(int arr[], int size);
int main() {
int arr[5];
int i;
printf("Enter 5 numbers: ");
for (i = 0; i < 5; ++i) {
scanf("%d", &arr[i]);
}
checkNumbers(arr, 5);
return 0;
}
void checkNumbers(int arr[], int size) {
for (int i = 0; i < size; ++i) {
if (arr[i] > 0) {
printf("%d is a positive number.\n", arr[i]);
} else if (arr[i] < 0) {
printf("%d is a negative number.\n", arr[i]);
} else {
printf("You entered zero.\n");
}
}
}
Explanation
- Include necessary header:
#include <stdio.h>
for input/output functions. - Input the numbers into an array:
scanf
reads integers into the arrayarr
. - Check each number in the array: A
for
loop iterates through the array and checks if each number is positive, negative, or zero. - Output the result:
printf
displays whether each number is positive, negative, or zero.
Output
Enter 5 numbers: 1 -2 3 0 -4
1 is a positive number.
-2 is a negative number.
3 is a positive number.
You entered zero.
-4 is a negative number.
2. Conclusion
In this article, we explored various methods to determine whether a number is positive or negative in C: using simple if-else
statements, using the ternary operator, using a function, using a struct, and using arrays. Each method demonstrates different aspects of handling conditions, modular programming, and array manipulation in C. By understanding these methods, you can choose the one that best fits your specific needs and enhance your skills in basic decision-making and structuring programs in C.
Using these examples as a guide, you can confidently determine the sign of numbers in various ways depending on your requirements, making your programs more flexible and efficient.