呢個C program點寫字?我用cygwin

2008-10-30 9:38 am
Please enter a positive integer (maximum 8 digits): 12345678
The number entered is: 12345678

The number of digit(s) is: 8
The sum of the digit(s) is: 36
The reverse of the number is: 87654321

Would you like to run this program again? (y/n): y

____________________________________________________________

Please enter a positive integer (maximum 8 digits): -6287

<<< Invalid input!! Try again!! >>>
Please enter a positive integer (maximum 8 digits): 24680
The number entered is: 24680

The number of digit(s) is: 5
The sum of the digit(s) is: 20
The reverse of the number is: 08642

Would you like to run this program again? (y/n): y

__________________________________________________________

Please enter a positive integer (maximum 8 digits): 864531
The number entered is: 864531

The number of digit(s) is: 6
The sum of the digit(s) is: 27
The reverse of the number is: 135468

Would you like to run this program again? (y/n): n

__________________________________________________________

回答 (1)

2008-11-02 1:32 pm
✔ 最佳答案
see
http://hk.knowledge.yahoo.com/question/question?qid=7008101802538
If you need more information, PM me.

2008-11-02 05:32:05 補充:
//#include <iostream.h>
#include <stdlib.h>
#include <stdio.h>

int main(int argc, char *argv[])
{
long n; // define the number, use long for up to 9 digits
int digit,i,sum; // digit=no. of digits, i=count, sum=sum of digits
unsigned char answer; // y/n for continuing to run program
char number[10]; // reversed number in string of characters
while(1==1){ // beginning loop
printf("Please enter a positive integer (maximum 8 digits): ");
scanf("%ld",&n);
printf("The number entered is: %ld\n\n\n",n);
if(n>0&&n<=99999999) // limit entry to 8 digits and positive number
{
digit=0; // initialize number of digits to zero
i=0; // initialize digit count to zero
sum=0; // initialize sum to zero
while(n>0) // process digit by digit, from the right
{
digit++; // increment number of digits
sum+=n%10; // accumulate sum of digits
number[i++]='0'+n%10; // store each digit from left to right
n/=10; // eliminate right-most digit
}
number[i]='\0'; // write end of string
printf("The number of digit(s) is: %d\n",digit);
printf("the sum of digits is %d\n",sum);
printf("The reverse of the number is: %s\n\n",number);
} else
{
printf("Sorry, the number must be non-negative and from 1-8 digits\n");
printf("Please try again\n\n");
}
printf("Would you like to run this program again? (y/n):");
fflush(stdin); // eliminate superfluous input in stdio stream
answer=getc(stdin);
if(answer=='n')break;
}
printf("Thank you, have a nice day!\n");
system("PAUSE"); // pause to see last dialog screen
return 0;
}


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

檢視 Wayback Machine 備份