【C++,C语言,Java】编写程序,打印出1000-2000之间所有的回文数

要求:定义一个判断回文数的函数,并打印出1000-2000之间所有的回文数。(回文数是指其各位数字左右对称的整数,如1221是回文数)

C++实现:

#include <iostream>
using namespace std;
int judge(int x)
{
    int a,b,c,d;
        a = x / 1000;
        b = x / 100 % 10;
        c = x / 10 % 10;
        d = x % 10;
        if(a == d && b == c)
        {
            return 1;
        }
        else
        {
            return 0;
        }
}
int main()
{
    cout<<"1000-2000间的回文数有:"<<"\n";
    for (int i = 1000; i < 2000; i++) {
        if(judge(i)==1)
        {
            cout<<i<<" ";
        }
    }
    return 0;
}

C语言实现:

#include<stdio.h>
int judge(int x)
{
    int a,b,c,d;
        a = x / 1000;
        b = x / 100 % 10;
        c = x / 10 % 10;
        d = x % 10;
        if(a == d && b == c)
        {
            return 1;
        }
        else
        {
            return 0;
        }
}
int main()
{
    printf("1000-2000间的回文数有:\n");
    for (int i = 1000; i < 2000; i++) {
        if(judge(i)==1)
        {
            printf("%d ",i);
        }
    }
    return 0;
}

Java实现:

public class c_work2 {
    public static int yjudge(int x)
    {
        int a,b,c,d;
        a = x / 1000;
        b = x / 100 % 10;
        c = x / 10 % 10;
        d = x % 10;
        if(a == d && b == c)
        {
            return 1;
        }
        else
        {
            return 0;
        }
    }
    public static void main(String[] args) {
        System.out.println("1000-2000之间的回文数:");
        for (int i = 1000; i <= 2000; i++){
            if(yjudge(i)==1)
        {
            System.out.print(i+" ");;
        }
    }
        }
}

运行结果:

  • 8
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值