-->





Translate

Showing posts with label triangle. Show all posts
Showing posts with label triangle. Show all posts

inverted triangle shape in c++ code using for loop

2 comments
write a c++ program which displays an inverted triangle using nested for loop and asterisk character

Use any Compiler IDE you want.
Required Output should be look like below image.
inverted triangle shape in c++ code using for loop and asterisk character

 

Compiler used: CodeBlocks c++ Compiler
c++ code:



#include<iostream>
using namespace std;
int main()
{

     cout<<"\"Inverted Triangle Shape\"\n\n";


  int w=6;

   for(int g=0;g<9;g++)
 {
  cout<<"*";    // Displaying asterisk here
 }
  cout<<endl; // endl is for new line

  for(int a=1;a<=3;a++)
 {

    for(int b=0;b<a;b++)
   {
    cout<<" ";    // displaying space here
   }
    cout<<"*";


      for(int c=1;c<w;c++)
     {
      cout<<" ";
     }
      cout<<"*"<<endl;
      w=w-2;
 }

  for(int e=1;e<=1;e++)
 {

    for(int f=4;f>=e;f--)
   {
    cout<<" ";
   }
    cout<<"*";
 }

  return 0;
}


sample output

inverted triangle shape code using for loop sample output c++ programming



These kinds of programs are basic level program can be coded in more than one way it is recommended to change the logic of program and do experiment with the code for better understanding

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

C++ code to display filled isosceles triangle using for loop and asterisk character

9 comments
Write a program which draw isosceles Triangle Shape with asterisk(*) and using nested for loop
 Program required output:

display  isosceles triangle using for loop and asterisk character




C++ Code:


#include<iostream>
using namespace std;

int main()
{

  int e=1;

  cout<<"\" Triangle Shape SHAPE \":\n\n";

  for(int a=1;a<=5;a++)
 {
    for(int b=4;b>=a;b--)
   {
    cout<<" ";  // Printing Space Here
   }
      for(int c=0;c<e;c++)
     {
       cout<<"*";  // Printing asterisk here
     }
      cout<<endl;   // new line
      e=e+2;
 }
}



Program Output:
sample output filled isosceles triangle asterisk character




More C++ Shapes Here


These kind of programs are very helpful in the understanding of nested for loop.
Read More...