Printing prime number

65 Views Asked by At

// Print all prime numbers from 1 to given number

C Programming

#include <stdio.h>
#include <math.h>

void checkprime(int);

int main()
{
    int num;

    printf("Enter the number till which you want prime numbers: ");
    scanf("%d\n", &num);

    checkprime(num);

    return 0;
}

void checkprime(int num)
{
    int flag, i, j;
    flag = 1;

    for (i = 1; i <= num; i++)
    {
         for (j = 2; j <= sqrt(i); j++)
             if (i % j == 0)
             {
                 flag = 0;
                 break;
             }

         if (flag)
             printf("%d\t", i);
         else
             printf("Not a prime number");
     }
}

// Output

Enter the number till which you want prime numbers: 10

It is showing build with errors and there is nothing in the output. I am not able to find the error.

The expected output should be all prime numbers form 1 to a given number, but there is nothing in the output.

I am new to C, so if you have any tips for me it will be helpful.

0

There are 0 best solutions below