3 Replies - 558 Views - Last Post: 11 December 2012 - 08:15 AM Rate Topic: -----

#1 AbdirahmanSL  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 20-November 12

C++ home using for loops to print stars from 1 to n

Posted 11 December 2012 - 04:08 AM

Hello everyone


The question is here :
Problem III: Write a C++ program that outputs a series of stars of even
number using a for-loop statement. Your screen should appear as follows:
Enter the value of the number n: 17
**
****
******
********
**********

Here is what i come up with :

#include <iostream>
using namespace std;
void main()
{ int n, i;
cout<<"Enter The value of n";
cin >>n;
for ( i = 1; i <= n; i++ )
{
for ( int j = 1; j <= i; j++)
 cout << "*";
cout << endl;
}
}
		




I even tried to replace j++ with j+=2 but it did not work

Thank you in advance.

Is This A Good Question/Topic? 0
  • +

Replies To: C++ home using for loops to print stars from 1 to n

#2 raghav.naganathan  Icon User is offline

  • Perfectly Squared ;)
  • member icon

Reputation: 408
  • View blog
  • Posts: 1,440
  • Joined: 14-September 12

Re: C++ home using for loops to print stars from 1 to n

Posted 11 December 2012 - 04:35 AM

So, what you basically want here is for the first line to start with 2 stars, 2nd line 4stars and so on...

you need to make a small change in your 1st for loop to i=i+2 and change your initial value of j to 0.

Well, one more very important thing you need to note here is to NEVER use void main() and practice using int main()...and in the end you need to return 0 as well.

regards,
Raghav

This post has been edited by raghav.naganathan: 11 December 2012 - 04:37 AM

Was This Post Helpful? 1
  • +
  • -

#3 AbdirahmanSL  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 20-November 12

Re: C++ home using for loops to print stars from 1 to n

Posted 11 December 2012 - 07:58 AM

Sorry for replying late.It worked. Thank you very much for your help.I thought that the change should be made to the second loop. Now my question is what is the need for second loop if i= i+2 did the trick, i removed the second loop and it did not work.


About void and int ,my Prof. told to use void when you are not calcualting anything. However if you want to claculate use int main and return 0 .

Any way your help is much appreciated.


regard
Was This Post Helpful? 0
  • +
  • -

#4 jimblumberg  Icon User is offline

  • member icon


Reputation: 4096
  • View blog
  • Posts: 12,673
  • Joined: 25-December 09

Re: C++ home using for loops to print stars from 1 to n

Posted 11 December 2012 - 08:15 AM

The function main() is "special", the C++ standard states main() must be defined to return an int, when using a hosted environment.

Jim
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1