repl.it
C

Print 10 prime numbers in reverse.

fork
loading
Files
  • main.c
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include <stdio.h>

int isPrime(int num) {

  int prime = 1;

  for(int k = 2; k < num; k++) {
    if(num % k == 0) {
      prime = 0;
      break;
    }
  }

  return prime;
}

int main(void) {
  
  printf("Please enter 5 numbers.\n");
  int arr[5];

  int i = 0;
  while(i < 5) {

    int num;
    scanf("%d", &num);

    arr[i] = num;

    i++;
  }

  int j = 4;
  while(j >= 0) {

    int prime = isPrime(arr[j]);

    if(prime == 1) {
      printf("%d", arr[j]);
    }

    j--;
  }

  return 0;
}
?