settingsAccountsettings
By using our mini forum, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy
Menusettings

Q: Print a Deck of 52 Cards - C# task

+5 votes

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

asked in C# category by user nikole
edited by user golearnweb

2 Answers

+2 votes
 
Best answer

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();
        }
    }
}
answered by user hues
edited by user golearnweb
+1 vote

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

https://www.youtube.com/watch?v=pd9vtszpGZg

answered by user eiorgert
edited by user golearnweb
...