Write code to create a checkerboard pattern with the words "black" and "white"

  • برمجة سي بلس بلس
  • برمجة

Write code to create a checkerboard pattern with the words "black" and "white"

Sample Output:

 Display checkerboard pattern with the words 'black' and 'white':      
---------------------------------------------------------------------  
 Input number of rows: 5                                               
black-white-black-white-black                                          
white-black-white-black-white                                          
black-white-black-white-black                                          
white-black-white-black-white                                          
black-white-black-white-black

الأجوبة

#include <iostream>
using namespace std;

int main()
{
    int i, j, rows;
    string b, w, t;
    b = "black";
    w = "white";
    cout << "\n\n Display checkerboard pattern with the words 'black' and 'white':\n";
    cout << "---------------------------------------------------------------------\n";
    cout << " Input number of rows: ";
    cin >> rows;
    for (i = 1; i <= rows; i++) 
    {
        for (j = 1; j <= rows; j++) 
        {
            if (j % 2 != 0) 
            {
                cout << b;
                if (j < rows) 
                {
                    cout << "-";
                }
            }
            else if (j % 2 == 0) 
            {
                cout << w;
                if (j < rows) 
                {
                    cout << "-";
                }
            }
        }
        t = b;
        b = w;
        w = t;
        cout << endl;
    }
}
هل كان المحتوى مفيد؟

تبحث عن مدرس اونلاين؟

محتاج مساعدة باختيار المدرس الافضل؟ تواصل مع فريقنا الان لمساعدتك بتأمين افضل مدرس
ماهو التخصص الذي تبحث عنه؟
اكتب هنا...