C Exercises: Check whether a number is positive or negative
Write a C program to check whether a given number is positive or negative.
Visual Presentation:
Sample Solution:
C Code:
#include <stdio.h> // Include the standard input/output header file.
void main()
{
int num; // Declare an integer variable 'num'.
printf("Input a number : "); // Prompt the user to input a number.
scanf("%d", &num); // Read and store the user's input in 'num'.
if (num >= 0) // Check if 'num' is greater than or equal to 0.
printf("%d is a positive number \n", num); // Print a message indicating that 'num' is a positive number.
else
printf("%d is a negative number \n", num); // Print a message indicating that 'num' is a negative number.
}
Sample Output:
Input a number :15 15 is a positive number
Flowchart:
C Programming Code Editor:
Previous: Write a C program to check whether a given number is even or odd.
Next: Write a C program to find whether a given year is a leap year or not.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics