Click here to Skip to main content
15,894,405 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
The Output isn't listed in a straight line, my code is below

What I have tried:

#include <stdio.h>
#include <stdlib.h>
int main()
{
    int noofpotato=1;
    int day=1;
    int month=1;
    
        while(month<=12){
         printf("Month:%d\t\t Day:%d\t\t Potato:%d\n",month,day,noofpotato);
         ++month;
         day*=2;
         
     }
 
return 0; 
}
Posted
Updated 8-Dec-17 6:04am

If you need properly aligned numbers (if I got you), then try
C
#include <stdio.h>
#include <stdlib.h>
int main()
{
  int noofpotato=1;
  int day=1;
  int month=1;

  while(month<=12)
  {
    printf("Month:%2d\tDay:%4d\t Potato:%d\n",month,day,noofpotato);
    ++month;
    day*=2;
  }
  return 0;
}
 
Share this answer
 
Comments
shreyas s 9-Dec-17 2:19am    
and if I want the digits to be printed in 'left alignment'?
for eg:
9
10
100
11232
CPallini 9-Dec-17 4:25am    
See here:
https://www.lemoda.net/c/printf-left-justify/
shreyas s 9-Dec-17 6:45am    
Tyvm;
you can do something like right padding as below

C++
printf("Month:%6d\t\t Day:%6d\t\t Potato:%d\n",month,day,noofpotato);


Output:
Month:     1		 Day:     1		 Potato:1
Month:     2		 Day:     2		 Potato:1
Month:     3		 Day:     4		 Potato:1
Month:     4		 Day:     8		 Potato:1
Month:     5		 Day:    16		 Potato:1
Month:     6		 Day:    32		 Potato:1
Month:     7		 Day:    64		 Potato:1
Month:     8		 Day:   128		 Potato:1
Month:     9		 Day:   256		 Potato:1
Month:    10		 Day:   512		 Potato:1
Month:    11		 Day:  1024		 Potato:1
Month:    12		 Day:  2048		 Potato:1
 
Share this answer
 
v2
Comments
shreyas s 9-Dec-17 2:24am    
and if I want the digits to be printed in 'left alignment'?
for eg:
9
10
100
1113
Bryian Tan 9-Dec-17 15:17pm    
Then you can try -6
printf("Month:%-6d\t\t Day:%-6d\t\t Potato:%d\n",month,day,noofpotato);
shreyas s 9-Dec-17 22:38pm    
Thanks, Bryian Tan.
Bryian Tan 10-Dec-17 2:05am    
You're welcome.
shreyas s 9-Dec-17 2:32am    
How to post the output of the code?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900