mathajax

Java program Is Integer Positive or Negative and sign function


  • Find a given integer is positive or negative integer  
  • Reader input num from scanner object
  • check num > 0 , print "positive"
  • check num==0  print "not positive/negative"
  • else , print "negative"


import java.util.Scanner;

public class PosNeg {

 public static void main(String[] args) 
  {
  
  Scanner sc= new Scanner(System.in);
  System.out.println("Find a given number is Positive or Negativen");
  System.out.println("Enter a integer");
  int num=sc.nextInt();  
  sc.close();
       
  if ( num>0 ) {   
  System.out.println( num + " is positive integer" );
  }
  else if(num==0)  {
  System.out.println( num + " not postiver/negative integer" );
  } 
  else  {
  System.out.println( num + " is negative integer" );
  }
 }
}


Output
Find a given number is Positive or Negative

Enter a integer
-6
-6 is negative integer

Output2
Find a given number is Positive or Negative

Enter a integer
11
11 is positive integer


  • find sign of integer
  • Read a input integer num from scanner object
  • check num> 0, print '1'
  • else check num==0, print '0'
  • else , print '-1'



import java.util.Scanner;
public class NumSign {

 public static void main(String[] args) 
{
  Scanner sc= new Scanner(System.in);  
  System.out.println("Sign  functionn");
  System.out.println("Enter a value");
  int num=sc.nextInt();  
  sc.close();  
     int sign=0;
     
  if ( num >0 ) {  
   sign = 1;   
  } else if (num < 0)  {
   sign = -1;   
  } else {
   sign =0;
  }
   
  System.out.println( "Sign(" + num + ") is: "+ sign );
 }

}


Output
Sign  function

Enter a value
10
Sign(10) is: 1

Output 2
Sign  function

Enter a value
-8
Sign(-8) is: -1

Output 3
Sign  function

Enter a value
0
Sign(0) is: 0

Comments

Popular posts from this blog

Matrix Forward and Back Substitution

Solving System of Linear Equations by Gauss Jordan Elimination

Solve System of Linear Equations by LU Decompose

Distance Metric - Euclidean Distance

Matrix in Java