/*
Program for DISPLAYING following shape (DIAMOND) using NESTED LOOP:
e.g: if user enter's "7" the output will be;
*
* *
* *
* *
* *
* *
*
*/
# include <iostream>
# include <iomanip>
using namespace std;
int main()
{
int size;
do // it will take only odd number as input
{
cout << "Enter the height for diamond (Only ODD Numbers>3)?\t";
cin >> size;
} while (size % 2 == 0 || size < 5);
int spaces = size / 2; // will half of number for spaces
int spaces_i = -1;
for (int i = 1; i <= size; i++) // will controll num of rows
{
cout << setw(40 - size / 2); // (Not necessary) for moving CROSS in the centre
for (int j = 1; j <= spaces; j++) // will print spaces before assterix
cout << " ";
cout << "*";
for (int k = 1; k <= spaces_i; k++) // will print assteix
cout << " ";
if (i != 1 && i != size)
cout << "*";
cout << endl; // jump on next line
if (i <= size / 2) // it will work for upper part of diamond
{
spaces_i += 2;
spaces--;
}
else // it will work for lower part of diamond
{
spaces_i -= 2;
spaces++;
}
}
return 0;
}
/*
Program for DISPLAYING following shape using NESTED LOOP:
e.g: if user enter's "7" the output will be;
*
* *
* * *
* * * *
* * *
* *
*
*/
#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
int c, r, s;
do
{
cout << "Enter size for \"DIAMOND\" (Only +ve ODD Numbers greater than 3)?\t";
cin >> r;
} while (r % 2 == 0 || r < 3);
for (int i = 1; i <= r / 2 + 1; i++) //for upper triangle
{
for (s = 1; s <= (r / 2 + 1) - i; s++) //controls the number of spaces
cout << " ";
cout << setw(40 - r / 2); // (Not necessary) for moving DIAMOND in the centre
for (c = 1; c <= i; c++) //Controls number of "*"
cout << " *";
cout << "\n";
}
for (int i = r/2; i >= 1; i--) //for lower triangle
{
for (s = 1; s <= (r / 2 + 1) - i; s++) //controls the number of spaces
cout << " ";
cout << setw(40 - r / 2); // (Not necessary) for moving DIAMOND in the centre
for (c = 1; c <= i; c++) //Controls number of "*"
cout << " *";
cout << "\n";
}
return 0;
}
/*
Program for DISPLAYING following shape using NESTED LOOP:
e.g: if user enter's "7" the output will be;
*
* *
* * *
*******
* * *
* *
*
*/
#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
int c, r, s;
do
{
cout << "Enter size for \"DIAMOND\" (Only +ve ODD Numbers greater than 3)?\t";
cin >> r;
} while (r % 2 == 0 || r < 3);
for (int i = 1; i <= r / 2 + 1; i++) //for upper triangle
{
cout << setw(40 - r / 2); // (Not necessary) for moving DIAMOND in the centre
if (i == r / 2 + 1) //For centre row
{
cout << " "; //displays one space
for (int i = r; i > 0; i--) //Controls number of "*" of centre row
cout << "*";
cout << endl; //Moves to the next line
continue;
}
for (s = 1; s <= (r / 2 + 1) - i; s++) //controls the number of spaces
cout << " ";
for (c = 1; c <= i; c++) //Controls number of "*"
cout << " *";
cout << "\n";
}
for (int i = r / 2; i >= 1; i--) //for lower triangle
{
cout << setw(40 - r / 2); // (Not necessary) for moving DIAMOND in the centre
for (s = 1; s <= (r / 2 + 1) - i; s++) //controls the number of spaces
cout << " ";
for (c = 1; c <= i; c++)
cout << " *"; //Controls number of "*"
cout << "\n";
}
return 0;
}
No comments:
Post a Comment