Sum of array elements in C++ using pointer || Pointers || C++
In this, we are going to see how to perform the Addition of all elements in an Array using Pointers in C++ Programming Language.

The Code given below can be used for TURBO C++ Compilers: -

//C++ program to perform sum of an array using pointers.

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

void main()
{
    clrscr();
    float num[1000], sum;
    int n;
    sum = 0;

    cout << "Enter total number of terms:-\n";
    cin >> n;

    cout << "Enter the value of the elements:-\n";
    for (int i = 0; i < n; i++)
    {
        cin >> num[i];
    }

    for (int i = 0; i < n; i++)
    {
        float *pt = &num[i];
        sum = sum + (*pt);
    }

    cout << "Sum of the elements:- " << sum;
    getch();
}
//...........Coded by YASH ALAPURIA
  

The Code given below can be used for gcc/g++ Compilers: -

//C++ program to perform sum of an array using pointers.

#include <iostream>
#include <stdlib.h>
using namespace std;

int main()
{
    system("cls");
    float num[1000], sum;
    int n;
    sum = 0;

    cout << "Enter total number of terms:-\n";
    cin >> n;

    cout << "Enter the value of the elements:-\n";
    for (int i = 0; i < n; i++)
    {
        cin >> num[i];
    }

    for (int i = 0; i < n; i++)
    {
        float *pt = &num[i];
        sum = sum + (*pt);
    }

    cout << "Sum of the elements:- " << sum;
    return 0;
}
//...........Coded by YASH ALAPURIA
  
#ENJOY CODING

Post a Comment

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

Previous Post Next Post