4-数字的顺序输出和逆序输出-程序员宅基地

技术标签: C语言  c语言  

数字的顺序输出和逆序输出

给一个正整数,要求 1.求出它几位数 2.分别输出每一位数字 3.按逆序输出各位数字,例如321,应输出123

一,对于第一题我们常用的方法是每次除以10,然后丢弃一位数,记一次数。经过循环,直到最后一位数,除以10为0,停止循环,停止计数

1.用for循环

#include<stdio.h>
#include<math.h>
int main()//for
{
    
	long long n;
	int i ;
	printf("请输入数字\n");
	scanf("%lld", &n);
	if (n == 0)
	{
    
		printf("数字是1位数\n");
		return 0;
	}
	for(i=0;n!=0;i++)
	{
    
		n = n / 10;
	}
	printf("数字为%d位数\n", i);
	return 0;
}

2.用while循环.

#include<stdio.h>
#include<math.h>
int main()//求几位数
{
    
	long long n;
	int count = 0;
	printf("请输入数字\n");
	scanf("%lld", &n);
	if(n==0)
	{
    
		printf("数字为1位数\n");
		return 0;
	}
	while(n!=0) 
	{
    
		n = n / 10;
		count++;
	}
	printf("数字为%d位数\n", count);
	return 0;
}

3…用do while循环

#include<stdio.h>
#include<math.h>
int main()//do while
{
    
	long long n;
	int count = 0;
	printf("请输入数字\n");
	scanf("%lld", &n);
	do
	{
    
		n = n / 10;
		count++;
	} while (n != 0);
	printf("数字为%d位数\n", count);
	return 0;
}

二.对于第二题我们常用的方法就是首先计算出数字(n)是几位数(count),然后每次数字除以10的(count-1)次方来输出最高位,然后对数字n进行取余,最后10的count次方除以10进行后移,直到n等于0,结束循环,输出结果、

#include<stdio.h>
#include<math.h>
int main()//do while
{
    
	long long n;
	int count = 0;
	printf("请输入数字\n");
	scanf("%lld", &n);
	long long m;
	m = n;
	do
	{
    
		n = n / 10;
		count++;
	} while (n != 0);
	long long power = pow(10, count-1);
	n = m;
	printf("顺序输出为:");
	if (n == 0)
	{
    
		printf("0\n");
		return 0;
	}
	while (n!=0)
	{
    
		printf("%d  ", n / power);
		n%=power;
		power/=10;
	}
	
	return 0;
}

三.对于第三题我们常用的方法就是每次对数字进行取余,然后输出,最后再除以10把末尾的数字丢弃,当n等于0时循环停止。输出结果。

#include<stdio.h>
#include<math.h>
int main()//逆序输出
{
    
	long long n;
	printf("请输入数字\n");
	scanf("%lld", &n);
	long long  i;
	if (n == 0)
	{
    
		printf("0\n");
		return 0;
	}
	while (n != 0)
	{
    
		i = n % 10;
		printf("%d ",i);
		n = n / 10;
	}
	return 0;
}

总的代码为:

#include<stdio.h>
#include<math.h>
 main()//do while
{
    
	long long n,m,i,j;
	int count = 0;
	printf("请输入数字\n");
	scanf("%lld", &n);
	m = n;
	j=n;
	do
	{
    
		n = n / 10;
		count++;
	} while (n != 0);
	printf("数字为%d位数\n", count);
	long long power = pow(10, count-1);
	n = m;
	printf("顺序输出为: ");
	do 
	{
    
		printf("%d ", n / power);
		n%=power;
		power/=10;
	}while (n!=0);
	printf("\n逆序输出为:");
	
	do
	{
    
		i = j % 10;
		printf("%d ",i);
		j /=  10;
	}while (j != 0);
	return 0;
}
版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/weixin_56046804/article/details/120752641

智能推荐

pytorch版本回退解决No Module named 'torch.legacy'_torch.legacy'安装-程序员宅基地

文章浏览阅读1.1w次。版本回退主要是为了解决新版本的pytorch往往会舍弃一些过去会用到的类与接口,比如torch.legacy等在torch1.0.1版本中是被舍弃了,无法正常使用的,而在0.4.1中是仍然保存的。一、Conda降级pytorch版本或安装指定版本pytorch如果你是使用conda包管理,你可以很容易实现版本降级,你只需要指定版本即可:# 比如你想降级到以前的v0.4.1版本c..._torch.legacy'安装

MyISAM,Memory,InnoDB-程序员宅基地

文章浏览阅读71次。MyISAM:表锁:表级锁。注意不要让它成为瓶颈不支持自动数据恢复:服务器崩溃或停电后,就应该在使用之前检查和执行可能的修复。不支持事务:实际上,MyISAM甚至不保证单个命令会完成。如果在多行UPDATE的中途有错误发生,一些行会被更新,而另外一些则不会。只有索引被缓存在内存中:MyISAM中缓存了MYSQL进程内部的索引,并保存在键缓冲区。操作系统缓存了表的数据,因此在MYSQ..._myisam、memory,

nginx的location和proxy_pass正则-程序员宅基地

