C program

2008-10-15 10:10 pm
想問下
如果要個user 入 一個4位數入 去 
如: 6458

跟住想佢出番個左右調轉ge answer 

即出: 8546

應該點寫好??

int x;
print("please enter a number (no more than 5 digits)");
scanf("%d",x);
............

回答 (2)

2008-10-16 7:25 pm
✔ 最佳答案
See:
http://hk.knowledge.yahoo.com/question/question?qid=7008101401531

Reproduced here and adapted for your convenience:

2) Write a program that reads in one positive integer, separates the number into its individual digits and prints the digits from right to left, separated from one another by three spaces each.
For example, if your program reads 13579, it should print:
9 7 5 3 1

#include <stdio.h>
int main(int argc, char *argv[])
{
long n=-1;
int digit;
while(n>99999||n<0)
{
printf("enter a positive number (max. 5 digits)");
scanf("%ld",&n);
}
while(n>0)
{
printf("%d ",n%10);
n/=10;
}
printf("\n");
}
2008-10-16 6:05 am
Use modulus 10 (%10) to obtain last digit one by one (perhaps using a while loop), multiply to become new digit at new integer, then use integer division to remove last digit of original number


收錄日期: 2021-04-13 16:10:16
原文連結 [永久失效]:
https://hk.answers.yahoo.com/question/index?qid=20081015000051KK00730

檢視 Wayback Machine 備份