# Java Source code: How to Add numbers inside an Array Using Recursion

## Get A Website Plus a Free Domain Name in Just 1 Hour!

Bring the new technology in your hands! Share your skills, improve and impress. Get Your Own Website and a Free Domain Name Here!

**Add numbers inside An Array**

The following codes is a Java source code on adding numbers inside the array. This is just a short program and also a very simple one to illustrate how an array works in Java programming language. I will also upload the code that uses for loop so that you can compare source codes on which you prepare the most, though in my own opinion I prepare looping in for loop but for the sake of knowledge lets learn another looping mechanism which is recursion. Here is the codes,

**Java Source code: How to Add Numbers inside an Array Using Recursion**

//Java source codes on adding numbers inside the array using recursion //java class public class Array { public static int array( int[] arr, int first, int last) { // int sum = 0; if(arr[first] == arr[last])/* must be if(first == last),but try this one too, study the code, it is interesting */ { return arr[first]; } else { return arr[first] + array(arr, first+1, last); } } } //main class import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.print("Enter the size of the input you want to enter: "); int size = input.nextInt(); int[] numArr = new int[size]; System.out.print("Enter "+ size +" numbers: "); for(int i=0; i<numArr.length; i++) { numArr[i]=input.nextInt(); } System.out.print("The sum of the numbers is: "+ Array.array(numArr, 0 , size-1) ); } }

**Sample Output**

Enter the size of the input you want to enter:* 5*

Enter 5 numbers:* 3 46 50 34 150*

**The sum of the numbers is: 283**

Since, I used static on my java method, I did not declare an object to call the class Array on the main class to access the method, simply code it on that way and it will run.

## Java Tutorials and Tips

- Java Class: Learn More About Classes in Java
- Java Tutorial Examples
- Java Tutorial for Beginners: A Beginners Guide on Learning Java Programming
- Java Simple Codes for Beginners
- How to Program in Java: Complete Simple Easy Steps
- Basic Knowledge Required in Programming
- 5 Important Tips to Learn Java Programming and Other Programming Languages

## Other Java Source Code Examples

- Java Simple Codes for Beginners
- A Java source code: How to Output Asterisk in Different Forms Using Recursion
- Java Source Code: A Recursive Asterisk Diamond Shape
- Java Source code on Adding Numbers inside the Array using For Loop
- Java Source Code: How to Sort Numbers using Selection Sort
- Java Source Code: How to Sort Numbers in Bubble Sort Using Recursion
- Java Source Code: Binary Search in Recursion
- Java Source code: How to Print a String Backward using Recursion
- Java Source Code on Linear Search Using Recursion
- Java Source code on Printing the Greatest Common Divisor (GCD) using Recursion
- Java source code: How to Output the Answer of the Integer X to the Power Y
- Java Source Code: How to make a Program that will determine a Person's Salutation and Current Age
- Java Source Code: Recursive Snow Flakes

## Comments

Hey, i was looking through this and i found an error(?) in the code.

Doesn't this:

if(arr[first] == arr[last])

screw up if you for example choose: 15 22 35 15

whereas the first and the last number is the same?

Or in any case where the last number is the same as any other number.

I believe it oughta be something like:

if(first == last)

Mastering how to work with loops and arrays is a must for the would be programmers. I'm glad you shared these basics here. By the way, could you be teaching Java? Just curious. Voted up, useful and shared.

Dear Aisha, you can get solve this issue with out development team i am giving you link of for you help for our developers.

Just wanna input on few general things, The website pattern is perfect, the subject material is real excellent. Believe those who are seeking the truth. Doubt those who find it. by Andre Gide. bgdfagcedeea

6