How to Create Inverted Triangle, Triangle, Diamond, Number Patterns Using For Loop Source Code (C++ and Pascal Programming)
Inverted Triangle Source Code in C++
#include<iostream>
using namespace std;
int main(){
int limit;
char chara[5];
cout<<"-------------------------\n";
cout<<" Input the Limit = ";cin>>limit;
cout<<" Input a character = ";cin>>chara;
cout<<"\n"; for (int i=limit;i>=1;i--){
for (int j=limit;j>=i;j--){
cout<<" ";
}
for (int k=1;k<=i;k++){
cout<<" "<<chara;
}
cout<<"\n";
}
return 0;
}
Nested For Looping Example in C++ (How to make a Triangle)
#include <iostream>
using namespace std;
int main(){
int limit;
char chara[5];
cout<<"-------------------------\n";
cout<<" Input the Limit = ";cin>>limit;
cout<<" Input characters = ";cin>>chara;
cout<<"\n";
for (int i=1;i<=limit;i++){
for (int j=limit;j>=i;j--){
cout<<" ";
}
for (int k=1;k<=i;k++){
cout<<" "<<chara;
}
cout<<"\n";
}
return 0;
}
Diamond Shape in Pascal
Here is the source code.
program diamondo;
uses crt;
var b,i,j,k,l,m,n:integer; a:char;
begin
clrscr;
write('Input Limit: ');readln(b);
write('Input Chara: ');readln(a);
writeln;
for i:= 1 to b do
begin
for j:= b downto i do
begin
write(' ');
end;
for k:= 1 to i do
begin
write(' ',a);
end;
writeln;
end;
for l:= b-1 downto 1 do
begin
for m:= b downto l do
begin
write(' ');
end;
for n:= 1 to l do
begin
write(' ',a);
end;
writeln;
end;
readln;
end.
Triangle Number Pattern in Pascal (Nested For)
program logicalgorithm5;
uses crt;
var
i,a,b,n,sp,c:integer;
begin
clrscr;
write('Input n value: ');readln(n);
a:=n;
c:=1;
a:=a-1;
for i:= 1 to n do
begin
for sp:= 1 to a do
begin
write(' ');
end;
for b:= 1 to c do
begin
write(' ',i);
end;
writeln;
a:=a-1;
c:=c+2;
end;
readln;
end.
Here is another source code for number pattern in pascal programming language.
program logicalgorithm3;
uses crt;
var
i,z,a,n,u: integer;
begin
clrscr;
writeln;
write(' Input Limit: ');readln(n);
writeln;
for i:= 1 to n do
begin
for a:= 1 to i do
begin
write(' ',i);
end;
writeln;
end;
for z := n-1 downto 1 do
begin
for u:= z downto 1 do
begin
write(' ',z);
end;
writeln;
end;
readln;
end.
Numbers Pattern (Nested For) in Pascal
Here is the source code for the pattern.
program numbers;
uses crt;
var a,i,j,k: integer;
begin
clrscr;
write('Input Limit : ');readln(a);
writeln;
for i:= 1 to a do
begin
for j:= a downto i do
begin
write(' ',j);
end;
for k:= 1-1 to i do
begin
write(' ',k);
end;
writeln;
end;
readln;
end.
Combinations of Numbers and Character (Nested For) in Pascal
Another example of nested for in pascal programming language.
program kotak;
uses crt;
var a,i,j,k: integer; n:char;
begin
clrscr;
write('Input Batas : ');readln(a);
write('Input Karakter: ');readln(n);
writeln;
for i:= 1 to a do
begin
for j:= a downto i do
begin
write(' ',j);
end;
for k:= 1 to i do
begin
write(' ',n);
end;
writeln;
end;
readln;
end.
Another Example of Nested For in Pascal
This source code is an example the use of nested for in pascal programming language by me.
program nestedforex;
uses crt;
var baru,b,i,j,k,l,m,n:integer; a:char;
begin
clrscr;
write('Input Limit: ');readln(b);
write('Input Chara: ');readln(a);
writeln;
for i:= 1 to b do
begin
for j:= b downto i do
begin
for baru:= 1 to j-5 do
begin
write(' ');
end;
end;
for k:= 1 to i do
begin
write(' ',a);
end;
writeln;
end;
for l:= b-1 downto 1 do
begin
for m:= b downto l do
begin
for baru:= m-5 downto 1 do
begin
write(' ');
end;
end;
for n:= 1 to l do
begin
write(' ',a);
end;
writeln;
end;
readln;
end.