victorialais

Função - 1 se é primo 0 se não for

Oct 2nd, 2018
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.37 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. int primo (int n){
  4. int a, i, div;
  5.  
  6.  
  7.   for (i = 1; i <= n; i++) {
  8.     if (n % i == 0) {
  9.      div++;
  10.     }
  11.   }
  12.    
  13.   if (div == 2)
  14.    a = 1;
  15.   else
  16.  a = 0;
  17.     return a;
  18.  
  19. }
  20.  
  21.  
  22. int main () {
  23. int x;
  24. printf("Digite um numero inteiro e positivo: ");
  25.     scanf("%d", &x);
  26. int a =  primo (x);
  27. printf ("%d", a);
  28. return 0;
  29. }
Advertisement
Add Comment
Please, Sign In to add comment