C program to check whether a number is positive or negative

 C program to check whether a number is positive or negative


A program to check whether a number is positive or negative. To find a number either positive or negative we use an if else statement. First we input a number into a variable 'n' and then check whether the number we scan is greater than or equal to zero if yes then it is a positive number otherwise that number is a negative number.

Algorithm:-

  1. Start
  2. Input n
  3. if n>=0, then positive otherwise negative
  4. print positive or negative
  5. Stop.

C program to check whether a number is positive or negative
C program to check positive or negative



Program:-

Here in this program we are checking whether the given number is positive or negative. First we input the number from user and then we check if n greater than or equal to zero then we print it is positive else that number is less than zero and we print the number is negative. Refer the program gjven below

#include <stdio.h>

int main()
{
    int n;
    printf("Enter the number to check whether positive or negative: ");
    scanf("%d", &n);
    
    if(n>=0){
        printf("The number %d is a Positive number", n);
    } else {
        printf("The number %d is a Negative number", n);
    }

    return 0;
}

Output:-

Enter the number to check whether positive or negative: 100                                                           
The number 100 is a Positive number 

Enter the number to check whether positive or negative: -15                                                           
The number -15 is a Negative number  

Program to check if it is Zero, Positive or Negative:-

We use if-elfe-if to check whether the number is zero or positive or negative. Here, first we check if the number is equal to zero if yes then prints the number is zero, else if check the number is less than zero if yes then prints number is negative else if both the conditions is false then prints the number is positive.

#include <stdio.h>

int main()
{
    int n;
    printf("Enter the number to check whether positive or negative: ");
    scanf("%d", &n);
    
    if(n==0){
        printf("The number is a Zero");
    } else if(n<0) {
        printf("The number %d is a Negative number", n);
    } else {
        printf("The number %d is a Positive number", n);
    }

    return 0;
}

Ouput:-

Enter the number to check whether positive or negative: 0                                                             
The number is a Zero 

Enter the number to check whether positive or negative: 11                                                            
The number 11 is a Positive number  

Post a Comment

0 Comments