c语言用循环的方法,把输入的一个整数,倒序输出 C语言编写程序 :输入一个正整数,将其逆序输出。例如,输入1...

c\u8bed\u8a00\u8f93\u5165\u4e00\u4e2a\u6574\u6570,\u7528\u9012\u5f52\u7b97\u6cd5\u5c06\u6574\u6570\u5012\u5e8f\u8f93\u51fa.

\uff03include\uff1cstdio\uff0eh\uff1e
voidorder\uff3fprint\uff08intn\uff09
\uff5b
if\uff08n\uff1c10\uff09
\uff5b
printf\uff08\uff02\uff05d\uff02\uff0cn\uff09\uff1b
return\uff1b
\uff5d
order\uff3fprint\uff08n\uff0f10\uff09\uff1b
printf\uff08\uff02\uff055d\uff02\uff0cn\uff0510\uff09\uff1b
\uff5d
void reverse\uff3fprint\uff08intn\uff09
\uff5b
if\uff08n\uff1c10\uff09
\uff5b
printf\uff08\uff02\uff05\uff0d5d\uff02\uff0cn\uff09\uff1b
return\uff1b
\uff5d
printf\uff08\uff02\uff05\uff0d5d\uff02\uff0cn\uff0510\uff09\uff1b
reverse\uff3fprint\uff08n\uff0f10\uff09\uff1b
\uff5d
int main\uff08void\uff09
\uff5b
int n\uff1b
printf\uff08\uff02Inputainteger\uff1a\uff02\uff09\uff1b
scanf\uff08\uff02\uff05d\uff02\uff0c\uff06n\uff09\uff1b
printf\uff08\uff02Orderprint\uff1a\uff02\uff09\uff1b
order\uff3fprint\uff08n\uff09\uff1b
printf\uff08\uff02\uff3cn\uff02\uff09\uff1b
printf\uff08\uff02Reverseprint\uff1a\uff02\uff09\uff1b
reverse\uff3fprint\uff08n\uff09\uff1b
return0\uff1b
\uff5d
\u8fd0\u884c\u6548\u679c\uff1a

