473,657 Members | 2,996 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

help on prime number list c program

3 New Member
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((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");
}


Any help would be greatly appreciated. Thanks.
Sep 23 '07 #1
5 2681
Ganon11
3,652 Recognized Expert Specialist
Does this not work?
Sep 23 '07 #2
ruskalym
65 New Member
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 New Member
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 Recognized Expert Top Contributor
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 New Member
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
2707
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 (testPrimes.py) with two algorithms that both check for primes by testing only odd numbers using factors up to the square root of the value, where Primes1 is based on all of the existing primes so far, and Primes2 is based on all odd numbers, I would...
11
5932
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 (inclusivly) evenly divides n.
0
2397
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 been made by A & A Group. Muhammad Ali: Roll # 1462 Class A-2 , B.Sc.(Hons.) in C.S.
7
7378
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 integer is prime if it is not divisible by an odd integer less than or equal to the square root of the number.) I was given this assignment in my c++ class and I'm having trouble with it. I've looked at the professors lab solutions but I just...
2
375
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 equal to the largest digit in your ID number; for example: if your id number is S0930350, n should be 9). The program should generate the following report. Total number of questions asked The number of questions correctly answered The number...
2
2602
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 shown) import math import sys print "Welcome to the Coordinate Geometry Calculator!"
12
6209
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 have so far. Problem 1: Is it a prime number? Write a Python program that allows the user to enter a whole number greater than 1 and that determines whether or not this number is a prime number. If it is a prime number, then this information is...
6
6995
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 description of the problem to be solved: "A positive whole number n > 2 is prime if no number between 2 and the square root of n (inclusive) evenly divides n. Write a program that accepts a value of n as input and determines if the value is prime. If n...
2
2441
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 txt. I was able to write something that checks whether something is composite or not but able to incorporate it into a loop as such. for example: n = 50 then the composite numbers are 4 6
0
8384
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8718
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8499
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8601
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7314
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6162
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5630
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4150
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2726
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.