Examples C

WAP in C to print all prime numbers from 1 to 300

The Program in C that prints all prime numbers from 1 to 300 is given below:

#include <stdio.h>

int main()
{
    int i, j, isPrime;

    printf("Prime numbers between 1 and 300 are: \n");

    for(i=2; i<=300; i++)
    {
        isPrime = 1;

        for(j=2; j<=i/2; j++)
        {
            if(i%j == 0)
            {
                isPrime = 0;
                break;
            }
        }

        if(isPrime == 1)
            printf("%d ",i);
    }

    return 0;
}

Output:

Prime numbers between 1 and 300 are:
2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97 101 103 107 109 113 127 131 137 139 149 151 157 163 167 173 179 181 191 193 197 199 211 223 227 229 233 239 241 251 257 263 269 271 277 281 283 293

Pro-Tips💡

This program uses a nested for loop to check if each number between 1 and 300 is prime.

The outer loop iterates through all the numbers between 1 and 300, and the inner loop checks if the number is prime by dividing it by all numbers between 2 and half of the number.

If no divisors are found, the number is prime and is printed to the screen.


Note that this program is using the basic method to check the primality, there are more optimized algorithms to find prime numbers.

Learn C-Sharp ↗

C-sharp covers every topic to learn about C-Sharp thoroughly.

Learn C Programming ↗

C-Programming covers every topic to learn about C-Sharp thoroughly.

Learn C++ Programming↗

C++ covers every topic to learn about C-Sharp thoroughly.

Codeauri is Code Learning Hub and Community for every Coder to learn Coding by navigating Structurally from Basic Programming to Front-End Development, Back-End Development to Database, and many more.

Related Posts

C# Program to Find Sum of Rows & Columns of a Matrix

The Program in C# Program to Find Sum of Rows & Columns of a Matrix is given below: Output: Hello Codeauri Family,enter the number of rows and columns…

C# Program to Calculate Determinant of Given Matrix

The Program in C# Program to Calculate Determinant of Given Matrix is given below: Output: Hello Codeauri Family, enter the number of rows and columns of the matrix…

C# Program to Find Sum of right Diagonals of a Matrix

The Program in C# Program to Find Sum of right Diagonals of a Matrix is given below: Output: Hello Codeauri Family, enter the number of rows and columns…

C# Program to Find Transpose of Given Matrix

The Program in C# Program to Find Transpose of Given Matrix is given below: Output: Hello Codeauri Family, enter the number of rows and columns in the matrix:22Enter…

C# Program for Multiplication of two square Matrices

The Program in C# Program for Multiplication of two square Matrices is given below: Output: Hello Codeauri Family, enter the number of rows/columns in the matrices:2Enter the elements…

C# Program to Delete Element at Desired position From Array

The Program in C# Program to Delete Element at Desired position From Array is given below: Output: Hello Codeauri Family, enter the number of elements in the array:4Enter…

Leave a Reply

Your email address will not be published. Required fields are marked *

Your Journey into Code Begins Now: Discover the Wonders of Basic Programming

X