Results 1 to 4 of 4

Thread: C++ Question

  1. #1
    Join Date
    Jun 2003
    Posts
    186

    C++ Question

    Hello,

    I need a little help . I want a program whose output is :

    Enter a positive integer : 5

    1
    22
    333
    4444
    55555
    I know its gotta be done using for loops but can anyone help?

    I have made so far:

    Code:
    #include<iostream.h>
    #include<conio.h>
    void main(void)
    {
    int x;
    cout<<"Enter a positive integer ?";
    cin>>x;
    for ( int i=1; i<=x; i++ )
    {
    cout<<i<<endl;
    }
    getch();
    }

  2. #2
    Join Date
    Nov 2004
    Location
    HK
    Posts
    309
    you need to write a nested for loop
    which means a loop inside another loop
    Code:
    for(){
       for(){
       }
    }
    Rails in DA - Ruby on Rails plugin for Directdmin | DA-Tomcat - Tomcat Manager plugin for Directdmin
    DA-PgSQL - PostgreSQL plugin for Directdmin | IP Deny Manager - IP Deny Manager plugin for Directdmin
    DeeperAdmin - Manager your DirectAdmin server "deeper"
    Order now at http://www.daplugin.com

  3. #3
    Join Date
    Sep 2002
    Location
    Nashville, TN
    Posts
    237
    #include<iostream.h>
    #include<conio.h>
    void main(void)
    {
    int x;
    cout<<"Enter a positive integer ?";
    cin>>x;
    for ( int i=1; i<=x; i++ )
    {
    for(int ii = 1; ii < i; ii++)
    {
    cout<<i<<endl;
    }
    }
    getch();
    }

  4. #4
    Join Date
    Jun 2003
    Posts
    186
    Thanks Chris . It worked , Just had to make a little modification to your code:

    #include<iostream.h>
    #include<conio.h>
    void main(void)
    {
    int x;
    cout<<"Enter a positive integer ?";
    cin>>x;
    for ( int i=1; i<=x; i++ )
    {
    cout<<endl;
    for(int ii = 1; ii <= i; ii++)
    {
    cout<<i;
    }
    }
    getch();
    }


    ----


    added cout endl in first loop to make it go to next line , and in second loop made it ii<= instead of ii< as it missed 1 otherwise .

    Thanks for your help

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •