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

实验3 for循环结构

【实验目的】

通过本实验,掌握循环结构程序设计的编程方法,掌握循环方面的编程技巧。

【实验要求】

⑴学会使用for语句;

⑵掌握循环结构程序设计方法;

【实验内容】

将所有三位数中的对称数以5个一行显示在屏幕上。

#include

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<

            if(n%5==0) cout<

        }

    }

    return 0;

}

判断完数

#include

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!"<

    else cout<<"/n/tNO!"<

    return 0;

}

你可能感兴趣的:(C++编程实例,编程,c++)