0% found this document useful (0 votes)
52 views7 pages

Program 1:: The User Enters N, and The Program Outputs A Right-Angled Triangle With Height N

The document describes 5 programs that output different shapes using nested for loops in C++. Program 1 outputs a right-angled triangle. Program 2 outputs a right-angled triangle and its vertical inverse. Program 3 outputs an isosceles triangle. Program 4 outputs an isosceles triangle and its vertical inverse. Program 5 outputs a parallelogram. Each program prompts the user to enter a number n to determine the size of the shape and uses loops to output the appropriate number of characters.

Uploaded by

Tarek Ramah
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
52 views7 pages

Program 1:: The User Enters N, and The Program Outputs A Right-Angled Triangle With Height N

The document describes 5 programs that output different shapes using nested for loops in C++. Program 1 outputs a right-angled triangle. Program 2 outputs a right-angled triangle and its vertical inverse. Program 3 outputs an isosceles triangle. Program 4 outputs an isosceles triangle and its vertical inverse. Program 5 outputs a parallelogram. Each program prompts the user to enter a number n to determine the size of the shape and uses loops to output the appropriate number of characters.

Uploaded by

Tarek Ramah
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd

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‬‬

You might also like