Monday, 17 August 2020

Positive or Negative number.

 

Write a program to find  Number is a Positive number or Negative number.


The following concept will test weather; a number is positive or negative. It is done by checking where the number lies on the number line. 

Input:  Enter Any Number
If Number = 53
Output = Number is Positive
If Number = -89
Output = Number is Negative

Code in C:


#include<stdio.h>
int main()
{
    int number;
       printf("Insert any number: ");
       scanf("%d", &number);

//Logic: Condition to check if the number is negative or positive
     if (number == 0)
     {
          printf("The number is neither Negative nor Positive; number is Zero 0.");
      }
     else if (number <0)
    {
          printf("The number is negative");
    }
    else
    {
         printf("The number is positive");
    }
}
Code in C++:
#include<iostream>
using namespaces std;
int main()
{
   int number;
     cout<<"Insert any number: ";
     cin>>number;
//Logic: Condition to check if the number is negative or positive
     if (number == 0)
     {
         cout<<"The number is neither Negative nor Positive; number is Zero 0.";
     }
     else if (number <0)
    {
         cout<<"The number is negative";
    }
    else
    {
         cout<<"The number is positive";
    }
}

Code in Java:
import java.util.Scanner;
public class positiveandnagative {
public static void main(String[] args) {
Scanner scan = new java.util.Scanner(System.in);
System.out.println("Enter the number: ");
int number=scan.nextInt();
//condition used to identify number
if (number == 0)
{
System.out.println("The number is neither Negative nor Positive; number is Zero 0.");
        }
else if (number < 0)
{
System.out.println("The number is negative");
}
else
{
System.out.println("The number is positive");
}
}
}
output:
Enter the number: 
-29
The number is negative
Enter the number: 
34
The number is positive
Enter the number: 
0
The number is neither Negative nor Positive; number is Zero 0.

No comments:

Post a Comment

If you have any doubts, please let me know

Pattern 31 & 32