How to Print The Given Pattern
What will be the solution of the given pattern in c programming language?
For this programming there are many type of programming solution. But I will discus only two of them one the best solution and one the common solution which you found on Internet.
1st type of solution and it's logic.
Programming:-
#include<stdio.h>
#include<conio.h>
int main()
{
int row,col; //variable deceleration.
for(row=1;row<=5;row++) // For row
{
for(col=1;col<=row;col++) //For column
{
printf(" %d",col); //Printing Number pattern
}
printf("\n"); //For new line
}
getch();
return 0;
}
Output:-
Logic:-
In this programming, you will see there are two for loop I have use. One is for row and 2nd is for column. In 2nd loop you will see the condition as col<=row means loop will execute till when column is smaller then or equal to row If you will see in Fig 3. Then you will see that all time column is smaller or equal to row so I have use it.
2nd type of solution and it's logic.
Programming:-
#include<stdio.h>
#include<conio.h>
int main()
{
int row,col; //Variable deceleration
for(row=1;row<=5;row++) // For rows
{
for(col=1;col<=5;col++) //For columns
{
if(col<=row) //Condition for printing pattern
{
printf(" %d",col); //Printing number
}
else //For blank space
{
printf(" ");
}
}
printf("\n"); //For new line
}
getch();
return 0;
}
Output:-
Fig:-4
Logic:-
In this logic is same as before but the condition has been changed. In this I have make a box of 5 row & 5 column in fig:-5. In this both loop is executed when row and col both are smaller or equal to 5. And after loop execution there is a If - else statement in which if contain the condition col <= row and it will execute till column is smaller then or equal to row and print the column for the pattern. After that when row is greater then column then else will execute and print the blank space as given in fig:-5.
There are many ways to solve a single programming problem,you may use any. But these two are popular in programming and good way of programming. But 2nd way is not the best way of programming. For beginner it is good but for intermediate and expert it is not good. So always go for the 1st way.
If you face any problem then comment in comment section of blog and you may also visit my YouTube Channel to understand it. It is in Hindi language.
Thank you bro for uploading such things 💐✨😍
ReplyDeleteLots of brst wishes