C Program to Calculate Sum and Average of 1D Array. 1D Array के Elements का Sum और Average निकालने का C प्रोग्राम
A one-dimensional (1D) array stores multiple elements of the same data type. We can calculate the sum of all elements and their average using a loop.
One-dimensional (1D) Array में एक ही Data Type के कई Elements Store किए जाते हैं। Loop की सहायता से सभी Elements का Sum और Average निकाला जा सकता है।
Formula (सूत्र)
Sum = Addition of all array elements (सभी Array Elements का जोड़)
Average = Sum / Total number of elements (Sum / कुल Elements की संख्या)
C Program
#include <stdio.h>#define N 5int main(){int list[N], i, sum = 0;float avg;printf("Enter Elements of List Array\n");for (i = 0; i < N; i++){printf("Enter %d Element = ", i + 1);scanf("%d", &list[i]);sum = sum + list[i];}avg = (float)sum / N;printf("Sum = %d and Average = %.2f\n", sum, avg);return 0;}
Sample Output (आउटपुट)
Enter Elements of List ArrayEnter 1 Element = 10Enter 2 Element = 20Enter 3 Element = 30Enter 4 Element = 40Enter 5 Element = 50Sum = 150 and Average = 30.00
Explanation (व्याख्या)
1. Array Declaration (Array की घोषणा)
int list[N], i, sum = 0;float avg;
The array stores five integer values. sum is initialized to zero, and avg stores the average.
Array में पाँच Integer Values Store होती हैं। sum को Zero से Initialize किया गया है और avg Average Store करता है।
2. Calculating Sum (Sum निकालना)
sum = sum + list[i];Each array element is added to sum inside the loop.
Loop के अंदर प्रत्येक Array Element को sum में जोड़ा जाता है।
3. Calculating Average (Average निकालना)
avg = (float)sum / N;The sum is divided by the total number of elements. Type casting ensures that the average can contain a decimal value. This program demonstrates how to use a 1D array and a loop to calculate the sum and average of its elements.
Sum को कुल Elements की संख्या से Divide किया जाता है। Type Casting के कारण Average में Decimal Value भी आ सकती है। यह प्रोग्राम 1D Array और Loop की सहायता से उसके Elements का Sum और Average निकालना सिखाता है।
Comments
Post a Comment