ProAnswers.org

C program to print following number pattern?

use only 2or3 variable i.e. i,j,k in for loop

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

1 Like

home work questions ? :slight_smile:

I hope this will help you…

#include
int main()
{
int i, j, k, c = 5;
for (i = 1; i <= 5; i++)
{
/* k is taken for spaces /
for (k = 1; k <= c; k++)
{
/
blank space /
printf(" ");
}
for (j = 1; j <= i; j++)
{
/
%2d ensures that the number is printed in two spaces for alignment and the numbers are printed in the order. */
printf("%2d", i);
}
printf("
");
/*c is decremented by 1 */
c–;
}
return 0;
}

#include
void main()
{
int i,j,k,n;
printf(“enter n”);
scanf("%d",&n);
for(i=1;i<=n;i++)
{
for(k=1;k<=n-i;k++)
printf("");
for(j=1;j<=i;j++)
printf("%2d",i);
printf("
");
}
}
output:
enter n:5
1
22
333
4444
55555