Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- prime_numbers_abc_bac_v1.c
- Task:
- A 3-digit prime number 'ABC' is such that C= A+B and 'BAC' is also prime.
- Find all possible values of 'ABC'.
- You can find all my C programs at Dragan Milicev's pastebin:
- https://pastebin.com/u/dmilicev
- */
- #include <stdio.h>
- // If the number is prime, function returns that number, otherwise returns 0
- int IsPrimeNumber_1(int number)
- {
- int i; // counter for loop
- // i<number !!!
- for(i=2; i<number; i++) // it is enough to go to the number !
- if(number % i == 0) // if the number is divisible by i,
- return 0; // it is not prime number
- return number; // number is prime number
- }
- int main(void)
- {
- int a, b, c, abc, bac, solved=0;
- for(a=0; a<10; a++)
- for(b=0; b<10; b++)
- for(c=0; c<10; c++)
- {
- abc = a*100 + b*10 + c;
- bac = b*100 + a*10 + c;
- if( IsPrimeNumber_1(abc) &&
- IsPrimeNumber_1(bac) &&
- c == a + b &&
- a != 0 &&
- b != 0 )
- {
- solved = 1;
- printf("\n a = %d \t b = %d \t c = %d \t %5d \t %5d \n", a, b, c, abc, bac );
- }
- }
- if( !solved )
- printf("\n There is no such digits. \n");
- return 0;
- } // main()
Add Comment
Please, Sign In to add comment