Advertisement
Guest User

Untitled

a guest
Dec 16th, 2017
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.50 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <locale.h>
  4. #include <ctype.h>
  5. #include <math.h>
  6. #include <string.h>
  7.  
  8. int prime (int a);
  9.  
  10. int main()
  11.     {
  12.     //FILE *in;
  13.     setlocale (LC_ALL, "Rus");
  14.  
  15.  
  16.  
  17.     long int n = 1;
  18.     int  i = 0, k = 1;
  19.     //in = fopen("in.txt", "rt");
  20.  
  21.     char c;
  22.     int pos;
  23.     char* num = malloc(10);
  24.  
  25.     printf ("Желательно ввести небольшое натуральное число: \n");
  26.     input:
  27.     memset(num, ' ', 10);
  28.     pos = 0;
  29.     while (scanf("%c", &c)) {
  30.         if (!pos && (isspace(c) || c == '\t' || c == '0')) {
  31.             continue;
  32.         } else if (pos > 9) {
  33.             getline(&num, 0, stdin);
  34.             printf("о-оо-он с-слишком большой, он не влезет, семпай\n");
  35.             goto input;
  36.         } else if  (c == '\n') {
  37.             break;
  38.         }   else if (isdigit(c)) {
  39.             num[pos] = c;
  40.             ++pos;
  41.         }  else {
  42.             getline(&num, 0, stdin);
  43.             printf("нехорошие у тебя символы, попробуй ещё раз\n");
  44.             goto input;
  45.         }
  46.     }
  47.     n = atoi (num);
  48.     printf ("\n%ld\n", n);
  49.     i = 2;
  50.     while (k < n) {
  51.         if (prime (i)) {
  52.             printf ("%10d", i);
  53.             k ++ ;
  54.         }
  55.         i++;
  56.     }
  57.     return 0;
  58. }
  59.  
  60. int prime (int a) {
  61.     int i;
  62.     for (i = 2; i < (int)(ceil(sqrt(a))); i++) {
  63.         if (a % i == 0) return 0;
  64.     }
  65.     return 1;
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement