实验3 for循环结构
【实验目的】
通过本实验,掌握循环结构程序设计的编程方法,掌握循环方面的编程技巧。
【实验要求】
⑴学会使用for语句;
⑵掌握循环结构程序设计方法;
【实验内容】
将所有三位数中的对称数以5个一行显示在屏幕上。
#include<iostream>
using namespace std;
int main(){
int n=0,a,b;
for(int i=100;i<=999;i++){
a=i/100;
b=i%10;
if(a==b){
n++;
cout.width(5);
cout<<i;
if(n%5==0) cout<<endl;
}
}
return 0;
}
判断完数
#include<iostream>
using namespace std;
int main(){
int x;
cout<<"/n/tInput a number: "; cin>>x;
int s=0;
int k=x/2;
for(int i=1;i<=k;i++)
if(x%i==0) s+=i;
if(s==x) cout<<"/n/tYES!"<<endl;
else cout<<"/n/tNO!"<<endl;
return 0;
}
本文通过两个实例,介绍了如何使用C++的for循环结构进行编程,包括找出所有三位数中的对称数并以特定格式输出,以及判断一个数是否为完数的程序设计方法。
1733

被折叠的 条评论
为什么被折叠?



