473,378 Members | 1,447 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

help on prime number list c program

3
Hi, I'm a beginning C student and I'm working on a program that is supposed to display all the prime numbers between 50 and 100. I'm having trouble though. Can anyone help me out. This is what I have so far:

/*
* File: prime.cpp
* This program displays all the prime
* numbers between 50 and 100.
*/

#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((double)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");
}


Any help would be greatly appreciated. Thanks.
Sep 23 '07 #1
5 2675
Ganon11
3,652 Expert 2GB
Does this not work?
Sep 23 '07 #2
Hi,

There is another approach that is simpler in the case of restricted range.
Looks like that :

Expand|Select|Wrap|Line Numbers
  1. int tab[101];
  2. int i;
  3. int j
  4.  
  5. /* suppose all numbers are prime */
  6. for (i = 0; i < 101; ++i) {
  7.   tab[i] = 1;
  8. }
  9.  
  10. /* process even numbers */
  11. tab[0] = 0;
  12. for (i = 4; i < 101; i += 2) {
  13.   tab[i] = 0;
  14. }
  15.  
  16. /* process odd numbers by marking multiples as not prime */
  17. for (i = 3; i < 101; i += 2) {
  18.   if (tab[i]) {
  19.     for (j = 2 * i; j < 101; j += i) {
  20.       tab[j] = 0;
  21.     }
  22.   }
  23. }
Finally, you can extract prime numbers from array (tab[i] == 1).
Sep 23 '07 #3
cchris
3
Does this not work?
Yes, it does not work. Right now, if you run the program, it displays 53,55,59,61,65,67,71,73,77,79,83,89,91,95,97. (the bold numbers aren't pime.) Any ideas why those non-prime numbers are showing up?

Hi,
There is another approach that is simpler in the case of restricted range.
Looks like that :
Thank you for responding but the code you provided is too advanced for me. I'm only a beginner and I don't know what tab[] does. Also, I forgot to mention that my assignment requires me to use the sqrt() function. But thanks a lot for responding and maybe your code will be able to help someone else.
Sep 23 '07 #4
Savage
1,764 Expert 1GB
Here seems to be your problem:

Expand|Select|Wrap|Line Numbers
  1. for (i=3; i<=limit; i+=2)
  2. {
  3.    if (n%2==0) break;
  4.    if (n%i==0) break;
  5.    printf("\n%d",n);
  6.    break;
  7.  
  8. }
Because of break not all conditions are checked,reform it to something like:

Expand|Select|Wrap|Line Numbers
  1.      k=0;
  2. for (i=3; i<=limit; i+=2)
  3. {
  4.    if (n%2==0) k=1;
  5.    if (n%i==0) k=1;
  6. }
  7. if(!k)printf("\n%d",n);
and it should work.

Savage
Sep 23 '07 #5
cchris
3
Thanks a lot Savage! The program works now.
Sep 23 '07 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

9
by: Greg Brunet | last post by:
In doing some testing of different but simple algorithms for getting a list of prime numbers, I ended up getting some results that seem a bit contradictory. Given the following test program...
11
by: lostinpython | last post by:
I'm having trouble writing a program that figures out a prime number. Does anyone have an idea on how to write it? All I know is that n > 2 is prim if no number between 2 and sqrt of n...
0
by: AshifToday | last post by:
this was my and my frineds little project in earlier classes, the program seperates the composite and prime numbers in two sections of the screen ===================== /* This program has...
7
by: brian.digipimp | last post by:
Write a program that prompts the user to input a positive integer. It should then output a message indicating whether the number is a prime number. (Note: An even number is prime if it is 2. An odd...
2
by: nicks | last post by:
Hi there ! i need a bit of help as i am a new c++ user to write a program. The program should ask the user n questions or until the user enters -1 to Exit (n should be a constant and it should be...
2
by: QHorizon | last post by:
Hello, I'm new to Python (I've learned everything up to iterators so far) and fairly new to Programming. This would be my first real program: #Coordinate Geometry (The whole program is not...
12
by: electric916 | last post by:
I have a homework assignment i Am totally confused on. I started with a basic code to determine if a number is prime or not, but need guidance from here. I will post assignment details then what I...
6
by: sigkill9 | last post by:
I'm doing some reading in a python book and am doing one of the chapter exercises but I cant figure out how to get it to work and was hoping some of you python guru's could help out? Heres...
2
by: clouddragon | last post by:
Hi, i am in desperate need for any help regarding one of my assignments. I am to write a python program that lists the numbers that are composite from 1 to n(input) and write it to an external...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.