Q:

C program to calculate sum, product of all One Dimensional Array Elements

0

C program to calculate sum, product of all One Dimensional Array Elements

This program will read N One Dimensional Array Elements, and calculate the Sum and Product of all elements and print the sum and product.

Logic to implement this program - Read array, Run a loop from 0 to N-1 and add each element in SUM variable and multiply each element in PRODUCT variable. Don’t forget to assign 0 in SUM and 1 in PRODUCT variables before running the loop.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Sum and Product of all 1D Array Elements using C program

/*Program to calculate Sum, Product of all elements.*/
 
#include <stdio.h>
  
int main() 
{ 
    int arr[10]; 
    int sum,product,i;
  
    /*Read array elements*/
    printf("\nEnter elements : \n"); 
    for(i=0; i<10; i++) 
    { 
        printf("Enter arr[%d] : ",i); 
        scanf("%d",&arr[i]); 
    } 
     
    /*calculate sum and product*/
    sum=0;
    product=1;
    for(i=0; i<10; i++)
    {
        sum=sum+arr[i];
        product=product*arr[i];
    }
       
    printf("\nSum of array is     : %d"  ,sum); 
    printf("\nProduct of array is : %d\n",product); 
  
    return 0; 
}

Output

    Enter elements : 
    Enter arr[0] : 11 
    Enter arr[1] : 22 
    Enter arr[2] : 3 
    Enter arr[3] : 4 
    Enter arr[4] : 5 
    Enter arr[5] : 66 
    Enter arr[6] : 7 
    Enter arr[7] : 8 
    Enter arr[8] : 9 
    Enter arr[9] : 10 

    Sum of array is     : 145 
    Product of array is : 534965504

Using User Define Function

/*Program to calculate Sum, Product of all elements.*/
 
#include <stdio.h>
 
/** funtion :   readArray() 
    input   :   arr ( array of integer ), size 
    to read ONE-D integer array from standard input device (keyboard). 
**/
void readArray(int arr[], int size) 
{ 
    int i =0; 
 
    printf("\nEnter elements : \n"); 
    for(i=0; i<size; i++) 
    { 
        printf("Enter arr[%d] : ",i); 
        scanf("%d",&arr[i]); 
    } 
} 
 
 
/** funtion :   getSum() 
    input   :   arr ( array of integer ), size 
    to get sum of all elements of array. 
**/
int getSum(int arr[], int size) 
{ 
    int i=0,sum=0; 
 
    for(i=0; i<size; i++) 
    { 
        sum += arr[i]; 
    } 
    return sum; 
} 
 
/** funtion :   getProduct() 
    input   :   arr ( array of integer ), size 
    to get product of all elements of array. 
**/
int getProduct(int arr[], int size) 
{ 
    int i=0,product=1; 
 
    for(i=0;i < size; i++) 
    { 
        product *= arr[i]; 
    } 
      
    return product; 
} 
 
int main() 
{ 
    int arr[10]; 
 
    readArray(arr,10); 
      
    printf("\nSum of array is     : %d"  , getSum    (arr,10)   ); 
    printf("\nProduct of array is : %d\n", getProduct(arr,10)   ); 
 
    return 0; 
}

Output

    Enter elements : 
    Enter arr[0] : 11 
    Enter arr[1] : 22 
    Enter arr[2] : 3 
    Enter arr[3] : 4 
    Enter arr[4] : 5 
    Enter arr[5] : 66 
    Enter arr[6] : 7 
    Enter arr[7] : 8 
    Enter arr[8] : 9 
    Enter arr[9] : 10 

    Sum of array is     : 145 
    Product of array is : 534965504

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

One Dimensional Array Programs / Examples in C programming language

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
C program to find Smallest and Largest elements fr... >>
<< C program to read and print One Dimensional Array ...