Algorithm
Problem link- https://www.spoj.com/problems/CPTTRN1/
CPTTRN1 - Character Patterns (Act 1)
Using two characters: . (dot) and * (asterisk) print a chessboard-like pattern. The first character printed should be * (asterisk).
Input
You are given t < 100 - the number of test cases and for each of the test cases two positive integers: l - the number of lines and c - the number of columns in the pattern (l, c < 100).
Output
For each of the test cases output the requested pattern (please have a look at the example). Use one line break in between successive patterns.
Example
Input: 3 3 1 4 4 2 5 Output: * . * *.*. .*.* *.*. .*.* *.*.* .*.*.
Code Examples
#1 Code Example with C Programming
Code -
C Programming
#include<stdio.h>
int main()
{
int t,i,j,x,y,z;
scanf("%d",&t);
for(i=1;i<=t;i++)
{
scanf("%d%d",&x,&y);
for(j=1;j<=x;j++)
{
for(z=1;z<=y;z++)
{
if((j+z)%2==0)
{
printf("*");
}
else
{
printf(".");
}
}
printf("\n");
}
printf("\n");
}
return 0;
}
Copy The Code &
Try With Live Editor
Input
3 1
4 4
2 5
Output
.
*
*.*.
.*.*
*.*.
.*.*
*.*.*
.*.*.
#2 Code Example with Python Programming
Code -
Python Programming
def main():
t = int(input())
while t:
m = list(input().split())
l = int(m[0])
c = int(m[1])
pos = 0
for j in range(l):
for i in range(c):
if pos % 2 == 0:
print ('*', end='')
pos += 1
else:
print('.', end='')
pos += 1
if c % 2 == 0:
pos += 1
print()
t -= 1
if __name__ == '__main__':
main()
Copy The Code &
Try With Live Editor
Input
3 1
4 4
2 5
Output
.
*
*.*.
.*.*
*.*.
.*.*
*.*.*
.*.*.
Demonstration
SPOJ Solution-Character Patterns (Act 1)-Solution in C, C++, Java, Python