By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,654 Members | 936 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,654 IT Pros & Developers. It's quick & easy.

how to get

P: n/a
i am not getting the numerical figure .
1

12

123

1234

12345

plse help me.
i am writing these following logic:
# include <iostream.h>
void main()
{
int i,j;
for(i=0;i<=5;i++)
{
for(j=1;j<i;j++)
cout<<j;
cout<<endl;
}

}


this program displays 11234455 on screen.
please help me.
Advait Vaidya

Oct 2 '06 #1
Share this Question
Share on Google+
5 Replies


P: n/a
Advait Vaidya wrote:
i am not getting the numerical figure .
1

12

123

1234

12345

plse help me.
i am writing these following logic:
# include <iostream.h>
void main()
{
int i,j;
for(i=0;i<=5;i++)
{
for(j=1;j<i;j++)
cout<<j;
cout<<endl;
}

}


this program displays 11234455 on screen.
please help me.
Advait Vaidya
Try writing code that is a little neater and easier to read. Also don't
use depreciated headers.

#include <iostream>

int main(int argc, char * argv[])
{
for(int i(0); i < 6; i++)
{
for(int j(1); j < i + 1; j++) std::cout << j;
std::cout << std::endl;
}
}

Oct 2 '06 #2

P: n/a

"imperfectfreak" <im************@gmail.comwrote in message
news:11**********************@m7g2000cwm.googlegro ups.com...
Advait Vaidya wrote:
>i am not getting the numerical figure .
1

12

123

1234

12345

plse help me.
i am writing these following logic:
# include <iostream.h>
void main()
{
int i,j;
for(i=0;i<=5;i++)
{
for(j=1;j<i;j++)
cout<<j;
cout<<endl;
}

}


this program displays 11234455 on screen.
please help me.
Advait Vaidya

Try writing code that is a little neater and easier to read. Also don't
use depreciated headers.
You mean "deprecated", right?
>
#include <iostream>

int main(int argc, char * argv[])
or just:

int main()

{
for(int i(0); i < 6; i++)
Why start at 0? And there's nothing wrong with initialization using =.
Most people would write this as:

for (int i = 1; i <= 5; ++i) // (or i++, it doesn't matter here)
{
for(int j(1); j < i + 1; j++) std::cout << j;
Why put the loop body on the same line? That makes it difficult to debug.
(It also goes against your "easier to read" statement above, in my opinion.)

How about:

for (int j = 1; j <= i; ++j)

You seem to not like using <= for comparisons. I'm guessing this is because
you've heard that it's quicker to compare using < than <=?

While it _may_ be that in some case of un-optimized code, using <= might
take more machine cycles, it seems more readable to me to use <=, because
that shows your actual upper bound right there in the code, without having
to mentally subtract one to get the real upper bound. (Also, in cases where
the compiler isn't optimizing the loop anyway, the use of i+1 would mean an
extra step of adding 1 every iteration before comparing, negating your
assumed speedup from using < instead of <=.)
std::cout << std::endl;
}
}

Oct 2 '06 #3

P: n/a

"Advait Vaidya" <ad***********@gmail.comwrote in message
news:11**********************@h48g2000cwc.googlegr oups.com...
>i am not getting the numerical figure .
1

12

123

1234

12345

plse help me.
i am writing these following logic:
# include <iostream.h>
void main()
int main()

You always need to specify int as the return type from main, if you want to
write C++ code according to the standard. Some compilers allow void, but
it's wrong.
{
int i,j;
for(i=0;i<=5;i++)
Why start at 0? You want 5 lines, you should have 5 counts: 1,2,3,4,5.
{
for(j=1;j<i;j++)
Why use j<i? That skips the last value, so when i i 5, your output stops
after 4. You should use j<=i instead.
cout<<j;
cout<<endl;
}
}

this program displays 11234455 on screen.
I don't know how you got that! It looks like this code isn't what generated
that output. Try rebuilding the project completely, and checking the date
on the file you're running to see if it's been updated.

For me, that program (after fixing main to return int) prints:

-----start of listing-----

1
12
123
1234
-----end of listing-----

which is what I'd expect, given the code. Notice the blank first line.
That's because you used j<i, instead of j<=i.

Also, if you need to skip a line after each line of output (like your post
implies), then you need another endl there in that second cout statement.

-Howard


Oct 2 '06 #4

P: n/a
Howard wrote:
>
"imperfectfreak" <im************@gmail.comwrote in message
news:11**********************@m7g2000cwm.googlegro ups.com...
Advait Vaidya wrote:
# include <iostream.h>
Try writing code that is a little neater and easier to read. Also
don't use depreciated headers.

You mean "deprecated", right?

It should be pointed out that iostream.h isn't a deprecated header,
it's completely non-standard.


Brian
Oct 2 '06 #5

P: n/a
Default User wrote:
Howard wrote:
>>
"imperfectfreak" <im************@gmail.comwrote in message
news:11**********************@m7g2000cwm.googlegr oups.com...
Advait Vaidya wrote:
# include <iostream.h>
Try writing code that is a little neater and easier to read. Also
don't use depreciated headers.

You mean "deprecated", right?


It should be pointed out that iostream.h isn't a deprecated header,
it's completely non-standard.
It never has been a standard header, so when only looking at the standard,
you're right. However, on some actual implementations (like GCC e.g.), it
does exist, but is deprecated.

Oct 3 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.