C program to check whether a number is positive or negative

by | Jan 14, 2023 | C, C Programs

Introduction

In this C program, you will learn how to check if a number is positive number or negative.

The value is entered by the user, and that particular value is computed under some conditions.

A number that is greater than 0 is considered a positive number, and a number less than 0 is considered a negative number.

To understand this program, you should know about

1. C programming operators

2. C nested if…else statement

Program

#include<stdio.h>
#include<math.h>
int main()
{
float number;
printf("Enter a value:\n");
scanf("%f",&number);


if(number>0) // condition for a number to be positive
{
printf("%f is a positive number\n",number);
}


else if(number==0)
{
printf("Entered value is 0\n");
}


else // a number which is neither positive nor 0, thus it is a negative number
{
printf("%f is a negative number\n",number);
}
return 0;
}

 

Output

Explanation

To generalize the code, we take input from the user as a float type value.

The value of the variable ‘number’ entered by the user is -5.6. Since it is less than 0 that’s why the first two ‘if’ and ‘else if’ statement doesn’t get executed.

Else condition becomes true, and the statement written inside the body of this statement is executed.

 

0 Comments

Submit a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.