Printing Pattern Using Loops in C HackerRank Solution
Hello Friends in this article i am gone to share Hackerrank C programming Solutions with you. | Printing Pattern Using Loops in C Hackerrank Solution.
Also visit: Bitwise Operators – Solution
Objective
Print a pattern of numbers from to as shown below. Each of the numbers is separated by a single space.
4 4 4 4 4 4 4 4 3 3 3 3 3 4 4 3 2 2 2 3 4 4 3 2 1 2 3 4 4 3 2 2 2 3 4 4 3 3 3 3 3 4 4 4 4 4 4 4 4
Input Format
The input will contain a single integer n.
Constraints
1<n<1000
Sample Input 0
2
Sample Output 0
2 2 2 2 1 2 2 2 2
Sample Input 1
5
Sample Output 1
5 5 5 5 5 5 5 5 5 5 4 4 4 4 4 4 4 5 5 4 3 3 3 3 3 4 5 5 4 3 2 2 2 3 4 5 5 4 3 2 1 2 3 4 5 5 4 3 2 2 2 3 4 5 5 4 3 3 3 3 3 4 5 5 4 4 4 4 4 4 4 5 5 5 5 5 5 5 5 5 5
Sample Input 2
7
Sample Output 2
7 7 7 7 7 7 7 7 7 7 7 7 7 7 6 6 6 6 6 6 6 6 6 6 6 7 7 6 5 5 5 5 5 5 5 5 5 6 7 7 6 5 4 4 4 4 4 4 4 5 6 7 7 6 5 4 3 3 3 3 3 4 5 6 7 7 6 5 4 3 2 2 2 3 4 5 6 7 7 6 5 4 3 2 1 2 3 4 5 6 7 7 6 5 4 3 2 2 2 3 4 5 6 7 7 6 5 4 3 3 3 3 3 4 5 6 7 7 6 5 4 4 4 4 4 4 4 5 6 7 7 6 5 5 5 5 5 5 5 5 5 6 7 7 6 6 6 6 6 6 6 6 6 6 6 7 7 7 7 7 7 7 7 7 7 7 7 7 7
Hackerrank C programming Solutions
Printing Pattern Using Loops in C Hackerrank Solution -1
#include <stdio.h>
int main()
{
int n;
scanf("%d", &n);
// Complete the code to print the pattern.
int len = n*2 - 1;
for(int i=0;i<len;i++){
for(int j=0;j<len;j++){
int min = i < j ? i : j;
min = min < len-i ? min : len-i-1;
min = min < len-j-1 ? min : len-j-1;
printf("%d ", n-min);
}
printf("\n");
}
return 0;
}
Printing Pattern Using Loops in C Hackerrank Solution -2
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main()
{
int n;
scanf(“%d”, &n);
for(int i = n; i >= 1; i–){
for(int j = n; j > i; j–)
printf(“%d “, j);
for(int j = 1; j <= 2 * i – 1; j++)
printf(“%d “, i);
for(int j = i + 1; j <= n; j++)
printf(“%d “, j);
printf(“\n”);
}
for(int i = 1; i < n; i++){
for(int j = n; j > i; j–)
printf(“%d “, j);
for(int j = 1; j <= 2 * i – 1; j++)
printf(“%d “, i + 1);
for(int j = i + 1; j <= n; j++)
printf(“%d “, j);
printf(“\n”);
}
return 0;
}
Printing Pattern Using Loops in C Hackerrank Solution -3
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main()
{
int n;
scanf(“%d”, &n);
for(int i = n; i >= 1; i–){
for(int j = n; j > i; j–)
printf(“%d “, j);
for(int j = 1; j <= 2 * i – 1; j++)
printf(“%d “, i);
for(int j = i + 1; j <= n; j++)
printf(“%d “, j);
printf(“\n”);
}
for(int i = 1; i < n; i++){
for(int j = n; j > i; j–)
printf(“%d “, j);
for(int j = 1; j <= 2 * i – 1; j++)
printf(“%d “, i + 1);
for(int j = i + 1; j <= n; j++)
printf(“%d “, j);
printf(“\n”);
}
return 0;
}
I hope this article help you , for more C programming Hackerrank Solution follow my website and this link..
Link update Soon: