-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPrintingPatternUsingLoops.c
More file actions
38 lines (29 loc) · 1.04 KB
/
PrintingPatternUsingLoops.c
File metadata and controls
38 lines (29 loc) · 1.04 KB
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
// Question link - https://www.hackerrank.com/challenges/printing-pattern-2/problem?isFullScreen=true
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main()
{
int n;
scanf("%d", &n);
// Complete the code to print the pattern.
int newN = 2 * n - 1; // Correct calculation for the size of the pattern
int originalN = n;
for (int i = 0; i < newN; i++) {
for (int j = 0; j < newN; j++) {
int left = j;
int up = i;
int down = newN - 1 - i;
int right = newN - 1 - j;
// Calculate the value to be printed
int value = originalN - (left < up ? (left < down ? (left < right ? left : right) : (down < right ? down : right))
: (up < down ? (up < right ? up : right) : (down < right ? down : right)));
// Print the value
printf("%d ", value);
}
// Move to the next line after each row
printf("\n");
}
return 0;
}