help me make this shorter.. i think this is too long.. i'm a newbie student..

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

int main()
{
int x, n=0;
clrscr();
for(x=1;x<=5;x++)
cout << " " << x;
if(++n==5)
{
cout << " " << endl;
}
n=0;
for(x=2;x<=5;x++)
cout << " " << x;
if(++n==4)
{
cout << " " << endl;
}
n=0;
for(x=3;x<=5;x++)
cout << " " << x;
if(++n==3)
{
cout << " " << endl;
}

n=0;
for(x=4;x<=5;x++)
cout << " " << x;
if(++n==2)
{
cout << " " << endl;
}
n=0;
for(x=5;x<=5;x++)
cout << " " << x;
}
getch();
return 0;
}


help me pls.. ^_^

>#include <iostream.h>
C++ is defined by the ISO standard, and that standard doesn't specify iostream.h; it's a pre-standard header that most compilers no longer support. If you learn this old version of the language, you'll be virtually useless with modern C++.

>#include <conio.h>
I'd recommend you forget that this header even exists for now. You're not using it correctly, and I doubt you have any real need for it at this point.

>int x, n=0;
I'd recommend only declaring one variable per line. That way you don't hide variables and it makes commenting easier.

>clrscr();
This isn't needed, and it's likely to be very frustrating for users that run your programs from the command line as it'll remove everything they had on the screen previously. Personally, I don't like it when an arrogant program decides to wipe out all of the results from other programs.

>getch();
This throws away portability for no reason. You can just as easily use cin.get(); and the effect is largely the same. Statistically speaking, most users will hit either the space bar or the Enter key when asked to press "any key". So limiting that key to Enter isn't really that much of a restriction.

As for your logic, the effect is the same as this nested loop:

#include <iostream>

int main()
{
  const int max = 5;

  for ( int i = 1; i <= max; i++ ) {
    for ( int j = i; j <= max; j++ )
      std::cout<< j <<' ';
  }

  std::cout<<'\n';
}

However, I can't fathom what you're trying to do with n, so most likely that's not what you were looking for.

If I were you, I would shorten to this:

#include <iostream>

int main()
{
    for(int i=1; i<=5; i++) {
        for(int j=i; j<=5; j++)
            std::cout << j << ' ';
        std::cout << std::endl;
    }

    return 0;
}

Narue, how can you put cplusplus sysntax highlighter in your post?

if(++n==5)
{
cout << " " << endl;
}
....
if(++n==4)
{
cout << " " << endl;
}
....

Honestly speaking, these lines of code have no effect in your code and it is totally useless.

Don't start multiple threads on essentially the same topic.

Narue, how can you put cplusplus sysntax highlighter in your post?

[code[B]=cplusplus[/B]]
[I]your code here[/I]
[/code]

Narue, how can you put cplusplus sysntax highlighter in your post?

[code[B]=cplusplus[/B]]
[I]your code here[/I]
[/code]

Isn't that on the background of the textbox in which you entered the question?

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.