In this program!
This program takes an integer input from the user. Then the while loop is used until n != 0 is false.
In each iteration of while loop, the remainder when n is divided by 10 is calculated and the value of n is reduced by times.
#include <stdio.h>
int main()
{
int n, reversedNumber = 0, remainder;
printf("Enter an integer: ");
scanf("%d", &n);
while(n != 0)
{
remainder = n%10;
reversedNumber = reversedNumber*10 + remainder;
n /= 10;
}
printf("Reversed Number = %d", reversedNumber);
return 0;
}
Output:
This program takes an integer input from the user. Then the while loop is used until n != 0 is false.
In each iteration of while loop, the remainder when n is divided by 10 is calculated and the value of n is reduced by times.
#include <stdio.h>
int main()
{
int n, reversedNumber = 0, remainder;
printf("Enter an integer: ");
scanf("%d", &n);
while(n != 0)
{
remainder = n%10;
reversedNumber = reversedNumber*10 + remainder;
n /= 10;
}
printf("Reversed Number = %d", reversedNumber);
return 0;
}
Output:

No comments:
Post a Comment