C++ | Loops specials

Introduction

This tutorial prints numbers in different forms. The first one prints odd numbers using for loop. The next one prints numbers in reverse using for loop. The last one prints alternate numbers negative using for loop. You can create this program using different loops i.e. while loop and do while loop. So enjoy the program. Lets begin…………..

Program to print odd numbers

#include<iostream.h>
#include<conio.h>
void main()
{
//clear the screen.
clrscr();
//declare variable type int
int i;
//loop structure
for(i=1;i<20;i++)
{
cout<<i<<endl;
i++;
}
//get character
getch();
}

Output

1
3
5
7
9
11
13
15
17
19

Program to print numbers in reverse

#include<iostream.h>
#include<conio.h>
void main()
{
//clear the screen.
clrscr();
//declare variable type int
int i;
//loop structure
for(i=10;i>0;i--)
{
cout<<i<<endl;
}
//get character
getch();
}

Output

10
9
8
7
6
5
4
3
2
1

Program to print alternate numbers negative

#include<iostream.h>
#include<conio.h>
void main()
{
//clear the screen.
clrscr();
//declare variable type int
int i,n=1;
//loop structure
for(i=1;i<=10;i++)
{
cout<<i*n<<endl;
n=n*-1;
}
//get character
getch();
}

Output

1
-2
3
-4
5
-6
7
-8
9
-10

How does it work

The outputs are directly printed. no need to input.

Extending it

The program can be extended by using different types of loops. Go ahead and extend it…..

Explanation.

  1. Include ‘iostream.h’ and ‘conio.h’ files.
  2. Add void main.
  3. Start program by first clearing the screen.
  4. Declare the variable as int (name them as you want.)
  5. Add the loop.
  6. Print the numbers.

At the end

You learnt creating the c++ program of creating different programs using loops. So now enjoy the program.

Please comment on the post and share it.
And like it if you liked.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s