Sum of array Elements without using loops and recursion
Last Updated :
21 May, 2024
Given an array of N elements, the task is to find the Sum of N elements without using loops(for, while & doWhile) and recursion.
Examples:
Input: arr[]={1, 2, 3, 4, 5}
Output: 15
Input: arr[]={10, 20, 30}
Output: 60
j
Approach: Unconditional Jump Statements can be used to solve this problem.
Unconditional Jump Statements:
Jump statements interrupt the sequential execution of statements, so that execution continues at a different point in the program. A jump destroys automatic variables if the jump destination is outside their scope. There are four statements that cause unconditional jumps in C: break, continue, goto, and return.
To solve this particular problem, goto statement can be useful.
goto Statement:
The goto statement is a jump statement which is sometimes also referred to as unconditional jump statement. The goto statement can be used to jump from anywhere to anywhere within a function.
Syntax:
Syntax1 | Syntax2
----------------------------
goto label; | label:
. | .
. | .
. | .
label: | goto label;
In the above syntax, the first line tells the compiler to go to or jump to the statement marked as a label. Here label is a user-defined identifier which indicates the target statement. The statement immediately followed after 'label:' is the destination statement. The 'label:' can also appear before the 'goto label;' statement in the above syntax.

Below is the implementation of the above approach:
C++
// C++ program to find the sum of
// N elements with goto statement
#include <iostream>
using namespace std;
// Function to perform desired operation
int operate(int array[], int N)
{
int sum = 0, index = 0;
label:
sum += array[index++];
if (index < N) {
// backward jump of goto statement
goto label;
}
// return the sum
return sum;
}
// Driver Code
int main()
{
// Get N
int N = 5, sum = 0;
// Input values of an array
int array[] = { 1, 2, 3, 4, 5 };
// Find the sum
sum = operate(array, N);
// Print the sum
cout << sum;
}
C
// C program to find the sum of
// N elements with goto statement
#include <stdio.h>
// Function to perform desired operation
int operate(int array[], int N)
{
int sum = 0, index = 0;
label:
sum += array[index++];
if (index < N) {
// backward jump of goto statement
goto label;
}
// return the sum
return sum;
}
// Driver Code
int main()
{
// Get N
int N = 5, sum = 0;
// Input values of an array
int array[] = { 1, 2, 3, 4, 5 };
// Find the sum
sum = operate(array, N);
// Print the sum
printf("%d", sum);
}
Java
// Java program to find the sum of
// N elements
class GFG
{
// Function to perform desired operation
static int operate(int array[], int N)
{
int sum = 0, index = 0;
while(true)
{
sum += array[index++];
if (index < N)
{
// backward jump of goto statement
continue;
}
else
{
break;
}
}
// return the sum
return sum;
}
// Driver code
public static void main(String[] args)
{
// Get N
int N = 5, sum = 0;
// Input values of an array
int array[] = { 1, 2, 3, 4, 5 };
// Find the sum
sum = operate(array, N);
// Print the sum
System.out.print(sum);
}
}
// This code is contributed by divyeshrabaiya07
Python3
# Python3 program to find the sum of
# N elements
# Function to perform desired operation
def operate(array, N) :
Sum, index = 0, 0
while(True) :
Sum += array[index]
index += 1
if index < N :
# backward jump of goto statement
continue
else :
break
# return the sum
return Sum
# Get N
N, Sum = 5, 0
# Input values of an array
array = [ 1, 2, 3, 4, 5 ]
# Find the sum
Sum = operate(array, N)
# Print the sum
print(Sum)
# This code is contributed by divyesh072019
C#
// C# program to find the sum of
// N elements with goto statement
using System;
class GFG
{
// Function to perform desired operation
static int operate(int[] array, int N)
{
int sum = 0, index = 0;
label:
sum += array[index++];
if (index < N)
{
// backward jump of goto statement
goto label;
}
// return the sum
return sum;
}
// Driver Code
public static void Main()
{
// Get N
int N = 5, sum = 0;
// Input values of an array
int[] array = { 1, 2, 3, 4, 5 };
// Find the sum
sum = operate(array, N);
// Print the sum
Console.Write(sum);
}
}
// This code is contributed
// by Akanksha Rai
PHP
<?php
// PHP program to find the sum of N
// elements with goto statement
function operate($array, $N)
{
$sum = 0;
$index = 0;
label:
$sum += $array[$index++];
if($index < $N)
{
// backward jump of goto statement
goto label;
}
// return the sum
return $sum;
}
// Driver code
$N = 5;
$array = array(1, 2, 3, 4, 5);
echo operate($array, $N);
// This code is contributed
// by Mohit kumar 29
?>
JavaScript
<script>
// Javascript program to find the sum of
// N elements
// Function to perform desired operation
function operate(array, N)
{
let sum = 0, index = 0;
while(true)
{
sum += array[index++];
if (index < N)
{
// backward jump of goto statement
continue;
}
else
{
break;
}
}
// return the sum
return sum;
}
// Get N
let N = 5, sum = 0;
// Input values of an array
let array = [ 1, 2, 3, 4, 5 ];
// Find the sum
sum = operate(array, N);
// Print the sum
document.write(sum);
// This code is contributed by suresh07.
</script>
Time complexity: O(N) where N is size of given array
Auxiliary space: O(1)
Similar Reads
Sum of array elements using recursion Given an array of integers, find sum of array elements using recursion. Examples: Input: arr = [1, 2, 3]Output: 6Explanation: 1 + 2 + 3 = 6Input: arr = [15, 12, 13, 10]Output: 50Explanation: 15 + 12 + 13 + 10 = 50We have discussed iterative solution in this Post Sum of elements in a given array. In
2 min read
Tail recursion to calculate sum of array elements. Given an array arr, we need to find the sum of its elements using Tail Recursion Method. We generally want to achieve tail recursion so that compilers can optimize the code. If the recursive call is the last statement, the compiler can optimize it by eliminating the need to store the parent call's s
3 min read
Ways to sum to N using array elements with repetition allowed Given a set of m distinct positive integers and a value 'N'. The problem is to count the total number of ways we can form 'N' by doing sum of the array elements. Repetitions and different arrangements are allowed. Examples : Input: arr = {1, 5, 6}, N = 7Output: 6Explanation: The different ways are:1
6 min read
Sum of array elements after reversing each element Given an array arr[] consisting of N positive integers, the task is to find the sum of all array elements after reversing digits of every array element. Examples: Input: arr[] = {7, 234, 58100}Output: 18939Explanation:Modified array after reversing each array elements = {7, 432, 18500}.Therefore, th
7 min read
Modified Range Sum in a given Array without updates Given an array arr[] of size N containing distinct numbers from 1 to N in any order, the task is to perform a modified range sum in this array according to the following rules. For each index 'i' in array arr: The starting index of the range 'L' is selected as i + 1The ending index of the range 'R'
8 min read
Sum of odd Array elements after each update query Given an integer array Arr[] of length N and an array Queries[] of length Q where Queries[i] = [valuei, indexi]. For each query i, update Arr[indexi] = Arr[indexi] + valuei, then print the sum of the all the odd values of Arr[]. Examples: Input: N = 4, Arr = [1,2,3,5], Q = 4, Queries = [[1,0],[-3,1]
15+ min read