Home Tutorial C C calculate sum of first and last element of array

 
 

Share on Google+Share on Google+
C calculate sum of first and last element of array
Posted on: January 29, 2010 at 12:00 AM
Advertisement
In this section, you will learn how to calculate sum of first and last element from the array of five numbers.

C calculate sum of first and last element of array

In this section, you will learn how to calculate sum of first and last element from the array of five numbers. You can see in the given example, we have allowed the user to enter five numbers of their choice. These numbers will get stored into the array as the user enters the number. Then using the index of the array, we have found first and the last number and determine their sum.

Here is the code:

#include <stdio.h>
#include
<conio.h>
void
main() {
           int
i;
           int list[5];
           int first, last;
           int sum, num;
           clrscr();
           printf("Enter five numbers:");
           for (i = 0; i < 5; i++) {
                 scanf("%d", &num);
                 list[i] = num;
           }
           first = list[0];
           last = list[4];
           sum = first + last;
           printf("Sum of first and last number is: %d", sum);
           getch();
}

Output:

Enter five numbers:1
2
3
4
5

Sum of first and last number is: 6

 

Advertisement

Related Tags for C calculate sum of first and last element of array:


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: January 29, 2010

Recommend the tutorial

Advertisements Advertisements
 

 

 

DMCA.com