This C program prints the square and cube of every two-digit odd number — that is, the odd numbers from 11 to 99. A number is odd if it is not divisible by 2 (n % 2 != 0). Two-digit odd numbers begin at 11 (the smallest two-digit odd) and end at 99.
The original post had only a GitHub Gist shortcode — no actual code in the page. This rewrite provides the complete program, a formula reference, and a partial output table.
Formulas
| Value | Formula | Example (n=11) | Example (n=99) |
|---|---|---|---|
| Square | n × n = n² | 11 × 11 = 121 | 99 × 99 = 9801 |
| Cube | n × n × n = n³ | 11 × 11 × 11 = 1331 | 99 × 99 × 99 = 970299 |
C Program: Squares and Cubes of Two-Digit Odd Numbers
/* Squares and cubes of all two-digit odd numbers (11–99)
* Compile: gcc -ansi -Wall -Wextra squarescubes.c -o squarescubes */
#include <stdio.h>
int main(void)
{
int n;
printf("%-8s %-10s %-12s\n", "Number", "Square", "Cube");
printf("%-8s %-10s %-12s\n", "------", "------", "----");
for (n = 11; n <= 99; n += 2) {
printf("%-8d %-10d %-12d\n", n, n * n, n * n * n);
}
return 0;
}
How to Compile and Run
gcc -ansi -Wall -Wextra squarescubes.c -o squarescubes
./squarescubes
Sample Output (first 10 rows)
Number Square Cube ------ ------ ---- 11 121 1331 13 169 2197 15 225 3375 17 289 4913 19 361 6859 21 441 9261 23 529 12167 25 625 15625 27 729 19683 29 841 24389 ... 99 9801 970299
Code Explanation
for (n = 11; n <= 99; n += 2)— starts at 11 (the first two-digit odd) and steps by 2 each iteration, which always produces odd numbers. Stepping by 2 from any odd number gives the next odd number: 11→13→15→…→99. There are 45 two-digit odd numbers in total.n * nfor square,n * n * nfor cube — direct multiplication in C. The compiler optimizes these with no performance concern for small n. An alternative is to computesq = n * nonce and reuse it assq * nfor the cube.%-8d %-10d %-12dformatting — the minus sign left-aligns within the field width. This produces aligned columns. Without it, numbers are right-aligned (default): 8-character column for the number, 10-character for the square, 12-character for the cube (needed for 99³ = 970299, six digits).- Why start at 11, not 1? — Two-digit numbers are 10–99. The number 10 is even, so the first two-digit odd number is 11. Single-digit odd numbers (1, 3, 5, 7, 9) are excluded.
Variation: Check a Single Number
To check one specific number entered by the user:
int main(void)
{
int n;
printf("Enter a number: ");
scanf("%d", &n);
if (n >= 11 && n <= 99 && n % 2 != 0)
printf("%d: square=%d, cube=%d\n", n, n*n, n*n*n);
else
printf("%d is not a two-digit odd number.\n", n);
return 0;
}
What This Program Teaches
- Stepping a for loop by 2 —
n += 2is a compact way to iterate over all odd (or all even) numbers in a range. It’s twice as fast as checkingn % 2 != 0inside a loop that steps by 1. - Column-aligned output with printf — the
%-Ndformat (negative width = left-align) makes tabular output readable. This is the manual equivalent of using tabs, but with controlled column widths. - Integer arithmetic for power — in C,
pow(n, 3)works but returns double. For integer exponents, direct multiplication (n*n*n) is more accurate (no floating-point rounding) and does not require linking-lm. - Two-digit number range — the numbers 10–99 form the “two-digit” set. Starting at 11 and stepping by 2 generates exactly the two-digit odd subset without any extra checks inside the loop.
Related Programs
- Even Numbers Square and Sum in C
- Armstrong Number in C
- Perfect Number in C
- Strong Number in C
- Floyd’s Triangle in C
Recommended book:
The C Programming Language — Kernighan & Ritchie (India) |
(US)
|
C Programming: A Modern Approach — K.N. King (India) |
(US)
Practice what you learned: C Aptitude Questions — or try our C Programming Quiz App on Android.