zxpr.net
当前位置:首页 >> jAvA编写一个程序,求s=1+(1+2)+(1+2+3)+...+(1+2+3+...+n)。 >>

jAvA编写一个程序,求s=1+(1+2)+(1+2+3)+...+(1+2+3+...+n)。

//用java编写一个程序,计算s=1+(1+2)+(1+2+3)+…+(1+2+3+4+5+…+100).public class sum { public static void main(string[] args) { int sum1 = 0; int sum = 0; for (int i = 1; i { sum1 += i; sum += sum1; } system.out.println(sum); }

好简单的,用两个for循环就可以.

#include "stdio.h" void main() { int n,i,j; long s=0; printf("Please input n:"); scanf("%d",&n); for(i=1;i<=n;i++) for(j=1;j<=i;j++) { s+=j; } printf("s=1+(1+2)+(1+2+3)+(1+2+3+4)+(1+2+3+4+…+n)=%ld",s); }

#include "stdio.h" int main(void){ int n,i,s,t; while(1){ printf("please input n(int n>0)\nn="); if(scanf("%d",&n) && n>0) break; printf("error, must be n>0, redo: "); } for(s=t=0,i=1;i printf("the result is %d\n",s); return 0; }

public class N { public static void main(String[] args) { int n = 100; long sum=sum(n); System.out.println(sum); } private static long sum(int max){ if(max>0)return haha(max)+sum(max-1); return max; } private static long haha(int i) { return (1+i)*i/2; }}

新建一个程序(文件菜单新建程序新建文件),写代码: input "请输入n的值:" to n s=0 temp=0 for i=1 to n temp=0 for j=1 to i temp=temp+j next s=s+temp next ?"s=1+(1+2)+(1+2+3)++(1+2+3++n)的值是:",s 然后,直接点击感叹号,保存并运行,图:

# include int main(){int n;printf("请输入要计算到多少项:");scanf("%d",&n);int sum =0 ;int s=0;for(int i= 1; i

#include"stdio.h" int main() {int s=0,t=0,n,i; scanf("%d",&n); for(i=1;i {t+=i; s+=t; } printf("s=%d\n",s); system("pause"); return 0; }

#include <iostream> using namespace std; int main() { int s=0,sum=0,i,n; cout<<"请输入n的值:"; cin>>n; for(i=1;i<=n;i++) { sum+=i; s+=sum; } cout<<s<<endl; return 0; }

相关文档
网站首页 | 网站地图
All rights reserved Powered by www.zxpr.net
copyright ©right 2010-2021。
内容来自网络,如有侵犯请联系客服。zhit325@qq.com