Sum of array elements || user input || Arrays || C++

In this, we are going sum of array elements by taking input from user in C++.

The code given below can be used for TURBO C++ Compiler:-

#include<iostream.h>
#include<conio.h>

void main ()
{
    clrscr();
    int a[10], n, i, sum = 0;
    cout << "Enter the size of the array : ";
    cin >> n;
    cout << "\nEnter the elements of the array : ";
    for (i = 0; i < n; i++)
    cin >> a[i];
    for (i = 0; i < n; i++)
    {
        sum += a[i];       
    }
    cout << "\nSum of array elements : " << sum;  
    getch();
}
  
  

The code given below can be used for g++/gcc Compiler:-

#include<iostream>
using namespace std;
int main ()
{
    int a[10], n, i, sum = 0;
    cout << "Enter the size of the array : ";
    cin >> n;
    cout << "\nEnter the elements of the array : ";
    for (i = 0; i < n; i++)
    cin >> a[i];
    for (i = 0; i < n; i++)
    {
        sum += a[i];       
    }
    cout << "\nSum of array elements : " << sum;  
    return 0;
}

#ENJOY CODING


Post a Comment

FOR ANY DOUBTS AND ERRORS FEEL FREE TO ASK. YOUR DOUBTS WILL BE ADDRESSED ASAP

Previous Post Next Post