Here is the answer to your problem, but I do think that you should think of the solution yourself first before taking a look into the answer.
Here is some tips for your answer:
- Try to think about how you print 1 , 12, 123 and how it work reversely (123,12,1)
- Replace the number with pure char or string: '' or ""
- Think about the place you should put your code
- Test it
- If it is not the result you want but close enough, try to modify the code section of
for([initialization];[end condition];[incremental])
And also here is some advice on your future road of programming:
- Don't use
using namespace std;
, it might be convenience for now, but it is a bad practice to use it on your coding.
- Use starting index from 0. This practice can help you loop easily through out an array, list, vector and any other data structure that work like an array (IEnumerable in .NET).
- You have already initialize the integer inside the for loop, so actually you can omit the code section
int i,j;
Answer:
#include <iostream>
int main () {
for(int i=0;i<7;i++) {
for(int j=0;j<=i;j++)
std::cout<<j+1;
for(int k=0;k<(7-(i+1));k++)
std::cout<<"*";
std::cout<<std::endl;
}
return 0;
}