C Programming Question on Number Pattern

GDSC Submission for Competitive Coding Domain

I have selected the following problem for the domain of Competitive Programming:

click here

Language: C

The code I have written to solve the problem:

#include <stdio.h>

int digits(int x){

int count = 0;

while(x != 0){

x /=10;

count += 1;

}

x = count;

return x;

}

int main() {

int num, value, count, count_2, end;

scanf("%d", &num);

if(num>=1 && num<=1000){

count = digits(num);

int size = (2 * num) - 1;

for (int i = 1; i <= size; i++) {

for (int j = 1; j <= size; j++) {

if(i<j){

value = i;

}

else{

value = j;

}

if(value < (size -i) + 1){

}

else{

value = (size - i) + 1;

}

if(value < (size - j) + 1){

}

else{

value = (size - j) + 1;

}

end = num - value + 1;

printf("%d",end);

count_2 = digits(end);

if(count_2==1){

for(int k = 1; k <= count; k++){

printf(" ");

}}else{

for(int k = 2; k <= count; k++){

printf(" ");

}

}

}

printf("\n");

}

}

else{

printf("Invalid Input");

}

return 0;

}

Explanation:

The code works by using two for loops,

The first for loop represents the row number and the second defines the number that is to be printed.

In the second for loop I used an if statement to check the row number and the number to be printed by finding the minimum of the two and subtracting the min valur to get the number to be printed.

For example:

if(i<j){

value = i;

}

else{

value = j;

}

if(value < (size -i) + 1){

}

else{

value = (size - i) + 1;

}

if(value < (size - j) + 1){

}

else{

value = (size - j) + 1;

}

Here if i = 1 i.e the row number is 1 and j > i then it takes i as value,

Now, it subtracts value from ‘size’ which is the number of columns required to make a square pattern,

And now it checks whether value is less than (size - i + 1) which basically acts as a position identifier.

Now, it subtracts the variable ‘value’ from num and adds 1 to print according to the pattern its supposed to print in so since this is the first row the +1 makes it so that all the values printed are the user inputted ‘num’.

But there is a problem here as when the user inputs double digit integers the output becomes this:

To solve this, we have to give no.of spaces acc. to the no.of digits for the number inputted, I have used a recursion function here as it is easier since i have to find the no. of digits for more than one variable, it checks the no.of digits and prints the required no.of spaces to form the pattern of a square.

if(count_2==1){

for(int k = 1; k <= count; k++){

printf(" ");

}}else{

for(int k = 2; k <= count; k++){

printf(" ");

}

}

It checks the no.of digits of ‘num’ globally and then again checks the no.of digits of individual digits that are printed i.e also checks inside the loop, if no.of digits is one then it prints “ “ (space) for the no.of digits present in the ‘num’ and prints a “ “ one less for no. having the same no.of digits or more.

This ensures that a square pattern is formed as the two digit numbers take up more space and end up becoming bigger than the row having more single digit numbers.

When the first for loop ends it goes into the second loop and prints a \n (newline character) and the cycle repeats until both for loops are satisfied i.e i and j become greater than ‘size’ and the end result is a pattern of numbers printed in the shape of a square.

Result:

For single digit inputs:

For double digit inputs:

The code also works for higher digit numbers but since the output is so big the result, becomes jumbled instead of looking like a pattern but does work when given adequate space to position itself correctly.

Submission by

Lokdeep Adimulam

Btech CSE

I Semester - C

GITAM University, Visakhapatnam