Check whether a given number is positive or negative

Problem statement:- Program to Check whether a given number is positive or negative.

Data requirement:-

   Input Data:- num

  Output Data:-
string output

Program in C

Here is the source code of the C Program to Check whether a given number is positive or negative.

Code:

#include<stdio.h>
int main()
{
    int num;
    printf("Enter a number:");
    scanf("%d",&num);
   if(num<0)
    printf("The number is negative");
   else if(num>0)
    printf("The number is positive");
   else
    printf("The number is neither negative nor positive");
}

Input/Output:
Enter a number:789
The number is positive

Program in C++

Here is the source code of the C++ Program to Check whether a given number is positive or negative.

Code:

#include<iostream>
using namespace std;

int main()
{
    int num;
    cout<<"Enter a number:";
    cin>>num;
   if(num<0)
    cout<<"The number is negative";
   else if(num>0)
    cout<<"The number is positive";
   else
    cout<<"The number is neither negative nor positive";
}

Input/Output:
Enter a number:-455
The number is negative

Program in Java

Here is the source code of the Java Program to Check whether a given number is positive or negative.

Code:

import java.util.Scanner;
public class PositiveNegative {

public static void main(String[] args) {
Scanner cs=new Scanner(System.in);
int num;
   System.out.println("Enter a number:");
    num=cs.nextInt();
   if(num<0)
   System.out.println("The number is negative");
   else if(num>0)
   System.out.println("The number is positive");
   else
   System.out.println("The number is neither negative nor positive");
cs.close();
}
}

Input/Output:
Enter a number:
897
The number is positive

Program in Python

Here is the source code of the Python Program to Check whether a given number is positive or negative.

Code:

num=int(input("Enter a number:"))
if(num<0):
    print("The number is negative")
elif(num>0):
    print("The number is positive")
else:
     print("The number is neither negative nor positive")

Post a Comment

0 Comments