文章浏览阅读2.1k次。location里的正测表达式,是怎么匹配到proxy_pass上的比如 [code="java"]//配置Alocation ~* /image/ { proxy_pass http://192.168.1.1/}[/code][code="java"]//配置Blocation ~* /image/ { ..._nginx location proxy_pass 使用正则

缓存雪崩、缓存击穿、缓存穿透概念及其解决方案-程序员宅基地

文章浏览阅读1.3k次。缓存雪崩概念在一个较短的时间内,缓存中较多的key集中过期。此周期内访问请求过期的数据,Redis未命中,便将请求发送给数据库。数据库一时间无法处理如此多的请求,导致Redis中大量请求被积压,且出现超时现象。解决方案更多的页面静态化处理构建多级缓存架构Nginx缓存+ redis缓存+ ehcache缓存即使Redis未命中,ehcache能命中便可以减轻压力检测Mysq严重耗时业务进行优化对数据库的瓶颈排查:例如超时查询、耗时较高事务等灾难预警机制监控 redis服务器性能指标_缓存雪崩

matlab 分布拟合,曲线拟合和分布拟合 - MATLAB & Simulink Example - MathWorks 中国-程序员宅基地

文章浏览阅读4.8k次,点赞3次,收藏27次。在曲线拟合与分布拟合之间进行选择曲线拟合和分布拟合是不同类型的数据分析。当您要将某个响应变量建模为预测变量的函数时,请使用曲线拟合。当您要为单一变量的概率分布建模时,请使用分布拟合。曲线拟合在以下试验数据中,预测变量为 time,即服用药物之后的时间。响应变量为 conc,即血液中的药物浓度。假设只有响应数据 conc 受试验误差的影响。time = [ 0.1 0.1 0.3 0.3..._分布拟合

import spacy中‘en‘模块时报错OSError: [E941]Can‘t find model ‘en‘._import spacy ioerror(errors.e941.format-程序员宅基地

文章浏览阅读611次。import spacyeng_model = spacy.load('en_core_web_sm')s = 'I love natural language processing technology!'s_token = eng_model(s)for token in s_token: print(token, token.pos_, token.pos)load里把en换成以上,下载的时候会提示包名,填进来即可。..._import spacy ioerror(errors.e941.format

随便推点

IDEA安装Lombok插件失败的解决方案_fail to load plugin descriptor from file lombok.ja-程序员宅基地

文章浏览阅读1.4w次。报错问题一般就是网络问题或者是RP问题。插件下载地址:1和2都可以 ,2 的速度稍微快点1.http://plugins.jetbrains.com/plugin/6317-lombok-plugin2.https://github.com/mplushnikov/lombok-intellij-plugin/releases选择下载的插件版本号要和自己的IDE版本对应。..._fail to load plugin descriptor from file lombok.jar

spark-submit 命令使用详解-程序员宅基地

文章浏览阅读2w次,点赞2次,收藏35次。spark-submit 命令使用详解spark-submit 用户打包 Spark 应用程序并部署到 Spark 支持的集群管理气上,命令语法如下:spark-submit [options] <python file> [app arguments]app arguments 是传递给应用程序的参数,常用的命令行参数如下所示:–master: 设置主节点 URL 的参数..._spark-submit

RecastNavigation(3D场景建模、网格导航)-程序员宅基地

文章浏览阅读928次。一、RecastNavigation详解  RecastNavigation定义:    RecastNavigation是一个导航寻路工具集,使用邻接的凸多边形集合描述一个3D场景,A*寻路算法使3D场景的可达性得到保证。    Polygon是Detour的基本寻路单元,在Poly(Polygon凸多边形)中,任意两个点是可以直线到达的。    github:https://gi..._recastnavigation

java.sql.SQLException: ${jdbc.driver}-程序员宅基地

文章浏览阅读1.4k次。一直显示读不到文件,后来发现是忘记加这句话了<bean id="myDataSource" class="com.alibaba.druid.pool.DruidDataSource">加到访问数据库的代码前面 <context:property-placeholder location="classpath:jdbc.properties"></context:property-placeholder> <bean id="myDataSource_java.sql.sqlexception: ${jdbc.driver}

排名(sort结构体)_今天的上机考试虽然有实时的ranklist,但上面的排名只是根据完成的题数排序,没有考-程序员宅基地

文章浏览阅读246次。Description 今天的上机考试虽然有实时的Ranklist,但上面的排名只是根据完成的题数排序,没有考虑 每题的分值,所以并不是最后的排名。给定录取分数线,请你写程序找出最后通过分数线的 考生,并将他们的成绩按降序打印。 Input 测试输入包含若干场考试的信息。每场考试信息的第1行给出考生人数N ( 0 < N < 1000 )、考题数M ( 0 < M < = 10 )、分数_今天的上机考试虽然有实时的ranklist,但上面的排名只是根据完成的题数排序,没有考

19年冬季第二题 PAT甲级 1166 Summit (25分)-程序员宅基地

文章浏览阅读256次。7-3 Summit (25分)A summit (峰会) is a meeting of heads of state or government. Arranging the rest areas for the summit is not a simple job. The ideal arrangement of one area is to invite those heads so that everyone is a direct friend of everyone.Now given

推荐文章

热门文章

相关标签