Jun 9, 2020

C program to reverse a given number using while loop

Ex: write a C program for reverse a given number. How to write a C program to reverse a given number. C program for reverse a given number.

Input from user:

Enter the number: 12345


Expected output:

Given number in reverse order: 54321





Step by step logic of the given program:

1. Accept input (number) from user.

2. Use one while loop to check given number is greater than 0 or not.

3. After that inside the while loop add reverse number logic.

4. Inside the loop the reverse number computed using..

reverse=reverse*10;
reverse=reverse+no%10;
no=no/10;

5. Print reverse number on the output screen.





Program:


#include<stdio.h>
int main()
{
 int no,reverse=0;

printf("Enter the number\n");
scanf("%d",&no);

    while(no>0)
    {
reverse=reverse*10;
reverse=reverse+no%10;
no=no/10;
    }

printf("Given number in reverse order:%d",reverse);

return 0;
}




Above program shows the following output:






Share This:    Facebook Twitter

0 comments:

Post a Comment

If you have any doubts, please discuss here...