Ad

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)


Ad
E
Sections
1
Exercises
#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;
}
Ad

© 2024 Garbage Valuegarbage value logo