\u6269\u5c55\u8d44\u6599\uff1areturn\u7684\u7528\u6cd5\uff1a
return\u7684\u4f5c\u7528\u662f\u7ed3\u675f\u6b63\u5728\u8fd0\u884c\u7684\u51fd\u6570\uff0c\u5e76\u8fd4\u56de\u51fd\u6570\u503c\u3002return\u540e\u9762\u53ef\u4ee5\u8ddf\u4e00\u4e2a\u5e38\u91cf\uff0c\u53d8\u91cf\uff0c\u6216\u662f\u8868\u8fbe\u5f0f\u3002\u51fd\u6570\u7684\u5b9a\u4e49\u4e00\u822c\u662f\u8fd9\u6837\u7684\uff0c\u4f8b\u5982\uff1a
inta\uff08inti\uff09\uff0f\uff0f\u7b2c\u4e00\u4e2aint\u662f\u51fd\u6570\u7684\u8fd4\u56de\u503c\u7684\u7c7b\u578b\uff0c\u4e5f\u5c31\u662freturn\u540e\u9762\u8ddf\u7684\u503c\u7684\u7c7b\u578b\uff0ca\u662f\u51fd\u6570\u7684\u540d\u79f0\uff0c\u62ec\u53f7\u91cc\u7684\u662f\u4f20\u9012\u7ed9\u51fd\u6570\u7684\u53c2\u6570\uff0cint\u662f\u53c2\u6570\u7684\u7c7b\u578b\uff0ci\u662f\u53c2\u6570\u7684\u540d\u5b57
\uff5b
\uff0e\uff0e\uff0e\uff0f\uff0f\u7701\u7565\u51fd\u6570\u4f53\u5185\u5bb9
returnb\uff1b\uff0f\uff0fb\u5fc5\u987b\u4e0e\u51fd\u6570\u5934\u7684\u8fd4\u56de\u503c\u4e00\u81f4\uff08\u6b64\u5904\u4e3aint\u578b\uff09
\uff5d
\u7b80\u5355\u51fd\u6570\u4e3e\u4f8b\uff1a
intaddOne\uff08intb\uff09
\uff5b
returnb\uff0b1\uff1b
\uff5d
\u8be5\u51fd\u6570\u7684\u4f5c\u7528\u662f\u53d6\u5f97\u4e00\u4e2a\u6570\uff0c\u5c06\u8fd9\u4e2a\u6570\u52a0\u4e0a1\uff0c\u518d\u5c06\u7ed3\u679c\u8fd4\u56de
\u8c03\u7528\u65f6\u8fd9\u6837\uff1a
intresult\uff1daddOne\uff082\uff09\uff1b\uff0f\uff0f\u6b64\u65f6result\u7684\u503c\u4e3a3
\u51fd\u6570\u62ec\u53f7\u91cc\u7684\u53c2\u6570\u4e5f\u53ef\u4ee5\u4e3a\u53d8\u91cf\u6216\u80fd\u7b97\u51fa\u503c\u7684\u8868\u8fbe\u5f0f
\u4ee5\u4e0a\u5c31\u662f\u4e00\u4e2a\u57fa\u672c\u7684\u51fd\u6570\uff0c\u4e00\u822c\u7684\u51fd\u6570\u90fd\u6709\u8fd4\u56de\u503c\uff0c\u4e5f\u5c31\u662freturn\u540e\u9762\u8ddf\u7684\u503c\uff0c\u8fd4\u56de\u503c\u53ef\u4ee5\u4e3a\u5404\u79cd\u6570\u636e\u7c7b\u578b\uff0c\u5982\uff1aint\uff0cfloat\uff0cdouble\uff0cchar\uff0ca\uff3b\uff3d\uff08\u6570\u7ec4\uff09\uff0c\uff0aa\uff08\u6307\u9488\uff09\uff0c\u7ed3\u6784\u6216\u7c7b\uff08c\uff0b\uff0b\uff09
\u4f46\u4e0d\u662f\u6240\u6709\u51fd\u6570\u90fd\u6709\u8fd4\u56de\u503c\uff0c\u5982\u679c\u67d0\u4e2a\u51fd\u6570\u65e0\u8fd4\u56de\u503c\uff0c\u90a3\u4e48\u8fd4\u56de\u503c\u7684\u4f4d\u7f6e\u5219\u4e3a\u201cvoid\u201d\u5173\u952e\u5b57\uff0c\u6b64\u65f6\u51fd\u6570\u4f53\u4e2d\u65e0\u8fd4\u56de\u503c\uff0c\u5373\u65e0return\u7684\u503c\u3002\u4f46\u662f\u51fd\u6570\u4e2d\u4e5f\u53ef\u51fa\u73b0return\uff0c\u5373\u4e00\u4e2a\u7a7a\u7684return\u53e5\u5b50\uff0c\u5176\u4f5c\u7528\u662f\u4f7f\u51fd\u6570\u7acb\u5373\u7ed3\u675f\uff0c\u5982voidprint\uff08\uff09\uff0f\uff0f\u62ec\u53f7\u4e2d\u4e3a\u7a7a\u8868\u793a\u65e0\u4f20\u9012\u53c2\u6570\u3001
\uff5b
printf\uff08\uff02a\uff02\uff09\uff1b
printf\uff08\uff02b\uff02\uff09\uff1b
return\uff1b\uff0f\uff0f\u51fd\u6570\u6267\u884c\u5230\u6b64\u5904\u7ed3\u675f
printf\uff08\uff02c\uff02\uff09\uff1b
\uff5d\uff0f\uff0f\u8be5\u51fd\u6570\u53ea\u6267\u884c\u5230return\u8bed\u53e5\u5904\uff0c\u5373\u5c4f\u5e55\u4e0a\u8f93\u51fa\u7684\u4e3a\uff02ab\uff02

#include
int main()
{
int num;
scanf("%d",&num);
int a,i;
for(i = 0;num > 0;i++)
{
a = num % 10;
printf("%d",a);
num = num/10;
}
return 0;
}

\u5206\u6790\uff1a\u9996\u5148\uff0c\u8f93\u5165\u7684\u662f\u4e00\u4e2a\u6574\u6570\uff0c\u56e0\u6b64\u6700\u524d\u9762\u4e00\u4f4d\u6570\u4e0d\u662f\u96f6\uff0c\u6240\u4ee5\u6211\u4eec\u53ef\u4ee5\u7528\u966410\u53d6\u4f59\u6cd5\u5199\u3002
a = num % 10
\u5c31\u662f\u8f93\u51fa\u6570\u5b57\u7684\u6700\u540e\u4e00\u4f4d
\u7136\u540e\u966410\u5254\u9664\u6570\u5b57\u6700\u540e\u4e00\u4f4d\uff0c\u8fd9\u6837\u6570\u5b57\u5012\u6570\u7b2c\u4e8c\u4f4d\u5c31\u4f1a\u8f93\u51fa\u3002\u4ee5\u6b64\u7c7b\u63a8\uff0c\u5c31\u80fd\u9006\u5e8f\u8f93\u51fa\u6570\u5b57\u3002
\u62d3\u5c55\u8d44\u6599
for\u5faa\u73af\u662f\u7f16\u7a0b\u8bed\u8a00\u4e2d\u4e00\u79cd\u5f00\u754c\u7684\u5faa\u73af\u8bed\u53e5\uff0c\u800c\u5faa\u73af\u8bed\u53e5\u7531\u5faa\u73af\u4f53\u53ca\u5faa\u73af\u7684\u7ec8\u6b62\u6761\u4ef6\u4e24\u90e8\u5206\u7ec4\u6210\uff0cfor\u5faa\u73af\u5176\u5728\u5404\u79cd\u7f16\u7a0b\u8bed\u8a00\u4e2d\u7684\u5b9e\u73b0\u4e0e\u8868\u8fbe\u6709\u6240\u51fa\u5165\uff0c\u4f46\u57fa\u672c\u4e3a\u4ee5C\u8bed\u8a00\u548cpascal\u8bed\u8a00\u4ee3\u8868\u7684\u4e24\u79cd\u5f62\u5f0f\u3002
C\u8bed\u8a00\u4e2d\u7684for\u5faa\u73af\u5982\u4e0b
1.\u8bed\u53e5\u6700\u7b80\u5f62\u5f0f\u4e3a\uff1a
for( ; ; )
2.\u4e00\u822c\u5f62\u5f0f\u4e3a\uff1a
for\uff08\u5355\u6b21\u8868\u8fbe\u5f0f;\u6761\u4ef6\u8868\u8fbe\u5f0f;\u672b\u5c3e\u5faa\u73af\u4f53\uff09
{
\u4e2d\u95f4\u5faa\u73af\u4f53\uff1b
}

