C++ Program to add the distinct elements of an array [DEVCPP/GCC]


PROGRAM

//Program to add the distinct elements of an array

# include <iostream>
using namespace std;

int main()
{
int arr[5],i,j,sum=0;
     bool flag;

cout<<"ENTER THE ELEMENTS OF AN ARRAY:\n";
for(i=0;i<5;i++)     // Input an array
{
cin>>arr[i];
}

for(i=0;i<5;i++)
{
flag=true;
for(j=0;j<i;j++)
{
if(arr[i]==arr[j]) //check for uniqueness
{
flag=false;
break;
}
}
if(flag)
{
sum=sum+arr[i]; //summation of distinct elements
}
}

cout<<"SUM IS:"<<sum;
return 0;
}

OUTPUT

C++ Program to add the distinct elements of an array with output

Share this

Related Posts

FIND US ON FACEBOOK!