Pattern Printing coding 10
CODE NO:- 12
Print the below pattern
#LOGIC
#Program( In C):
In C language we use ' // ' for Single line comment and /*---*/ for multi line comment
#include<stdio.h>// header file for i/p and o/p
int main()
{
int n,i,k,a,b=0;
printf("\n enter the no.of rows:");
scanf("%d",&n);
a=(((n*(n+1))/2)+2)-(n-1);
for(i=0;i<n;i++)
{
b=a;
for(k=0;k<n-i;k++)
{
printf("%d ",b++); // incrementing the value column wise
}
a=a-(n-i-1); // decrementing the value row wise
printf("\n");
}
return 0;
}
We got this output by using DEV C++ (IDE)
Link: For other pattern printing problems
Print the below pattern
#LOGIC
Take two variables and assign it with value (((n*(n+1))/2)+2)-(n-1) and 0(zero) here n means the no. of entered rows and (((n*(n+1))/2)+2)-(n-1) is the first element of the pattern.
ex: if n is 4 then (((n*(n+1))/2)+2)-(n-1) results 9.
Now copy the value (((n*(n+1))/2)+2)-(n-1) in other variable and increment it by 1 in the inner loop whereas in the outer loop decrement it by value (n-i-1). Proceed in the same manner to print the pattern.
#Program( In C):
In C language we use ' // ' for Single line comment and /*---*/ for multi line comment
#include<stdio.h>// header file for i/p and o/p
int main()
{
int n,i,k,a,b=0;
printf("\n enter the no.of rows:");
scanf("%d",&n);
a=(((n*(n+1))/2)+2)-(n-1);
for(i=0;i<n;i++)
{
b=a;
for(k=0;k<n-i;k++)
{
printf("%d ",b++); // incrementing the value column wise
}
a=a-(n-i-1); // decrementing the value row wise
printf("\n");
}
return 0;
}
We got this output by using DEV C++ (IDE)
Link: For other pattern printing problems
👍
ReplyDelete