您的位置:首页 > 编程语言 > C语言/C++

C++编程实例-for循环结构

2009-12-16 17:41 239 查看
实验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;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: