+ 1

How to add n integers?

3rd Oct 2016, 9:44 AM
Raj shah
Raj shah - avatar
15 Respostas
+ 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; }
3rd Oct 2016, 10:57 AM
Emma
Emma - avatar
+ 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)
3rd Oct 2016, 10:10 AM
Emma
Emma - avatar
+ 3
in c++ we can ask user with "cin>>n;" and save this value in this n variable
2nd Nov 2016, 7:12 PM
Damian Soƛnicki
Damian Soƛnicki - avatar
+ 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.
3rd Nov 2016, 2:30 AM
Caio Santos
Caio Santos - avatar
+ 1
Can we just ask user the value without giving any pre defined value for n
2nd Nov 2016, 6:04 PM
Ashwin George
Ashwin George - avatar
0
Is there a simple way then this? @emma
3rd Oct 2016, 10:19 AM
Raj shah
Raj shah - avatar
0
What's all this?
2nd Nov 2016, 5:53 PM
Thabang Sidwell Naanyane
Thabang Sidwell Naanyane - avatar
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; }
10th Nov 2016, 11:34 PM
Bashar
Bashar - avatar
0
start array n=0 and sum=sum+arr[i] sum=0 and i=0 first time execute 3 and result is 18.
15th Nov 2016, 11:26 AM
Rehan hussain
Rehan hussain - avatar
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); }
18th Nov 2016, 12:03 PM
Rehan hussain
Rehan hussain - avatar
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.
6th Jan 2017, 4:18 AM
shailesh kumar
shailesh kumar - avatar
- 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
18th Nov 2016, 7:13 AM
joakim haslund
joakim haslund - avatar
- 1
java 01
25th Nov 2016, 6:26 PM
Bachir
Bachir - avatar
- 1
Hi guys plz help for larnig programm I have more problem how can I start from the start point.
14th Dec 2016, 8:15 PM
brahmadev kumar
brahmadev kumar - avatar
- 1
man ask for c#
16th Dec 2016, 3:03 PM
Stefan Milosevic
Stefan Milosevic - avatar