0

I am working on printing a loop statement.

I worked on this to try the concept:

#include "stdafx.h"
#include "stdio.h"

int main()
{
	int times,num;
	for (times=1; times<3; times++)
	{
		for (num=1; num<5; num++)
		{ printf("%d",num); }
		printf("\n");
	}
	return 0;
}

What I need is to print out
012
0123
012
01
01
01
01

but cannot get it to work so that is why I worked on the example above.

Thanks for your help!

2
Contributors
5
Replies
6
Views
9 Years
Discussion Span
Last Post by Ancient Dragon
0

try this: printf("%03d", num); In "%03d", the 3 means minimum of 3 digits, 0 means print 0's if num is less than 3 digits. So num = 1, that will print 001

0

If you want to put 5 spaces between columns, and the digits left-justified in the field, then use the - (minus) to left-justify printf("%-5d%-5d%-5d", 0, 1, 2);

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.