思路:可以利用while循环依次对其进行对10取余输出并除10操作直到其为0为止,输出的结果就是该整数的倒序。

参考代码:

#include <stdio.h>
int main()
{
int i;
scanf("%d",&i);
while(i)
{
    printf("%d ",i%10);
    i/=10;
}
return 0; 
}
/*
输出:
12345
5 4 3 2 1
*/


取余加除法就可以了

#include <stdio.h>
int main()
{
int a;
scanf("%d",&a);
while(a)
{
printf("%d",a%10);
a/=10;
}
return 0;
}


#include<stdio.h>
#include<string.h>
main()
{
char str[100];
int n;
printf("please input a number:");
gets(str);
n=strlen(str);
for(;n>=0;n--){
printf("%c",str[n])
};
}

关键你没说多少位数字,所以最好转换成字符串,这样处理起来比较方便

  • c璇█鐢ㄥ惊鐜殑鏂规硶,鎶婅緭鍏ョ殑涓涓暣鏁,鍊掑簭杈撳嚭
    绛旓細include<stdio.h>int main(){int i,j,n,a[15]; scanf("%d",&n); for(i=0;n;) {a[i++]=n%10; n/=10; } for(j=0;j
  • c璇█鐢ㄥ惊鐜殑鏂规硶,鎶婅緭鍏ョ殑涓涓暣鏁,鍊掑簭杈撳嚭
    绛旓細鎬濊矾锛氬彲浠ュ埄鐢╳hile寰幆渚濇瀵瑰叾杩涜瀵10鍙栦綑杈撳嚭骞堕櫎10鎿嶄綔鐩村埌鍏朵负0涓烘锛岃緭鍑虹殑缁撴灉灏辨槸璇ユ暣鏁扮殑鍊掑簭銆傚弬鑰冧唬鐮侊細include <stdio.h>int main(){int i;scanf("%d",&i);while(i){ printf("%d ",i%10); i/=10;}return 0; }/*杈撳嚭锛123455 4 3 2 1*/ ...
  • 鐢–++缂栧啓璇█寰幆缁撴瀯,瀵逛簬杈撳叆鐨勪竴涓暟瀛,璁$畻瀹冨悇涓綅涓婄殑鏁板瓧涓哄伓 ...
    绛旓細C璇█瀹炵幇濡備笅锛歩nclude<stdio.h>void main(){int n,i,sum=0;scanf("%d",&n); //杈撳叆涓涓暟瀛椼倃hile (n>0) //浠庝釜浣嶅紑濮嬫煡鎵俱倇i=n%10;if (i%2==0) sum+=i;//鍒ゆ柇璇ヤ綅涓婄殑鏁板瓧鏄惁涓哄伓鏁帮紝鏄垯缁欏彉閲弒um鍔犱笂璇ユ暟銆俷=n/10; //缁х画鏌ユ壘涓嬩竴浣嶃倉printf("%d\n",sum)...
  • C璇█缂栧啓绋嬪簭 :杈撳叆涓涓鏁存暟,灏鍏堕嗗簭杈撳嚭銆備緥濡,杈撳叆12345,杈撳嚭54321...
    绛旓細鍒嗘瀽锛氶鍏锛岃緭鍏ョ殑鏄竴涓暣鏁帮紝鍥犳鏈鍓嶉潰涓浣嶆暟涓嶆槸闆讹紝鎵浠ユ垜浠彲浠ョ敤闄10鍙栦綑娉曞啓銆俛 = num % 10 灏辨槸杈撳嚭鏁板瓧鐨勬渶鍚庝竴浣 鐒跺悗闄10鍓旈櫎鏁板瓧鏈鍚庝竴浣嶏紝杩欐牱鏁板瓧鍊掓暟绗簩浣嶅氨浼氳緭鍑恒備互姝ょ被鎺紝灏辫兘閫嗗簭杈撳嚭鏁板瓧銆
  • 鐢c璇█鐨勫惊鐜璇彞,杈撳叆涓涓鏁存暟n,鍦ㄨ緭鍏涓暣鏁,杈撳嚭鏈灏忓笺
    绛旓細1銆佹柊寤轰竴涓伐绋嬪拰.c鏂囦欢 锛杈撳叆澶存枃浠跺拰涓诲嚱鏁般2銆佸畾涔夊彉閲忕被鍨嬪拰璧嬪垵鍊笺3銆佽緭鍏ユ暣鏁般4銆佺敤for寰幆璁$畻鏁存暟鐨勯暱搴︺5銆佺敤for寰幆灏嗗悇浣嶆暟瀛楀瓨鍏ユ暟缁勪腑銆6銆佺敤for寰幆杈撳嚭鏁扮粍銆7銆佺紪璇戯紝杈撳嚭鏈灏忓笺
  • C璇█鐢for寰幆杈撳叆瀛楃(#缁撴潫杈撳叆)骞剁珛鍒昏緭鍑,濡備綍缁熻鍑轰竴鍏杈撳叆鐨...
    绛旓細缁熻杈撳叆瀛楃鐨勪釜鏁帮紙涓嶅寘鎷洖杞﹀拰#锛夈傛濊矾锛氬唴宓寰幆杈撳叆鏃犺鍥炶溅銆備笅闈㈡槸鏀瑰啓鍐呭锛歩nclude <stdio.h> int main(){ char chChar=0;int i=0;for (i=0;chChar!='#';i++){ while((chChar=getchar())=='\n');if(chChar=='#') break;if (chChar>='a'&&chChar<='x')chChar=...
  • 涓嶇敤鏁扮粍,鍙敤寰幆璇彞濡備綍鐢C璇█鍐杈撳叆1~5浜斾釜鏁板瓧,杈撳嚭鏈澶у兼渶灏...
    绛旓細澹版槑涓涓瓨鏀炬渶澶у肩殑鍙橀噺max锛屽啀澹版槑涓涓瓨鏀炬渶灏忓肩殑鍙橀噺min銆傝緭鍏ョ涓涓暟瀛楀悗锛屾妸瀹冨悓鏃惰祴缁檓ax鍜宮in锛屼互鍚庢瘡杈撳叆涓涓暟瀛楅兘鍜宮ax銆乵in鍒嗗埆姣旇緝锛屾妸姣攎ax澶х殑璧嬬粰max锛屾妸姣攎in灏忕殑璧嬬粰min锛岃繖鏍峰氨淇濊瘉max鏄墍杈撳叆鐨鏁板瓧涓兼渶澶х殑锛岃宮in鏄緭鍏ョ殑鏁板瓧涓兼渶灏忕殑锛屾渶鍚庤緭鍑簃ax鍜宮in鍗冲彲銆備唬鐮...
  • C璇█杩炵画杈撳叆5涓暟.鏄剧ず鏈澶鐨勪竴涓暟.瑕鐢涓涓寰幆鍐呴儴鏈塖CANF鍑芥暟鍜宨...
    绛旓細include<stdio.h> main(){ int i,a,b,c,d,f,max;for (i=1;i<=5;i++){ max=(b>c)?b:c;// printf("璇杈撳叆鏁板瓧\n");c=a;scanf("%d",&a);b=a;} printf("max=%d\n",max);}
  • C璇█鎬庝箞鐢(寰幆)鎶婁竴涓 鏁存暟杞崲鎴愬瓧绗︿覆 渚嬪鎴杈撳叆1234 杈撳嚭缁撴灉...
    绛旓細int i = 12345;char m[20];char* p = m, *q = m;while (i > 0) { *(p++) = '0'+i%10; i /= 10;}*p = 0;p--;while (q < p) {char t = *p; *(p--) = *q; *(q++) = t;}printf("%s", m);
  • 鐢C璇█寰幆缁撴瀯缂栧啓:杈撳叆涓涓暟,鍒ゆ柇璇ユ暟鏄惁涓虹礌鏁般
    绛旓細int i;scanf
  • 扩展阅读:扫一扫题目出答案 ... c语言循环n次输入 ... c语言的for循环例子 ... c语言循环输入代码 ... c语言循环编程题题库 ... c语言中三种基本循环 ... c语言while语句用法 ... c语言如何让程序循环 ... c语言怎么循环重新输入 ...

    本站交流只代表网友个人观点,与本站立场无关
    欢迎反馈与建议,请联系电邮
    2024© 车视网