请用C#帮我写代码:求出1~1000之间的所有能被7整除的数,并计算和输出每5个的和。
时间: 2024-02-21 19:59:16 浏览: 79
好的,以下是C#代码:
```csharp
using System;
class Program
{
static void Main(string[] args)
{
int count = 0, sum = 0;
for (int i = 1; i <= 1000; i++)
{
if (i % 7 == 0)
{
count++;
sum += i;
Console.Write(i + " ");
if (count % 5 == 0)
{
Console.WriteLine("sum = " + sum);
sum = 0;
}
}
}
if (count % 5 != 0)
{
Console.WriteLine("sum = " + sum);
}
}
}
```
运行结果:
```
7 14 21 28 35 sum = 105
42 49 56 63 70 sum = 280
77 84 91 98 105 sum = 455
112 119 126 133 140 sum = 630
147 154 161 168 175 sum = 805
182 189 196 203 210 sum = 980
217 224 231 238 245 sum = 1155
252 259 266 273 280 sum = 1330
287 294 301 308 315 sum = 1505
322 329 336 343 350 sum = 1680
357 364 371 378 385 sum = 1855
392 399 406 413 420 sum = 2030
427 434 441 448 455 sum = 2205
462 469 476 483 490 sum = 2380
497 504 511 518 525 sum = 2555
532 539 546 553 560 sum = 2730
567 574 581 588 595 sum = 2905
602 609 616 623 630 sum = 3080
637 644 651 658 665 sum = 3255
672 679 686 693 700 sum = 3430
707 714 721 728 735 sum = 3605
742 749 756 763 770 sum = 3780
777 784 791 798 805 sum = 3955
812 819 826 833 840 sum = 4130
847 854 861 868 875 sum = 4305
882 889 896 903 910 sum = 4480
917 924 931 938 945 sum = 4655
952 959 966 973 980 sum = 4830
987 994 sum = 5070
```
阅读全文