Pattern Printing coding 13
//If you are visiting this post in mobile then, please use desktop site mode //
CODE NO:- 22
Print the below pattern
#LOGIC
Here we will require two inner loops, one for printing the spaces and another for printing the pattern (numerical values).
Take a variable and initialize it the value 0(zero) then in the second inner loop keep incrementing it by 1.
As we can see in the second row we need to print first space then numerical value whereas in the first row we don't need to print any spaces.
#Program( In C):
CODE NO:- 22
Print the below pattern
#LOGIC
Here we will require two inner loops, one for printing the spaces and another for printing the pattern (numerical values).
Take a variable and initialize it the value 0(zero) then in the second inner loop keep incrementing it by 1.
As we can see in the second row we need to print first space then numerical value whereas in the first row we don't need to print any spaces.
#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=0,n,k;
printf("\n enter the no. of rows:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
for(j=0;j<i;j++)// for pinting spaces in each row
{
printf(" ");// here we are using three spaces
}
for(k=0;k<(2*n)-1-(2*i);k++)// logic upto how much loop need be iterated
{
printf("%3d",++a);// pre increment
}
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)
Link: For other pattern printing problems
Comments
Post a Comment