Hi guys
So i am brand new to programming. I have just started self learning about 4 days ago,
so far I've only learn't variables, input/output, conditional statements and loops. at the moment I'm stuck on a for loop problem;
"Write a program to print the diamond star(*) pattern based on an input number."
In my code so far without really knowing what I'm doing i tried to reverse what i did in my first for loop but it's not quite working.
I would appreciate if somebody could explain where I'm going wrong.
// Write a program to print the diamond star(*) pattern based on an input number.
#include <iostream>
usingnamespace std;
int main()
{
int number = 0;
cout << "Please enter a number: ";
cin >> number;
for (int i = 0; i < number; i++)
{
// prints space
for (int j = 0; j < (number - i - 1); j++)
{
cout << " ";
}
// prints stars
for (int j = 0; j < (2 * i + 1); j++)
{
cout << "*";
}
cout << endl;
}
// trying to reverse for loop here !
for (int i = number ; i >= 0; i--)
{
for (int j = 0; j < (number - i - 1); j++)
{
cout << " ";
}
for (int j = 0; j < (2 * i + 1); j++)
{
cout << "*";
}
cout << endl;
}
return 0;
}
Yeah that's the one :).
Yeah i was looking at those conditions but at the moment i seem to not be understanding nested for loops very well.
I'll keep having a little play around tomorrow, i can't think properly at the moment, my brain is fried but from what i hear i'll be feeling like this a lot.