-->





Translate

Showing posts with label for. Show all posts
Showing posts with label for. 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...

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

Introduction to Control Structures in C++

7 comments
Control Structures
"Statements used to control the flow of execution in a program".


  • Means a statement through which we control the behavior of our program like what kind of
  • function it will perform, when it will terminate or continue under certain circumstances or conditions.
  • These instructions enable us to group individual instructions into a single logical unit with one entry point and one exit point. To make it more clearer, suppose we have lot of statements in our program by using control structure we control their working like either all statements will evaluate at once on the basis of certain condition or will evaluate one by one or will not be evaluated.

All programs use control structures to implement the program logic.
There are three types of Control Structures
1. Sequence
2. Selection
3. Loops/ Repetition/ Iteration
 Sequence structure in C++:
As name refers instructions  are executed in the sequence in which they are written in the program.
Example: Compound Statement:
              {
                    Statements
                    Statements - 1
                    Statements - 2
                    - - - - - - - - -
                }
 Selection structure in C++:
A structure which select which statement or block of statements will execute on the basis of our
programming logic.
Examples:
1. if - else statement
2.  switch
Loops in C++:
These structures are used to repeat the statement or set of statements.
Examples:
1. for - loop
2. while - loop
3. do - while loop
Read More...