Java Program to calculate distance light travels


To calculate distance light travels, we need to follow the basic formulae to calculate distance.

Distance = Speed x Time

Here, the following are the parameters −

speed = 186000;
days = 365;
seconds = days * 24 * 60 * 60;

Above, we calculated the time in seconds for an year −

days = 365;
seconds = days * 24 * 60 * 60;

The following is the complete example to calculate distance light travels −

Example

 Live Demo

public class Demo {
   public static void main(String[] args) {
      int speed;
      long days, seconds, dist;
      speed = 186000;
      days = 365;
      seconds = days * 24 * 60 * 60;
      dist = speed * seconds;
      System.out.print("Light travels: "+dist + " miles");
   }
}

Output

Light travels: 5865696000000 miles

Updated on: 30-Jul-2019

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements