Home > other >  Using while loop statement create a program that enter 5 numbers and determine if it is a prime or n
Using while loop statement create a program that enter 5 numbers and determine if it is a prime or n

Time:12-19

In my code I can only input 1 number. How to enter 4 additional numbers and determine if it's a prime or not and then display only the prime numbers.

#include<stdio.h>
  
int main()
{
    
    int n, c = 2, f = 1;
  
    printf("Enter a number:");
    scanf("%d", &n);
  
    while(c < n)
    {
        if(n%c == 0)
        {
            f = 0;
            break;
        }
        c  ;
    }
  
    if(f) printf("%d is prime number\n\n", n);

    return 0;
}

Here is my output, using the code above:

Please enter a number:2

...Program finished with exit code 0
Press ENTER to exit console.

And here is the expected output:

Please enter a number:1

Please enter a number:2
2 is a prime number. 

Please enter a number:3
3 is a prime number.

Please enter a number:4

Please enter a number:5
5 is a prime number.

CodePudding user response:

Let's say you want to check 5 numbers whether they are prime or not.

The approach you followed, you just have to do the same for rest of the numbers.

To do that, you can run an additional loop. For example,

int inputSize = 5;
while(inputSize--)
{
    printf("Enter a number:");
    scanf("%d", &n);
    
    // now check if the number is prime or not
}

Note: Don't forget to initialize the values in proper place.

Sample code:

#include<stdio.h>

int main()
{
    int inputSize = 5;
    while(inputSize--)
    {
        int n, c = 2, f = 1;
        
        printf("Enter a number:");
        scanf("%d", &n);

        while(c < n)
        {
            if(n%c == 0)
            {
                f = 0;
                break;
            }
            c  ;
        }

        if(f) printf("%d is prime number\n\n", n);
    }

    return 0;
}

Check out the following resource to know more about primality of a number

https://www.studytonight.com/c-programs/c-program-to-check-whether-a-number-is-prime-or-not

CodePudding user response:

You need to read the numbers in a loop. You also need to check that the input really is a number. The function scanf returns the number of successfully read values. Try this:

#include <stdio.h>

int main(void)
{
    int c, ch, i, m, n;

    i = 0;
    while (i < 5) {
        printf("Enter a number: ");
        m = scanf("%d", &n);
        if (m == 1) {
            c = 2;
            while ((c < n) && (n % c != 0)) {
                c  ;
            }
            if (c == n) {
                printf("%d is prime number\n", n);
            }
            putchar('\n');
            i  ;
        } else {
            fprintf(stderr, "Not a number\n\n");
            do { ch = getchar(); } while (ch != '\n');
        }
    }
    return 0;
}

If the input is invalid we can give the user another chance. In this case we first need to read past the unread characters which represents the invalid input.

CodePudding user response:

It is evident that you need a loop with 5 iterations to enter 5 numbers.

As the notion of prime numbers is defined for natural numbers then you need to use an unsigned integer type to store entered numbers.

And your code is incorrect because for numbers 0 and 1 it outputs that they are prime numbers.

The program can look the following way.

#include <stdio.h>

int main( void )
{
    const size_t N = 5;
    
    for ( size_t i = 0; i < N; i   )
    {
        printf( "Please enter a number: " );
        
        unsigned int n = 0;
        
        if ( scanf( "%u", &n ) == 1 )
        {
            int prime = n % 2 == 0 ? n == 2 : n != 1;
        
            for ( unsigned int i = 3; prime && i <= n / i; i  = 2 )
            {
                prime = n % i != 0;
            }
        
            if ( prime ) printf( "%u is a prime number.\n", n );
        }
        else
        {
            while ( getchar() != '\n' );
        }
        
        putchar( '\n' );
    }
}

The program output might look like

Please enter a number: 1

Please enter a number: 2
2 is a prime number.

Please enter a number: 3
3 is a prime number.

Please enter a number: 4

Please enter a number: 5
5 is a prime number.
  • Related