hi! i m Gaurav. i m facing a problem in printing a series
i have print the series related to it.
like

    *
    * *
    * * *
    * * * * 
    * * * * *

but i m facing a problem in printing the following series:

          *
        *  *
      *  *   *
     *  *  *   *

can anyone help me out? i will be thankful

i m trying this logic:

for(i=1;i<=n;i++)
{
for(j=1;j<=i;j++)
{
cout<<"*";
}
cout<<endl;
}

now wt to do in the inner loop i a fail to understand
so plz help me

Member Avatar for iamthwee

Draw a flow chart on paper first.

i cant understand the logic. plz would u help me ?
i wanna print the following:
*
* *
* * *

Just replace "n" in the outer loop with 3 and you should have the results you are looking for.

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


This code will simply print this pattern :

*
**
***

The outer loop will print the character 'endl' 3 times as specified and the for each iteration of the outer loop the inner loop will run i-1 times so if i = 1, inner loop will run zero times if it is 2 inner loop will run 1 time and so on, and this loop simply prints the '*' (with no endlines) and outer loop will simply print endline for it and thus that triangle will be printed.

Regards,
QaiS

u hvnt given a new line charactr after inner loop.....acc to c language u mst use logic..:
for(i=1;i<=3;i++)
{{for(j=1;j<=3;j++)
printf("*");
}
printf("\n");
}

Well C is too old for 2007, i gave in c++ style and there indeed is an endline character the 'endl' !

Well C is too old for 2007

I disagree.

Let's say you want to develope harware drivers; what program lenguage dominates there?.
Let's say you want to develope operating system kernels; what program do you think is used?
Anything that has to do with "closer to the metal" C is there.

C is still kicking and alive!. :)

>Well C is too old for 2007
What facts do you have that support that statement? ;)

>there indeed is an endline character the 'endl' ! endl is not a character, it's a macro which: a) inserts a newline '\n' into the output buffer b) flushes the output buffer. Realistically, you should only be using endl when you actually need the output buffer flushed, because otherwise you're just cutting down your program's performance.

u can have required output by this code


for(a=1;a<=3;a++)
{

for(b=1;b<=a;b++)
printf("*");

printf("\n");
}

endl is not a character, it's a macro which: a) inserts a newline '\n' into the output buffer b) flushes the output buffer.

endl is a function

template<typename CHARTYPE, typename TRAITS> 
  basic_ostream<CHARTYPE,TRAITS >& endl( 
                             basic_ostream<CHARTYPE,TRAITS>& stm )
  {
      stm << '\n' ;
      returm stm.flush() ;
  }

this kind of function is called an ostream manipulator in c++
infact, any function that we write taking a single argument which
is a reference to a stream and returns a reference to a sream is a
manipulator for the stream.

Realistically, you should only be using endl when you actually need the output buffer flushed, because otherwise you're just cutting down your program's performance.

very true

1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
please solve this problem

commented: Please read the rules. -3
commented: Don't hijack, create a new thread +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.