Table of Contents
In this tutorial, we will discuss the Program to find if the given number is positive or negative or 0 in C
In this post, we are going to learn how to check whether the given number is positive or Negative or zero using 5 ways in C language
The logic to check positive, negative or zero
Program 1
#include <stdio.h> #include <stdlib.h> int main() { int num=100;//variable declaration and initialization if(num>0){//when the if statement is true printf("%d is a positive number",num);//positive number is displayed }else if(num<0){ //when the else-if statement is true printf("%d is a Negative number",num);//negative number is dispayed } else{ printf("The given number is zero");//otherwise, zero is displayed } getch(); return 0; }
When the above code is executed, it produces the following result
100 is a positive number
In this program,
Program 2
#include <stdio.h> #include <stdlib.h> int main() { int num; printf("Enter a number : \n");//ask input from the user scanf("%d",&num);//read the input value from the user if(num>0){//when the if statement is true printf("%d is a positive number",num); }else if(num<0){//when the else-if statement is true printf("%d is a Negative number",num); } else{//other wise zero is dispayed printf("The given number is zero"); } getch(); return 0; }
When the above code is executed, it produces the following result
Case 1
Enter a number : 240 230 is a positive number
Case 2
Enter a number : -123 -123 is a positive number
Case 3
Enter a number : 0 The given number is zero
Approach
Program 3
#include <stdio.h> #include <stdlib.h> int main() { int num; printf("Enter a number : \n");//ask input from the user scanf("%d",&num);//the entered value is stored in the num variable if(num<=0){ if(num==0){ printf("you entered zero",num); } else{ printf("you entered a negative number"); } } else{ printf("you entered a positive number"); } getch(); return 0; }
When the above code is executed, it produces the following result
Case 1
Enter a number : 401 you entered a positive number
Case 2
Enter a number : -101 you entered a negative number
Case 3
Enter a number : 0 you entered zero
Approach
Program 4
#include <stdio.h> #include <stdlib.h> int main() { int num; printf("Enter any number\n");//ask input from the user scanf("%d",&num); //the entered value is stored in the num variable switch(num>0){ case 1://check num is positive printf("%d is a positive number",num); break; case 0://num is either negative or zero switch(num<0){ case 1://check num is negative printf("%d is a negative number",num); break; case 0://num is zero printf("you entered zero"); break; } break; } getch(); return 0; }
When the above code is executed, it produces the following result
Case 1
Enter a number : 345 345 is a positive number
Case 2
Enter a number : -567 -567 is a negative number
Case 3
Enter a number : 0 you entered zero
Approach
Program 5
#include <stdio.h> #include <stdlib.h> int findPosNeg(int); int main() { int num; printf( "Enter a integer number:");//ask input from the user scanf("%d",&num); //read input from the user findPosNeg(num);//function call getch(); return 0; } findPosNeg(int num){//function definition if(num<=0){ if(num==0){ printf("you entered zero"); }else{ printf("%d is a Negative number",num); } } else{ printf("%d is a Positive number",num); } }
When the above code is executed, it produces the following result
Case 1
Enter a integer number: 101 101 is a positive number
Case 2
Enter a integer number: -123 -123 is a Negative number
Case 3
Enter a integer number: 0 you entered zero
Approach
Suggested for you
Similar post
Java program to find if the given number is positive or negative or 0
C program to find if the given number is positive or negative or 0
C++ program to find if the given number is positive or negative or 0
Python program to find if the given number is positive or negative or 0
Subtract two numbers using method overriding Program 1
PHP Star triangle Pattern program Here's a simple Java program that demonstrates how to print…
Using Function or Method to Write to temperature conversion: Fahrenheit into Celsius In this article,…
Function or method of temperature conversion from Fahrenheit into Celsius In this article, we will…
Write temperature conversion program: from Fahrenheit to Celsius In this article, we will discuss the…
How to write a program to convert Fahrenheit into Celsius In this article, we will…
This website uses cookies.