dmilicev

prime_numbers_abc_bac_v1.c

Sep 3rd, 2020
158
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.  
  3.     prime_numbers_abc_bac_v1.c
  4.  
  5.     Task:
  6.     A 3-digit prime number 'ABC' is such that C= A+B and 'BAC' is also prime.
  7.     Find all possible values of 'ABC'.
  8.  
  9.  
  10.     You can find all my C programs at Dragan Milicev's pastebin:
  11.  
  12.     https://pastebin.com/u/dmilicev
  13.  
  14. */
  15.  
  16. #include <stdio.h>
  17.  
  18. // If the number is prime, function returns that number, otherwise returns 0
  19. int IsPrimeNumber_1(int number)
  20. {
  21.     int i;                          // counter for loop
  22.                                     // i<number !!!
  23.     for(i=2; i<number; i++)         // it is enough to go to the number !
  24.         if(number % i == 0)         // if the number is divisible by i,
  25.             return 0;               // it is not prime number
  26.  
  27.     return number;                  // number is prime number
  28. }
  29.  
  30. int main(void)
  31. {
  32.     int a, b, c, abc, bac, solved=0;
  33.  
  34.     for(a=0; a<10; a++)
  35.         for(b=0; b<10; b++)
  36.             for(c=0; c<10; c++)
  37.             {
  38.                 abc = a*100 + b*10 + c;
  39.                 bac = b*100 + a*10 + c;
  40.  
  41.                 if( IsPrimeNumber_1(abc) &&
  42.                     IsPrimeNumber_1(bac) &&
  43.                     c == a + b &&
  44.                     a != 0 &&
  45.                     b != 0 )
  46.                 {
  47.                     solved = 1;
  48.                     printf("\n a = %d \t b = %d \t c = %d \t %5d \t %5d \n", a, b, c, abc, bac );
  49.                 }
  50.             }
  51.  
  52.     if( !solved )
  53.         printf("\n There is no such digits. \n");
  54.  
  55.     return 0;
  56.  
  57. } // main()
  58.  
RAW Paste Data Copied