Hello everyone,,am os glad to join daniweb finally !
i like all of u and i think u're really smart guys
am facing really hard assignment i couldn't do it at all :(
the issue is ;
1. Write a program that finds and prints all of the prime numbers between 3 and 100. A prime number is a number such that one and itself are the only numbers that evenly divide it (e.g., 3, 5, 7, 11, 13, 17, …).

plz if anyone knows plz help me am so depressed :( !

Recommended Answers

All 13 Replies

>i like all of u
I believe I can cure you of that.

>plz if anyone knows plz help me
Dude, slow down and take the time to compose a coherent post. This isn't a chat room. You've got all the time in the world to make your posts presentable and understandable.

>Write a program that finds and prints all
>of the prime numbers between 3 and 100.

Okay.

>A prime number is a number such that one and
>itself are the only numbers that evenly divide it

So now you know what a prime number is. Break the problem down into two parts:

  1. Identify if a given number is prime.
  2. Loop over N numbers and display each one that's prime.

#1 is the hardest, but it's still very straightforward. But on the off chance that you're not mathematically inclined, a good way to determine if one number is evenly divisible by another is with the remainder operator. If the result is 0, you've got an even division:

if (a % b == 0)
{
  std::cout<<"a is evenly divisible by b\n";
}
else
{
    std::cout<<"a is NOT evenly divisible by b\n";
}

Let's say you're trying to determine if x is prime. Loop over every number from 2 to (x-1) and perform that test. If none of them are evenly divisible, x is prime. This is the simplest method.

a lot of thankses for you darling
and am very sorry about the writting way :$!
do you recommend me to use cmath library or not ?!
and please could you show me what shall i do exactly,because am so lost :'(

If you have a fixed range to find prime numbers over, as in your case all primes below 100 another method is to create a list of all the numbers, 3,5,7,...97,99. Then you take each entry in the list in turn and remove all multiples of that value from the list. When you get to the end of the list the numbers that are left are all prime.

  1. Populate list with odd values 3,5,7,...97,99

  2. Loop until at end of list
  3. Get next entry in list (3 on the first iteration)
  4. Remove odd multiples of entry from list (9, 15, 21 ... 93, 99 on the first iteration)
  5. End Loop
  6. Print list

>do you recommend me to use cmath library or not ?!
Since <cmath> is completely unnecessary for solving the problem, no. A common optimization for checking the primality of x is only testing from 2 to sqrt(x), but you'd be wise to keep it as simple as possible for now.

>could you show me what shall i do exactly
No. I already described your code from start to finish. That's already crossing the line of doing your homework for you because I did the hard part of solving the problem. Now all you need to do is turn it into code, which is trivial. I refuse to help you any further unless you post code to prove an honest attempt and ask a specific question.

#include<iostream>
using namespace std;

void main()
{

int a,c=2;
cout <<"Enter the number\n";
cin >> a;
while (a!=1)
{
if(a%c==0)

a=a/c;
cout << c << "\n";

else
c++;
}
return ;
}

this is my code before fixing it !
to show you my honosty :)!

Have you run that?

It does not determine if a is prime, that is easy to tell because at no point is there a "a is prime" cout statement.

What it actually prints out is all the prime divisors of a.

That is of course ignoring the fact that you have main returning void so the entire program exhibits undefined behaviour because main always returns int.

Narue, you are awesome!! second here is some thing for the problem.
1. prime number can only be divided by themselves, so a composite number can be divided by other prime numbers. And there are only few prime numbers from 1-100.
2. so you should make an array of all the primes starting with 1 to n :1<i<=n<100 and for every x see if x%i =0 if not add it to the array. (nested for loops) Then you only have to check if the input item is in the array and not wast time recalculating it every time.

To the OP:
When we (at least I) ask you to show up a code and we will try to fix it we say it in a spirit that a car mechanic would say ``Sir, I can't build a car for you, but if you have a defective car, I can repair it". What you are doing is giving him a toy car and saying ``here is my car, please fix it so that I can sit in it".
In and all, it means that you won't be getting any pre-made code over here: learn the language, write the code (doesn't matter if it is not working), and then ask for help incorporating the details of all the experiments you did with it when trying to figuring out why it didn't work.
Use, code tags. void main is bad.

@Banfa
That essentially the Sieve of Eratosthenes


@Narue>A common optimization for checking the primality of x is only testing from 2 to sqrt(x)
I just went through one of the article which says that rather than checking if i<=sqrt(x) , it would be wise to check if i*i<=x. A small trick I thought worth sharing.

>@Narue: A common optimization for checking the primality of x is only testing from 2 to sqrt(x)
Yes, though you should consider not giving more information than necessary. The OP is clearly at a low level, so starting with the simplest algorithm possible is by far the fastest way to get him started.

He can start optimizing when the code actually runs correctly.

>The OP is clearly at a low level
Actually, I was sharing it with you. But then I realised `Oh no, whom do I think I am sharing it with?'
Never mind :)

Request to delete this post

Its very difficutl question. i dont know about it sory....

commented: I can see a spammer. -1
commented: Take your junk elsewhere -1

@Banfa
That essentially the Sieve of Eratosthenes

A-ha, I knew it had a name but as with so many algorithms I find it easier to remember how the algorithm works rather than its name. Which works fine except when I am trying to communicate with other programmers.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.