How numbers are converted to ASCII characters--rectification C after lesson 4.9

Source: Internet
Author: User

The original question is as follows:

Exercise 4.9 gives a positive integer of no more than 5 bits,

1. It is a number of digits;

2. Output each digit separately;

3. Output the figures in reverse.


Thinking: 1. require a positive integer with no more than 5 bits

2. To remove the number of each digit in the 5-bit

3. To be able to recognize the number of the first few digits.


Errors are made when the number of each digit is then converted into ASCII characters.

The error code is:

A= (char) k/10000;    k=k%10000; b= (char) k/1000;    k=k%1000; C= (char) k/100;    k=k%100;    D= (char) K/10; E= (char) k%10;

The result is always not getting the correct characters.

After debugging, it is found that the resulting 0-bit, is a null character. Only to be cast with (char) , the No. 0 character is obtained, not the character ' 0 '. Therefore, modify the code to:

A= (char) (k/10000+48);    k=k%10000; B= (char) (k/1000+48);    k=k%1000; C= (char) (k/100+48);    k=k%100;    D= (char) (k/10+48); E= (char) (k%10+48);

After the whole code is attached:

/****************************************************** Practice 4.9   give a positive integer of no more than 5 bits, ask for      1. It is a number of digits;     2. output each digit separately;     3. Output numbers in reverse. CREATE----------------------------by:     idooi liutime:   2015/ 09/18-1046----------------------------------******************************************************/#include  <stdio.h> #include  <stdlib.h>int main (void) {    int  integer;    int k;               //for Value     int grade;           //indicates a few digits     char a, b, c, d, e; //a= million, b= thousand, C= Hundred, d= 10, e=     do{        printf ("Please  Intput your number:\n ");     &nbSP;&NBSP;&NBSP;&NBSP;SCANF ("%d",  &integer);     }while (integer>99999 | |  integer<=0);     k=integer;    a= (char) (k/10000+48);  k=k% 10000;    b= (char) (k/1000+48);  k=k%1000;    c= (char) (k/100+48);  k=k%100;    d= (char) (k/10+48);     e= (char) (k%10+48);     if (a!= ' 0 ')     {        printf ("%d   is a five-digit ",  integer);         grade=5;    }     else if (b!= ' 0 ')     {         printf ("%d  is four digits \ n",  integer);        grade=4;     }    else if (c!= ' 0 ')     {         printf ("%d  is three digits \ n",  integer);        grade=3;     }    else if (d!= ' 0 ')     {         printf ("%d  is double-digit \ n",  integer);         grade=2;    }    else{         printf ("%d  is single digit \ n",  integer);        grade=1;     }    switch (grade)     {         case 1: printf ("%c\n",  e);p rintf ("%c\n",  e);break;         case 2: printf ("%c%c\n",  d,e);p rintf ("%c%c\n",  E , D); break;        case 3: printf ("%c%c%c\n",  C,D,E); printf ("%c%c%c\n",  e,d,c); break;        case 4: printf ("%c%c%c%c\n",  B,C,D,E);p rintf ("%c% C%c%c\n ",  e,d,c,b); break;        case 5: printf ("%c%c%c %c%c\n ",  a,b,c,d,e);p rintf ("%c%c%c%c%c\n ",  e,d,c,b,a);                 break;    }     return 0;}


How numbers are converted to ASCII characters--rectification C after lesson 4.9

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.