Q10 Write a program to print all prime numbers from 1 to 300. (Hint: Use Nested loops, break and continue)

Write a program to print all prime numbers from 1 to 300. (Hint: Use Nested loops, break and continue)

Program: 103

Write a c program to print all prime numbers from 1 to 300. (Hint: Use Nested loops, break and continue)

#include<stdio.h>
#include<conio.h>
int main()
{
    int num, max=300, i, flag;

    //create a for loop to run a counter from 1 to 300.

    for(num=1;num<=max;num++)
    {
        flag=0;
        //second for loop to check the number from 1 to 300 is prime or not.
        for(i=2;i<=num/2;i++)
        {
            if(num%i==0)
            {
                flag=1;
                break;
            }
        }

        if(flag==0 & num!=1) // we remove 1 from this condition
            printf("%d\t", num);
    }

}

Output:

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

Lokesh Kumar: Being EASTER SCIENCE's founder, Lokesh Kumar wants to share his knowledge and ideas. His motive is "We assist you to choose the best", He believes in different thinking.

View Comments (2)

  • Because if a number is not divided with the number till its half, then it will not be divided from further numbers. So we will check only till half of the number.

Related Post
Leave a Comment

This website uses cookies.