Prime / odd and even numbers C [closed]

0

I want to make a program in C in which you are asked to enter numbers until you enter a "0" or a negative number, when this happens the program should show:

  • How many numbers were entered
  • How many numbers are cousins
  • How many numbers are even
  • How many numbers are odd
  • I have done practically each paragraph separately, but I do not know how I could put everything in the same program.

    #include <stdio.h>
    int main()
    {
        int n, i, flag = 0;
    
        printf("Teclea un numero entero positivo: ");
        scanf("%d",&n);
    
        for(i=2; i<=n/2; ++i)
        {
            if(n%i==0)
            {
                flag=1;
                break;
            }
        }
    
        if (flag==0)
            printf("%d SI es un numero primo.",n);
        else
            printf("%d NO es un numero primo :(",n);
            getch();
    
        return 0;
    }
    
        
    asked by P. Garcìa 02.08.2017 в 19:14
    source

    2 answers

    3

    You could use something like this:

    #include <stdio.h>
    
    int esPrimo(int n)
    {
        int i, cant = 0;
        for(i = 1; i <= n; i++)
        {
            if(n % i == 0)
                cant++;
        }
        if(cant == 2)
            return 1;
        else
            return 0;
    }
    
    int main(void)
    {
        int n;
        int cantNum = 0, cantPrimos = 0, cantPar = 0, cantImpar = 0;
    
        printf("Teclea un numero entero positivo: ");
        scanf("%d",&n);
        while (n > 0)
        {
            cantNum++;
            if(n % 2 == 0)
                cantPar++;
            else
                cantImpar++;
            if (esPrimo(n))
                cantPrimos++;
            printf("Teclea un numero entero positivo: ");
            scanf("%d",&n);
        }
        printf("Cantidad de numeros ingresados: %d\n", cantNum);
        printf("Cantidad de numeros primos: %d\n", cantPrimos);
        printf("Cantidad de numeros pares: %d\n", cantPar);
        printf("Cantidad de numeros impares: %d\n", cantImpar);
    
        return 0;
    }
    
        
    answered by 02.08.2017 / 19:29
    source
    1

    What you have to add is a loop to repeat the whole process that you already have, in this example I am using a while loop, although you could also use a do-while .

    while (n > 0) {
    //realizar proceso
    }
    

    Your code would be:

    #include <stdio.h>
    int main()
    {
      int n=1, i, flag = 0;
      while (n > 0){
        printf("Teclea un numero entero positivo: ");
        scanf("%d",&n);
    
        for(i=2; i<=n/2; ++i)
          {
            if(n%i==0)
              {
                flag=1;
                break;
              }
          }
    
        if (flag==0)
          printf("%d SI es un numero primo.",n);
        else
          printf("%d NO es un numero primo :(",n);
        getch();
        printf("\n");
      }
      return 0;
    }
    

    Note : I initialized the variable n so that it can enter the loop.

        
    answered by 02.08.2017 в 19:39