C program

2008-03-30 1:54 am
write a program to convert a number input from the keyboard to a string. add sufficient number of"0" in front of the string so that its lenght is equal to that specified by the user as shown in the sample output below:

sample output
Enter a number:23
Enter the minimum lenght required:8
The number becomes 00000023

可解釋你是怎樣想到一個program中間入面的內容?

回答 (1)

2008-03-30 4:25 am
✔ 最佳答案
In the printf() function, the character * acceptss an input value and this value is substituted in the format string, thus if the user inputs 8 for the input string of "%0*d", it becomes %08d which is what is required to print 8 digits with padding of zeroes at the beginning. Here's the program.

#include < stdio.h>
#include < stdlib.h>
int main(int argc, char *argv[]){
int number,digits;
printf("Enter a number:");
scanf("%d",&number);
printf("Enter the minimum length required:");
scanf("%d",&digits);
printf("The number becomes %0*d\n",digits,number);
return 0;
}


收錄日期: 2021-04-13 15:21:33
原文連結 [永久失效]:
https://hk.answers.yahoo.com/question/index?qid=20080329000051KK02550

檢視 Wayback Machine 備份