If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

Re: Reversing the digits

Originally Posted by Tanushreeagr

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)

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.

Re: Reversing the digits

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.
}

Re: Reversing the digits

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.

* The Perfect Platform for Game Developers: Android
Developing rich, high performance Android games from the ground up is a daunting task. Intel has provided Android developers with a number of tools that can be leveraged by Android game developers.

* The Best Reasons to Target Windows 8
Learn some of the best reasons why you should seriously consider bringing your Android mobile development expertise to bear on the Windows 8 platform.