skip to main | skip to sidebar

Java Programs and Examples with Output

Pages

▼
 
  • RSS
  • Twitter
Monday, January 16, 2012

Write a program to generate 20 random values of type double between -50 and +50

Posted by Admin at 9:11 AM – 0 comments
 
Write a program to generate 20 random values of type double between -50 and +50 and use the printf() method for System.out to display them with two decimal places in the following form:

   1) +35.93   2) -46.94   3) +42.27   4) +32.09   5) +29.21
   6) +13.87   7) -47.87   8) +30.67   9) -25.20  10) +29.67
  11) +48.62  12)  +6.70  13) +28.97  14) -41.64  15) +16.67
  16) +17.01  17)  +9.62  18) -15.21  19)  +7.46  20)  +4.09
 
import static java.lang.Math.random;

public class FormatDoubleData {
  public static void main(String[] args) {
    double[] data = new double[20];

    for (int i = 0 ; i < data.length ; ++i) {
      data[i] = 100.0*random() - 50.0;
    }
    int i = 0;
    for(double x : data) {
      System.out.printf("%4d)%+7.2f", ++i, x);
      if(i%5 == 0) {
        System.out.println();
      }
    }
  }
}


Output of Above Java Program


   1) +20.79   2) +34.37   3) -49.38   4) +15.51   5) -23.63
   6) +41.13   7)  -3.22   8) +48.90   9)  +1.98  10)  -9.97
  11)  -0.05  12) +27.93  13) +22.23  14) -15.53  15) -11.51
  16) -32.27  17) +34.89  18) +49.76  19) +31.99  20) -25.20
Labels: Basic Java Programs , Random Function

Leave a Reply

Newer Post Older Post
Subscribe to: Post Comments ( Atom )
  • Popular
  • Recent
  • Archives
Powered by Blogger.
 
 
 
© 2011 Java Programs and Examples with Output | Designs by Web2feel & Fab Themes

Bloggerized by DheTemplate.com - Main Blogger