Program in C to find out reverse of a number (Dirty Code)
// Program to find reverse of a number
// 1234 => 4321
// 4 X 10^3 = 4000 + 3 X 10^2 = 4300 + 2 X 10^1 = 4320 + 1 X 10^0 = 4321
#include <stdio.h>
#include <math.h>
int main(int argc, char const *argv[])
{
int n, t, num = 0, count = 0;
printf("Enter some number: ");
scanf("%d", &n);
t = n;
// It's Counting number of digits
while (t > 0)
{
int rem = t % 10; // Last Digit
count++;
t /= 10;
}
count--;
// Reverse Code
while (n > 0)
{
int rem = n % 10; // Last Digit
num += rem * pow(10, count--);
n /= 10;
}
printf("Reverse is %d\n", num);
return 0;
}
// 1234 => 4321
// 4 X 10^3 = 4000 + 3 X 10^2 = 4300 + 2 X 10^1 = 4320 + 1 X 10^0 = 4321
#include <stdio.h>
#include <math.h>
int main(int argc, char const *argv[])
{
int n, t, num = 0, count = 0;
printf("Enter some number: ");
scanf("%d", &n);
t = n;
// It's Counting number of digits
while (t > 0)
{
int rem = t % 10; // Last Digit
count++;
t /= 10;
}
count--;
// Reverse Code
while (n > 0)
{
int rem = n % 10; // Last Digit
num += rem * pow(10, count--);
n /= 10;
}
printf("Reverse is %d\n", num);
return 0;
}
Comments
Post a Comment
Post Your Valuable Comments