C Programs
C Programs
#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;
}
Output
#include <stdio.h>
int main() {
int n, reversed = 0, remainder, original;
printf("Enter an integer: ");
scanf("%d", &n);
original = n;
return 0;
}
Program to Count the Number of Digits
#include <stdio.h>
int main() {
long long n;
int count = 0;
printf("Enter an integer: ");
scanf("%lld", &n);
// iterate at least once, then until n becomes 0
// remove last digit from n in each iteration
// increase count by 1 in each iteration
do {
n /= 10;
++count;
} while (n != 0);
Output
After the first iteration, the value of n will be 345 and the count is incremented to 1.
After the second iteration, the value of n will be 34 and the count is incremented to 2.
After the third iteration, the value of n will be 3 and the count is incremented to 3.
After the fourth iteration, the value of n will be 0 and the count is incremented to 4.
Then the test expression of the loop is evaluated to false and the loop terminates.