- 3
What is the missing part
This is how suppose to be the output 55554 4443 332 21 1 This is my code here #include <iostream> using namespace std; int main() { for(int i=5;i>1;i--) { cout<<i; for(int j=2;j<i;j++) { cout<<i; } cout<<i-1; cout<<endl; } return 0; } But the out is 55554 4443 332 21
17 Antworten
+ 6
Bacani Rocelle M.
Just follow the previous pattern logic and initialise y with 5 and then decrement it
https://code.sololearn.com/cDHj1fIrwt1T/?ref=app
+ 2
1) take i>=1 // instead of i>1
2) assign j = 1 , comment first cout.
Only print in loop.
Between, how last output is 1,... Is it not supposed to be 0?
+ 2
Then take
1) i >= 1
And print
if(i-1>0)
cout<<i-1; // ok or not efficient?
+ 2
A͢J if(i > 1 && x+1 >= i) 👍😎
+ 2
Your loop stops at 2
Because your condition states
i > 1
So it 2 is the last num that is greater than 1 here
Thats why you need to do
i>=1
Or
i>0
To make it work
+ 1
Yes the last output must 1
+ 1
Bacani Rocelle M.
Yes, I also think that it is better to use the full conditional expression "if... else" than "continue", yes and the variable j is superfluous:
for(int i = 5, x = 0; i > 0;){
if(x < i){
if(i > 1 && x+1 >= i)
cout << i-1 << endl;
else cout << i;
x++;
} else {
i--;
x = 0;
}
}
//Or:
for(int i=5, j=0; i > 0;){
if(++j >= i){
j = 0;
if(--i==0) cout << 1;
else cout << i << endl;
}else cout << i;
}
+ 1
Bacani Rocelle M. I edited the code.
If you want to address someone personally click on @ 😎
+ 1
Bacani Rocelle M. I mean, do you want me to explain your code? 🤔
You need five lines means i>0.
You don't need the last value of i, so stop the loop if(i==1)break;
If the teacher forbade the use of "break" when solving this task, then change the condition to if(i != 1) cout<<i-1<<endl;
for(int i=5;i>0;i--){
cout<<i<<' ';
for(int j=2;j<i;j++)
cout<<i<<'-';
if(i==1)break;
cout<<i-1<<endl;
}
P. S: "In the end, you could just add cout to the end of your code cout<<1;"😉
+ 1
this one works, u said only using for loop right?
#include <iostream>
using namespace std;
int main() {
for(int i=5;i>=1;i--)
{ cout<<i;
for(int j=2;j<i;j++)
{
cout<<i;
}
for(int k=i-1;k>0;k -= i) {
cout<<i-1<<endl;
}
}
return 0;
}
// edit : this code is not efficient, its better to use if statements
0
Solo
Same thing just way is different
0
Aj but our prof said that dont use break or continue just use loops
0
Thank you very much for the guide solo ill study this one
0
Solo ok if i cant figure out my code
0
Solo thank you for explaining
0
Please use this code
#include <iostream>
using namespace std;
int main() {
for(int i=5;i>=1;i--)
{
cout<<i;
for(int j=2;j<i;j++)
{
cout<<i;
}
if(i-1 > 0){
cout<<i-1;
}
cout<<endl;
}
return 0;
}
0
You could have just changed
; i>1;
to
; i>=1;
In the first for loop, and that would have given you the desired output.