how to use for loop for displaying no in following format?
-
1 2 2 3 3 3 4 4 4 4 5 5 5 5 5 4 4 4 4 3 3 3 2 2 1
-
1 2 2 3 3 3 4 4 4 4 5 5 5 5 5 4 4 4 4 3 3 3 2 2 1
You should read book on C to get more familiar with the loops that are avail in C. I prefer you to read "Let Us C - Yashwant Kanetkar". Please dont expect full code from here :) well this case i can help, but what about the other cases? So it is better to get familiar with C loops and then make a logic from your side. All the best. :thumbsup:
Величие не Бога может быть недооценена.
-
1 2 2 3 3 3 4 4 4 4 5 5 5 5 5 4 4 4 4 3 3 3 2 2 1
You need an outer loop and a inner one. :)
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles] -
1 2 2 3 3 3 4 4 4 4 5 5 5 5 5 4 4 4 4 3 3 3 2 2 1
Try this:
#include <iostream>
using namespace std;
void main()
{
const char *p[] = {"1", "2 2", "3 3 3", "4 4 4 4",
"5 5 5 5 5", "4 4 4 4", "3 3 3",
"2 2", "1"};
for (int i=0; i<sizeof(p)/sizeof(*p); ++i)
{
cout << p[i] << endl;
}
}Steve
-
1 2 2 3 3 3 4 4 4 4 5 5 5 5 5 4 4 4 4 3 3 3 2 2 1
-
1 2 2 3 3 3 4 4 4 4 5 5 5 5 5 4 4 4 4 3 3 3 2 2 1
It is very simple. The following is the generic code algorithm. int range = ; int outer,inner; //The following loop is for displaying pattern from 1 to n ( any value) for( outer loop... ) { for( inner loop... ) } //The following loop is for displaying pattern from n-1 to 1 range -= 1; for( outer loop... ) { for( inner loop.... ) } Understand the basic code flow and implement your code.
-
1 2 2 3 3 3 4 4 4 4 5 5 5 5 5 4 4 4 4 3 3 3 2 2 1
#include <iostream>
using namespace std;void main(){
const static int threshold(5);
for(int i(1), d(+1); i; i += d, d = (i == threshold)? -1 : d){
for(int j(0), end(i); j != end; ++j) {
cout << i << " ";
}
cout << endl;
}
}Getting rid of the ending space are trivial and left as an exercise :rolleyes: .
-
Try this:
#include <iostream>
using namespace std;
void main()
{
const char *p[] = {"1", "2 2", "3 3 3", "4 4 4 4",
"5 5 5 5 5", "4 4 4 4", "3 3 3",
"2 2", "1"};
for (int i=0; i<sizeof(p)/sizeof(*p); ++i)
{
cout << p[i] << endl;
}
}Steve