program to print triangle

guys plz help me to write this progam

123454321
1234 4321
123 321
12 21

the numbers must be printed in this form.😕

Replies

  • disaster
    disaster
    sorry the image is wrong

    123454321
    1234..4321
    123.....321
    12........21
    1...........1
  • silverscorpion
    silverscorpion
    I'll just give you the algorithm for now. try coding it yourself.. if you're not able to do it, let's see.
    It goes like this:

    Initialise 2 counters, let's say i and j.

    Use one for printing the numbers in a single line, and the other for the number of lines.

    Take care of the spaces in the lines. A simple thing to note is, if there are 'n' lines, then the k-th line has numbers from 1 to k-1, then 2*(k-1)-1 spaces, then numbers from k-1 to 1.

    this same algo should work for any pattern, that is, even if the first line is 1234567654321, you can do it by this algo. Just try it.

    Hope I'm clear..
  • safwan
    safwan
    bROTHER , I ALSO WANT ALMOST THIS KIND OF PROGRAMM BUT I NEED A PYRAMID TO BE PRINTED SAME AS HERE HOW TO KEEP SPACE LOOK MY ONE PROJECT.

    ----*
    ---***
    --*****
    BUT MY ONE IS
    *
    **
    ***
    ****
    SO PROBLEM HERE IS SAME
    OF YOUR AND MINE CHECKE OUT MY CODE IN MY THERED PRINTING PYRAMID IN VB.
  • disaster
    disaster
    silverscorpion
    I'll just give you the algorithm for now. try coding it yourself.. if you're not able to do it, let's see.
    It goes like this:

    Initialise 2 counters, let's say i and j.

    Use one for printing the numbers in a single line, and the other for the number of lines.

    Take care of the spaces in the lines. A simple thing to note is, if there are 'n' lines, then the k-th line has numbers from 1 to k-1, then 2*(k-1)-1 spaces, then numbers from k-1 to 1.

    this same algo should work for any pattern, that is, even if the first line is 1234567654321, you can do it by this algo. Just try it.

    Hope I'm clear..



    can u explain it more plzzz
    i am unable to get u.
  • shalini_goel14
    shalini_goel14
    Hi disaster

    Have a look at following thread, if it can be of any help to you.
    #-Link-Snipped-#

    [Note: Just replace "*" with the counter value you want to print. It is not exactly what you want but logic is almost same.]

    You can try once. If an issues let me know.
  • cinu97
    cinu97
    disaster
    guys plz help me to write this progam

    123454321
    1234 4321
    123 321
    12 21

    the numbers must be printed in this form.😕

    /* #-Link-Snipped-# */
    #include
    int main()
    {
    int i,j,n;
    printf("Enter no");
    scanf("%d",&n);
    for(i=0;i {
    for(j=1;j<=n-i;j++)
    printf("%d",j);
    for(j=0;j<((i*2)-1);j++)
    printf("*");
    for(j=n-i;j>0;j--)
    {
    if(j==n)
    {
    }
    else
    printf("%d",j);
    }
    printf("\n");
    }}
  • PraveenKumar Purushothaman
    PraveenKumar Purushothaman
    Guys, please refrain from posting EMail addresses in the public forums.

    Back to your question, it can be easily achieved by using two counters with a master counter, which counts less. And the same above code with some optimization.

    #include 
    int main()
    {
        
    int ijn;
        
    printf("Enter the number of Lines: ");
        
    scanf("%d", &n);
        for(
    i=0i<ni++)
        {
            for(
    j=1j<=n-ij++)
              
    printf("%d"j);
            for(
    j=0j<((i*2)-1); j++)
              
    printf("*");
            for(
    j=n-ij>0j--)
              if(
    j!=n)
                
    printf("%d"j);
            
    printf("\n");
        }
    }
  • andremartin
    andremartin
    Use one for printing the numbers in a single line, and the other for the number of lines.
    #-Link-Snipped-#

You are reading an archived discussion.

Related Posts

Hi all, Answer this now.. I have a weighing scale and there is an empty plastic bottle on the scale. A certain reading is shown in the scale. Now, a...
Hi all, I'm thinking we can share our favorite firefox extenisons. Firefox is my most favorite browser, and what makes it special are the extensions. share the ones you like...
from past few days ,iam facing an issie with the outlook express 6.0 everytime time I start OE,the password stored in it vanishes and i have to reconfigure OE each...
I found a nice article on the following link. Go through it - squawkfox » 6 Words That Make Your Resume Suck
Hello all, I have a portable media player based on 32bits MIPS processor,which has USB interfaces. I want to integrate a Blue Tooth Module to this system through its USB...