Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6039201
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T06:20:03+00:00 2026-05-23T06:20:03+00:00

It was pointed out to me that the statement below is not recursion. I

  • 0

It was pointed out to me that the statement below is not recursion. I thought recursion only means that it calls itself until the answer is found. What would make this recursion?

public static double totalDistance(int[] x, int[] y, String[] city, int i){

    double xSub = x[i] - x[i-1];
    double ySub = y[i] - y[i-1];
    double distance = Math.pow(xSub, 2) + Math.pow(ySub, 2);
    distance = Math.round(Math.sqrt(distance));
    System.out.println("Distance From " + city[i] + " to " + city[i-1] + " is " + distance + " miles.");

   if (i == 1){
       return distance;  
   }
   else {
      return distance+totalDistance(x,y,city, i-1);
   }
}

This is entire code below in case anyone is curious to what is going on…

import java.util.Scanner;
class distance {


public static void main(String[] args) {

    System.out.println("Welcome to Travel Bliss Distance Calculator!");
    Scanner input = new Scanner(System.in);
    int[] x = new int[5];
    int[] y = new int[5];
    String[] city = new String[5];


    int i=0;
    for (i=0; i < 5;i++){
        System.out.println("Enter City>>");
        city[i] = input.next();
        System.out.println("Enter X Coordinates>>");
        x[i] = input.nextInt();
        System.out.println("Enter Y Coordinates>>");
        y[i] = input.nextInt();
        System.out.println("You Entered: " + city[i] + " with Coordinates: (" + x[i] + "," + y[i] + ") ");


    }
    i = i-1;
    System.out.println("============================================================");

    System.out.println("Calculating Distance Between: " + city[0] +", " + city[1] + ", " + city[2] + ", " + city[3] + ", " + city[4]+" >>>");
    System.out.println("TOTAL of: "+ totalDistance(x, y, city, i)+ " miles.");

}


public static double totalDistance(int[] x, int[] y, String[] city, int i){

    double xSub = x[i] - x[i-1];
    double ySub = y[i] - y[i-1];
    double distance = Math.pow(xSub, 2) + Math.pow(ySub, 2);
    distance = Math.round(Math.sqrt(distance));
    System.out.println("Distance From " + city[i] + " to " + city[i-1] + " is " + distance + " miles.");

   if (i == 1){
       return distance;  
   }
   else {
      return distance+totalDistance(x,y,city, i-1);
   }
}
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T06:20:04+00:00Added an answer on May 23, 2026 at 6:20 am

    The totalDistance(…) function is indeed recursive (since it calls itself).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

A lot of people on SO pointed out that I should use using statement
I'm reading about STL algorithms and the book pointed out that algorithms like find
Recently, a coworker pointed out to me that compiling everything into a single file
Edit: It's not a bug as Martin pointed out. I'm just crossing the daylight
I always thought they were about the same thing but someone pointed me out
Quick edit: As was pointed out below I should be doing a BFS but
As pointed out in an answer to this question , the compiler (in this
As gdoron pointed out, var a = a; var b = b; a =
In a reply to a post I made the other day Dimitre pointed out,
A friend brought this to my attention, and after I pointed out an oddity,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.