Program to identify of the a number is positive or negative

Program to identify of the a number is positive or negative

21 April 2023

21 April 2023

Write a program to identify of the a number is positive or negative



Description

Get an input number from the user and check whether it is a positive or negative number.

Input

-10

Output

Negative number

Input

0

Output

Neither positive nor negative

Input

15

Output

Positive number


C Program

Method 1

#include <stdio.h>

int main()

{

    int num;

    printf("Enter a number: ");

    scanf("%d", &num);

    if (num > 0)

         printf("Positive number");

    else if (num < 0)

        printf("Negative number");

    else

        printf("Neither positive nor negative");

    return 0;

}

 

Method 2

#include <stdio.h>

int main()

{

    int num;

    printf("Enter a number: ");

    scanf("%d", &num);

    if((num >= 0) ? (num==0?printf("Neither positive nor negative"):printf("Positive")): printf("Negative"))

    return 0;

}


C++ Program

Method 1

#include <iostream>

using namespace std;

int main()

{

    int num;

    cout<<"Enter a number: ";

    cin>>num;

    if (num > 0)

         cout<<"Positive number";

    else if (num < 0)

        cout<<"Negative number";

    else

        cout<<"Neither positive nor negative";

    return 0;

}

 

Method 2

#include <iostream>

using namespace std;

int main()

{

    int num;

    cout<<"Enter a number: ";

    scanf("%d", &num);

    if((num >= 0) ? (num==0?cout<<"Neither positive nor negative":cout<<"Positive"): cout<<"Negative")

    return 0;

}


Java Program

Method 1

import java.util.Scanner;

public class Main

{

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

System.out.print("Enter the number : ");

int num = sc.nextInt();

    if (num > 0)

        System.out.println ("Positive number");

    else if (num < 0)

        System.out.println ("Negative number");

    else

        System.out.println ("Neither positive nor negative");

}

}

 

Method 2

import java.util.Scanner;

public class Main

{

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

System.out.print("Enter the number : ");

int num = sc.nextInt();

    if (num == 0)

      {

System.out.println ("Neither positive nor negative");

      }    

    else{

        String output = num > 0 ? "Positive number" : "Negative number";

        System.out.println (output);

}

}

}


Python

Method 1

num = int(input("Enter a number: "))

if num ==0:

  print('Neither positve nor negative')

elif num>0:

  print('Positive number')

else:

  print('Negative number')

 

Method 2

num=int(input("Enter a number:"))

print("Positive" if num>0 else ("Negative" if num<0 else "Neither positive nor negative"))



Related Articles

Ask Us Anything !