Java long type

Introduction

Java long type is a signed 64-bit type.

It is useful when an int type is not large enough to hold the desired value.

Calculate the number of miles that light will travel in 100 days using Java

We need to calculate the distance that light can travel in 100 days.

The approximate speed of light in miles per second 186000 miles.

Use that number to calculate the number of miles that light will travel in 100 days.

You can use the following code structure:


// Compute distance light travels using long variables.  
public class Main{  
  public static void main(String args[]) {  
    //your code here
  }  
} 



// Compute distance light travels using long variables.  
public class Main{  
  public static void main(String args[]) {  
    int lightspeed;  
    long days;  
    long seconds;  
    long distance;  
  
    // approximate speed of light in miles per second  
    lightspeed = 186000;  
  
    days = 1000; // specify number of days here  
  
    seconds = days * 24 * 60 * 60; // convert to seconds  
  
    distance = lightspeed * seconds; // compute distance  
  
    System.out.print("In " + days);  
    System.out.print(" days light will travel about ");  
    System.out.println(distance + " miles.");  
  }  
} 



PreviousNext

Related