C programs to print various patterns.

Question 1(a): Write a program to print the following pattern:

1
1 2
1 2 3
1 2 3 4
1 2 3 4 5

Answer:

#include<stdio.h>
#include<conio.h>

void main()
{
	int i,j,num_lines;
	clrscr();

	printf("\nEnter the number of lines to print: ");
	scanf("%d", &num_lines);

	for(i=1; i<=num_lines; i++)
	{
		for(j=1; j<=i; j++)
		{
			printf("%d ", j);
		}
		printf("\n");
	}

	getch();
}

Question 1(b): Write a program to print the following pattern:

1
2 2
3 3 3
4 4 4 4
5 5 5 5 5

Answer:

#include<stdio.h>
#include<conio.h>

void main()
{
	int i,j,num_lines;
	clrscr();

	printf("\nEnter the number of lines to print: ");
	scanf("%d", &num_lines);

	for(i=1; i<=num_lines; i++)
	{
		for(j=1; j<=i; j++)
		{
			printf("%d ", i);
		}
		printf("\n");
	}

	getch();
}