C Language

Program to find if the given number is positive or negative or 0 in C

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

if statements in c language

Nested if in C language

Operator in C language

Switch case in C language

 

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

Karmehavannan

Recent Posts

Subtract two numbers using method overriding

Subtract two numbers using method overriding   Program 1

3 months ago

PHP Star triangle Pattern program

PHP Star triangle Pattern program Here's a simple Java program that demonstrates how to print…

3 months ago

Using function or method to Write temperature conversion : Fahrenheit into Celsius

Using Function or Method to Write to temperature conversion: Fahrenheit into Celsius In this article,…

1 year ago

Function or method:temperature conversion from Fahrenheit into Celsius – Entered by user

Function or method of temperature conversion from Fahrenheit into Celsius In this article, we will…

1 year ago

Write temperature conversion program: Fahrenheit into Celsius

Write temperature conversion program: from Fahrenheit to Celsius In this article, we will discuss the…

1 year ago

How to write a program to convert Fahrenheit into Celsius

How to write a program to convert Fahrenheit into Celsius In this article, we will…

1 year ago

This website uses cookies.