Click here to Skip to main content
15,443,631 members
Please Sign up or sign in to vote.
1.00/5 (4 votes)
See more:
hello,

i want to print string pyramid as : it should display first character from entered string in first line, 2 character from entered string in second line, 3 character from entered string in third line and so on.


for e.g. csharp

c
cs
csh
csha
cshar
csharp


can anybody please help me?
Posted
Updated 27-Feb-13 21:59pm
v2

We do not do your homework: it is set for a reason. It is there so that you think about what you have been told, and try to understand it. It is also there so that your tutor can identify areas where you are weak, and focus more attention on remedial action.

Try it yourself, you may find it is not as difficult as you think!

But this really isn't difficult: all you need is two nested loops: one goes through the number of characters and prints a line each time. It contains another loop which prints the number of characters for that line number.

Seriously - try it. It's about four lines of code if you ignore brackets and comments!
 
Share this answer
 
Solution for e.g. csharp

c
cs
csh
csha
cshar
csharp

Below is the code e.g above
post: by kakada
--------------------------------------------------------------------------------------
for (int i = 0; i<=7; i++){

for (int j = 1; j <i;>
{
if (j == 1) { Console.Write("C"); }
if (j == 2) { Console.Write("S"); }
if (j == 3) { Console.Write("h"); }
if (j == 4) { Console.Write("a"); }
if (j == 5) { Console.Write("r"); }
if (j == 6) { Console.Write("p"); }
}

Console.WriteLine();

}
 
Share this answer
 
v2
Comments
Dave Kreskowiak 2-Jul-15 9:45am     CRLF
Do NOT post answers to homework questions! You are only helping the person fail. Oh, and your solution is a bad one.
Matt T Heffron 2-Jul-15 13:05pm    
Dave ... on a 2 year old question? ;-)
Dave Kreskowiak 2-Jul-15 15:30pm    
Yeah, I keep missing the stupid date it was posted. F'in spammers are resurrecting old questions to post their sh*t.
So easy just copy and paste this code...
C#
using System;
namespace PiramidPrint
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.Write("Enter any string: ");
            string input = Console.ReadLine();
            for (int i = 0; i < input.Length; i++)
            {
                for (int j = 0; j < i + 1; j++)
                {
                    Console.Write(input[j]);
                }
                Console.WriteLine();
            }
            Console.ReadKey();

        }
    }
}
 
Share this answer
 
Comments
Dave Kreskowiak 2-Jul-15 9:45am    
Do NOT post answers to homework questions! You are only helping the person fail.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900