Example of for loop in c++ programing

0

A for loop is a repetition control structure that allows you to efficiently write a loop that needs to execute a specific number of times.

Example :

for (statement 1; statement 2; statement 3)

{

  // code block to be executed 

}

All Contributions

Example of how to Write a program Print Pascal's triangle :

#include <iostream>
using namespace std;

int main()
{
    int rows, coef = 1;

    cout << "Enter number of rows: ";
    cin >> rows;

    for(int i = 0; i < rows; i++)
    {
        for(int space = 1; space <= rows-i; space++)
            cout <<"  ";

        for(int j = 0; j <= i; j++)
        {
            if (j == 0 || i == 0)
                coef = 1;
            else
                coef = coef*(i-j+1)/j;

            cout << coef << "   ";
        }
        cout << endl;
    }

    return 0;
}

Example of How to Write a program to  Program to print half pyramid using alphabets:

#include <iostream>
using namespace std;

int main()
{
    char input, alphabet = 'A';

    cout << "Enter the uppercase character you want to print in the last row: ";
    cin >> input;

    for(int i = 1; i <= (input-'A'+1); ++i)
    {
        for(int j = 1; j <= i; ++j)
        {
            cout << alphabet << " ";
        }
        ++alphabet;

        cout << endl;
    }
    return 0;
}

 How to Write a program that prints even numbers from 0 to 100:

 

#include
using namespace std;
int main()
{
    for (int i = 0 ; i <= 100; i++)
    {
        if ( i % 2 ==0) 
	{
	    cout << i << endl; 
    }
	return 0 ;
} 

Example of How to Write a program to  Program to print half pyramid using alphabets:

#include <iostream>
using namespace std;

int main()
{
    char input, alphabet = 'A';

    cout << "Enter the uppercase character you want to print in the last row: ";
    cin >> input;

    for(int i = 1; i <= (input-'A'+1); ++i)
    {
        for(int j = 1; j <= i; ++j)
        {
            cout << alphabet << " ";
        }
        ++alphabet;

        cout << endl;
    }
    return 0;
}

Example of How to Write a program to  Program to print half pyramid using alphabets:

#include <iostream>
using namespace std;

int main()
{
    char input, alphabet = 'A';

    cout << "Enter the uppercase character you want to print in the last row: ";
    cin >> input;

    for(int i = 1; i <= (input-'A'+1); ++i)
    {
        for(int j = 1; j <= i; ++j)
        {
            cout << alphabet << " ";
        }
        ++alphabet;

        cout << endl;
    }
    return 0;
}

total contributions (8)

New to examplegeek.com?

Join us