Positive and Negative

Java program to find a number is positive or negative 

There are different ways to identify positive or negative number in Java.

  1. Write a program to find Number is a Positive or Negative in simple way.
  2. Program to Count Positive and Negative number in an Array

Program 1: 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.



Program 2 : Program to Count Positive and Negative Numbers in an Array

Step by step execution:

  1. Take Input as a array size and then array elements.
  2. Traverse the elements in the array one by one.
  3. For each element, check if the element is less than 0. If it is, then increment the count of negative elements.
  4. For each element, check if the element is greater than 0. If it is, then increment the count of positive elements.
  5. Print the count of negative and positive elements.
Code in JAVA:
import java.util.Scanner;
public class count 
{
public static void main(String[] args) 
{
int Positive_count=0,Negative_count=0;
Scanner scan = new java.util.Scanner(System.in);
System.out.println("Enter the Size of Array: ");
int size = scan.nextInt();
int [] number=new int[size];
System.out.println("Enter the elements of the Array: ");
for(int i=0;i<size;i++)
{
number[i]=scan.nextInt();
}
for(int i=0;i<size;i++)
{
if (number[i] >= 0)
{
Positive_count++;
}
else
{
Negative_count++;
}
}
System.out.println("The total number of Positive numbers in array are:- "+ Positive_count);
System.out.println("The total number of Negative numbers in array are:- "+ Negative_count);
}
}
Output:
Enter the Size of Array:9
Enter the elements of the Array: 12
-5
3
0
-9
-62
81
6
69
The total number of Positive numbers in array are:- 6
The total number of Negative numbers in array are:- 3


No comments:

Post a Comment

If you have any doubts, please let me know

Pattern 31 & 32