Write C program to Check Whether a Number is Positive or Negative


In this tutorial we'll learn how to check whether a given integer is positive or negative. We have the the number that we need to check that integer is positive or negative number using c program. Problem Description The program takes the given integer and checks whether the integer is positive or negative. Problem Solution 1. Take the integer which you want to check as input. 2. Check if it is greater or lesser than zero and print the output accordingly. 3. Exit. Program/Source Code Here is source code of the C program which checks a given integer is positive or negative. The C program is successfully compiled and run on a Linux system. The program output is also shown below.
#include <stdio.h>
 
void main()
{
    int number;
 
    printf("Enter a number \n");
    scanf("%d", &number);
    if (number >= 0)
        printf("%d is a positive number \n", number);
    else
        printf("%d is a negative number \n", number);
}
  Output :
Case:1
Enter a number
-10
-10 is a negative number
 
Case:2
Enter a number
45
45 is a positive number
Program Explanation 1. Take the integer which you want to check as input and store it in a variable number. 2. Using if,else statements check whether the integer is greater or lesser than zero. 3. If it is greater than or equal to zero, then print the ouput as “it is a positive number”. 4. If it is lesser than zero, then print the ouput as “it is a negative number”. 5. Exit.
       

Advertisements

ads