Program to Check Number is Positive or Negative

 

How to Check Number :

Number can be positive or negative, if number is greater than or equal to zero,than it is positive otherwise it will be negative.

Check Number is Positive or Negative

Working of Program :

In the program, we will take a value from the user and check conditional statement to solve the problem.

Run
#include<stdio.h>
int main() {
    float val;
    printf("Enter a number to be check :");
    scanf("%f", &val);
    
    if(val >=0){
        printf("Given number %f is positive", val);
    }
    else{
        printf("Given number %f is negative", val);
    }
    return 0;
}

Output :

Enter a number to be check : 7
7
Given number 7.000000 is positive

In the above program,

  • We take a value from the user.
  • In step 1, we use if condition to check if number is greater than or equal to zero to find positive number.
  • If Number is negative, else statement will check in step 2.

Example :

Let make problem little complex by check following condition :

  • If number is greater then zero , print given number is Positive.
  • If number is zero, print given number is zero and positive.
  • If number is less than zero, print number is negative.
Run
#include<stdio.h>
int main() {
    float val;
    printf("Enter a number to be check :");
    scanf("%f", &val);
    
    if(val >0){
        printf("Given number %f is positive ", val);
    }
    else if(val == 0){
        printf("Given number %f is zero and positive", val);
    }
    else{
        printf("Given number %f is negative", val);
    }
    return 0;
}

Output :

Enter a number to be check :0
Given number 0.000000 is zero and positive

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription