Q. Write An Algorithm And Program to Check Whether a Number is Positive or Negative?

Here you will find an Algorithm And Program to Check Whether a Number is Positive or Negative. Given a numbers and our task is to find Whether a Number is Positive or Negative.

Positive Number : A number is said to be positive if the number is greater than 0 (zero). For Example : 1 to infinity.

Negative Number : A number is said to be negative if the number is less than 0 (zero). For Example : -1 to negative infinity.

Now let us understand what logic we need to use in this -
Given a number num1, we will check these conditions :
1. If the number num1 greater than 0 then output will be "You entered a positive number".
2. If the number num1 less than 0 then output will be "You entered a negative number".
3. If the above both condition is false then output will be "You entered 0".

For Example :-

Input : Enter the number num1: 5
Output : You entered a positive number

Explanation : As we can see that the number given in input, which is 5 which is greater than 0 therefore 5 is positive number.

Algorithm To Check Whether a Number is Positive or Negative

\\Algorithm To Check Whether a Number is Positive or Negative
START
Step 1: [ Take Input ] Read: Number Num
Step 2: If Num > 0
			If true, print You entered a positive number.
		Else If Num < 0
            If true, print You entered a negative number.
        Else 
            print You entered 0.
END


Program to Check Whether a Number is Positive or Negative

  • C
  • C++
  • Java
  • Python
  • C#
  • PHP
  • //C Program to Check Whether a Number is Positive or Negative
    #include <stdio.h> 
      
    int main() 
    { 
        int num1; 
        //Enter the value for num1
        printf("Enter the number num1: "); 
        scanf("%d", &num1); 
        
        if (num1 > 0) 
            printf("You entered a positive number"); 
        else if (num1 < 0) 
            printf("You entered a negative number"); 
        else
            printf("You entered 0"); 
      
        return 0; 
    } 
    
    //C++ Program to Check Whether a Number is Positive or Negative
    #include <iostream>
    using namespace std;
    
    int main()
    {
        int num1;
        
        //Enter the value for num1
        cout<<"Enter the number num1: ";
        cin>>num1;
        
        if (num1 > 0) 
            cout<<"You entered a positive number"; 
        else if (num1 < 0) 
            cout<<"You entered a negative number"; 
        else
            cout<<"You entered 0"; 
    
        return 0;
    }
    
    //Java Program to Check Whether a Number is Positive or Negative
    
    import java.util.Scanner;
    public class LFC
    {
        public static void main(String[] args) {
            Scanner reader = new Scanner(System.in);
            //Enter the value for num
            System.out.print("Enter the number num1: ");
            int num = reader.nextInt();
            
            if (num > 0) 
            System.out.println("You entered a positive number");
            else if (num < 0) 
            System.out.println("You entered a negative number");
            else
            System.out.println("You entered 0");
    
        }
    }
    
    
    #Python Program to Check Whether a Number is Positive or Negative
    
    #Enter the value for num
    num = int(input("Enter the number num1: "))
    
    if num > 0:
      print("You entered a positive number")
    elif num < 0:
      print("You entered a negative number")
    else:
      print("You entered 0")
    
    //C# Program to Check Whether a Number is Positive or Negative
    
    using System;
    class LFC {
      static void Main() {
        string val;
        int num;
        Console.WriteLine("Enter the number num1: ");
        val = Console.ReadLine();
    
        // convert to integer
        //Enter the value for num
        num = Convert.ToInt32(val);
        
        if (num > 0) 
            Console.WriteLine("You entered a positive number");
        else if (num < 0) 
            Console.WriteLine("You entered a negative number");
        else
            Console.WriteLine("You entered 0");
    
      }
    }
    
    //PHP Program To Find the Largest Number Among Three Numbers
    
    //Enter the value for num
    $num = (int)readline('Enter the number num1: '); 
    
    if ($num > 0)
      echo "You entered a positive number";
    else if ($num < 0) 
      echo "You entered a negative number";
    else
      echo "You entered 0";
    
    //C Program to Check Whether a Number is Positive or Negative
    #include <stdio.h> 
      
    int main() 
    { 
        int num1; 
        //Enter the value for num1
        printf("Enter the number num1: "); 
        scanf("%d", &num1); 
        
        if (num1 > 0) 
            printf("You entered a positive number"); 
        else if (num1 < 0) 
            printf("You entered a negative number"); 
        else
            printf("You entered 0"); 
      
        return 0; 
    } 
    

    Output

    Enter the number num1: 2
    You entered a positive number
    

    Recommended Programs

       Program to find factorial of a number
       Program to count number of digits in a number