Triangle using While Loop in C++

Printing Triangle using While Loop in C++ programming. While Loop allows code to be executed and repeat a specific block of code until the boolean condition is met.

Program While Loop String Password Program

Program

#include<iostream.h>
#include<conio.h>
#include<string>
void main()
{
string a;
string str = "Bilal";
while(a != str) //loop stops when 'a' value Equal to 'str' value.
{
cout<<"Please Enter Password: ";
cin>>a; //user Enter value storing in 'a'.
}
cout<<"Password Correct!";
getch();
}
Output
Please Enter Password: khan
Please Enter Password: abc
Please Enter Password: Bilal
Password Correct!

Program While Loop using Break Statement

Program

#include<iostream.h>
#include<conio.h>
void main()
{
int a = 0;
while(a <= 10) //loop looping from 0 to 10.
{
if(a == 5) //if 'a' value equal to 5 then
{
cout<<"The Statement is Break on 5";
break; //break the process.
}
cout<<a<<endl;
a++; //adding 1 to 'a' value.
}
getch();
}
Output
0
1
2
3
4
The Statement is Break on 5

Program Printing Triangle using While Loop

Program

#include<iostream.h>
#include<conio.h>
void main()
{
int a = 0;
while(a <= 10) //loop looping from 0 to 10.
{
int b = 0;
while(b <= a) //loop looping from 0 to 'a' value.
{
cout<<"* ";
b++; //adding 1 to 'b' value.
}
cout<<"\n"; //used for New Line, U also use 'endl'.
a++; //adding 1 to 'a' value.
}
getch();
}
Output
*
* *
* * *
* * * *
* * * * *
* * * * * *
* * * * * * *
* * * * * * * *
* * * * * * * * *
* * * * * * * * * *
* * * * * * * * * * *