Question

In: Computer Science

Three Patterns Given two numbers N and K, output three squares with the size N ×...

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

Solutions

Expert Solution

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:


Related Solutions

THIS IS JAVA Magic squares. An n × n matrix that is filled with the numbers...
THIS IS JAVA Magic squares. An n × n matrix that is filled with the numbers 1, 2, 3, . . ., n^2 is a magic square if the sum of the elements in each row, in each column, and in the two diagonals is the same value. Write a program that randomly generates 16 numbers, and it assigns them to the array after testing that the number was not already assigned. The program should test whether they form a...
For each given integer n, determine if n is a sum of two squares. (You do...
For each given integer n, determine if n is a sum of two squares. (You do not have to find the squares.) (a) n = 19 (b) n = 45 (c) n = 99 (d) n = 999 (e) n = 1000
For each given integer n, determine if n is a sum of two squares. (You do...
For each given integer n, determine if n is a sum of two squares. (You do not have to find the squares.) (a)n= 19 (b)n= 45 (c)n= 99 (d)n= 999 (e)n=1000
There are at most two squares (not necessarily with the same size) such that the sum...
There are at most two squares (not necessarily with the same size) such that the sum of the area(s) is 8 in^2 . Maximize and minimize the sum of the perimeter(s)?
Given an array A[1..n], with distinct values and k with 1 ≤ k ≤ n. We...
Given an array A[1..n], with distinct values and k with 1 ≤ k ≤ n. We want to return the k smallest element of A[1.....n], in non-decreasing order. For example: A = [5, 4, 6, 2, 10] and k = 4, the algorithm returns [2, 4, 5, 6]. There are at least the following four approaches: a. heapify A and then extract k elements one by one b. sort the array (e.g. using MergeSort or HeapSort) and then read the...
For a given positive integer n, output the first n primes. Example: n=3, output: 2,3,5; n=7,...
For a given positive integer n, output the first n primes. Example: n=3, output: 2,3,5; n=7, output: 2,3,5,7,11,13,17. In Java please
1. Prove that given n + 1 natural numbers, there are always two of them such...
1. Prove that given n + 1 natural numbers, there are always two of them such that their difference is a multiple of n. 2. Prove that there is a natural number composed with the digits 0 and 5 and divisible by 2018. both questions can be solved using pigeonhole principle.
in javascript OR JAVA You are given two numbers n and m representing the dimensions of...
in javascript OR JAVA You are given two numbers n and m representing the dimensions of an n × m rectangular board. The rows of the board are numbered from 1 to n, and the columns are numbered from 1 to m. Each cell has a value equal to the product of its row index and column index (both 1-based); in other words, board[i][j] = (i + 1) * (j + 1). Initially, all the cells in the board are...
Two of the three electrons in a lithium atom have quantum numbers of n = 1,...
Two of the three electrons in a lithium atom have quantum numbers of n = 1, l = 0, ml = 0, ms = +1/2 and n = 1, l = 0, ml = 0, ms = -1/2. What quantum numbers can the third electron have if the atom is in (a) its ground state and (b) its first excited state?
Given an array A of n distinct real numbers, we say that a pair of numbers...
Given an array A of n distinct real numbers, we say that a pair of numbers i, j ∈ {0, . . . , n−1} form an inversion of A if i < j and A[i] > A[j]. Let inv(A) = {(i, j) | i < j and A[i] > A[j]}. Answer the following: (a) How small can the number of inversions be? Give an example of an array of length n with the smallest possible number of inversions. (b)...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT