decision making in c++

Using nested for loops write a program to show all prime numbers in range given by user.
 
 
post what you have and you will more than likely get some help.
And what did you try to write?
what changes should i made to the following program ?


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
#include <stdlib.h>
using namespace std;
int main(){ 
    long n; cout << "Enter a positive integer: "; cin >> n;
    if (n < 2) cout << n << " is not prime." << endl;
    else if (n < 4) cout << n << " is prime." << endl;
    else if (n%2 == 0) cout << n << " = 2*" << n/2 << endl;
    else{ 
       for (int d=3; d <= n/2; d += 2)
          if (n%d == 0){
             cout << n << " = " << d << "*" << n/d << endl;
             system("PAUSE"); exit(0);
          }
          cout << n << " is prime." << endl;
    }
    system("PAUSE"); return 0; 
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <iostream>
using std::cout;
using std::cin;
using std::endl;

int main()
{
int range_of_choice = 0;
bool isprime;
do
{
cout<<"Enter a number greater than zero: "<<endl;
cin>>range_of_choice;
} while ( range_of_choice <= 0 );

 for (int i = 2; i < range_of_choice; ++i) 
    {
        isprime = true;
        for (int j=2; j*j<=i; ++j)
        {
            if (i % j == 0) 
            {
                isprime = false;
                break;    
            }
        }   
        if(isprime)
        {
            cout << i << " ";
            }
    }
char k;
cin>>k;
return 0;
}


I think they have this policy thing on this website where you have to show your attempt firsthand before having someone helping you with your exercise. Luckily for you though, such policies do not apply to me, i am sacrosanct. However, i do encourage you to type out your first attempt next time, it's for your own good. Or you could simply pm me, i'd love to do all your homework for you whenever you want (lol).
Topic archived. No new replies allowed.