jeudi 17 septembre 2020

Diamond pattern using while loop in c++

I am trying to print a diamond pattern using while loop in c++ that is based on the diamond's length however i'm not getting the output i wanted to see. what seems to be the problem with my code?

#include <iostream>
using namespace std;

int main ()
{
    cout << "Length of diamond:";
    int length = 0;
    cin >> length; 
    length = length/2;
    int r = -length;
    while (r <= length) {
        r++;
        int c = -length;
        while (c <= length) {
            c++; 
            if (abs (r) + abs (c) == length) cout << ".";
            else cout << " ";
        }
    
        cout << endl;
    }

    return 0;
}

output

Aucun commentaire:

Enregistrer un commentaire