You are here:

C/c programe for prime number

Advertisement


Question

prime
First, i need to calculate, and print out all the prime numbers less than or equal to the number 17 followed by the total number of prime numbers found.

Then the program should ask the user to enter a 3-digital positive integer and to repeat the processes as described above. In addition, your program should ask the user whether he/she wants to run the program again. If so, prompt the user to enter a 3-digital number again and so on. There is no preset number of times that the user can enter a number. The program will be terminated when it receives appropriate input from the user.

The output sample is attrached.

I have go though that question several time and finally written something below:

#include<iostream.h>

void main()
{
 int n , isprime;
 cout<<"Enter range :";
 cin>>n;
 if ( n >= 100 )
 {
   cout<<"Error : Out of range ..";
   exit(0);
 }


 for( int i = 2 ; i <= n ; i++)
 {
    isprime  = 1;
    for( int j = 2 ; j <= i ; j++)
    {
       if( i == j)
          continue;
       else if( i % j == 0)
       isprime = 0;
    }
    if(isprime)
    cout<<i<<" ";
 }
}

obiously my attempt is not correct bocause im not totally follow the requires for that question and there are many ideas i still haven't get.

so, can anyone can help?

THX

Answer
Hi, Ming.

You are very, very close with your answer already.  I've made a few changes to the program for optimization purposes, formatting purposes, and some minor functionality.  The code is below.  If you have any questions about it, please do not hesitate to ask.  I am here to help. :)

#include <iostream>
using namespace std;

void main()
{
   char doAgain = 'n';
   do
   {
       int n, isprime, numPrime = 0;
       cout << "Enter range :";
       cin >> n;

       if (n > 999)
       {
           cout << "Error : Out of range";
           return;
       }

       cout << "The prime numbers less than or equal to " << n << " are:\n";
       for (int i = 2 ; i <= n ; i++)
       {
           isprime  = 1;
           for (int j = 2 ; j < i ; j++)
           {
               if (i % j == 0)
                   isprime = 0;
           }

           if (isprime)
           {
               cout << "\t" << i << endl;
               ++numPrime;
           }
       }

       cout << "The total number of prime numbers is: " << numPrime << "\n\n";

       cout << "Would you like to enter the number again? (y/n)";
       cin >> doAgain;
       cout << endl;
   } while (doAgain == 'y');
}

Joseph Moore

Expertise

I've been programming in one form or another since my brother taught me BASIC when I was 6. I've been programing professionally since I was 20, first web development with HTML, JS, DHTML, CSS, etc., then I became a video game developer, writing code in C, C++, C#, SQL, assembly, and various scripting languages. I've even written my own scripting languages, custom designed for the games I was making. I also dabble in Java, PHP, and Perl. I've worked on pretty much every aspect of game development, including graphics, audio, gameplay, tool, UI, input, animation, and physics.

Experience

I've been writing C code for 12 years, both on my own in my spare time and professionally.

Organizations
IGDA

Education/Credentials
Bachelor of Science in Game Design and Development, Full Sail University, Winter Park, FL

Awards and Honors
Salutatorian and Advanced Achiever Awards at Full Sail; Independent Games Festival Student Showcase winner, 2004; Featured article on Gamasutra about an experimental game developed in 2004

©2012 About.com, a part of The New York Times Company. All rights reserved.