rev[i] = num % 10;// IS THIS RIGHT WAY TO ASSIGN CHARACTER ARRAY?????
You're extracting the rightmost digit from the integer held in num. It will be an integer between 0 and 9. What you want to do now is to convert it to a char between '0' and '9' before assigning it to the array. Since those characters lie next to each other in the ASCII encoding this will work (although it isn't that elegant)
//Reverse an integer
int ReverseInteger(int no)
{
int rnum = 0;
while (no) {
rnum = rnum * 10 + (no % 10);
no /= 10;
}
return rnum;
}
I considered that approach as well, but whether it actually is valid depends on the exact requirements set up by the assignment: It behaves different from the char[]-based approach when the subject number ends with one or more zeroes. These would become insignificant in processing and thus omitted in the output. That was irrelevant in the emirp context since there are no primes ending with 0 anyway.
I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.
This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.
Here are some fixes to your code. Study it and my comments.
Originally Posted by Tanushreeagr
Code:
//PROGRAM TO REVERSE THE 5 DIGIT NUMBER
#include <stdio.h>
void main()
{
int num, i; // Initializing i here is not needed since it is done later.
char rev[6]; // You need to allocate space for the null character to terminate the string.
// This is required by the printf() function.
printf("Enter any 5 digit number\n");
scanf("%d", &num);
i = 0;
while(num & (i < 5)) // It is very important that you check that the number is not too long.
// Without this check data could be written outside of the rev array causing memory corruption.
{
rev[i] = '0' + (num % 10);
i++;
num = num/10;
}
rev[i] = '\0'; // Make sure you terminate the string with a null character.
printf("%s\n", rev); // This prints a new line after the result.
}
To make the revised program work, the while condition test should be the logical and not the bitwise and.
Code:
while(num && (i < 5))
Also it would be good practice to declare and initialise i as close as possible to where it is first used (or at least initialise it where it is defined).
main() should return an integer by the ANSI standard.
Code:
//PROGRAM TO REVERSE THE 5 DIGIT NUMBER
#include <stdio.h>
int main()
{
char rev[6]; // You should allocate space for a null character to terminate the string.
int num;
printf("Enter any 5 digit number: ");
scanf("%d", &num);
int i = 0;
while (num && (i < 5)) // It is very important that you check that the number is not too long.
// Without this check data could be written outside of the rev array causing memory corruption.
{
rev[i++] = '0' + (num % 10);
num /= 10;
}
rev[i] = '\0'; // Make sure you terminate the string with a null character.
printf("%s\n", rev); // This prints a new line after the result.
return (0);
}
All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.
Bookmarks