Qn: Write the C++ program logic to print the triangle pattern as follows
  Type-1           Type-2
  1                1 2 3 4 5
  1 2              1 2 3 4
  1 2 3            1 2 3
  1 2 3 4          1 2
  1 2 3 4 5        1
                   
  Type-3           Type-4
  1                5 4 3 2 1
  2 1              4 3 2 1 
  3 2 1            3 2 1 
  4 3 2 1          2 1 
  5 4 3 2 1        1 
-By Admin, Last Update On 10th June,2019 12:25 pm

Type-1 Program

#include<iostream.h>
#include<conio.h>
int main(){
  int n,row,col;
  cout<<"Enter how many rows you want: ";
  cin>>n;
  for(row=1;row<=n;row++){
      for(col=1;col<=row;col++){
       cout<<col;
      }
  cout<<endl;
  }
return 0;
}
Output:
Enter how many rows you want: 6
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
1 2 3 4 5 6
இதில் col value print செய்வதற்கு பதிலாக "*" print செய்தால் கீழ்க்கண்டவாறு output கிடைக்கும்
Enter how many rows you want: 5
*
* *
* * *
* * * *
* * * * *

மேற்கண்ட அதே program logic-ல் row-ன் value 1-லிருந்து துவங்கி n-ல் முடிவதற்கு பதிலாக n-லிருந்து துவங்கி 1-ல் முடியவேண்டும் அதாவது for(int row=n;row>=1;row--). எனில் கீழ்க்கண்டவற்று output கிடைக்கும்.

Type-2 Program

#include<iostream.h>
#include<conio.h>
int main(){
  int n,row,col;
  cout<<"Enter how many rows you want: ";
  cin>>n;
  for(row=n;row>=1;row--){
      for(col=1;col<=row;col++){
       cout<<col;
      }
  cout<<endl;
  }
return 0;
}
Output:
Enter how many rows you want: 6
1 2 3 4 5 6
1 2 3 4 5
1 2 3 4
1 2 3
1 2
1
printing in "*"
Enter how many rows you want: 5
* * * * *
* * * *
* * *
* *
*

மேற்கண்ட Type-1 program logic-ல் col-ன் value 1-லிருந்து துவங்கி row-ல் முடிவதற்கு பதிலாக row-லிருந்து துவங்கி 1-ல் முடியவேண்டும் அதாவது for(int col=row;col>=1;col--). எனில் கீழ்க்கண்டவற்று output கிடைக்கும்.

Type-3 Program

#include<iostream.h>
#include<conio.h>
int main(){
  int n,row,col;
  cout<<"Enter how many rows you want: ";
  cin>>n;
  for(row=1;row<=n;row++){
      for(col=row;col>=1;col--){
        cout<<col;
      }
  cout<<endl;
  }
return 0;
}
Output:
Enter how many rows you want: 6
1 
2 1 
3 2 1 
4 3 2 1 
5 4 3 2 1 
6 5 4 3 2 1

மேற்கண்ட Type-3 program logic-ல் row-ன் value 1-லிருந்து துவங்கி n-ல் முடிவதற்கு பதிலாக n-லிருந்து துவங்கி 1-ல் முடியவேண்டும் அதாவது for(int row=n;row>=1;row--). எனில் கீழ்க்கண்டவற்று output கிடைக்கும்.

Type-4 Program

#include<iostream.h>
#include<conio.h>
int main(){
  int n,row,col;
  cout<<"Enter how many rows you want: ";
  cin>>n;
  for(row=n;row>=1;row--){
    for(col=row;col>=1;col--){
      cout<<col;
    }
  cout<<endl;
  }
return 0;
}
Output:
Enter how many rows you want: 6
6 5 4 3 2 1 
5 4 3 2 1 
4 3 2 1 
3 2 1 
2 1 
1

Pgcomments

Comments