How to make a condition where the row will stop making new lines when the column reach its limit value?
Expected output:
Enter a number: 23
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
16 17 18 19 20 21
22 23
My code:
#include <stdio.h>
int main(){
int n;
int count=1;
printf("Enter a number: ");
scanf("%d", &n );
/*how to find the relation between row and count? when count reach its limit the row must stop executing the new line.*/
for(int i=1 ; i<= ?? ; i )
{
for(int j = 1; j<=i; j )
{
if(count<=n)
printf("%d",count );
}
printf("\n");
}
return 0;
}
CodePudding user response:
c
void task() {
unsigned int n(0);
unsigned int row(1);
std::cout << "Enter a number: ";
std::cin >> n;
for (int i = 1, buf = 1; i <= n; i, buf) {
std::cout << i;
if (buf == row) {
std::cout << std::endl;
row;
buf = 0;
}
}
}
Output:
Enter a number: 50
1
23
456
78910
1112131415
161718192021
22232425262728
2930313233343536
373839404142434445
4647484950
CodePudding user response:
See this:
You can easily understand:
for(int i=1;i<=50;i ){
System.out.print(i " "); //for printing number on the same line
}
//your output will be
//1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50