Given a number, write a program using while loop to reverse the digits of the number. For example, the number 12345 should be written as 54321
(Hint: Use modulus operator to extract the last digit and the integer division by 10 to get the n-1 digit number from the n digit number.)
#include <stdio.h>
#include <math.h>
void main()
{
int x,y,i=0,ans,P,n;
clrscr();
printf("Enter a number and number of digits:");
scanf("%d %d",&x,&n);
while(i<n)
{
y=pow(10,i);
P=x/y;
ans=P%10;
printf("%d",ans);
i++;
}
getch();
}
(Hint: Use modulus operator to extract the last digit and the integer division by 10 to get the n-1 digit number from the n digit number.)
#include <stdio.h>
#include <math.h>
void main()
{
int x,y,i=0,ans,P,n;
clrscr();
printf("Enter a number and number of digits:");
scanf("%d %d",&x,&n);
while(i<n)
{
y=pow(10,i);
P=x/y;
ans=P%10;
printf("%d",ans);
i++;
}
getch();
}
No comments:
Post a Comment