+ 1

# How to add n integers?

15 Answers

+ 40

I do not really understand the meaning of a simple , but here is the basis algorithm for adding n numbers (in the example my n=4) , hope this will help
int main() {
int arr[4]={3,4,10,1};
int sum=0;
for(int i=0;i<4;i++)
{
sum+=arr[i];
}
cout<<sum;
}

+ 20

You can store that n integers in array, and then add array elements using a for loop (instead of an array you may use other data structures such as list)

+ 3

in c++ we can ask user with "cin>>n;" and save this value in this n variable

+ 2

declare n (int n=0;), add a loop('for' or 'while' function), add 1 to the 'n' variable with 'n++;' (n++ is an abbreviation for 'n = n + 1'). It can't get simpler than that.

+ 1

Can we just ask user the value without giving any pre defined value for n

0

Is there a simple way then this? @emma

0

What's all this?

0

for me i think this will do it perfectly as a simple program to add n integers (you can improve it by use another variable to count n ) :
int main () {
int a ;
int sum = 0;
char yesNo = 'y';
while ( yesNo == 'y' ){
cout << "Please input No. to add \n" ;
cin >> a;
sum+=a;
cout << "Do you want to add another integer ? (y/n)\n";
cin >> yesNo;
}
cout << "Summation is = " << sum;
}

0

start array n=0 and sum=sum+arr[i] sum=0 and i=0 first time execute 3 and result is 18.

0

{
int n,sum=0,r;
printf("enter an integer:");
scanf("%d",&n);
while( n!=0)
{
r=n%10;
sum=sum+r;
n=n/10;
}
printf("sum=%d",sum);
}

0

Lets take n is total number you want to add. Suppose n=5.
Now get all 5 values you want to add.
for(i=0; i<n; i++) {
Scanf("%d", & value[i] );
}
We have entered our 5 values, similarly you can add any no. Of n value.
lets add them.
for(i=0;i<n;i++){
sum += value[i] ; // or sum=sum+value[i];
}
Both terms are same so you can write any one.
Now lets display on the screen.
Printf(" %d", sum) ;
int main() {
Int n, i, vlaue[n], sum=0;
Scanf("%d", &n );
for (i=0;i<n;i++){
Scanf("%d", & value[i] );
}
for(i=0;i<n;i++){
sum += value[i] ; // or sum=sum+value[i];
}
Printf(" %d", sum) ;
return 0;
}
This all done. Is for C, you can now do this for any language you want. Except HTML, and CSS.

- 1

in c# you can do it really simple
int [] num = {4,6,1,7,9};
int sum = 0;
foreach (int item in num){
sum += item;
}
the foreach loop runs through all elements in the array and runs the code in the body on each element.
hope this helped you :)
if you have any questions feel free to ask :3

- 1

java 01

- 1

Hi guys plz help for larnig programm I have more problem how can I start from the start point.

- 1

man ask for c#