My assignment is to write a program that produces a diamond when the user inputs an integer, if the integer is even it should be increased to the next odd number.

if the number is 7, it should print:

___*
__***
_*****
*******
_*****
__***
___*

(Without the underscores.)

So far I am able to produce the following output with the code I have written:

Enter number: 7
3

*
**
***
Press any key to continue



#include <iostream>
using std::cout;
using std::cin;
using std::endl;


int main()
{


int n;


cout<<"Enter number: ";
cin>>n;


cout<<n/2<<endl<<endl;


for(int i=0; i<=n/2; i++){
cout<<" ";


for(int st=1; st<=i; st++)
{
cout<<"*";
}
cout<<endl;
}


return 0;
}

This is an assignment on nested loops, so we're supposed to use 4 for loops. I suspect that there's going to be 2 pairs of nested for loops. Can anyone help me out?

Greetings.
Sorry, I didn't have time to really sit down and think.
Can't get 4 for loops, but I've got a while nested with 3 for loops.
Take it as a reference if you like.
I'll spend some more time to come out with one with 4-For loops. :cheesy:

#include <iostream>
using std::cout;
using std::cin;
using std::endl;

int main()
{

     int num, odd, increment, space;
     bool go;

     num = 0;
     increment = 1;
     odd = 1;
     go = false;

     do{
          cout<<"Enter number: ";
          cin>>num;
     }while(num<3);

     if(num%2==0)
          num = num + 1;

     while(odd<=num)
     {
          space = num/2 - odd + increment;
          if(go==false)
               increment += 1;
          else
               increment -= 1;
   
          for(int x=0; x<space; x++)
               cout<<" ";
		
          for(x=0; x<odd; x++)
               cout<<"*";

          for(x=0; x<space; x++)
               cout<<" ";
          cout<<endl;

          if(go==false)
               odd += 2;
          else
               odd -= 2;
          if(odd>num && go==false)
          {
               odd = num - 2;
               increment = num/2;
               go = true;
          }
          if(odd<0)
               odd = num + 1;
     }

     return 0;
}

Hope this helps.

red_evolve,you really helpful person.
actually my college is having assignment soon and the title is address book system,since i am very fresh in programming,hope can get help from u too... :idea: hope meet you soon

red_evolve,you really helpful person.
actually my college is having assignment soon and the title is address book system,since i am very fresh in programming,hope can get help from u too... :idea: hope meet you soon

I agree, thanks for replying. I can't belive you went out of your way to write that code. I'm not sure if I understanding all the logics of it so I'll continue to study it and pursue the 4 for loops method. I've gotten a bit further already.

Greetings.
No problemo.
To Eddie,
I'm ready to help as long as you show some effort. ;)
To mrlucky0,
Sorry for being unable to come out with the exact result you wanted. Try to post
what you can get and I'll be sure to help out.

hi mrlucky0, heres a similar prog.chk this also out.i feel this is quite simple than that of red_evolves.

#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int n,k,no,i,j,a,b,c;
cout<<"\nenter n:";
cin>>n;
if(n%2==0)
n++;
for(i=1;i<=n;i+=2)
{
for(j=i;j<n;j+=2)
cout<<" ";
for(k=1;k<=i;k++)
cout<<"*";
cout<<"\n";
}
for(i=n-2;i>=1;i-=2)
{
for(j=i;j<n;j+=2)
cout<<" ";
for(k=1;k<=i;k++)
cout<<"*";
cout<<"\n";
}
getch();
}

Yes, I was attempting to write a program similar to that. Looks like you found it. Eventually I was able to figure it out. Here's my rendition, which is not too different. But I swear, I did it on my own!

#include <iostream>
using std::cout;
using std::cin;
using std::endl;

int main()
{

	int num, p=1;

	
	cout<<"Enter number: ";
	cin>>num;

	if(num%2==0)
		num+=1;

	int sp=num/2;

	for(int i=0; i<=num/2; i++)
	{
		for(int j=0; j<sp; j++)
		{
			cout<<" ";
		}

		for(int k=0; k<p; k++)
		{
			cout<<"*";
		}

		cout<<endl;
		p+=2;
		sp--;
	}
		sp=1;
		p=num-2;
	
	
	 for(i=0; i<num/2; i++)
	{
		for(int j=0; j<sp; j++)
		{
			cout<<" ";
		}

		for(int k=0; k<p; k++)
		{
			cout<<"*";
		}

		cout<<endl;
		p-=2;
		sp++;
	}


return 0;
}
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.