Problem statement: Create and design an application that lets the user enter a string containing a series of numbers separated by commas. This is an example of valid input: 7, 9, 10, 2, 18, 6 The program should calculate and display the sum of all the numbers entered as input.

Respuesta :

Answer:

Following are the program which is given below:  

#include <iostream> //defining header file

using namespace std;

int main() //defining main method

{

int a[10],i=0,sum=0,n2; //defining integer variables

cout<<"Enter total element you want to insert: "; //print message

cin>>n2; //input values

for(i=0;i<n2;i++) //defining loop to input value from user end

{

   cin>>a[i]; //input values

}

cout<<"array elements: "; //print message

for(i=0;i<n2;i++) //defining loop for print value

{

   cout<<a[i]<<",";

}

for(i=0;i<n2;i++) //defining loop to calculate sum

{

   sum=sum+a[i];//add all values

}

cout<<endl<<"sum: "<<sum; //print sum

   return 0;

}

Output:

Enter total element you want to insert: 6

7

9

10

2

18

6

array elements: 7,9,10,2,18,6,

sum: 52

Explanation:

The description of the program code as follows:

  • In the above program integer variables "i, n, and sum" and a single dimension array "a[]" is declared.
  • In the next step variable "n" is used, that input a value from the user end.
  • Then three for loop is declared, in the first, for loop it will input values from the user-end, in the second loop it will print all the value of the array and separated by commas.
  • In the last loop, it will calculate the sum of the given number and print its final value.