[Solved] Print a Deck of 52 Cards - C# task

  

5
Topic starter

Write a program that generates and prints all possible cards from a standard deck of 52 cards:  https://en.wikipedia.org/wiki/Standard_52-card_deck  (without the jokers). The cards should be printed using the classical notation (like 5♠, A♥, 9♣ and K♦). The card faces should start from 2 to A. Print each card face in its four possible suits: clubs, diamonds, hearts and spades.

Use 2 nested for-loops and a switch-case statement.

print a deck of 52 cards

2 Answers
4

Interesting task; Here is my solution and the output screenshot:

print a deck of 52 cards in C#

using System;
 
class PrintADeckOf52Cards
{
    static void Main()
    {
        for (int i = 2; i <= 14; i++)
        {
            for (int j = 5; j < 7; j--)
            {
                if (i < 11)
                {
                    Console.Write("{0}{1} ", i, (char)j);
                }
                switch (i)
                {
                    case 11: Console.Write("J{0} ", (char)j);
                        break;
                    case 12: Console.Write("Q{0} ", (char)j);
                        break;
                    case 13: Console.Write("K{0} ", (char)j);
                        break;
                    case 14: Console.Write("A{0} ", (char)j);
                        break;
                }
                if (j == 3)
                {
                    j = 7;
                }
                if (j == 6)
                {
                    break;
                }
            }
            Console.WriteLine();
        }
    }
}
3

Here's a video with more complex solution (using methods):

Share: