Need a project done?

C++ Programming Developer

Search This Blog

Triangle and Diamond in C++




#include <iostream>
using namespace std;

int main()
{
for (int i = 1; i<5; i++)
{
for (int j = 0; j < (2*i - 1); j++)
{
cout << "*";
}

cout << endl;
}

return 0;
}
//===============================================================================



#include <iostream>
#include <iomanip>
using namespace std;

int main()
{
int s = 10;
for (int i = 1; i<5; i++)
{
cout << setw(s);
s--;
for (int j = 0; j < (2*i - 1); j++)
{
cout << "*";
}

cout << endl;
}

return 0;
}
//===============================================================================


//===============================================================================

#include <iostream>
#include <iomanip>
using namespace std;

int main(){
int n = 4;
for (int i = 1; i<= 9; i++)
cout << "*";
cout << endl;

for (i = 1; i<4; i++){
cout << "*" << setw(n);
for (int j = 1; j<=2*i-1; j++)
cout << "*";
cout << setw(n) << "*";
cout << endl;
n--;
}
for (i = 1; i<= 9; i++)
cout << "*";
return 0;
}



Now write it by yourself, so that you can have grip on the working of loops which will help a lot in the future.

3 comments:

  1. *
    **
    ****
    ********
    ****************

    ReplyDelete
    Replies
    1. After getting above codes, you should be able to implement it yourself.

      Delete

"Don't let anyone ever make you feel like you don't deserve what you want."