Check value

C program to check whether a number is Positive or Negative or Zero

C program to check whether a number is Positive or Negative or Zero

In this tutorial, we will discuss a concept in C program to check whether a number is Positive or Negative or Zero

In this post, we will learn how to check if the number is positive or negative or zero in C programming language

 

C program to number is Positive or Negative or Zero

Logic of the program

If a number is greater than zero it is a positive number(if the number>0)

If a number is less than to zero it is  Negative number (if the number<0)

if the number is equal to zero the number is zero(if the number==0)

 

C program to check whether a number is Positive or Negative or Zero

Using if else if statements

Program 1

This program allows the user to enter a number and check if the number(entered by user) is positive or negative or zero using if-else if statements

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int num1;
    printf("Enter a number: ");
    scanf("%d",&num1);//get input from user for check
    if(num1>0){
            printf("%d",num1);
    printf(" is a positive number");
}
else if(num1<0){
        printf("%d",num1);
    printf(" is a Negative number");
}
else{
    printf("You entered zero");
}
getch();
    return 0;

}

When the above code is executed, it produces the following results

case 1

Enter a number: 35
35 is a positive number

case 2

Enter a number: -10
-10 is a Negative number

case 3

Enter a number: 0
you entered zero

 

 

Using Nested  if statements

Program 2

This program allows the user to enter a number and check if the number is positive or negative or zero using Nested if statements

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int num1;
    printf("Enter a number: ");
    scanf("%d",&num1);//get input from user for check

    if(num1<=0){

    if(num1==0){
    printf("you entered zero");
}
else{
    printf("%d",num1);
    printf(" is a negative number");
}
}
else{
    printf("%d",num1);
    printf(" is a positive number");

}
getch();
    return 0;
}

 

When the above code is executed, it produces the following results

 

case 1

Enter a number: 10
10 is a positive number

case 2

Enter a number: -6
-6 is a negative number

case 3

Enter a number: 0
you entered zero

 

 

 

Similar  post

C++ program to check whether a number is Positive or Negative or Zero

Java program to check whether a number is Positive or Negative or Zero

Python program to check whether a number is Positive or Negative or Zero

C program to check whether a number is prime or not

C++ program to check whether a number is prime or not

Python  program to check whether a number is prime or not

Java program to check whether a number is prime or not

 

Suggested for you

if statements in C language

Nested if statements in C language

Operator n C language

Variable in C language

Data type in C language

input-output function in C language

 

Java program to check whether a number is Positive or Negative or Zero
C++ program to check whether a number is Positive or Negative or Zero
Karmehavannan

I am Mr S.Karmehavannan. Founder and CEO of this website. This website specially designed for the programming learners and very especially programming beginners, this website will gradually lead the learners to develop their programming skill.

Recent Posts

How to find reverse number using method in Java

How to find reverse number using method In this article, we will discuss the concept…

2 days ago

C# inverted full pyramid star pattern

C# inverted full pyramid star pattern In this article, we will discuss the concept of…

3 weeks ago

C# Full Pyramid star pattern program

C# Full Pyramid star pattern program In this article, we will discuss the concept of…

1 month ago

Program to count vowels,consonants,words, characters and space in Java

Program to count vowels, consonants, words, characters and space in Java In this article, we…

1 month ago

How to print multiplication table using Array in C++ language

How to print multiplication table using Array in C++ language In this post, we will…

1 month ago

C Program to multiplication table using Array

C Program to multiplication table using Array In this tutorial , we will discuss about…

2 months ago