Sum and Product of all Numbers in an Array coderinme

Sum and Product of all Numbers in an Array coderinme

Sum and Product of all Numbers in an Array coderinme
In this program we will first ask the user for the total number of elements in the array. After scanning the number of elements, we will run a for loop to enter the values in the array. Later on, once again we run a for loop for adding up and producting all the elements present in the given array. This can also be done using a single for loop.
To find the Sum and Product of all numbers, present in a floating point array, using single for loop we can code as follows:

#include<stdio.h>
int main()
{
float a[20],sum = 0, product = 1;
int n,i;
printf("Enter the number of elements: ");
scanf("%d",&n);
for( i = 0 ; i < n ; i++ )
{
printf("A[%d]: ",i);
scanf("%f",&a[i]);
sum += a[i];
product *= a[i];
}
printf("The Sum of numbers: %.2f\n",sum);
printf("The product of numbers: %.2f\n",product);
getch();
return 0;
}

For more programs in C, visit our C Coder Archives

Sum and Product of all Numbers in an Array coderinme

All rights reserved. No part of this Post may be copied, distributed, or transmitted in any form or by any means, without the prior written permission of the website admin, except in the case of brief quotations embodied in critical reviews and certain other noncommercial uses permitted by copyright law. For permission requests, write to the owner, addressed “Attention: Permissions Coordinator,” to the admin @coderinme

Leave a reply:

Your email address will not be published.