answersLogoWhite

0


Best Answer

100% working

void main()

{

int n,i=1,j,c;

clrscr();

printf("Enter Range To Print Prime Numbers")

scanf("%d",&n);

printf("Prime Numbers Are Following;

while(i<=n)

{

c=0;

for(j=1;j<=i;j++)

{

if(i%j==0)

c++;

}

if(c==2)

printf("%d ",i)

i++;

}

getch();

}

User Avatar

Wiki User

12y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

11y ago

#include<stdio.h>

void main()

{

int i, prime, lim_up, lim_low, n;

clrscr();

printf("\n\n\t ENTER THE LOWER LIMIT…: ");

scanf("%d", &lim_low);

printf("\n\n\t ENTER THE UPPER LIMIT…: ");

scanf("%d", &lim_up);

printf("\n\n\t PRIME NUMBERS ARE…: ");

for(n=lim_low+1; n<lim_up; n++)

{

prime = 1;

for(i=2; i<n; i++)

if(n%i == 0)

{

prime = 0;

break;

}

if(prime)

printf("\n\n\t\t\t%d", n);

}

getch();

}

This answer is:
User Avatar

User Avatar

Wiki User

9y ago

#include<iostream>

bool is_prime (const size_t num)

{

// The first two primes are 2 and 3

switch (num)

{

case (0): case (1): return false;

case (2): case (3): return true;

}

// All even numbers > 3 are non-prime.

if (num%2==0) return false;

// Initialise a maximum divisor (the square root of num).

size_t max = (size_t) std::sqrt ((double) num);

// Add 1 if even, 2 if odd (so that max is odd).

max += (max%2) ? 2 : 1;

// Test every divisor in the half-closed range [3:max)

for (size_t div=3; div!=max; div+=2)

// Is the number evenly divisible?

if (num%div==0)

// Yes, it is non-prime.

return false;

// The number is definitely prime.

return true;

}

int main()

{

using std::cout;

cout << "Prime numbers in closed range [0:100]:\n";

for (size_t num=0; num<=100; ++num)

if (is_prime (num))

cout << num << '\n';

cout << std::endl;

}

This answer is:
User Avatar

User Avatar

Wiki User

13y ago

#include<stdio.h>

#include<conio.h>

void main ()

{

int i,j,k;

clrscr();

for(i=50;i<100;i++)

{

k=1;

for(j=1;j<i;j++)

{

if(i%j==0)

{

k++;

}

}

if(k==2)

{

printf("\n %d is prime",i);

}

}

getch ();

}

This answer is:
User Avatar

User Avatar

Wiki User

14y ago

#include

#include

#include

void main()

{

int i,j;

clrscr();

for(i=3;i<=1000;i++)

{

for(j=2;j<=i;j++)

{

if(i%j==0)

break;

}

if(i==j)

cout<

}

getch(); // this is the easiest method to print prime nos made by Taabi

}

This answer is:
User Avatar

User Avatar

Wiki User

12y ago

#include<stdio.h>

#include<conio.h>

#include<stdlib.h>

int main(int argc, char **argv)

{

int iNum,iRem,iCount=2;

printf("enter the no");

scanf("%d",&iNum);

if(iNum==1)

{

printf("The no. is not prime");

exit(0);

}

if(iNum==2)

{

printf("The no. is prime");

exit(0);

}

do

{

iRem=iNum%iCount;

if(iRem==0)

{

printf("the given no is not prime");

exit(0);

}

iCount++;

}

while(iCount<=(iNum/2));

printf("the given no is prime");

}

This answer is:
User Avatar

User Avatar

Wiki User

6y ago

bool is_prime (unsigned);

for (unsigned n=1; n<=50; ++n) if (is_prime (n)) std::cout << n << " is prime" << std::endl;

You will have to implement the is_prime() function yourself. The following is a reasonably efficient but simple implementation given the narrow range of values to be considered. A larger range will require a more efficient implementation.

bool is_prime (unsigned n) {

if (n<2) return false; // 0 and 1 are not prime

if !(n%2)) return n==2; // 2 is the only even prime

// test all odd divisors from 3 up to the square root of n

unsigned max {(unsigned) std::sqrt ((double) n))};

