MY problem is .....
Given an input 'n' came from the user and display a pattern.
but my code is very long .... my code is only for 1-10 numbers i need to display any numbers that input of the users can u give me some short code or advice to make it easy or briefly...Thxxxxx
Ex. if n=6, display

Ex. output

  6 5 4 3 2 1
   6 5 4 3 2
    6 5 4 3 
     6 5 4 
      6 5
        6
#include<iostream.h>
#include<conio.h>
main()
{
int n1,i;
char c1;
textcolor(YELLOW);
a:
clrscr();
cout<<"Enter a number :";
cin>>n1;
for (i=n1;i>0;i--)
{
cout<<" "<<i;
}
cout<<endl<<" ";
for (i=n1;i>1;i--)
{
cout<<" "<<i;
}
cout<<endl<<"  ";
for (i=n1;i>2;i--)
{
cout<<" "<<i;
}
cout<<endl<<"   ";
for (i=n1;i>3;i--)
{
cout<<" "<<i;
}
cout<<endl<<"    ";
for (i=n1;i>4;i--)
{
cout<<" "<<i;
}
cout<<endl<<"     ";
for (i=n1;i>5;i--)
{
cout<<" "<<i;
}
cout<<endl<<"      ";
for (i=n1;i>6;i--)
{
cout<<" "<<i;
}
cout<<endl<<"       ";
for (i=n1;i>7;i--)
{
cout<<" "<<i;
}
cout<<endl<<"        ";
for (i=n1;i>8;i--)
{
cout<<" "<<i;
}
cout<<endl<<"         ";
for (i=n1;i>9;i--)
{
cout<<" "<<i;
}
{
cout<<""<<endl<<""<<endl;
cout<<"Do you want to try another number or EXIT...?"<<endl;
b:
cout<<"Press 'y' if you want to try another number and 'n' to exit:";
cin>>c1;
	if ( c1=='y' || c1== 'Y')
	goto a;
	else if (c1=='n' || c1=='N')
	cout<<endl<<"Press Any Key to EXIT";
	else
	{
	cout<<"Invalid input, Choose again"<<endl;
	goto b;
	}
}
getch();
}

Yeah your code is very long, thanks for the problem :) Why use so many for loops cant you do it with only
THis is what I did

#include <iostream>
#include <string>

using namespace std;



int main()
{
	int num = 10;
	while (num !=0)
	{
	 for(int i = 0;  i < num; ++i)
		 {
			cout << " " << i;
		 }
	 cout << endl;
	 if(num == 10)
		cout << " ";
		 if(num == 9)
			 cout <<  "  ";
			if(num == 8)
				cout << "   ";
			 if(num == 7)
				 cout << "    ";
				 if(num == 6)
					 cout << "     ";
					 if(num == 5)
						 cout << "      ";
						 if(num == 4)
							 cout << "       ";
							 if(num == 3)
								 cout << "        ";
							 if(num == 2)
								 cout << "         ";
							
							
	 --num;
	}
}

help me plssssss...... i need it ASAP...
THxxxxx......

i don't know how.... Because I'am a begginer in c++... Can u help me to my prob?

Well, for starters have a good think about your problem.

Take another look at your desired output and really think about it for a minute.
After the user has entered a number (num) between 1-10, you want the first line to count down from num to 1, so your initial minimum value to count down to is 1, but for each line afterwards the minimum is increased by 1.

To generate each line of output, you're going to need some kind of loop to count down from n to minimum and output each value.

And you want to keep outputting lines until the minimum value is greater than the entered number (num). (the very last line output is equal to the entered number, so as soon as the minimum becomes greater than num, you want to stop!)

So this implies that you'll need a second loop to keep track of the minimum value.

Bearing all of that in mind, you should now be able to code a solution for this, it's really not a difficult problem to solve. It may help you to take a piece of paper and actually draw out a flowchart or write some pseudo-code before coding your solution.

When trying to tackle any programming problem, don't just try to dive in and start coding without properly thinking things through and getting a solid program design first. Programmers of all levels have to practice this, even the most experienced. But as you are still starting out it is especially important for you!

Once you've got a bit more experience with designing and coding programs, you'll find yourself able to code solutions to simple problems like this in your sleep, but until then I'd advise using a bit more caution and forethought.

Cheers for now,
Jas.

[edit] This applies to invisi as well! I see where you were going with that incomplete attempt, but it is also an inelegant solution to the problem!

