*this is my output
1234
234
34
4
code //variable declaration
#include<iostream>
using namespace std;
int main(){
int i,j,space,star,n;
cin>>n;
i=1;
//for printing spaces
while(i<=n){
space=i-1;
while(space){
cout<<" ";
space--;
}
// for counting variables
j=1;
star=n-i 1;
// for printing numbers
while(star){
int num=i j-1;
cout<<num;
star--;
j ;
}
cout<<"\n";
i ;
}
return 0;
}
Que- I want that as an output :-
1 2 3 4
2 3 4
3 4
4
CodePudding user response:
Well, when printing spaces, you do this:
cout<<" ";
You can do this:
cout<<" ";
That is, print two spaces instead of one. That's half your answer.
Later you do this:
cout<<num;
You can do this:
cout << num << " ";
That is -- print the digit plus a space.
Done. However, if you do that, then you get an extra trailing space at the end of the line, So you can do this.
std::string delim = "";
while (star) {
int num=i j-1;
cout << delim << num;
delim = " ";
star--;
j ;
}
This is your code with very very simple changes. I added a variable delim and changed the cout statement slight.
The first time through the loop, delim
is an empty string, so adding it to the cout
statement does nothing. After that, delim
is a single space, so now you print a space before all but the very first digit.
On a side note -- please make more use of whitespace. Cramming everything together becomes very hard to read for those of us getting older, and it's a common way to obscure bugs. That's a style thing, but it WILL save you HOURS of time in the future.