Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. how to use for loop for displaying no in following format?

how to use for loop for displaying no in following format?

Scheduled Pinned Locked Moved C / C++ / MFC
tutorialquestion
9 Posts 9 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    poonam jagdale
    wrote on last edited by
    #1

    1 2 2 3 3 3 4 4 4 4 5 5 5 5 5 4 4 4 4 3 3 3 2 2 1

    A C S R S 6 Replies Last reply
    0
    • P poonam jagdale

      1 2 2 3 3 3 4 4 4 4 5 5 5 5 5 4 4 4 4 3 3 3 2 2 1

      A Offline
      A Offline
      Adam Roderick J
      wrote on last edited by
      #2

      You should read book on C to get more familiar with the loops that are avail in C. I prefer you to read "Let Us C - Yashwant Kanetkar". Please dont expect full code from here :) well this case i can help, but what about the other cases? So it is better to get familiar with C loops and then make a logic from your side. All the best. :thumbsup:

      Величие не Бога может быть недооценена.

      1 Reply Last reply
      0
      • P poonam jagdale

        1 2 2 3 3 3 4 4 4 4 5 5 5 5 5 4 4 4 4 3 3 3 2 2 1

        C Offline
        C Offline
        CPallini
        wrote on last edited by
        #3

        You need an outer loop and a inner one. :)

        If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
        This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
        [My articles]

        1 Reply Last reply
        0
        • P poonam jagdale

          1 2 2 3 3 3 4 4 4 4 5 5 5 5 5 4 4 4 4 3 3 3 2 2 1

          S Offline
          S Offline
          Stephen Hewitt
          wrote on last edited by
          #4

          Try this:

          #include <iostream>
          using namespace std;
           
          void main()
          {
          const char *p[] = {"1", "2 2", "3 3 3", "4 4 4 4",
          "5 5 5 5 5", "4 4 4 4", "3 3 3",
          "2 2", "1"};
           
          for (int i=0; i<sizeof(p)/sizeof(*p); ++i)
          {
          cout << p[i] << endl;
          }
          }

          Steve

          N 1 Reply Last reply
          0
          • P poonam jagdale

            1 2 2 3 3 3 4 4 4 4 5 5 5 5 5 4 4 4 4 3 3 3 2 2 1

            R Offline
            R Offline
            Rozis
            wrote on last edited by
            #5

            hmmm, this is a difficult one... Can you see any pattern in it?

            T 1 Reply Last reply
            0
            • R Rozis

              hmmm, this is a difficult one... Can you see any pattern in it?

              T Offline
              T Offline
              Tim Craig
              wrote on last edited by
              #6

              Rozis wrote:

              Can you see any pattern in it?

              The laser pointer and cat design pattern? :laugh:

              You measure democracy by the freedom it gives its dissidents, not the freedom it gives its assimilated conformists.

              1 Reply Last reply
              0
              • P poonam jagdale

                1 2 2 3 3 3 4 4 4 4 5 5 5 5 5 4 4 4 4 3 3 3 2 2 1

                S Offline
                S Offline
                sthalasayanam
                wrote on last edited by
                #7

                It is very simple. The following is the generic code algorithm. int range = ; int outer,inner; //The following loop is for displaying pattern from 1 to n ( any value) for( outer loop... ) { for( inner loop... ) } //The following loop is for displaying pattern from n-1 to 1 range -= 1; for( outer loop... ) { for( inner loop.... ) } Understand the basic code flow and implement your code.

                1 Reply Last reply
                0
                • P poonam jagdale

                  1 2 2 3 3 3 4 4 4 4 5 5 5 5 5 4 4 4 4 3 3 3 2 2 1

                  J Offline
                  J Offline
                  Jonas Hammarberg
                  wrote on last edited by
                  #8

                  #include <iostream>
                  using namespace std;

                  void main(){
                  const static int threshold(5);
                  for(int i(1), d(+1); i; i += d, d = (i == threshold)? -1 : d){
                  for(int j(0), end(i); j != end; ++j) {
                  cout << i << " ";
                  }
                  cout << endl;
                  }
                  }

                  Getting rid of the ending space are trivial and left as an exercise :rolleyes: .

                  1 Reply Last reply
                  0
                  • S Stephen Hewitt

                    Try this:

                    #include <iostream>
                    using namespace std;
                     
                    void main()
                    {
                    const char *p[] = {"1", "2 2", "3 3 3", "4 4 4 4",
                    "5 5 5 5 5", "4 4 4 4", "3 3 3",
                    "2 2", "1"};
                     
                    for (int i=0; i<sizeof(p)/sizeof(*p); ++i)
                    {
                    cout << p[i] << endl;
                    }
                    }

                    Steve

                    N Offline
                    N Offline
                    normanS
                    wrote on last edited by
                    #9

                    You've got my 5, for the best non-answer of the day. I don't know why others gave you a "1" rating, I guess they didn't read what you were suggesting!

                    1 Reply Last reply
                    0
                    Reply
                    • Reply as topic
                    Log in to reply
                    • Oldest to Newest
                    • Newest to Oldest
                    • Most Votes


                    • Login

                    • Don't have an account? Register

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • World
                    • Users
                    • Groups