How do you come to the output?
Sep 27, 2021 at 11:09pm UTC
Code:
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 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
void createList(node*& head)
{
for (int i = 5; i < 20; i++)
{
bool condition = true ;
for (int j = 2; j <= i / 2; ++j)
{
if (i % j == 0)
{
condition = false ;
break ;
}
}
if (condition)
{
node* temp = new node(i);
temp->next = head;
head = temp;
}
}
node* temp = head;
while (temp != nullptr )
{
cout << temp->value << " " ;
temp = temp->next;
}
}
int main(){
node* head;
head = nullptr ;
createList(head);
}
The answer to this is 19 17 13 11 7 5, but how does the answer come to be? I am not sure why the output is that, would someone be able to explain why?
Last edited on Sep 27, 2021 at 11:09pm UTC
Sep 27, 2021 at 11:34pm UTC
It gets the prime numbers between 5 and 20 and sequentially prepends them to a linked list, then traverses that list, thus printing them in decreasing order.
Topic archived. No new replies allowed.