In: Computer Science
Three Patterns Given two numbers N and K, output three squares with the size N × N each with their own set of rules:
• The first square is made entirely using the ‘#’ symbol.
• The second square is made using the ‘.’ symbol except for every K rows use the ‘#’ symbol instead. • The third square is made using the ‘.’ symbol except for every K columns use the ‘#’ symbol instead.
Also print new line (\n) after printing each square. Look at sample input/output for more clarity.
Format Input The input consists of one line containing two numbers N and K.
Format Output The output consists of three squares with the size N × N according to the rules stated above. Don’t forget to print new line after printing each square!
Constraints
• 1 ≤ N, K ≤ 100
Sample Input 1 (standard input) :
5 2
Sample Output 1 (standard output):
#####
#####
#####
#####
#####
.....
#####
.....
#####
.....
.#.#.
.#.#.
.#.#.
.#.#.
.#.#.
Sample Input 2 (standard input):
9 3
Sample Output 2 (standard output):
#########
#########
#########
#########
#########
#########
#########
#########
#########
.........
.........
#########
.........
.........
#########
.........
.........
#########
..#..#..#
..#..#..#
..#..#..#
..#..#..#
..#..#..#
..#..#..#
..#..#..#
..#..#..#
..#..#..#
Sample Input 3 (standard input):
1 3
Sample Output 3 (standard output):
#
.
.
Note: Use long long int and c language
Below is the complete C code. If you face any difficulty while understanding the code, Please let me know in the comments.
Code:
#include <stdio.h>
int main() {
// declare variables and take input of N and K
long long int N,K;
scanf("%lld %lld", &N, &K);
// declare and initialize two variables to traverse the loop
long long int i = 1, j = 1;
// Print first square
for(i = 1 ; i <= N ; i++) {
for(j = 1 ; j <= N ; j++)
printf("#");
printf("\n");
}
printf("\n");
// Print second square
for(i = 1 ; i <= N ; i++) {
for(j = 1 ; j <= N ; j++)
if(i % K == 0)
printf("#");
else
printf(".");
printf("\n");
}
printf("\n");
// Print Third square
for(i = 1 ; i <= N ; i++) {
for(j = 1 ; j <= N ; j++)
if(j % K == 0)
printf("#");
else
printf(".");
printf("\n");
}
return 0;
}
Screenshots:
Output: