Looping Structure Computer Programming Exercise

What is Looping Structure?

We use loops whenever we have to repeat a statement or set of statement number of time. For example you have to print your name in c++ programming language, so, you have to use looping structure. Learn more about looping structure

Today we are going to discuss a c++ programming question which prints the following series using looping structure

Write a program in c++ which prints folloing series using looping structure

1       -4       7        -10        13        -16...-40
include"iostream"
using namespace std;
main()
{
int n,p=0,i;
for(i=1;i<=40;i=i+3)
{
if(p==1)
{
cout<<i<<"\t";
p=0;
}
else
{
cout<<-i<<"\t";
p=1;
}
}
}

Above Question in while loop:

 
include"iostream"
using namespace std;
main()
{
int n=-40,p=0,i=1;
while(i<=n)
{
if(p==1)
{
cout<<i<<"\t";
p=0;
}
else
{
cout<<-i<<"\t";
p=1;
i = i +3;
}
}

Leave a Reply