Dremendo Tag Line

Print the number series 1 -2 3 -4 5 using while loop in C++

while Loop - Question 5

In this question, we will see how to print the number series 1 -2 3 -4 5 -6... up to nth term in C++ programming using while loop. To know more about while loop click on the while loop lesson.

Q5) Write a program in C++ to print the number series given below using while loop.

1 -2 3 -4 5 -6... up to nth term

Program

#include <iostream>
#include <conio.h>

using namespace std;

int main()
{
    int i=1,s=1,n;

    cout<<"Enter nth term ";
    cin>>n;

    while(i<=n)
    {
        cout<<i*s<<" ";
        s=s*-1;		// changing the sign
        i=i+1;
    }
    return 0;
}

Output

Enter nth term 10
1 -2 3 -4 5 -6 7 -8 9 -10
video-poster