- HubPages»
- Technology»
- Computers & Software»
- Computer Science & Programming»
- Programming Languages
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