-->





Translate

Showing posts with label hollow. Show all posts
Showing posts with label hollow. Show all posts

hollow diamond using for loop c++ code user enter input size of diamond

9 comments
write a c++ program which takes size of diamond and display hollow diamond using nested for loop and asterisk character.
Output should be look like bellow
hollow diamond using for loop c++ code user enter input size of diamond



 User can enter its desired size of diamond either even or odd

For Example
If user enters size of 3
3 lines of will be formed above middle and 3 lines will be formed below middle line
sample output for program hollow diamond using for loop c++ code user enter input size of diamond
Concept used:
c++ code:


#include<iostream>
using namespace std;
int main()
{
cout<<"Enter size of  Daimond:  ";

    int size;
  cin>>size;

    int z=1;
  for ( int i=0; i<=size; i++)
  {
    for (int j=size; j>i; j--)
    {
      cout<<" "; // printing space here
    }

    cout<<"*";  // printing asterisk here

    if ( i>0)
    {
      for ( int k=1; k<=z; k++)
      {
        cout<<" ";
      }
      z+=2;
      cout<<"*";
    }
    cout<<endl; // end line similar  to \n
  }

  z-=4;

  for (int i=0; i<=size-1; i++)
  {
    for (int j=0; j<=i; j++)
    {
      cout<<" ";
    }

    cout<<"*";

    for (int k=1; k<=z; k++)
    {
      cout<<" ";
    }
    z-=2;

    if (i!=size-1)
    {
      cout<<"*";
    }
    cout<<endl;
  }
return 0;
}


If user enter size equal to 10
hollow diamond using for loop c++ code user enter input size of diamond size equal 10

Recommended: Dry run the code line by line it will help to understand quickly and also clear the working of nested for loop.

More C++ Shapes Here
Read More...

Hollow triangle shape in c++ programing code

2 comments
Write a program which display a hollow triangle shape using asterisk character and nested for loop

Output should be look like below image

Hollow triangle shape in c++ programing code using for loop and asterisk character




c++ code:


#include<iostream>
using namespace std;
int main()
{
   cout<<"\"Hollow Triangle Shape\"\n\n";
  int z=1;

  for (int i=0; i<7; i++)
  {
    for (int j=7; j>i; j--)
    {
      cout<<" "; // displaying space here
    }
    cout<<"*";   // displaying asterisk here

    if (i!=0)
    {
      for (int k=1; k<=z; k++)
      {
        cout<<" ";
      }
      cout<<"*";
      z+=2;
    }
    cout<<endl;  // endl is for new line
  }

  for (int i=0; i<=z+1; i++)
  {
    cout<<"*";
  }

return 0;
}


Sample Output of the program
sample output Hollow triangle shape in c++ programing code


To increase or decrease the size of triangle change the value of loop variables. It is recommend to change the code and examine the output it will be more helpful.
More C++ Shapes Here
Read More...