Skip to content

Latest commit

 

History

History
171 lines (154 loc) · 5.88 KB

File metadata and controls

171 lines (154 loc) · 5.88 KB

Problema

27 - Escreve um programa que solicite um número de linhas ao utilizador e imprima um losango com números indicando o número da linha do losango. Por exemplo, se o utilizador inserir o número 4, deve ser impresso o seguinte losango:

   1
  222
 33333
4444444
 33333
  222
   1

Se o utilizador inserir um valor superior a 9 o programa deve terminar com uma mensagem de erro.

Soluções

Solução 1

using System;

namespace _27bases
{
    class Program
    {
        public static void Main()
        {
            // Variables 
            int i, j, k, spc, rows;

            // Ask user to input row number: less than 9
            Console.Write("Input number of rows (less than 9): ");
            // Reads input and converts string to int
            rows = Convert.ToInt32(Console.ReadLine());

            spc = rows;
            Console.WriteLine();

            // When input of rows is less than 9
            if (rows <= 9)
            {
                // Draws top of diamond shape
                for (i = 1; i <= rows; i++)
                {
                    // Draws spaces
                    for (k = spc; k >= 1; k--)
                    {
                        Console.Write(" ");
                    }
                    // Draws the numbers
                    for (j = 1; j <= 2 * i - 1; j++)
                        Console.Write("{0}", i);
                    Console.Write("\n");
                    spc--;
                }
                // Draws bottom of diamond shape
                for (i = rows - 1; i >= 1; i--)
                {
                    // Draws spaces
                    for (k = spc + 2; k >= 1; k--)
                    {
                        Console.Write(" ");
                    }
                    // Draws the numbers
                    for (j = 1; j <= 2 * i - 1; j++)
                        Console.Write("{0}", i);
                    Console.Write("\n");
                    spc++;
                }
                Console.WriteLine();
            }
            // se o input das linhas for maior de 9 dá erro
            else
                Console.WriteLine("Error: That's too many!");
        }
    }
}

Por Diana Nóia

Solução 2

using System;  
public class Exercise31  
{  
    public static void Main()
    {
        // Variables
        bool valid = true;

        // A While Loop to make sure the user has to insert a number below 10, if 
        // this doesn't happen, it will just keep asking until a valid number is
        // inserted.
        while(valid)
        {
            // Variables
            string userInputString;

            // Ask the user for an input
            Console.WriteLine("Please insert a number below 10: ");
            // Saves the user Input into a string variable
            userInputString = Console.ReadLine();
            // Converts the string to int
            int userInput = Convert.ToInt32(userInputString);
            
            // Conditioning the outcome of the user input. If he inserts a valid
            // input (below 10), the diamond structure will be made, if not a
            // warning message will appear asking the user for another number.
            if(userInput <= 9)
            {
                // for loop that builds the upper body of the diamond.
                for(int row = 0; row < userInput; row++)
                {
                    // Here we set the spaces so the upper body is centered in
                    // each row
                    for(int column = 1; column <= userInput - row; column++)
                    Console.Write(" ");
                    // Here we set the number of characters in each row,
                    // depending on the user input starting at 1 and continuing
                    // until it reaches the number of the row times 2 plus 1,
                    // so it has always on two characters than the row above.
                    for(int column = 1; column <= 2 * row + 1; column++)
                        if(userInput == 1)
                            Console.Write(1/userInput + row);
                        else
                            Console.Write(1/userInput + row + 1);
                    Console.WriteLine(); // Makes a new line for each row
                }
                /* This For loop is where we make the lower body of the diamond.
                For this for loop we will do the situation but inverted and 
                with some adjustments. The first row will be the userInput- 1
                and this is because on the upper body of the diamond we
                it already reached the max number of columns according to the
                user input and we don't want it to repeat again. We will also
                make 2 times row minus 1 instead of plus 1 because this time
                the diamond is decreasing instead of increasing.*/
                for(int row = userInput - 1; row >= 1; row--)
                {
                    for(int column = 0; column <= userInput - row; column++)
                    Console.Write(" ");
                    for(int column = 1; column <= 2*row - 1; column++)
                        Console.Write(1/userInput + row);
                    Console.WriteLine();
                }
                // After the operation is complete valid turns false and the 
                // program ends.
                valid = false;
            }
            // If the input was invalid the valid boolean will stay true
            // so the program loop of asking you for an input keeps going.
            else
            {
                Console.WriteLine("\nInvalid Option!\n");
                valid = true;
            }
        }
    }
}  

Por Diogo Henriques, usando esta solução como referência.