Page 1 of 2 12 LastLast
Results 1 to 10 of 11

Thread: How to get a reversed number

  1. #1

    How to get a reversed number

    I am new to c language, I got a question: how to prints the reversal of a two-digit number without using arithmetic to split the number into digits. could anyone give me a hint? thanks
    I\'d found my best love, but I didn\'t treasure her. I felt regretful after that. It\'s the ultimate pain in the world. If God can give me a chance, I will tell her three word: \"I love you\". If God wanna give me a time limit, I\'ll say this love will last 10 thousand years!

  2. #2
    Senior Member
    Join Date
    Jun 2002
    Posts
    394
    don't store the number as a number, use a string or a character array (same thing) and use the subscript operator to output it

    string thingey = "23";
    cout << thingey[1] << thingey[0];
    Hmm...theres something a little peculiar here. Oh i see what it is! the sentence is talking about itself! do you see that? what do you mean? sentences can\'t talk! No, but they REFER to things, and this one refers directly-unambigeously-unmistakably-to the very sentence which it is!

  3. #3

    string....

    The best way i know is by using a string for storing your number:
    (this is c++ not c)

    int string[20];
    cin.get(string,20);
    cout<<strrev(string);
    there is another way you can use:

    (this is in c)


    #include<stdio.h>
    #include<conio.h>
    void main(){
    int num,digit=0;
    clrscr();
    while(1){
    printf("\nenter a number:\n");
    scanf("%d",&num);
    printf("\nInverse=");
    do{
    digit=num%10;
    printf("%d",digit);
    num/=10;
    }while(num!=0);
    }
    }

    The above was compiled and debuged.it works fine but you'll never see a pro coder
    use such thing in his work.
    i recommend reading your number from a file,storing it in a string,reversing it
    using the strrev function.

    note:if you want to turn your string to an int use the following command:
    #include <stdlib.h>
    int atoi(const char *s);
    example
    ----------------------------------------------------------------------------------------------
    #include <stdlib.h>
    #include <stdio.h>

    int main(void)
    {
    int n;
    char *str = "12345.67";

    n = atoi(str);
    printf("string = %s integer = %d\n", str, n);
    return 0;
    }



    ------------------------------------------------------------------------------------------------------------------------
    As far as the laws of mathematics refer to reality, they are not certain, and as far as they are certain, they do not refer to reality.-- Albert Einstein

  4. #4
    Senior Member
    Join Date
    Oct 2001
    Posts
    114
    This is a cheeky one..mind u its just for fun ...............

    use gotoxy and print the number twice consecutevly........ 7272 now overwrite the first and last digit by a space......and what u see is a magic!!!!!!!!!!

    No Strings Attached

    most general methods u have seen and there are plenty of methods if u involve methematics
    Better Laugh At Your Own Problems..
    Coz...The World Laughs At Them

  5. #5
    Hi mom!
    Join Date
    Aug 2001
    Posts
    1,103
    Haha, lol coolnads

    That's cheezy indeed
    I wish to express my gratitude to the people of Italy. Thank you for inventing pizza.

  6. #6
    thanks you all.

    but as you mentioned by using "String" way. how could I let user input a two-digit nubmer and then reverse it?

    how do i read a string, such as "56", seperately? I mean, let a integer x = 5, and another int y = 6. thanks again

    All I mean is using c language. I tried many times, mession failed!! (Mession Impossible 3!! ^&^)
    I\'d found my best love, but I didn\'t treasure her. I felt regretful after that. It\'s the ultimate pain in the world. If God can give me a chance, I will tell her three word: \"I love you\". If God wanna give me a time limit, I\'ll say this love will last 10 thousand years!

  7. #7
    Senior Member
    Join Date
    Nov 2001
    Posts
    4,785
    I found this by going to google and inputing "c/c++ reversing numbers"

    this was the first non advertised link! youd be much better off learning to search before you learn to program!

    ****************************

    /* array2.c -- Read/writing/reversing integer arrays
    */

    #include <stdio.h>

    #define NMAX 10

    void intSwap(int *x, int *y);
    int getIntArray(int a[], int nmax, int sentinel);
    void printIntArray(int a[], int n);
    void reverseIntArray(int a[], int n);

    int main(void) {
    int x[NMAX];
    int hmny;

    hmny = getIntArray(x, NMAX, 0);
    printf("The array was: \n");
    printIntArray(x,hmny);
    reverseIntArray(x,hmny);
    printf("after reverse it is:\n");
    printIntArray(x,hmny);
    }

    void intSwap(int *x, int *y)
    /* It swaps the content of x and y */
    {
    int temp = *x;
    *x = *y;
    *y = temp;
    }

    void printIntArray(int a[], int n)
    /* n is the number of elements in the array a.
    * These values are printed out, five per line. */
    {
    int i;

    for (i=0; i<n; ){
    printf("\t%d ", a[i++]);
    if (i%5==0)
    printf("\n");
    }
    printf("\n");
    }

    int getIntArray(int a[], int nmax, int sentinel)
    /* It reads up to nmax integers and stores then in a; sentinel
    * terminates input. */
    {
    int n = 0;
    int temp;

    do {
    printf("Enter integer [%d to terminate] : ", sentinel);
    scanf("%d", &temp);
    if (temp==sentinel) break;
    if (n==nmax)
    printf("array is full\n");
    else
    a[n++] = temp;
    }while (1);
    return n;
    }

    void reverseIntArray(int a[], int n)
    /* It reverse the order of the first n elements of a */
    {
    int i;

    for(i=0;i<n/2;i++){
    intSwap(&a[i],&a[n-i-1]);
    }
    }

    ********************************

    i still like coolnads better!
    Bukhari:V3B48N826 “The Prophet said, ‘Isn’t the witness of a woman equal to half of that of a man?’ The women said, ‘Yes.’ He said, ‘This is because of the deficiency of a woman’s mind.’”

  8. #8
    thanks, I went to google and try to find what I need, but I didn't. that's all not what I want.

    I want let a user input a string from keyboard, such as "56". then I need to assign a integer x = 5, and another Integer y = 6!! How could I do that? thanks
    I\'d found my best love, but I didn\'t treasure her. I felt regretful after that. It\'s the ultimate pain in the world. If God can give me a chance, I will tell her three word: \"I love you\". If God wanna give me a time limit, I\'ll say this love will last 10 thousand years!

  9. #9
    I got some idea. what do you think?
    I\'d found my best love, but I didn\'t treasure her. I felt regretful after that. It\'s the ultimate pain in the world. If God can give me a chance, I will tell her three word: \"I love you\". If God wanna give me a time limit, I\'ll say this love will last 10 thousand years!

  10. #10
    you will soon learn what buffer overflow means as soon as the user inputs somthing like
    44444444444444444444 or aaaaaaaaaaaaaaaaaaa !
    you have got to read some books brah..
    ------------------------------------------------------------------------------------------------------------------------
    Never judge a hacker by the color of his hat
    DS:5395 2816 3933 319 8968 2208 6325 9399 3663 1821 4916 2139 9300 3748 5119 1140

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •