PRINTING DIAMOND PATTRENS USING C++

in Steem4Bloggers2 years ago (edited)


GOOD EVENING



Hopefully you all will be well by the blessings and mercy of Allah Almighty and enjoying good health.Welcome to my post here today I am going to talk about the some codes of C++ .Before we move on lets talk little about for what purpose C++ used for.

C++ is a programming language that is used for the development of different softwares,games ,desktop apps ,browsers and different operating systems.Its little complicated but helps a lot in differnt developments in the technology.There a lot of programms and different functions that are intoduced in C++.C++includes different concepts opp,fundamentals,classes and a lot of styled programming is included in the C++.

source-code-583537__480.webp.jpg

Source

We have to write exact codes the little mistakes even of thw comma can destroy the compiling of the whole programm so we have to be accurate in our work.C++ can be said as the main language beacuse after learning it becomes easy to learn java,python and html languages to progress in the technology development.

So lets talk about some codes or programms by which we will be able to make different shapes of diamond such as ,diamond pattren of stars,numbers and alphabets etc in an easy method.In this post we will discuss about the three pattrens of diamonds in different ways.In the aplphabets with increasing alphabets in the row amd with increasing order of stars and numerical values.

First of all for the start of printing the pattern we have to asked the user to give us the information about the numbers of rows in which he wants to print a diamond.In the begginig we always wrote our code in the main functions and we have to take different files store in the background of C++ prograaming.Such as we have to start writing our programm from

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

I have written the return 0 for the successful compilment of the programm with exit code 0.To start the furthur coding we have to write in the main function.So this was the basic pattren to start writing our code in C++ for fundamental programming.Now lets move towards the start of printing diamond pattrens .

I am using the online compiler for the compiling the link is online gdb compiler



To Print the Diamond Pattren of Stars



First of all introduce all the integers requires to complete our codes.Such as to represent no of rows I will take rowno and will take the input Of number of rows from user using this integer and other like i for intilization and random alphabets to complete different steps.

There are many platforms developed for the compiling of the programm that we use I will use here gdb compiler link will be given below.So the complete code is wrote below.



Input



   #include<iostream>
   using namespace std;
  int  main()
   {
        int i, j, rowno, space;
        cout<<"Enter the Number of Rows to print star 
   pattren";
   cin>>rowno;
   space = rowno-1;
   for(i=1; i<=rowno; i++)
   {
    
     for(j=1; j<=space; j++)
              cout<<" ";
           space--;
           for
      (j=1; j<=(2*i-1); j++)
              cout<<"*";
       cout<<endl;
    }
    space = 1;
    for(i=1; i<=(rowno-1); i++)
   {
    
for(j=1; j<=space; j++)
           cout<<" ";
       space++;
       for(j=1; j<=(2*(rowNum-i)-1); j++)
           cout<<"*";
       cout<<endl;
    }
   cout<<endl;
   return 0;
  }

image.png
source

In this code I use three loops.Here I will talk little about the loop that why we use loop.To repeat the same things or for specific conditions.

  • I use first loop for the row numbers.
  • Second for the spaces in the pattren
  • Third for the coloumns to print starts .

The number of rows such as five in this case shows the upper side of the diamond and to print the lower side we use same three loops and the row will bw one number less than the upper pattren which will be the middle line of stars.



Output



image.png

image.png

So this is the output in returns of this programm.



How it works



  • When we take input from the user in this case user put the 5 and the row number 1 less than 5 store in the number or in the space.

  • The second steps goes with the execution of the loop according to the given condition.

  • Now the code inside the loop executes and print stars and spaces accordingly.

  • The spaces and stars printed until the condition given become the false.After the execution of loops the value of space decreased according to the condition we applied .So in every next line of stars the spaces will be one less according to our condition.

  • So the process is same for the upper as well as for the lower diamond pattren.We use the <<endl to end up a line or code pattren and move to the next.
  • We can also fix the number of rows and coloumns but in the programms that I discussed we have givem the oppurtunity to the user to take input from it and decided the number and size of the diamond pattrens.


To Print the Diamond Pattren of Numbers.



The code and principal is almost same as that of diamond in stars but here we have to print the numbers in the increasing order instead of stars only so the code we use to print this pattern is shown below.



Input



 #include<iostream>
  using namespace std;
 int main()
 {
    int i, j, rowno, space, num=1;
 
     cout<<"Enter the Number of Rows to print the 
 diamond pattren of numbers";
     cin>>rowno;
     space = rowno-1;
     for(i=1; i<=rowno; i++)
     {
         for(j=1; j<=space; j++)
             cout<<" ";
         space--;
          for
     (j=1; j<=(2*i-1); j++)
        {
           cout<<num;
            num++;
         }
         cout<<endl;
        num = 1;
      }
       space = 1;

 for(i=1; i<=(rowno-1); i++)
      {
         for(j=1; j<=space; j++)
            cout<<" ";
         space++;
          for
      (j=1; j<=(2*(rowno-i)-1); j++)
          {
            cout<<num;
             num++;
           }
          cout<<endl;
         num = 1;
      }
      cout<<endl;
      return 0;
     }

image.png

image.png

So this is a Code that we use to print the pattern Diamond pattern in the numbers here I have used the input of 8 rows it will be upper and lower pattern separately 8 rows in upper and one less than 8 in lower.

Here you can see that the numerical values appeared code according to the increasing number in ascending order.In this code I also use three loops as same in the above one.



Output

image.png

image.png

Output you can see in the above screenshots,this is the increasing number pattren of diamond with every line.So the execution and working of the loops will be same as I have discussed above.

So now I will show the same diamond pattren with alphabetical letter and the code will be almoat same with little changes.



To Print the Diamond Pattren of Alphabets



The programming code will be the same but just to take the alphabets we use the character and the chracter is always taken in double quotes as You can see in the below programms.Here I have taken the capital aplhabets small can also be taken.



Input



            #include<iostream>
            using namespace std;
             int main()
          {
              int i, j, rowno, space;
               char ch='A';
             cout<<"Enter the Number of Rows to print 
              diamond pattren of alphabets ";
            cin>>rowno;
            space = rowno-1;
           for(i=1; i<=rowno; i++)
           {
             for(j=1; j<=space; j++)
              cout<<" ";
           space--;
           for(j=1; j<=(2*i-1); j++)
          {
             cout<<ch;
             ch++;
             }
           cout<<endl;
           ch = 'A';
             }
          space = 1;
          for(i=1; i<=(rowno-1); i++)
            {
           for(j=1; j<=space; j++)
            cout<<" ";
          space++;
         for(j=1; j<=(2*(rowno-i)-1); j++)
          {
             cout<<ch;
            ch++;
             }
         cout<<endl;
        ch = 'A';
            }
        cout<<endl;
         return 0;
            } 

So know After compiling it on the online compiler lets see

image.png

image.png

So this is the code of the diamond pattren in the alphavets when I have compiled it on the online compiler the out put is shown below.Here we have given choice to the user to select the number of rows we can select as amny as we want to see the pattren in the diamond shape.



Output



image.png

So this is the printed pattren of the diamond in alphabets the executions of the loop is same as I have discussed above for the upper and lower pattrens of the diamond.

So that was all about the codes and printing of diamond pattrens in numbers,aplhabets and stars.There is a large list of the C++ programms and the codes under which the webs,games and different desktop sites are developed.I hope that you all will like this post.Thanks.



Regards
@mateenfatima


Sort:  
Your post has been successfully curated by our team via @steemdoctor1 at 60%. Thank you for your committed efforts, we invite you to do more and continue to post high-quality posts for a chance to win a valuable upvote from our curating team and why not be selected for an additional upvote later this week in our Top Seven

Note: Always use the tag #fintech to quickly access your post
 2 years ago 

Dear @mateenfatima your post written is very well manner.related to programming c++ and print alphabet.i like it very much

Loading...

Coin Marketplace

STEEM 0.15
TRX 0.12
JST 0.025
BTC 56002.99
ETH 2458.17
USDT 1.00
SBD 2.28