for (unsigned div=3; div<=max; div+=2) if (!(n%div)) return false;

// if we get this far, n is prime

return true;

}

This answer is:
User Avatar

User Avatar

Wiki User

7y ago

// Returns true if n is prime, false if n is composite (non-prime). bool is_prime (unsigned n) {

if (n<2) return false; // 2 is the first prime

if (!(n%2)) return n==2; // 2 is the only even prime

unsigned max = (unsigned) sqrt ((double) n); // calculate max factor

for (unsigned f=3; f<=max; f+=2) if (!(n%f)) return false; // n is composite (it has a prime factor)

return true; // n is prime

}

// Returns the next prime greater than n.

unsigned next_prime (unsigned n) {

if (n<2) return 2; // 2 is the first (and only) even prime

if (n==2) return 3; // 3 is the first odd prime

if (!(n%2))++n; else n+=2; // increment n, ensuring n is odd

while (!is_prime(n)) n+=2; // check every odd value until n is prime

return n;

}

void print_primes (unsigned max) {

unsigned n=0;

printf ("List of primes up to %u\n", max);

while ((n=next_prime (n))<=max) printf ("%u\n", n);

}

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: How do you write a c program to print all prime numbers from 1 to n by using while loop with output?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

Program for print prime all number from 1 to 100 in foxpro?

Prime numbers are numbers that are only divisible by themselves and the number 1. You can write a program to print all prime numbers from 1 to 100 in FoxPro.


What BASIC program can compute and display all prime numbers from 1 to 40?

PRINT 2,3,5,7,11,13,17,19,23,29,31,37


Write a program to print first 100 alternative prime numbers?

This would require some computer knowledge. It can make it easier to find out the prime numbers without figuring it out in your head.


Q2 Write a program to print even numbers between 10 and 50?

You can use int i; for (i = 10; i &lt;= 50; i += 2) {//print i} as a program to print even numbers between 10 and 50.


How do you write a program to print numbers to 50 except prime?

First, create a for loop from a,1 to 50. Inside of that create another for loop b,2 to a-1. If a/b=int(a/b) then you know it is not prime


How to print the code of a program in the output terminal when the program is being executed in cpp?

Such a program is called a Quine. http://en.wikipedia.org/wiki/Quine_(computing)


Write a java script program to print first ten odd natural numbers in C?

Q.1 Write a program to print first ten odd natural numbers. Q.2 Write a program to input a number. Print their table. Q.3 Write a function to print a factorial value.


Write a C program to find the prime numbers from 1 to 300?

/*the program to print prime no from 1 to 300*/ #include&lt;stdio.h&gt; #include&lt;conio.h&gt; void main() { int i,j; clrscr(); printf("The prime numbers from 1 to 300 are\n"); for(j=2;j&lt;=300;j++) { for(i=2;i&lt;=j/2;i++) if(j%i==0) break; if(i&gt;j/2) { printf("%d ",j); } } }


Write a C Program to print sum of squares of odd numbers?

#include


Write a c program to print prime numbers from 1 to 10000 that has an unit digit which is multiple of 3?

This is a homework question and does not deserve an answer because you will learn nothing other than being lazy.


What does the print command do in Python?

The print command is a way to display output to the console. The hello world program, for example, can be written in python as simply print("Hello world") Other values can also be used in a print statement: a = 4 print(a) #will print the number 4


How can print prime numbers in between 1-100 but not adjust 40-50 in php language?

Use a counted loop in the closed range [1:100]. If the count is in the closed range [40:50], print the number. For all other numbers outwith this range, only print the number if it is prime.