Pattern Printing coding 12

//If you are visiting this post in mobile then, please use desktop site mode //   

CODE NO:- 21
Print the below pattern
                                                                                                                                                   



#LOGIC
Take two variables and assign it with value (n*n)-(n-1) and 0(zero)  here n means the no. of entered rows and (n*n)-(n-1) is the first element of the pattern.
ex: if n is 4 then (n*n)-(n-1) results 13. 
Now copy the value (n*n)-(n-1) in other variable and increment it by 1 in the inner loop whereas in the outer loop decrement it by value n. 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 i,j,a,n,b=0;;
printf("\n enter the no. of rows:");
scanf("%d",&n);
a=(n*n)-(n-1);
for(i=0;i<n;i++)
{
b=a;
for(j=0;j<n;j++)
{
printf("%3d",b++);// %3d is used to display the pattern correctly(spacing)
}// b++ is post increment
a=a-n;
printf("\n");
}
/* %3d is a format specifier where
% means "print the variable here"
3 means "use at least 3 spaces to display, padding as needed"
d means integer
putting above sentences together we can say that:
"print an integer, taking minimum 3 spaces" */
return 0;
}
We got this output by using DEV C++ (IDE)
        

Comments

Popular posts from this blog

WHY PROGRAMMING?

Series code 2

Pattern Printing coding 7