Output the values from biggest to smallest.

Hello World!

I have browsed this forum since a long time and now I am finally asking a question :D

I have completely finished my code and I am stumped on one last thing. I need to output the prime numbers from biggest to smallest but I can't seem to do it. I have tried various for loop variations but they don't seem to work. Can you please help me? THanks so much in advance.

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
36
37
38
39
40
41
42
43
44
45
#include "Lab5_3_2.h"
#include <iostream>
#include <iomanip>
#include <cstdlib>

using namespace std;

// declaring variables. Start is 1, since we start from 1 to the input value. The input is for the integer that we will be given.
int start;
int i;
int x;
int input;

int main() {
    
    // Ask for input.
    
    cout << "Enter a positive integer to count all the prime numbers till"  << endl;
    cin >> input;
    
    // The outer loop will define the parameters for out input, it will start from 1 and end when the value is wither biger than or equal to the input. Until then, the value will increment by addition of 1.
    
    for (start=1; start<=input;start++){
        x = 0;
        
        // The inner loop is to check if a number is prime or not. If the number is not prime, the function for the inner for loop is terminated and the function ends with break.
        
    for (i=2; i<=start/2; i++)
    {
        if (start%i==0) {
        x++;
            break ;
    }
    }
        // This last statement is necessary, so that when people enter negative as an integer value, the function shouldn't try to calculate it. Instead is should terminate without any feedback at all.
        
        if (x==0 && start !=1)
            // for (start<=input; start>i; start--) // not sure how to put this, would this work like this? or do i need to do something else? 
                cout << (start) << setw(5);
    }
  
    return 0;
    

}
Any help is appreciated, thanks.
Well, for one, you need to store the value of every single prime number that is obtained. This is simply due to the nature of the prime numbers themselves, in that you can easily tell whether a number is prime by dividing it by all preceding prime numbers. Once you have that vector of primes, just display it in reverse.
How would I do that? they could vary by so much... It does print those prime numbers just fine, I just need to flip the array.
Topic archived. No new replies allowed.