Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Develop and implement a program that checks if an integer input is in the range

ID: 3801735 • Letter: D

Question

Develop and implement a program that checks if an integer input is in the range between 10 and 31 inclusively. After receiving a valid integer, the program uses it as the height to print a tree-style pattern as shown below in the screenshot. Enter a number between 10 and 31: 9 Enter a number between 10 and 31: 20 When the input is n, 10 lessthanorequalto n lessthanorequalto 31, the first line of the pattern always starts with 1, preceded by n^-1 spaces. The kth line of the pattern, 1 lessthanorequalto k lessthanorequalto n. starts with n - k spaces, followed by a sequence of characters for numbers beginning from k. In the kth line, the number increases one by one until the middle of the line and then decreases one by one until returning to k. The character to display for a number is the number itself if it is less than ten. Otherwise, a character between 'a' and 'z' is used for a number in the range from 10 to 35, and between 'A' and 'Z' for a number in the range from 36 to 61 correspondingly. For better practice of programming, the program cannot use any global variables. For the purpose of exercise and learning, the program must, use a recursive function. The program needs to produce correct results and is easy to understand.

Explanation / Answer

#include<stdio.h>
//Main function definition
int main()
{
//n for number of rows
int n, r, c, k, sp, res, res1;
//Loops till number entered by the use is not between 10 and 31
do
{
//Accept a number for number of rows
printf(" Enter an number between 10 and 31: ");
scanf("%d", &n);
//Checks for the number is less than 10 then continue to ask number
if(n < 10)
continue;
//Checks for the number is greater than 31 then continue to ask number
else if(n > 31)
continue;
//Otherwise stop asking the number
else
break;
}while(1);
//Loops till the number of rows
for(r = 1; r < n; r++)
{
//Initializes the res to row value and re1 (for character form) to 86 + row value
res = r; res1 = 86+r;
//Displays space
for(sp = (n * 2 - r); sp > 0; sp--)
printf(" ");
//For column iteration
for(c = 1; c <= r; c++)
{
//If res is greater than 9 than print the character form
if(res > 9)
{
printf("%c", (res1+c));
res++;
}
//Otherwise display the integer form
else
printf("%d", res++);
}
res -=2;
res1 += (c-1);
//For reverse display of data
for(k = 1; k < r; k++)
{
//If res is greater than 9 than print the character form
if(res > 9)
{
printf("%c", (res1-k));
res--;
}
//Otherwise display the integer form
else
printf("%d", res--);
}
//For next line
printf(" ");
}//End of row loop
}//End of main

Output 1:

Enter an number between 10 and 31: 14
1
232
34543
4567654
567898765
6789aba9876
789abcdcba987
89abcdefedcba98
9abcdefghgfedcba9
abcdefghijihgfedcba
bcdefghijklkjihgfedcb
cdefghijklmnmlkjihgfedc
defghijklmnoponmlkjihgfed

Output 2:

Enter an number between 10 and 31: 16
1
232
34543
4567654
567898765
6789aba9876
789abcdcba987
89abcdefedcba98
9abcdefghgfedcba9
abcdefghijihgfedcba
bcdefghijklkjihgfedcb
cdefghijklmnmlkjihgfedc
defghijklmnoponmlkjihgfed
efghijklmnopqrqponmlkjihgfe
fghijklmnopqrstsrqponmlkjihgf