China DOS Union

-- Unite DOS · Advance DOS · Grow DOS --

Union site: www.cn-dos.net Forum site: www.cn-dos.net/forum
DOS stands for freedom, openness and progress. Let us work hard, learn from the openness and GNU spirit of FreeDOS and Linux, and together build and grow a free GNU GPL world!

中国DOS联盟论坛
The time now is 2026-08-05 18:35
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » [Solved] Question about using a batch file to calculate the sum of numbers within 100 View 785 Replies 5
Original Poster Posted 2007-09-06 10:13 ·  中国 山西 太原 联通
中级用户
★★
Credits 216
Posts 81
Joined 2007-08-06 12:23
18-year member
UID 94721
Gender Male
Status Offline
I want to use a batch file to calculate the sum of numbers within 100, but I just couldn't figure it out. I thought about using a loop for a long time, but got no results, none of it worked, so I'm here to ask everyone for advice.

[ Last edited by icyheart on 2007-9-6 at 10:51 AM ]
Crossings must be my teacher
Modest progress can be
Floor 2 Posted 2007-09-06 10:24 ·  中国 湖南 长沙 联通
初级用户
Credits 48
Posts 24
Joined 2007-08-31 17:37
18-year member
UID 96273
Gender Male
Status Offline
Floor 3 Posted 2007-09-06 10:45 ·  中国 山西 太原 联通
中级用户
★★
Credits 216
Posts 81
Joined 2007-08-06 12:23
18-year member
UID 94721
Gender Male
Status Offline
Thanks, I never thought about it the other way around. What I kept thinking of was for /l %%i in (1,1,100)........ I kept thinking along those lines and just couldn't get it out, hehe thanks
Crossings must be my teacher
Modest progress can be
Floor 4 Posted 2007-09-06 10:47 ·  中国 河北 廊坊 三河市 移动
金牌会员
★★★★
Credits 2,725
Posts 1,160
Joined 2006-09-23 12:00
19-year member
UID 63486
From 河北廊坊
Status Offline
Can't it work in the forward direction?
三人行,必有吾师焉。 学然后知不足,教然后知困,然后能自强也。
Floor 5 Posted 2007-09-06 10:50 ·  中国 山西 太原 联通
中级用户
★★
Credits 216
Posts 81
Joined 2007-08-06 12:23
18-year member
UID 94721
Gender Male
Status Offline
@echo off
for /l %%i in (1,1,100) do (
set /a sum+=%%i
)
echo %sum%

By analogy, this works too, hehe
Crossings must be my teacher
Modest progress can be
Floor 6 Posted 2007-09-07 07:39 ·  中国 辽宁 本溪 联通
银牌会员
★★★
Credits 1,212
Posts 464
Joined 2006-12-13 21:11
19-year member
UID 73417
Gender Male
Status Offline
A dumb method, writing one...

@echo off
:loop
set /a a+=1
set /a s=%s%+%a%
if %a%==100 echo %s% & pause & exit
goto loop
Forum Jump: