Java Program to calculate distance light travels

Java 8Object Oriented ProgrammingProgramming

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
raja
Published on 26-Dec-2018 10:50:21
Advertisements