从1到100的数字总和 [英] sum of numbers from 1 to 100

查看:601
本文介绍了从1到100的数字总和的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们如何使用asp.net将1到100的数字相加.我曾尝试使用数据列表..但我却无能为力..

how can we sum the numbers from 1 to 100 using asp.net. I have tried using datalist.. but I was unabl to get it..

推荐答案

只是一个想法.
Just a thought.
Sum from 1 to n = n*(n+1)/2



因此,在这里您可以简单地使用100*(100+1)/2,没什么花哨的.



So here you can simply use 100*(100+1)/2, nothing fancy.


您可以尝试一下
You can try this
int Total=0;
for(i=1;i<=100;i++)
{
    Total=Total+i;
}
Response.Write("Total = "+Total.ToString());


int i=1;
int total=0;
while(i<=100)
{
     total=total+i;
     i++;
}
response.write("Ans :"+total.ToString());


这篇关于从1到100的数字总和的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