w3resource

C Exercises: Check whether a number is positive or negative

C Conditional Statement: Exercise-3 with Solution

Write a C program to check whether a given number is positive or negative.

Visual Presentation:

Check whether a number is positive or negative

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:

Flowchart: Check whether a number is positive or negative

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.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://www.w3resource.com/c-programming-exercises/conditional-statement/c-conditional-statement-exercises-3.php