ok what you need to do is come up with a basic algorithm. your basic algorithm in this case would be the first line contains the n number of characters. taking from your eg lets say its a 6. note that ur max characters is also the max num of lines you will have. so basically with a 6 u have 6 lines of display wea first line has no space sec has one and so forth. note this would give u a irregular pyramid. now lets say you have a num x = 0 so in in the first line u print n and do n-1 until u reach x.wen u reach x print a \n. for the sec line start printing at n again n go until x+1.wen u reach x+1 print a \n. go ahead using this algorithm and you will come with your solution.now to make your program small and since you will use the same algorithm again for n number of lines you will need to get help from a loop.try and go with small eg nums you will be able to get it. if any other queries let me know. note that i have assumed that the pyramid will be irregular coz u cannot print a half space in ur screen

Oh yeah, good point kesh...Looking at my previous post, I forgot to include a bit about the leading spaces in the output. (My bad, I spotted it when writing my original post, but completely forgot to include it in my description! Very careless of me!)

@OP:
If you look at your desired output, the first line has no leading space, the 2nd has one leading space, the 3rd has two etc.etc. .
So you can see that for each additional line you output, you will need an additional space.
So you start with 0 leading spaces.
Before you use the loop to count down and output the values from n to minimum, you'll need to use another loop to output the required number of spaces.
After outputting each line you'll need to increment the number of spaces.

In light of my glaring omission, I'll give you some pseudo code for the solution! What the hell I'm feeling generous...

In pseudo code, your algorithm should be something like this:

1. Input a number (num) between 1 and 10
(NOTE: Don't forget to do any error checking, prompt user until they enter something valid)
2. Set minimum value to 1
3. Set number of Leading spaces to 0
4. While minimum is <= to num:
Do the following:
(i). if leading spaces > 0: use a loop to output the required number of spaces.
(ii). Using another loop, count down from num to minimum, outputting each value.
(iii). output a newline character
(iv). increase the minimum value
(v). increase the number of leading spaces.

And that is it!

Cheers for now,
Jas.

is there any code for increamenting the space?
like for example in
cout<<" "; // I need to increament this space to make my work right...

thats the only thing that i need to work for it....

Use a loop printing one space to the screen each time through the loop. When you are done with the loop it will look like you printed a string of spaces all at once.

As an alternative to doing everyting on the fly you could use two arrays (tables) of strings, one containing the string of spaces you need and the other containing the string of numbers you need, printing each string as you need them for each line of the pyramid. In addition to being comfortable using loops, table look up protocols are another nice tool to have in your tool box, so working out the solution using both approaches isn't necessarily the worst idea.

Add a variable called something like leading_spaces and set it to 0 as per my algo, then inside your main loop, if the number of spaces is greater than 0, set up a loop to output the appropriate number of spaces.
After the loop to output the spaces you'll have your loop to output the rest of the line (i.e. the count-down).
Towards the end of the main loop, increment the number of spaces ready for the next iteration of the main loop.

So if you've followed my algo correctly, you should have something that looks a bit like this:

#include <iostream>

int main()
{
	int num=0;
	// your code to get user input here....
	...

	// set initial values for minimum and the number of leading spaces
	int minimum = 1;
	int leading_spaces=0;

	// loop until minimum is greater than the entered number.
	while(minimum<=num)
	{
		// display any leading spaces
		if(leading_spaces>0)
		{
			for(int s=0; s<leading_spaces; ++s)
				std::cout << " ";
		}
		
		// your loop to count down and output values from num to minimum to go here....
		...

		// terminate the current line
		std::cout << std::endl;

		// increment minimum and leading_spaces
		minimum++;
		leading_spaces++;
	}
	std::cout << std::endl;
	return 0;
}

Note: I've deliberately left out the code for getting user input and the code for outputting the numbers. You've already got that by the sounds of it..

Cheers for now,
Jas.

Yeah! I did it It works, thank to the space thing that other guy( why didn't I think of that )

#include <iostream>
#include <string>

using namespace std;



int main()
{
	int num = 0;
	cout << "Enter Number: " ;
	cin >> num;
	cout << endl;
	int num_two = 1;
	int num_one = 0;
		while (num !=0)
		{
		 for(int i = num;  i > num_one; --i)
			 {
				cout << " " << i;
				
			 }
		 cout << endl;
		 
		 if(num > 0)
			 {
				for(int j = 0; j < num_two; j++)
				{
					cout << " ";
					
				}
			 }
	 		num_two++;			
			if (num > num_one)
				{
				 ++num_one;
				}
			else {num_one = num; num = 0;}
		}
	system("PAUSE");
}
#include <iostream>
#include <string>
using namespace std;

int height, row, spaces, character;

int main()
{
    cout<<"Enter N: ";
    cin>>height;

for (row = 0; row < height; row ++)
{
    for (spaces = 0; spaces < row; spaces ++)
        cout<<" ";
        
        for (character =1; character < 2*(height-row); character ++)
            cout<<"#";
            cout<<"\n";
}

    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.