w3resource

C Exercises: Check whether a number is positive or negative


3. Positive or Negative Check

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.
}

Output:

Input a number :15                                                                                            
15 is a positive number  

Flowchart:

Flowchart: Check whether a number is positive or negative


For more Practice: Solve these Related Problems:

  • Write a C program to check whether a number is positive, negative, or zero using only ternary operators.
  • Write a C program to determine the sign of each number in an array without using conditional statements.
  • Write a C program to compare two numbers and print which one is positive, negative, or zero.
  • Write a C program to check a number’s sign by directly inspecting its sign bit.

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.



Follow us on Facebook and Twitter for latest update.