Professional Documents
Culture Documents
LAB 3 - For LOOP
LAB 3 - For LOOP
Here,
#include <iostream>
int main() {
for (int i = 1; i <= 5; ++i) {
cout << i << " ";
}
return 0;
}
Output
1 2 3 4 5
#include <iostream>
using namespace std;
int main() {
int num, sum;
sum = 0;
return 0;
}
Output
In the above example, we have two variables num and sum . The sum variable
is assigned with 0 and the num variable is assigned with the value provided
by the user.
Note that we have used a for loop.
Here,
... + 10 .
Example 2: Program in C++ to print inverted half star pyramid
pattern
In the following C++ program, the user can enter the number of rows to
print the inverted half star pyramid pattern as he wishes, then the result
Numbers From 1 to 5
#include <iostream>
using namespace std;
int main()
{
int i, j, n;
cout << "Enter number of rows: ";
cin >> n;
for(i = n; i >= 1; i--)
{
for(j = 1; j <= i; j++)
{
cout << "* ";
}
// ending line after each row
cout << "\n";
}
return 0;
}
Output :