'How can I print integers of this sequence with a for-loop: (1, -2, 3, -4, 5, -6, 7, -8, 9, -10)?
What's the best way to print these integers? I want every other integer to be positive and every other to be negative.
I've tried this and it works but I wonder is there a better way to do it.
#include <iostream>
using namespace std;
int main(){
for(int i=1; i<=10; i++){
if(i%2==0) i*=-1;
cout<<i;
if(i%2==0) i*=-1;
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
