Chapter - 4: The Loop Control Structure

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


E
Sections
1
Exercises

A

B

C

D

E

1

2

3

4

5

6

7

8

10

11

12

13

14

15

16

9

#include<stdio.h>
#include<conio.h>
#include<math.h>
int main()
{
	int i,j, flag;
	printf("2, 3, ");
	for(i = 3; i <= 300; i += 2)
	{
		for(j = 2; j <= (sqrt(i)); j++)
		{
			if( ! (i % j) )
			{
				flag = 0;
				break;
			}
			flag = 1;
		}
		if(flag)
			printf("%d, ", i);
	}
	printf("are the prime number.");
	getch();
	return 0;
}

© 2020 Garbage Valuegarbage value logo