Hello!
I have made a program that prints a diamond shape. But my program should read an odd number in the range 1 to 19 to specify the number of rows in the diamond, then it should display
a diamond of the appropriate side in which it is at present. I am a beginner. I don't know how to do that. Can any one help? I am in a need of it...!

#include <iostream.h>
#include <conio.h>

void main()
    {
    int i, j, k;
    for(i=1; i<=5; i++)
        {
        for(j=1; j<=5-i; j++)
        cout << " ";
        for(k=1; k<=2*i-1; k++)
        cout << "*";
        cout << endl ;
        }
        int l,m,n;
        for (n = 4; n > 0;n--) 
        {
        for (l = 1; l <= 5- n; l++)
        cout<<" ";
        for (m = 1; m<= 2 * n- 1; m++)
        cout<<"*";
        cout<<endl;
        }
            getch();
       }

Recommended Answers

All 7 Replies

Here is one of my beginning projects last year :

const int row = 1;

	for(int i = 0; i < row; i++)
	{
		for(int j = row-i; j ; j--)
			cout<<" ";
		
		for(int k = i*2; k >= 0; k--)
			cout<<"*";	

		cout<<endl;
	}

	for(int i = row; i >= 0; i--)
	{
		for(int j = i; j < row; j++)
			cout<<" ";
		
		for(int k = i*2; k >= 0; k--)
			cout<<"*";	

		cout<<endl;
	}

This might help you

#include <iostream>
#include <conio.h>

using namespace std;

int main()//you should use int main not void main
    {
    int i, j, k, rows;
    cout<<"Specify the number of rows (between 1-19) in the dimond: ";
    cin>>rows;
    cout<<endl;
    for(i=1; i<=rows; i++)
        {
        for(j=1; j<=rows-i; j++)
        cout << " ";
        for(k=1; k<=2*i-1; k++)
        cout << "*";
        cout << endl;
        }
        int l,m,n;
        for (n = rows-1; n > 0;n--)
        {
        for (l = 1; l <= rows- n; l++)
        cout<<" ";
        for (m = 1; m<= 2 * n- 1; m++)
        cout<<"*";
        cout<<endl;
        }
            getch();
return 0;//since i used int main i returned a value
       }

Didnt changed much so you can understand it

Well, Its quite simple, Use the same formula that you have in the existing code, But just change all the instances of '5' with a variable.

For Eg:

for(i=1; i<=5; i++)

This could be something like/

int len=5;
for (i=1;i<=len;i++)

Now you will then need to further need to take in the value of len from the user,.

Check if it lies between 1 - 19 and then look if it is odd. with the % operator. .

Then if all those conditions are satisfied. continue with the for loops or display an message stating that you have entered a wrong value or . Put a default value for len and continue.

poncho4all Your effort of posting down the code is much appreciated, However This forum tends to provide paths to the solution, But not to the solution itself. Read the rule Here. .

I love the idea but can you please explain this in c for me.

its good that you liked the idea but tell me what do you want to understand?

Please i want to know how this can be solved in c.
So i can also play around it.

Well,

To get this program running in C, there are only a few minor editions that have to be done.

Firstly,

<iostream> header must be replaced with the standard 'C' Header file 'stdio.h'

Next consider all cout<< statements as printf statements, Note that cout <<endl is equal to printf("\n");


And cin>> refers to scanf()..

Now since you have a brief Idea. Try experimenting and get to the solution.
And maybe from the next-time, Start a new thread instead of Continuing this one.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.