# 以C++寫出1+2+3+4+...+N=?

#include<stdio.h>

#include<stdlib.h>

#include<stdio.h>

#include<stdlib.h>

int main()

{

int a=1,n,s=0;

printf(\"請輸入一數n:\");

scanf(\"%d\",&n);

while(a<=n)

{

s=s+a;

a++;

}

printf(\"%d\",&s);

system(\"pause\");

return 0;

}

Update:

Rating

#include <iostream.h>

void main()

{

int sum,total=0;

cout << "Key in a Max Value:" << endl;

cin >> sum;

for(int i=0;i<=sum;i++)

{

total=i+total;

}

cout << total << endl;

}

2006-04-05 14:03:29 補充：

#include <iostream.h>void main(){int sum,total=0,i=0;cout << "Key in a Max Value:" << endl;cin >> sum;while(i<=sum){total=total+i;i++;}cout << total << endl;}

給Mars:...我最佳評價按錯人了= =""...真的很抱歉@@""...

你才是最棒的@@"

• 青嶼
Lv 4

#include<stdio.h>

void main(){

int sum = 0;

int n = 0;

printf("請輸入一個數值N\n");

scanf("%d",&n);

for(int i=1;i<=n;i++){

sum = sum + i;

}

printf("\n累加之後的結果\n");

printf("%d",&sum);

system("pause");

}

#include <stdio.h>

int main()

{

int n;

scanf("%d",&n);

printf("%d",(1+n)*n/2);

}

It's just a math problem.

2006-04-05 00:56:15 補充：

#include 檔案在C裡面是必要的不會的話就不用管他我說過 這只是一個數學問題可以用常數時間解決的問題 何必用到while-loop來浪費CPU的時間?

Source(s): me

#include<stdio.h>

#include<stdlib.h>

#include<iostream.h>

int main(){

int x=0,n,i;

cout<<"Input:";

cin>>n;

for (i=1 ; i<=n ; i++){

x+=i;

}

cout<<x<<endl;

system("PAUSE");

return 0;

}

有問題請提出

2006-04-05 02:19:48 補充：

#include<stdio.h>#include<stdlib.h>#include<iostream.h>int main(){int x=0,n;cout<<"Input:";cin>>n;while(n>=1){x+=n;n--;}cout<<x<<endl;system("PAUSE");return 0;}

Source(s): 自己