473,473 Members | 1,970 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

coding for the prime number

3 New Member
i want u to help me to check my program either it is true or false...
when i run the program, the output will be like:
prime:3 prime:5 and so on. i didn't want the output will be like that.
i just want the output will be like this: (e.g., 3,5,7,11......).
thank you....

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>     
  2. using namespace std;
  3. void prime_num(int);
  4. int main(void)
  5. {
  6.  
  7. int n = 100;     
  8. prime_num(n);
  9.  
  10. }
  11.  
  12. void prime_num( int n)
  13.  
  14. {
  15.         bool isPrime=true;    //The Bool structure provides some basic operations on boolean values.
  16.         for ( int k = 3; k <= n; k++)
  17.         {
  18.                 for ( int r = 2; r <= n; r++)
  19.                 {
  20.                         if ( k!=r && k % r == 0 )
  21.                         {
  22.                           isPrime=false;
  23.                           break;         // to stop looping
  24.                         }
  25.                 }
  26.                 if (isPrime)
  27.                 {
  28.                   cout << "  Prime: " << k <<  "";        // To show the output 
  29.                 }
  30.                 isPrime=true; 
  31.         }
  32.         system("pause");
  33. }
Mar 15 '07 #1
1 1542
sicarie
4,677 Recognized Expert Moderator Specialist
If you want it to just say:

Prime: 3, 5, 7, 11 ...

Then move the 'cout << "Prime: "; outside the loop.

As for testing, a compiler will do you better, and you'll get more experience than trying to get us to do and check your homework.
Mar 15 '07 #2

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

Similar topics

36
by: Dag | last post by:
Is there a python module that includes functions for working with prime numbers? I mainly need A function that returns the Nth prime number and that returns how many prime numbers are less than N,...
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: don | last post by:
Ok, this is a homework assignment, but can you help me out anyway...... I need a routine for figuring out if a number inputted by the user is a prime number or not...... all I'm asking for is Not...
2
by: wudoug119 | last post by:
This is my code and it will take any number that I input and say it is a prime number. Please help me... int Prime(int prime) //declares isPrime as a function using integers { ...
4
by: SweetLeftFoot | last post by:
Hello, i have designed some code that works out the first 250 prime numbers and prints them to the screen. However i need to implement 2 functions, one of which returns a 1 if the number is a prime...
60
by: rhle.freak | last post by:
Here is my code to generate prime numbers.It works absolutely fine when the range is *not very large*. However on initializing i with a large integer it produces erroneous results (some numbers...
7
by: newstips6706 | last post by:
1, 2, 3, 5, 7... PRIME Numbers ________________________________ Definitions What is a PRIME Number ?
8
by: yana | last post by:
Question... An approximate value of pi can be calculated usong the series given below: Pi=4 ((-1)n)=n is a power for-1(for your information) Write a C++ program to calculate the...
1
by: parasuit | last post by:
HI every body I need following programs for java .........please urgent i m biggner so help me................but urgent Problem # 1 An integer is said to be prime if it is...
0
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,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
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,...
1
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...
0
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...
0
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 ...
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.