Reputation: 13
#1234567
1#345678
23#56789
345#7890
4567#901
56789#12
678901#3
7890123#
Here is my code
int main() {
int pattern;
int rows, columns;
printf("Enter the pattern: ");
scanf("%d", & pattern);
for (rows = 1; rows <= pattern; rows++) {
for (columns = 1; columns <= pattern; columns++) {
if (rows == columns)
printf("#");
else
printf("%d", columns);
}
printf("\n");
}
return 0;
}
Upvotes: 0
Views: 110
Reputation: 707
Start both inner and outer loop from 0 to pattern-1. Print (columns+rows)%10
. Your work will done. See the change in code bellow:
int main() {
int pattern;
int rows, columns;
printf("Enter the pattern: ");
scanf("%d", & pattern);
for (rows = 0; rows < pattern; rows++) {
for (columns = 0; columns < pattern; columns++) {
if (rows == columns)
printf("#");
else
printf("%d", (columns+rows)%10);
}
printf("\n");
}
return 0;
}
Upvotes: 1