c語言:用三種循環結構,求1000以內所有能被5和7整除的數的和

題目:

c語言:用三種循環結構,求1000以內所有能被5和7整除的數的和
用三種循環結構,求1000以內所有能被5和7整除的數的和.
這是說要分別用上三種循環結構嗎?就是要做三個答案出來?
這是我做的,for,運行出來應該是正確的吧.
#include
main()
{
int a,s=0;
for(a=35;a

解答:

這是一個while的寫法:
#include<stdio.h>
main()
{
int a=35,s=0;
while(a<=1000)
{
if(a%5==0&&a%7==0)
s=s+a;
a++;
}
printf("s=%d\n",s);
getch();
}
最後是do while的寫法:#include<stdio.h>
main()
{
int a=35,s=0;
do
{
if(a%5==0&&a%7==0)
s=s+a;
a++;
}while(a<=1000);
printf("s=%d\n",s);
getch();
}

添加新評論

暱稱
郵箱
網站