Computer Science, asked by sdkjack8052, 10 months ago

How to find the sum of array elements without loop

Answers

Answered by gurukulamdivya
0

Answer:

we can find using recursive function or goto statement

/ 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;  

}

Explanation:

Similar questions