8

I am little struggling how to make my output to show like this:

  a
 aa
aaa

My current output shows this instead:

a
aa
aaa

Below are my code:

void displayA(int a){
    for(int i = 0; i < a; i++)
        printf("a");
}

int main(void){
    displayA(1);
    printf("\n");
    displayA(2);
    printf("\n");
    displayA(3);
    printf("\n");
    return 0;
}

Any suggestion? Thanks.

Thanks for the answer. I realized that my coding logic was wrong. Using the suggestion below helped me figure it out. Thanks!

Salman2013
  • 93
  • 1
  • 2
  • 5

6 Answers6

17

You can use printf("%*s", <width>, "a"); to print any text right aligned by variable no. of spaces.

Check here live.

0xF1
  • 6,046
  • 2
  • 27
  • 50
  • Thanks for your posting. Hope this will help me out a little. Just curious if you have another solution that involves for loop? I do not want to use hard-coded. Thanks! Suggestion would be great! – Salman2013 Sep 02 '14 at 15:27
  • 3
    @Salman2013: "*I do not want to use hard-coded.*" You do not want use what? – alk Sep 02 '14 at 16:10
3

Aligning text right is done like that:

printf("%3s", some_text);

3 means that the max length of the line is 3.

Aligning text left is the opposite:

printf("%-3s", some_text);
Dharman
  • 30,962
  • 25
  • 85
  • 135
Kateridzhe
  • 227
  • 3
  • 4
1
void staircase(int n) {
    int counter = n-1;
    for(int i = 1; i <= n; i++){
        for(int m = 0; m < counter; m++){
            printf(" ");
        }
        for(int a = 0; a < i; a++){
            printf("%c", '#');
        }
        counter--;
        printf("\n");
    }
}

If we call the function as staircase(6);

It prints:

     #
    ##
   ###
  ####
 #####
######
Frightera
  • 4,773
  • 2
  • 13
  • 28
0

Here the parameter w determines the character width to align the a's to

void displayA(int a, int w){
  int i;
  for(i = 0; i < w; i++) {
    if( i < w - a ) printf(" ");
    else printf("a");
  }
}
thelaws
  • 7,991
  • 6
  • 35
  • 54
0

This may be helpful.

To print staircase pattern:

     #
    ##
   ###
  ####
 #####
######

Following are the codes:

#include<stdio.h>

void print(int n)
{   int i,j;
    for(i=1;i<=n;i++)
    {     printf("%*s",n-i,"");   //for right alignment by n-1 spaces.
         for(j=0;j<i;j++)
         {
                  printf("%s","#");    
         }   
         printf("\n");  
    }   
}    
int main(void)
{   
    print(6);   
    system("pause"); 
    return 0;   
} 
ravi
  • 1
  • 1
0

I made some small changes to the void displayA() you made:

void displayA(int a)
{
    for(int i = 0; i < 3; i++)
    {
        if (i >= 3 - a)
        {
            printf("a");
        }
        else
        {
            printf(" ");
        }
    }
}

Above written code will work fine.

There are others methods, which could also be used instead of the change I made in the void displayA() your made. You could have simply set the width of the line and right aligned the text to be printed.

However, the entire program you made, along with my modification on the function, will be limited to only three letters.

Dharman
  • 30,962
  • 25
  • 85
  • 135