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.
#include "Lab5_3_2.h"
#include <iostream>
#include <iomanip>
#include <cstdlib>
usingnamespace 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;
}
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.