Dr. A.P.J. Abdul Kalam Technical University, Lucknow
KCS101 / KCS201 Programming for Problem Solving - Using C Language
Lab Exercises
21. Write a program that simply takes elements of the array from the user and finds the sum of these elements.
/*
File: Prgrm21.c
Author: Aditya Saini
Date: Sept 30, 2019
Description: Program to find the sum of elements of an array.
*/
#include <stdio.h>
int main (void)
{
int array [5];
int sum;
int i;
//Input elements of array
for (i = 0; i < 5; i++)
{
printf ("Input %d element: ", i + 1);
scanf ("%d", &array [i]);
}
//Finding the sum of elements
for (i = 0, sum = 0; i < 5; i++)
{
sum = sum + array [i];
}
//Output the result
printf ("Sum of elements: %d", sum);
int sum;
int i;
//Input elements of array
for (i = 0; i < 5; i++)
{
printf ("Input %d element: ", i + 1);
scanf ("%d", &array [i]);
}
//Finding the sum of elements
for (i = 0, sum = 0; i < 5; i++)
{
sum = sum + array [i];
}
//Output the result
printf ("Sum of elements: %d", sum);
};
Output
Input 1 element: 2
Input 2 element: 5
Input 3 element: 8
Input 4 element: 7
Input 5 element: 4
Sum of elements: 26
Input 2 element: 5
Input 3 element: 8
Input 4 element: 7
Input 5 element: 4
Sum of elements: 26
No comments:
Post a Comment
Please do not post spam links.