栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

学习c语言练习题

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

学习c语言练习题

题目:

回文数的形成。任取一个十进制整数,将其倒过来后与原来的整数相加,得到一个新的整数后,重复以上步骤,最终可得到一个回文数,请编程验证。
**输入格式要求:"%ld" 提示信息:"please enter a number optionaly:"  "The generation process of palindrome:n"  "  input error, break.n"  
**输出格式要求:"   [%d]: %ld+%ld=%ldn"  "Here we reached the aim at last !n"
程序运行示例如下:
please enter a number optionaly:345
The generation process of palindrome:
   [1]: 345+543=888
Here we reached the aim at last !
 

代码如下:

#include 
#include 
long int reverse(long int n);//用来反转数字
int check(long int n);//用来判断是否是回文数

int main()
{
    int count=0;//计算反转的次数,初始化为0
    long int num,reve=0,sum=0,flag=0;
    printf("please enter a number optionaly:");
    scanf("%ld",&num);
    if(num<0)//错误输入终端程序
    {
        printf(" input error, break.n");
        return 0;
    }
    do{
        count++;//每循环一次,计数一次。
        sum=0;//计算反转和未反转的数字的和
        reve=reverse(num);//反转
        sum=num+reve;
        printf("   [%d]: %ld+%ld=%ldn",count,reve,reverse(reve),sum);//输出
        num=sum;//讲下轮要反转的数字赋值给num
        flag=check(sum);
    }while(flag);//当flag=0时,就跳出循环
    printf("Here we reached the aim at last !n");
}

long int reverse(long int n)
{
    int temp=n,count=0,temp1,w,sum=0;
    do//计算输入函数的数字的位数
    {
        temp/=10;
        count++;
    }while(temp>0);
    w=count;//临时存储一下位数
    for(int i=0;i 

运行结果如下:

 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/832644.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号