Write a program the outputs of number with the digits reversed.
#include <stdio.h>
#include <math.h>
int main()
{
int intnumber; /* can try long for bigger range, int range is the limit*/
int condition, remainder;
int counter = 0, i = 0, j = 0; /* counter to store the number of digit entered by user*/
int counter1 = 0; /* counter1 is similar, used as for loop sentinel*/
int reverseint[20];
printf("Enter an integer number: ");
scanf_s("%d", &intnumber); /* read and store input in intnumber*/
condition = intnumber; /* set the condition sentinel value to intnumber*/
while (condition > 0)
{
condition = condition /10;
counter = counter + 1;
counter1 = counter1 + 1; /* this counter for printing in reverse*/
}
counter = counter - 1;
printf("The number in reverse: ");
while (counter >= 0)
{
/* extract each of the decimal digits, need to cast to int*/
/* to discard the fraction part*/
/* pow(10, counter) used to determine the ...,10000, 1000, 100, 10, 1*/
remainder = intnumber % (int) pow(10, counter);
intnumber = intnumber/(int) pow(10,counter);
reverseint[i] = intnumber;
i++;
intnumber = remainder; /* update and repeat for the rest*/
counter = counter - 1;
}
/* print the array element in reverse*/
for (j=counter1-1; j >= 0; j--)
printf("%d ", reverseint[j]);
printf("\n");
return 0;
}
#include <stdio.h>
#include <math.h>
int main()
{
int intnumber; /* can try long for bigger range, int range is the limit*/
int condition, remainder;
int counter = 0, i = 0, j = 0; /* counter to store the number of digit entered by user*/
int counter1 = 0; /* counter1 is similar, used as for loop sentinel*/
int reverseint[20];
printf("Enter an integer number: ");
scanf_s("%d", &intnumber); /* read and store input in intnumber*/
condition = intnumber; /* set the condition sentinel value to intnumber*/
while (condition > 0)
{
condition = condition /10;
counter = counter + 1;
counter1 = counter1 + 1; /* this counter for printing in reverse*/
}
counter = counter - 1;
printf("The number in reverse: ");
while (counter >= 0)
{
/* extract each of the decimal digits, need to cast to int*/
/* to discard the fraction part*/
/* pow(10, counter) used to determine the ...,10000, 1000, 100, 10, 1*/
remainder = intnumber % (int) pow(10, counter);
intnumber = intnumber/(int) pow(10,counter);
reverseint[i] = intnumber;
i++;
intnumber = remainder; /* update and repeat for the rest*/
counter = counter - 1;
}
/* print the array element in reverse*/
for (j=counter1-1; j >= 0; j--)
printf("%d ", reverseint[j]);
printf("\n");
return 0;
}
No comments:
Post a Comment