Last active
August 26, 2015 14:04
-
-
Save checkaayush/acd1ceb641054db1ead7 to your computer and use it in GitHub Desktop.
Grofers - Campus Recruitment - Programming Problems
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
// Pattern (n = 4): | |
// 1*2*3*4*17*18*19*20 | |
// --5*6*7*14*15*16 | |
// ----8*9*12*13 | |
// ------10*11 | |
int calcOveralLastNum(int n) | |
{ | |
int temp = n, overall_last = 0; | |
while (temp) | |
{ | |
overall_last += temp; | |
temp--; | |
} | |
overall_last *= 2; | |
return overall_last; | |
} | |
void printDash(int line) | |
{ | |
int i, num_dashes = 2 * line; | |
for (i = 0; i < num_dashes; i++) | |
printf("-"); | |
} | |
void printFirstPart(int line, int n) | |
{ | |
int i, temp = n, start = 0, end; | |
for (i = 0; i < line; i++) | |
{ | |
start += temp; | |
temp--; | |
} | |
start += 1; | |
for (i = 0; i < (n - line); i++) | |
{ | |
printf("%d*", start); | |
start++; | |
} | |
} | |
void printLastPart(int line, int n) | |
{ | |
int overall_last = calcOveralLastNum(n); | |
// printf("Overall last number = %d\n", overall_last); | |
int end = overall_last, i; | |
for (i = 0; i < line; i++) | |
{ | |
end -= (n - i); | |
} | |
int start = end - (n - line) + 1; | |
for (i = 0; i < (n - line); i++) | |
{ | |
printf("%d", start); | |
if (i < (n - line - 1)) | |
printf("*"); | |
start++; | |
} | |
printf("\n"); | |
} | |
void printPattern(int n) | |
{ | |
int temp = n, overall_last = 0; | |
int line; | |
for (line = 0; line < n; line++) | |
{ | |
printDash(line); | |
printFirstPart(line, n); | |
printLastPart(line, n); | |
} | |
} | |
int main() | |
{ | |
int n; | |
scanf("%d", &n); | |
printPattern(n); | |
return 0; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
// Pattern (n = 4): | |
// 1*2*3*4*17*18*19*20 | |
// --5*6*7*14*15*16 | |
// ----8*9*12*13 | |
// ------10*11 | |
void printPattern(int i, int start, int mid, int n) | |
{ | |
// Dash | |
int j; | |
for (j = 0; j < (2 * i); ++j) | |
printf("-"); | |
// | |
// First Part | |
for (j = 0; j < n - i; j++) | |
{ | |
printf("%d*", start++); | |
} | |
// | |
// Second Part | |
for (j = 0; j < n - i; j++) | |
{ | |
printf("%d", mid++); | |
if (j != (n - i - 1)) | |
printf("*"); | |
} | |
// | |
printf("\n"); | |
} | |
int calcOverallLastNum(int n) | |
{ | |
int i, half_last = 0; | |
for (i = 0; i < n; i++) | |
half_last += (n - i); | |
return (2 * half_last); | |
} | |
int main() | |
{ | |
int i, n; | |
scanf("%d", &n); | |
int start = 1; | |
int end = calcOverallLastNum(n); | |
int mid = end - n + 1; | |
for (i = 0; i < n; i++) | |
{ | |
printPattern(i, start, mid, n); | |
start += (n - i); | |
mid -= (n - i - 1); | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment