Program 1: The user enters n, and the program outputs a right-angled triangle with
height n.
Example: n = 7
كود:
*
**
***
****
*****
******
*******
كود:
#include<iostream>
using namespace std;
void main()
{
int n;
cout<<"Enter n: ";
cin>>n;
for (int i = 1 ; i <= n ; i++)
{
for(int j = 1 ; j <= i ; j++)
{
cout<<'*';
}
cout<<endl;
}
}
Program 2: The user enters n, and the program outputs a right-angled triangle with
height n and its vertical inverse - mirror image
Written by : mo7amed 3la2 El din (totty)
Example: n = 7
كود:
*
**
***
****
*****
******
*******
******
*****
****
***
**
*
كود:
#include<iostream>
using namespace std;
void main()
{
int n;
cout<<"Enter n: ";
cin>>n;
for (int i = 1 ; i <= n ; i++)
{
for(int j = 1 ; j <= i ; j++)
{
cout<<'*';
}
cout<<endl;
}
for (int k = n-1 ; k >= 1 ; k--)
{
for(int m = 1 ; m <= k ; m++)
{
cout<<'*';
}
cout<<endl;
}
}
Program 3: The user enters n, and the program outputs an isosceles triangle with
height n
Written by : mo7amed 3la2 El din (totty)
isosceles triangle = مثلث ذو ضلعين متساويين
Example: n = 9
كود:
*
***
*****
*******
*********
***********
*************
***************
*****************
كود:
#include<iostream>
using namespace std;
void main()
{
int n;
cout<<"Enter n: ";
cin>>n;
int nSpaces = n-1;
int nStars = 1;
for(int i=1; i<=n ; i++)
{
for(int j=1; j<=nSpaces ; j++)
{
cout<<' ';
}
for(int k=1; k<=nStars ; k++)
{
cout<<'*';
}
cout<<endl;
nSpaces = nSpaces - 1;
nStars = nStars + 2;
}
}
Another implementation without using nStars or nSpaces
كود:
#include<iostream>
using namespace std;
void main()
Written by : mo7amed 3la2 El din (totty)
{
int n;
cout<<"Enter n: ";
cin>>n;
for(int i=1; i<=n ; i++)
{
for(int j=1; j<=n-i ; j++)
{
cout<<' ';
}
for(int k=1; k<=2*i-1 ; k++)
{
cout<<'*';
}
cout<<endl;
}
}
Program 4: The user enters n, and the program outputs an isosceles triangle and its
vertical inverse - mirror image
Example: n = 7
كود:
*
***
*****
*******
*********
***********
*************
***********
*********
*******
*****
***
*
كود:
#include<iostream>
using namespace std;
void main()
{
int n;
cout<<"Enter n: ";
cin>>n;
Written by : mo7amed 3la2 El din (totty)
int nSpaces = n-1;
int nStars = 1;
for(int i=1; i<=n ; i++)
{
for(int j=1; j<=nSpaces ; j++)
{
cout<<' ';
}
for(int k=1; k<=nStars ; k++)
{
cout<<'*';
}
cout<<endl;
nSpaces = nSpaces - 1;
nStars = nStars + 2;
}
nStars = nStars - 4;
nSpaces = 1;
for(int i=1; i<=n-1 ; i++)
{
for(int j=1; j<=nSpaces ; j++)
{
cout<<' ';
}
for(int k=1; k<=nStars ; k++)
{
cout<<'*';
}
cout<<endl;
nSpaces = nSpaces + 1;
nStars = nStars - 2;
}
}
Program 5: The user enters n, and the program outputs a parallelogram with sides
of length n
parallelogram = متوازي أضالع
Written by : mo7amed 3la2 El din (totty)
Example: n = 9
كود:
*
**
***
****
*****
******
*******
********
*********
********
*******
******
*****
****
***
**
*
كود:
#include<iostream>
using namespace std;
void main()
{
int n;
cout<<"Enter n: ";
cin>>n;
for(int i=1 ; i<=n ; i++)
{
for(int j=1 ; j<=n-i ; j++)
{
cout<<' ';
}
for(int k=1 ; k<=i ; k++)
{
cout<<'*';
}
cout<<endl;
}
for(int i=n-1 ; i >= 1 ; i--)
{
for(int j=1 ; j<=i ; j++)
{
cout<<'*';
Written by : mo7amed 3la2 El din (totty)
}
;cout<<endl
}
}
Note :
دلوقتي لو احنا عايزين نخلي بدل * نحط شكل تاني نعمل ايه ؟
charيبقى حرف يبقى نخليه variableيبقى نعرف
يعني كده
:كود
char x
مثال
..وبعدين نقوله دخل الشكل اللي انته عايزه وكده
:كود
;" cout<<"Enter the shape that you want :
;cin>>x
بس جوه اللوب بدل مانكتب
:كود
;"*"<<cout
هنكتب
:كود
;cout<<x
حاولوا بقى تفكروا في اشكال تانية تعملوها وربنا يوفق الجميع ان شاء هللا
)Written by : mo7amed 3la2 El din (totty