in Education by
I have written the following program in C# to display an ASCII pyramid. public static void method1() { int k; int kCond = 5; int i; int inc = 0; for (int p = 0; p <=5; p++) { for(k = 0; k <= kCond; k++) { Console.Write(" "); } kCond--; for(i=0; i<=inc; i++) { Console.Write("*") } inc +=2; Console.WriteLine(); } } Output: * *** ***** ******* ********* *********** My question is, is this good code from a performance point of view, or not? How could I improve it? JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
I took your code and started condensing it, and came up with the following improvements that could be made: Change the inner loops to use the string constructor that takes a character and a count instead of using a loop Give the method and variables more meaningful names Add a size parameter that can be passed to the method Put declarations and incrementing of variables in the for statement Use math operations on the counter rather than tracking multiple variables that increment at constant rates relative to the counter Use Enumerable.Range as a looping mechanism to select the strings along with string.Concat to join them together Have the method return a string so it can be written by the client to other things (like a log file or another control) Code: public static string GetPyramid(int size) { return string.Concat(Enumerable.Range(0, size).Select(count => new string(' ', size - count) + new string('*', count * 2 + 1) + Environment.NewLine)); }

Related questions

0 votes
    In your choice of language, write a program that prints the numbers ranging from one to 50....
asked Apr 26, 2021 in Technology by JackTerrance
0 votes
    How to write a program in JAVA to print the sum of ASCII values of a particular word input ? Select the correct answer from above options...
asked Dec 28, 2021 in Education by JackTerrance
0 votes
    Write a function that interchange and prints the value of two integers A and B without using any extra variable in php. Select the correct answer from above options...
asked Dec 23, 2021 in Education by JackTerrance
0 votes
    __________ prints out the function call stack after an error occurs. (a) trace() (b) traceback() (c) ... Debugging of R Programming Select the correct answer from above options...
asked Feb 12, 2022 in Education by JackTerrance
0 votes
    Which of the following ecological food chains does not represent an erect pyramid of numbers? (a) ... ,Science proposed by,electromagnetic theory engineering physics,Science nptel...
asked Nov 8, 2021 in Education by JackTerrance
0 votes
    An intricate network of food chains is called (a) Biosphere (b) Food web (c) Energy pyramid ... Science,Science proposed by,electromagnetic theory engineering physics,Science nptel...
asked Nov 7, 2021 in Education by JackTerrance
0 votes
    The pattern of energy exchange in an ecosystem is called a (a) Food chain (b) Food web (c) ... Science,Science proposed by,electromagnetic theory engineering physics,Science nptel...
asked Nov 7, 2021 in Education by JackTerrance
0 votes
    How to get prints of shared object memory maps or heap memory maps for a given process? (a) jmap ... programming questions and answers pdf, java interview questions for beginners...
asked Oct 26, 2021 in Education by JackTerrance
0 votes
    The SQL statement SELECT SUBSTR( 123456789', INSTR( abcabcabc','b'), 4) FROM EMP; prints (a) 6789 ... Answers, Database Interview Questions and Answers for Freshers and Experience...
asked Oct 11, 2021 in Education by JackTerrance
0 votes
    Given the following function UART_write(UART_Handle handle, const void *buffer, size_t size); I want to send ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 1, 2022 in Education by JackTerrance
0 votes
    A memory byte location contains the pattern 00101100. What does this pattern represent when interpreted as a binary ... ASCII code? Select the correct answer from above options...
asked Nov 29, 2021 in Education by JackTerrance
0 votes
    I have a List list=Arrays.asList(640,480,520,170,320,140,60); And I need to find sum for given elements ... 60); List newListWithSum=new ArrayList(); for(int mainIndex=0;mainIndex...
asked Apr 16, 2022 in Education by JackTerrance
0 votes
    I need to create a hollow pyramid with a solid top layer like so: Height: 5 * *** * * * ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 5, 2022 in Education by JackTerrance
0 votes
    First proposed the concept of Ecological Pyramid in 1927. (a) Darwin (b) Newton (c) Elton ( ... Science,Science proposed by,electromagnetic theory engineering physics,Science nptel...
asked Nov 7, 2021 in Education by JackTerrance
0 votes
    Ecological Pyramid is called (a) Hills (b) Mounts (c) Eltonian (d) Darwinism Select the correct ... ,Science proposed by,electromagnetic theory engineering physics,Science nptel...
asked Nov 7, 2021 in Education by JackTerrance
...