» » » C program to reverse a number using while loop and for loop

C program to reverse a number using while loop and for loop

• Finding reverse number means for example take 123 then we need to get 321 is its reverse number.
• In c programming we can do it by using for loop and while loop.
• Lets us see an example C program to get reverse number of a number by using while loop and for loop.
• while(n != 0)
•    {
•         rem = n%10;
•         reverse_Number = reverse_Number*10 + rem;
•         n /= 10;
•     }

Program #1 : Write a c program to reverse  number using for loop.

1. #include <stdio.h>
3. int main()
4. {
5.     int n, reverse_Number = 0, rem,Original_number=0;
6.
7.     printf("Enter a number to get reverse number ");
8.     scanf("%d", &n);
9.  Original_number=n;
10.     while(n != 0)
11.     {
12.         rem = n%10;
13.         reverse_Number = reverse_Number*10 + rem;
14.         n /= 10;
15.     }
16.
17.     printf("Reversed Number of %d is = %d",Original_number=0;,reverse_Number);
18.     getch();
19. }

Output:

Program #2 :  Write a C program to reverse a number using for loop

1. #include <stdio.h>
3. int main()
4. {
5.     int n, reverse_Number = 0, rem,Original_number=0;
6.
7.     printf("Enter a number to get reverse number ");
8.     scanf("%d", &n);
9.     Original_number=n;
10.     for(;n != 0;)
11.     {
12.         rem = n%10;
13.         reverse_Number = reverse_Number*10 + rem;
14.         n /= 10;
15.     }
16.
17.     printf("Reversed Number of %d is = %d",Original_number,reverse_Number);
18.
19.     getch();
20. }

Output:

1. Enter a number to get reverse number
2. 123987
3. Reversed Number of 123987 is = 789321