C Programming

PRACTISE EXERCISE 4

for loops

1. Write a for loop to print out the values 1 to 10 on separate lines.


	for( loop = 1; loop <= 10; loop = loop + 1 )
		printf("%d\n", loop) ;

2. Write a for loop which will produce the following output (hint: use two nested for loops)

	1
	22
	333
	4444
	55555

	for( loop = 1; loop <= 5; loop = loop + 1 )
	{
		for( count = 1; count <= loop; count  = count + 1 )
			printf("%d", loop );
		printf("\n");
	}

3. Write a for loop which sums all values between 10 and 100 into a variable called total. Assume that total has NOT been initialized to zero.


	for( loop = 10, total = 0; loop <= 100; loop = loop + 1 )
		total = total + loop;

4. Write a for loop to print out the character set from A-Z.


	for( ch = 'A'; ch <= 'Z'; ch = ch + 1 )
		printf("%c", ch );
	printf("\n");


©Copyright B Brown. 1984-1999. All rights reserved.