i need a simple program to write prime numberts from 1 to 100
prime number
Collapse
X
-
Tags: None
-
What have you tried so far on this problem? We are not allowed to write the whole code for you (Posting Guidelines )Originally posted by gaurav1430i need a simple program to write prime numberts from 1 to 100
Regards -
[code=c]Originally posted by zodilla58What have you tried so far on this problem? We are not allowed to write the whole code for you (Posting Guidelines )
Regards
#include<stdio. h>
#include<math.h >
int main()
{
int n,i,limit;
printf("The prime numbers between 50 and 100 are:");
for(n=50;n<100; n++)
{
limit=sqrt((dou ble)n+1);
for (i=3; i<=limit; i+=2)
{
if (n%2==0) break;
if (n%i==0) break;
printf("\n%d",n ); break;
}
}
printf("\n");
}[/code]
i have tried this one only. but i m getting some non prime numbers in the output plz bct themComment
-
The trouble is the printf statement inside your inner for loop. The if statements before are working to break out of the loop when a non-prime number has been found, but just because a number passes the test the first time, you print it out and break out of the loop anyway. Rethink the logic here - consider using a boolean variable. Or even consider using a function isPrime(int a) to indicate if a is prime or not.Comment
-
I recommend looking into the modulus operator ( % )
with this, you can do something like...
7 % 4 ... which is essentially 7 divided by four ... and the remainder is returned
so if you have
int x = 7 % 4
x would be 3
you can write code to come up with prime numbers as big as you want using this, and by using literally just a few lines of code in a for loopComment
-
code removed - read the Guidelines of the site, please
Hiiii Dear, i have tried to find program for prime numbers from 1 to 100, u can try this program. If u want further guidance, u can also contact me , I m Gursimar, my mobile no. is removedComment
Comment