Tuesday, September 30, 2014

Example the question ask you to write a program to determine the number that user enter which is positive,negative or Zero number...

The program below will ask the user to enter one number, and the program will determine the number whether the number is positive, negative or zero. and the program will ask the user whether they want to continue or stop the program.

Sample Output:

Source code:

import java.util.Scanner;
public class posnegnum
{
 public static void main (String [] arg)
 {
 Scanner input = new Scanner(System.in);

 int j = 0;

 do
  {
   System.out.println("Enter a number:");
   double n = input.nextDouble();
 
   if( n>0 )
    System.out.println(n +" is a positive number");
   else if( n<0 )
    System.out.println(n +" is a negative number");
   else
    System.out.println(n +" is a zero number");
   
    j++;
   
    System.out.println("do you want to continue? (enter 1 to continue 0 to stop) " );
    j = input.nextInt();
   
  }while(j == 1);
 
 }

}

Tagged:

0 comments:

Post a Comment

Zul Ameen Blog © 2013 | Powered by Blogger | Blogger Template by DesignCart.org