Vaibhav Katre
Vaibhav Katre

Reputation: 59

Can we reverse the digits of a number using Array in c?

Can you please help me to reverse the digits of a number entered by the user using the concept of array in c. I know there is an option given below using which we can reverse the digits.

#include <stdio.h>
int main() {
    int n, rev = 0, remainder;
    printf("Enter an integer: ");
    scanf("%d", &n);
    while (n != 0) {
        remainder = n % 10;
        rev = rev * 10 + remainder;
        n /= 10;
    }
    printf("Reversed number = %d", rev);
    return 0;
}

Using the above program I can easily reverse the digits. But I want to use the concept of c to reverse the digits. Let me illustrate how:

#include<stdio.h>
int main(){
    int n,count=0;
    scanf("%d",&n);
    do{
        n=n/10;
        count++;
    }while(n>0);
    
    int i,a[100];
    
    for(i=0;i<count;i++){
        a[i]=n%10;
    }
    
    for(i=0;i<count;i++){
        printf("%d",a[i]);
    }
    return 0;
    
}

In the Above code, using Do-While loop I have counted the no of digits present in the number, so that I can use the count in my for loop.

for(i=0;i<count;i++){
        a[i]=n%10;
    }

In this for loop I have inserted all the digits of an number in reverse order. So that i can print all these elements using the next for loop which is given below

for(i=0;i<count;i++){
        printf("%d",a[i]);
    }

This for loop should print all the numbers stored in the array a[100] in reverse order. My expected output is:

     12345
     54321

Output Which I am getting

     12345
     00000

Can anyone suggest me the required changes and help me clear my concepts?

Upvotes: 0

Views: 873

Answers (2)

do{
        n=n/10;
        count++;
    }while(n>0);

print n after this loop and see if it's value is what you expect. n is 0 after this loop.

I'd suggest creating a temporary variable like this:

int temp;

and assign the value of n to temp(temp=n;).

Now when the do while loop ends, assign temp back to n so that n contains it's initial value.

Also, in this loop,

for(i=0;i<count;i++){
        a[i]=n%10;
 }

you should do n=n/10 (like you did while counting the number of digits) after a[i]=n%10.

So your final code may look like this:

#include<stdio.h>
int main(){
    int n,count=0;
    int temp;
    scanf("%d",&n);
    temp=n;
    do{
        n=n/10;
        count++;
    }while(n>0);
    n=temp;
    int i,a[100];
    
    for(i=0;i<count;i++){
        a[i]=n%10;
        n/=10;
    }
    
    for(i=0;i<count;i++){
        printf("%d",a[i]);
    }
    return 0; 
}

Also, you didn't really need the do while loop to count the number of digits, you could've done this in the first for loop like this:

#include<stdio.h>
int main(){
    int n,count=0;
    scanf("%d",&n);
    int i,a[100];
    for(i=0;n>0;i++){
        a[i]=n%10;
        n/=10;
        count++; //counting the number of digits
    }
    
    for(i=0;i<count;i++){
        printf("%d",a[i]);
    }
    return 0;
    
}

Upvotes: 2

0___________
0___________

Reputation: 67476

If I understand the question right you want to put digits from the number into the array in reverse order

size_t reverseIntegerToArray(int *buff, unsigned number)
{
    size_t size = 0;

    while(number)
    {
        buff[size++] = number % 10;
        number /= 10;
    }
    return size;
}

int main(void)
{
    int arr[20];
    size_t size = reverseIntegerToArray(arr, 12345678);
    for(size_t i = 0; i < size; i++)
    {
        printf("%d", arr[i]);
    }
    printf("\n");
}

Upvotes: 0

Related Questions