Like Tree6Likes

Printing Numbers...

This is a discussion on Printing Numbers... within the C# Programming forums, part of the General Programming Boards category; Hey frndz...cn any1 giv me code for dis o/p : ----1 ---121 -12312 1234123 -12312 ---121 ----1 i tried alot ...

  1. #1
    Registered User anny's Avatar
    Join Date
    Aug 2012
    Posts
    13

    Question Printing Numbers...

    Hey frndz...cn any1 giv me code for dis o/p :
    ----1
    ---121
    -12312
    1234123
    -12312
    ---121
    ----1
    i tried alot bt i didn't gt dis o/p

  2. #2
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,678
    Could you please post one of your trials?(the one you think it's best)

  3. #3
    Registered User anny's Avatar
    Join Date
    Aug 2012
    Posts
    13
    bt m tring it in visual studio s/w....i send u code frm tht...u ok wid it na?

  4. #4
    Registered User anny's Avatar
    Join Date
    Aug 2012
    Posts
    13
    also till dis m jst tring to print upper triangle only....

  5. #5
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,678
    Ok,try to write one post at the time
    Copy the code you have in VS and paste it like this
    [key]HERE YOU PASTE YOUR CODE[/key]

    in order this to work replace key with code
    anny likes this.

  6. #6
    Registered User anny's Avatar
    Join Date
    Aug 2012
    Posts
    13
    Code:
    class Program    {
            static void Main(string[] args)
            {
                int p = 1;
                int sp = 3;
                int v = 1;
    
    
                for (int i = 1; i <= 4; i++)
                {
                    int c = v;
                    for (int j = 1; j <= sp; j++)
                        Console.Write(" ");
                    for (int k = 1; k <= p; k++)
                    {
                        Console.Write(c);
    
    
                        if (k <= (p / 2))
                        {
                            c++;                       
                        }
                        else
                        {
                            c--;
                        }
                    }
                    Console.WriteLine();
                    p = p + 2;
                    sp = sp - 1; 
    
    
                  
                    
                }
                Console.ReadLine();
            }
        }

  7. #7
    Registered User anny's Avatar
    Join Date
    Aug 2012
    Posts
    13
    dis code will giv o/p lik dis:
    ----1

    ---121
    -12321
    1234321
    bt i wnt o/p lik dis:
    ----1
    ---121
    -12312
    1234123

  8. #8
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,678
    If i were you i would use a flag.Below i will show you how i use it.The problem was that c should not be decremented but set again to 1.
    Code:
    class Program    {
            static void Main(string[] args)
            {
                int p = 1;
                int sp = 3;
                int v = 1;
                bool flag;
     
                for (int i = 1; i <= 4; i++)
                {
                    int c = v;
                    for (int j = 1; j <= sp; j++)
                        Console.Write(" ");
                    flag = true;
                    for (int k = 1; k <= p; k++)
                    {
                        Console.Write(c);
                         c++;
                         if(k>(p/2) && (flag)){
                               c=1;
                               flag=false;
                        }
                    }
                    Console.WriteLine();
                    p = p + 2;
                    sp = sp - 1;
     
     
                   
                     
                }
                Console.ReadLine();
            }
        }
    The flag was used because we want c to set again to 1 only once.The increment of c by one,is now done without any condition because we want it to be executed at every loop.If you have a question ask !
    anny likes this.

  9. #9
    Registered User anny's Avatar
    Join Date
    Aug 2012
    Posts
    13
    oh thnx....it shows correct o/p... now how to print remaining part?

  10. #10
    Registered User anny's Avatar
    Join Date
    Aug 2012
    Posts
    13
    plz tell me hw to print whole diamond....?

  11. #11
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,678
    At first you need set line 9 to execute the loop 8 times,not 4
    Then we have to think that all we have to do is follow the reverse way that brought us up to here(this way was found by you so bravo!!).So we should focus on lines 26-27.We have to do the opposite thing now.But when should we reverse our method?When sp becomes zero,because this means that the big 1234321 o/p has been produced.So
    Code:
                    p = p + 2;
                    sp = sp - 1;
    
    //the reverse
            p-=2;// p = p-2;
           sp++;
    But i would suggest to use another bool variable in order to use it again as a flag to inform us wich method should we follow.The reverse or the other one?
    So what i am saying is this
    Code:
    class Program    {
            static void Main(string[] args)
            {
                int p = 1;
                int sp = 3;
                int v = 1;
                bool flag;
                bool mirror=false;
      
                for (int i = 1; i <= 4; i++)
                {
                    int c = v;
                    for (int j = 1; j <= sp; j++)
                        Console.Write(" ");
                    flag = true;
                    for (int k = 1; k <= p; k++)
                    {
                        Console.Write(c);
                         c++;
                         if(k>(p/2) && (flag)){
                               c=1;
                               flag=false;
                        }
                    }
                    Console.WriteLine();
                 if(sp==0)
                mirror=true;
            if(mirror)
            {
                p-=2;
                sp++;
            }
            else
            {
                p = p + 2;
                sp = sp - 1;
            }
      
      
                    
                      
                }
                Console.ReadLine();
            }
        }

  12. #12
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,678
    Quote Originally Posted by anny View Post
    plz tell me hw to print whole diamond....?
    I was thinking and typing.You should be patient anny,because i do not have all the answers ready at once
    anny likes this.

  13. #13
    Registered User anny's Avatar
    Join Date
    Aug 2012
    Posts
    13
    OMG..... gr888888.....thnxxxx alot....finally i got d solution for dis... & bt offcures bcz of YOU only!!!!! thnx once again....

  14. #14
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,678
    The important thing is that you understand why this works

    PS -> welcome to the forum
    anny likes this.

  15. #15
    Registered User anny's Avatar
    Join Date
    Aug 2012
    Posts
    13
    m solliiiiiiii i jst thought ki u didn't saw my msg!!!!

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Printing perfect numbers
    By DRBILOUKOS in forum C Programming
    Replies: 12
    Last Post: 12-07-2010, 12:34 AM
  2. printing out the unique numbers
    By epidemic in forum C++ Programming
    Replies: 3
    Last Post: 01-14-2007, 11:08 AM
  3. Printing numbers in hex format
    By miclus in forum C++ Programming
    Replies: 7
    Last Post: 01-29-2005, 06:04 AM
  4. printing only prime numbers
    By Micko in forum C Programming
    Replies: 30
    Last Post: 06-10-2004, 10:23 PM
  5. Printing float numbers
    By gustavosserra in forum C++ Programming
    Replies: 4
    Last Post: 10-17-2003, 08:14 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21