Go to Top

Thursday 31 March 2011

To check whether a number is negative or positive

Question 3 : Write a program in Java to to check whether a number given by the user is negative or positive.


Java Program :


public class number_type
{
 public static void main(int a)
 {
     if (a>0)
     System.out.println("The number "+a+" is positive");
     if (a<0)
     System.out.println("The number "+a+" is negative");
     if (a==0)
     System.out.println("The number "+a+" is neutral");
 }
}

Explanation : There is a simple logic applied here that if a number is greater than 0, it is positive, if it is less than 0, it is negative, and if it is equal to 0, then it is neutral.

Input : 


(i) a=10


(ii) a=-10


(iii) a=0


Output : 


(i) The number 10 is positive


(ii) The number -10 is negative


(iii) The number 0 is neutral

5 comments:

  1. yoo hoooooooooooooooooo I like it man nice work .but it's not the thing I need .

    ReplyDelete
  2. All are saying the same thing repeatedly, but in your blog I had a chance to get some useful and unique information, I love your writing style very much, I would like to suggest your blog in my dude circle, so keep on updates.
    Selenium training in Chennai
    Selenium training in Bangalore
    Selenium training in Pune
    Selenium Online training

    ReplyDelete

ShareThis