Blog C Interview Tutorials

C Program to get the product of array elements

We’ve been given an array A[N] of N numbers of element. We need to find the product of it’s element.

Like we have an array arr[9] of 9 elements so its product will be like

Product=8,977,500,000,000

Example

Input: arr[] = { 10, 20, 3, 4, 8 }
Output: 19200
Explanation: 10 x 20 x 3 x 4 x 8 = 19200
Input: arr[] = { 1, 2, 3, 4, 3, 2, 1 }
Output: 144

Steps for solving this problem

  • Take an input for the size of array
  • Take an array as input
  • Iterate the array and multiply each element of the array
  • Print the result
Algorithm

Start
In function int prod_mat(int arr[], int n)
Step 1-> Declare and initialize result = 1
Step 2-> Loop for i = 0 and i < n and i++ result = result * arr[i]; Step 3-> Return result
int main()
Step 1-> Declare an array arr[]step 2-> Declare a variable for size of array
Step 3-> Print the result

Example

#include<stdio.h>
int main()
{
	int N;
	scanf("%d",&N);
	long answer=1;
	int A[N];

		for(int i=0;i<N;i++)
		{
			scanf("%d",&A[i]);
		}
		for(int i=N-1;i>0;i--)
		{
			
			answer=(answer*A[i]);
			
		}
		printf("%ld",answer);
	
}

Input

9
1 200 50 70 30 15 10 19 150

Output

If run the above code it will generate the following output −

8,977,500,000,000

For more Python related blogs Visit Us Geekycodes . Follow us on Instagram.

If you’re a college student and have skills in programming languages, Want to earn through blogging? Mail us at geekycomail@gmail.com

Leave a Reply

%d bloggers like this: