One Loop Design


Write a code to make this design below using one loop

         *
        **
       ***
      ****
     *****
    ******
   *******
  ********
 *********
**********

Solutions:

#include <stdio.h>

int main ()
{
    char a [10];
    for ( int i = 0; i < 10; i++ ) {
        a [i] = '*';
        a [i + 1] = 0;
        printf ("%10s", a);
        printf ("\n");
    }
    return 0;
}
/* Alternative Solutions : Md. Saidul Islam
Dept : CSE, UIU */
#include <stdio.h>
int main ()
{
    int j = 1;
    for ( int i = 1; i <= 110; i++ ) {
       if ( i % 11 == 0 ) {
            printf ("\n");
            j++;
            continue;
        }
       int k = i % 11;
        if ( k > 10 - j)
            printf ("*");
        else
            printf (" ");
    }
    return 0;
}

Similarly, Given a String, less than 16 character in length.
Produce this design using one loop

Sample Input:
Bangladesh

Sample Output:

B
Ba
Ban
Bang
Bangl
Bangla
Banglad
Banglade
Banglades
Bangladesh

Solutions:

#include <stdio.h>
int main ()
{
    char *a;
    scanf ("%s", a);
    for ( int i = 1; *(a + i - 1) != 0; i++ )
        printf ("%.*s\n", i, a);
    return 0;
}
/*Alternative Solutions : Md. Saidul Islam
Dept : CSE, UIU */
#include <stdio.h>
#include <string.h>

int main ()
{
    char a [50];
    scanf ("%s", a);
    int length = strlen (a);
    char temp;
    for ( int i = 1; i <= length; i++ ) {
        temp = a [i];
        a [i] = 0;
        printf ("%s\n", a);
        a [i] = temp;
    }
    return 0;
}

One thought on “One Loop Design

  1. Aita ki start krso tmra 2jon mila???
    Amito voi pailam tmader ai oti tough problem er solution dekhe……………:p

Comments are closed.