views:

300

answers:

5

like 17, is a prime, when reversed, 71 is also a prime.

We manage to arrive at this code but we cant finish it.

#include <stdio.h>
main()
{
    int i = 10, j, c, sum, b, x, d, e, z, f, g;

    printf("\nPrime numbers from 10 to 99 are the follwing:\n");

    while (i <= 99)
    {
        c=0;

        for (j = 1; j <= i; j++)
        {
            if (i % j == 0) c++;
        }

        if (c == 2)
        {
            b = i;
            d = b / 10;
            e = b - (10 * d);
            x = (e * 10) + d;

            {
                z = 0;
                f = x;

                for (j = 1; j <= f; j++)
                {
                    if (f % j == 0) z++;
                }

                if (z == 2)
                {
                    printf("%d %d\n", i, f);
                }
            }
        }

        i++;
    }

    getch();
}

my problem is how to add all the fs..

the answer should be 429.

how can i add all the f?

+8  A: 

Why don't you break up the code into some functions:

  • bool isPrime(int number) that checks if a number is prime.
  • int reverse(int number) that reverses the number.

Then the algorithm would be:

sum = 0;
for (i = 2; i <= 99; ++i)
   if (isPrime(i) && isPrime(reverse(i)))
      sum += i;
Mehrdad Afshari
and use more descriptive variable names :-)
toolkit
+1, but don't use "bool" in pure C, just use an int, and return 1 for true and 0 for false. This code *screams* to be broken into functions, so just do it.
unwind
unwind: I used `bool` because I wanted to describe what the function does.
Mehrdad Afshari
bool is pure, standard C, since ten years back...
Lars Wirzenius
@liw.fi: I think in society, C normally defaults to C89 not C99. :)
Mehrdad Afshari
A: 

At the beginning initialize sum = 0;. Then, next to your printf count the prime: sum += i;. you can then print it at the end.

kmkaplan
+1  A: 

If this is a basic programming class and you are just interested in the result then this will get you there, however if it is an algorithms class then you may want to look at the Sieve of Eratosthenes.

You may also want to think about what makes a 2 digit number the reverse of another 2 digit number and how you would express that.

Steve Weet
+1  A: 
batbrat
"tested up to (num/2)" incorrect, it should be sqrt(num)
vartec
Thanks vartec. I am editing to fix that now.
batbrat
Done :). The error is fixed. I had forgotten the rule. Thanks for saving me :)
batbrat
A: 

Hahaha..

I forgot to initialize int sum..

include

main() { int i=11,j,c,sum=0,b,x,d,e,z,f,g;

  printf("\nPrime numbers from 10 to 99 are the follwing:\n");
  while(i<=125)
  {
        c=0;
     for(j=1;j<=i;j++)
     {
          if(i%j==0)
     c++;
     }
       if(c==2) 
       {
       b=i;
       d=b/10; 
       e=b-(10*d);
       x=(e*10)+d;
             {
                  z=0;
                  f=x;
                  for(j=1;j<=f;j++)
                   {
                   if(f%j==0)
            z++;
                 }
                    if(z==2)
                    {
                     printf("%d %d  \n",i,f);  
                     sum+=f;
                    }                                                         
               }               
       }
        i++;

}
printf("sum --------> %d ", sum); getch(); } i got this code last night..

now i want to raise my maximum number to 125..

^^

what will i do next.?

@all

sorry..

im just new to C so i cant get better solutions..

sorry..

No need to be sorry. You'll learn with practice. I am learning too. All the best.
batbrat