Table of Contents
Program to find if the given number is positive or negative or 0 in C
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
- Check the condition if(num<0), then number is negative
- Check the condition if(num>0), then number is positive
- Check the condition if(num==0), then number is zero
Program to find if the given number is positive or negative or 0
Find if the given number is positive or negative -using if else-if
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,
- integer variables num is declared and initialized
- The given numbers are tested whether it is positive, negative or zero using if- else statements
- Then,the program is displayed the output of using printf() function.
Find if the given number is positive or negative -Entered by user
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
- integer variable num is declared.
- The program asks input from the user
- Then the user enters the input values for num.
- The program will read the input using scanf() and store to the variable num
- The given number is tested whether it is positive, negative or zero using if- else statements
- Then,the program is displayed the output of using printf() function,
Find if the given number is positive or negative -using Nested if
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
- integer variable num is declared.
- The program asks input from the user
- Then the user enters the input values for num.
- The program will read the input using scanf() and store to the variable num
- The given number is tested whether it is positive, negative or zero using Nested if- else statements
- Then,the program is displayed the output of using printf() function,
Find if the given number is positive or negative -using switch case
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
- integer variable num is declared.
- The program asks input from the user
- Then the user enters the input values for num.
- The program will read the input using scanf() and store to the variable num
- The given number is tested whether it is positive, negative or zero using switch case statements
- Then,the program is displayed the output of using printf() function,
Find if the given number is positive or negative -using function
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
- Declare a function named as findPosNeg(int); with parameter
- integer variables num is declared.
- The program asks input from the user
- Then the user enters the input values for num.
- The program will read the input using scanf() and store the variable num.
- Define the function findPosNeg(); to find whether the number is Negative or Positive or zero
- Call the function to produce output
- finally, the program displays the output using printf() function
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