C program to check whether a number is positive or negative
In this post, you will learn how to write C program to check whether a number is positive or negative. If the input number is greater than zero, then its positive else it is a negative number.
Check Positive and Negative using If Else
The given C program allows the user to enter a number and checks whether the given input is positive or negative using the If Else statement.
#include <stdio.h>
void main()
{
int num;
printf("Please enter a number: \n");
scanf("%d", &num);
if (num > 0)
printf("%d is a positive number \n", num);
else if (num < 0)
printf("%d is a negative number \n", num);
else
printf("0 is neither positive nor negative");
}
Output1:
Please enter a number:
23
23 is a positive number
Output2:
Please enter a number:
-12
-12 is a negative number
Check Positive and Negative using Nested If Else
The given program allows user to enter a number and checks whether the given input is positive or negative using Nested If Else statement.
#include <stdio.h>
int main()
{
int num;
printf("Please enter a number:\n");
scanf("%d",&num);
if (num >= 0)
{
if (num > 0)
printf("%d is a positive number.", num);
else
printf("You have entered zero value.");
}
else
printf("%d is a negative number.", num);
return 0;
}
Output1:
Please enter a number:
71
71 is a positive number.
Output2:
Please enter a number:
-19
-19 is a negative number.
Related Articles
Prime factors of a number in cArmstrong number program in c
Write a program to check leap year in c
C program to find area of rectangle
C program to convert celsius to fahrenheit
Fibonacci series program in C using recursion
Write a program to find area of circle in C
C program to find greatest of three numbers
C program for addition of two numbers
C program to calculate compound interest
C program to find the ASCII value of a character
C program to convert Decimal to Octal
C program to convert decimal to binary
Write a C program to calculate Simple Interest
C program to check whether a number is even or odd
C program to reverse a number
C program to check palindrome number
C program to check whether an alphabet is a vowel or consonant
Program to find square root of a number in C
C program to check whether a number is positive or negative