There might be some error in the logic used by me. Could someone please point that out to me?
//Reverse the number
#include<stdio.h>
int main()
{
int m,a,b,c,d,e,n;
printf("\nEnter a five digit number: ");
scanf("%d",m);
a = m/10000; //1st digit
b = (m - a*10000)/1000; //2nd digit
c = ((m-a*10000) - b*1000)/100; //3rd digit
d = (((m-a*10000) - b*1000) - c*100)/10; //4th digit
e = ((((m-a*10000) - b*1000) - c*100) - d*10); //5th digit
n = e*10000 + d*1000 + c*100 + b*10 + a; //reverse of the number
printf("Reverse of the number given by you is %d\n", n);
return 0;
}
As already pointed out in a comment your bug is here:
scanf("%d",m); --> scanf("%d", &m);
^ ^^
wrong correct
as scanf
must be passed a pointer to the object where the converted value is to be stored.
Your calculation can be simplified by using %
like
e = m % 10; //5th digit
m = m / 10;
d = m % 10; //4th digit
m = m / 10;
c = m % 10; //3rd digit
m = m / 10;
b = m % 10; //2nd digit
m = m / 10;
a = m % 10; //1st digit
As you can see the calculation is the same every time so you could use a loop.
int factor = 10000;
n = 0;
while(m != 0)
{
n = n + (m % 10) * factor;
m = m / 10;
factor = factor / 10;
}
Note: The loop above only works when the input has exactly 5 digits. A more generic solution could be:
n = 0;
while(m != 0)
{
n = n * 10;
n = n + (m % 10);
m = m / 10;
}