简体   繁体   中英

How to reverse a 3 digit number

Code:

#include <stdio.h>

int main()
{
    int n, num1,num2,num3, reverse;    

    printf("Enter the number to reverse:\n");
    scanf("%d", &n);

    num1 = n / 100;
    num2 = (n % 100) / 10;
    num3 = (n % 10) / 100;

    reverse = num3*100+ num2*10;

    printf(" The reverse is %d", reverse);

    system("pause");
    return 0;
}

I wanted to reverse a number without using any loops, I included the steps so far, but Im not sure if it is right or not:

  1. Declare the int into num1, num2 and num3 along witht the output, reverse
  2. Ask the user to enter a number to reverse
  3. Store the number using scanf
  4. Get the last digit of the number , num1= n/100
  5. Get the middle digit from the user, num2=(num%100)/10
  6. To get the last digit from the user, num3=(num%10)/100

Are these steps right? I tried working with the program but no luck whatsoever, until I get the steps correct.

Example:

Enter a number: 263
Output: 362

The number is always 3 digits, no change whatsoever

First of all , your num3 is wrong , that is modified below and secondly , reverse is calculated wrong

#include <stdio.h>

int main()

{


int n, num1,num2,num3, reverse ;


    printf("Enter the number to reverse:\n");
    scanf("%d", &n);

    num1 = n / 100;
    num2 = (n % 100) / 10;
    num3 = n%10 ;

    // num1 , num2 , num3 are digits only  , to make a number use the below step
    reverse = 100*num3 + 10*num2 + num1;

        printf(" The reverse is %d", reverse);



    system("pause");
    return 0;
}

Using a diverse approach

int main()
{
    char string[4];
    int reverse = 0;

    printf("Enter the number to reverse:\n");
    scanf_s("%d", &reverse);

    if (reverse > 999)
        return 0;

    sprintf_s(string, "%d", reverse);

    char c1 = string[0];
    string[0] = string[2];
    string[2] = c1;

    reverse = atoi(string);

    printf(" The reverse is %d", reverse);
    return 0;
}

or if you don't want to use atoi

int main()
{
    char string[4];
    int reverse = 0;

    printf("Enter the number to reverse:\n");
    scanf_s("%d", &reverse);

    if (reverse > 999)
        return 0;

    sprintf_s(string, "%d", reverse);

    printf(" The reverse is %c%c%c", string[2], string[1], string[0]);

    return 0;
}

if the middle digit is calculated like

num2=(num%100)/10;

then the less significant digit is calculated like

num3 = (num % 10)/1;

or just

num3 = num % 10;

This statement

reverse = num3+ num2+ num1;

does not give what you are expecting. You should write instead

reverse = 100 * num3 + 10 * num2 + num1;

In fact you need not to calculate the digits. Just enter a three-digit number as three separate digits.:)

For example

#include <stdio.h>

int main( void )
{
    unsigned int n1, n2, n3;

    printf("Enter a three-digit number: ");
    scanf("%1u%1u%1u", &n1, &n2, &n3);

    printf("Reversed number is %u%u%u\n", n3, n2, n1);

    return 0;
}

The program output might look like

Enter a three-digit number: 263
Reversed number is 362

If you do not want to output leading zeroes then you can include if statements in your program. That is instead of this statement

printf("Reversed number is %u%u%u\n", n3, n2, n1);

you can write

printf("Reversed number is ");
if (n3) printf("%u", n3);
if (n3 || n2) printf("%u", n2);
printf("%u\n", n1);

In this case if to enter for example

100

then the output will be

1

instead of

001

Or you can use the expression 100 * n1 + 10 * n2 + n1 . For example

printf("Reversed number is %u\n", 100 * n3 + 10 * n2 + n1);

here is a better version i think

   #include<stdio.h>
   int main()
   { 
   int num,n1,n2;
   printf("enter the number : ");
   scanf("%d",&num);
   n1=num%10;
   num=num/10;
   n2=num%10;
   printf("%d",n1*100+n2*10+num/10);
   return 0;
   }

i think this code can not get any cleaner, although you can use recursion via making a new function and reverse n number of digits.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM