Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 months ago.
Improve this question
#include <stdio.h>
#include <math.h>
int main(void)
{
// input value
int num, lower, upper;
double squareroot;
int square;
int cube;
printf("enter your number:\n");
scanf_s("%d", &num);
do
{
printf("the lower value limit is ");
scanf_s("%d", &lower);
} while (lower < 0 || lower > 50);
do
{
printf("the upper value limit is ");
scanf_s("%d", &upper);
} while (upper < 0 || upper > 50);
// the formular to find the squareroot, square, cube
squareroot = sqrt(num);
square = num * num;
cube = num * num * num;
//a for loop
for (num = 0; num <= upper; num++) {
printf("*base number* || *square root* || *square* || *cube*\n");
printf("*%d* || *%f* || *%ld* || *%ld*\n",
lower, squareroot, square, cube);
}
return 0;
}
i try to make a table to display the base number, square root, square, and cube
and set a limit for the table.
for example, if I input the lower number is 1 and the upper number is 5 then the table will stop at 5 then display the square root, square, and cube
At least this problem:
Mismatched specifiers/type
int square;
int cube;
...
printf("*%d* || *%f* || *%ld* || *%ld*\n",
lower, squareroot, square, cube);
Use "%d" with int, not "%ld".
Move assignments
Following assignments need to be inside the loop.
for (num = 0; num <= upper; num++) {
square = num * num;
cube = num * num * num;
Save time. Enable all compiler warnings
Try the Below Code Make all Changes I have added Comments to Clarify why I made The
#include <stdio.h>
#include <math.h>
int main(void)
{
// input value
int lower, upper;
double squareroot;
int square;
int cube;
//Read the Limits First
printf("Enter the Lower Limit: ");
scanf("%d", &lower);
printf("Enter the Upper Limit: ");
scanf("%d", &upper);
//Instead of Declaring an Entire Loop You Can Just Use an If Statement this Reduces Code Complexity
//You can also Set Your Limits
if(upper > 0 && upper < 50 && lower > 0 && lower < 50)
{
//THen Enter Actual Code
//Also Dont Set Lower to 0 It will Change Your Actual Value Instead Take another Loop Var
for (int i = lower; i <= upper; i++)
{
//Then Perform all Functions on i
squareroot = sqrt(i);
square = i * i;
cube = i * i * i;
printf("*base number* || *square root* || *square* || *cube*\n");
printf("*%d* || *%f* || *%d* || *%d*\n", i, squareroot, square, cube);
}
}
return 0;
}
Several issues:
You go to the trouble of asking for lower, but you don't use it to control your loop - your loop should befor( int num = lower; num <= upper; num++)
{
...
}
Put another way, if you always intend for your loop to start from zero, then you don't need lower at all.
You need compute the square root, square, and cube of num for each iteration of the loop. Instead, you're doing it once before the loop and just printing those same values over and over again;
You only need to print your table header once, outside the body of the loop;
You can compute your square root, square, and cube all as part of the printf statement:
printf( "%d %f %d %d\n", num, sqrt((double) num), num * num, num * num * num );
Field width specifiers are your friends - you can tell printf exactly how wide you want each column to be. Example:
printf( "%6d%12.2f%12d%12d", num, sqrt((double) num), num * num, num * num * num );
This means the column for num is 6 characters wide, the column for square root is 12 characters wide, with 3 characters reserved for the decimal point and two following digits, and the columns for square and cube are 12 characters wide. Example:
printf( "%6s%12s%12s%12s\n", "base", "root", "square", "cube" );
printf( "%6s%12s%12s%12s\n", "----", "----", "------", "----" );
for (int i = lower; i <= upper; i++ )
printf( "%6d%12.2f%12d%12d\n", i, sqrt( (double) i ), i*i, i*i*i );
Which gives output like this (lower == 1, upper == 10):
base root square cube
---- ---- ------ ----
1 1.00 1 1
2 1.41 4 8
3 1.73 9 27
4 2.00 16 64
5 2.24 25 125
6 2.45 36 216
7 2.65 49 343
8 2.83 64 512
9 3.00 81 729
10 3.16 100 1000
Full example:
#include <stdio.h>
#include <math.h>
int main( void )
{
int lower = 0, upper = 0;
printf( "Gimme a lower value: " );
while ( scanf( "%d", &lower ) != 1 || (lower < 0 || lower > 50 ))
{
/**
* Clear any non-numeric characters from the input stream
*/
while ( getchar() != '\n' )
; // empty loop
printf( "Nope, try again: " );
}
printf( "Gimme an upper value: " );
while ( scanf( "%d", &upper ) != 1 || (upper < lower || upper > 50 ))
{
while( getchar() != '\n' )
; // empty loop
printf( "Nope, try again: " );
}
printf( "%6s%12s%12s%12s\n", "base", "root", "square", "cube" );
printf( "%6s%12s%12s%12s\n", "----", "----", "------", "----" );
for (int i = lower; i <= upper; i++ )
printf( "%6d%12.2f%12d%12d\n", i, sqrt( (double) i ), i*i, i*i*i );
return 0;
}
I've written the input section such that it will reject inputs like foo or a123. It will not properly handle inputs like 12w4, but that would make the example more complicated than it needs to be (you're not asking about input validation, you're asking about computation and formatting). Example run:
$ ./table
Gimme a lower value: foo
Nope, try again: a123
Nope, try again: 123
Nope, try again: 1
Gimme an upper value: 100
Nope, try again: 50
base root square cube
---- ---- ------ ----
1 1.00 1 1
2 1.41 4 8
3 1.73 9 27
4 2.00 16 64
5 2.24 25 125
6 2.45 36 216
7 2.65 49 343
8 2.83 64 512
9 3.00 81 729
10 3.16 100 1000
11 3.32 121 1331
12 3.46 144 1728
13 3.61 169 2197
14 3.74 196 2744
15 3.87 225 3375
16 4.00 256 4096
17 4.12 289 4913
18 4.24 324 5832
19 4.36 361 6859
20 4.47 400 8000
21 4.58 441 9261
22 4.69 484 10648
23 4.80 529 12167
24 4.90 576 13824
25 5.00 625 15625
26 5.10 676 17576
27 5.20 729 19683
28 5.29 784 21952
29 5.39 841 24389
30 5.48 900 27000
31 5.57 961 29791
32 5.66 1024 32768
33 5.74 1089 35937
34 5.83 1156 39304
35 5.92 1225 42875
36 6.00 1296 46656
37 6.08 1369 50653
38 6.16 1444 54872
39 6.24 1521 59319
40 6.32 1600 64000
41 6.40 1681 68921
42 6.48 1764 74088
43 6.56 1849 79507
44 6.63 1936 85184
45 6.71 2025 91125
46 6.78 2116 97336
47 6.86 2209 103823
48 6.93 2304 110592
49 7.00 2401 117649
50 7.07 2500 125000
Related
I have created a program to search for prime numbers. It works without problems until the entered number is smaller than 52, when it is bigger output prints out some blank (0) numbers and I don't know why. Also other numbers have blank output.
My code is:
#include <stdio.h> //Prime numbers
#include <stdlib.h>
#include <math.h>
#include <string.h>
#include <unistd.h>
int c[100], n, a[50], d, e, b = 1;
void sort() {
for (int i = 1; i < n; i++) {
if (c[i] > 1) {
a[b] = c[i];
printf("%d %d %d\n", a[1], b, i);
b++;
e = 2;
d = 0;
while (d <= n) {
d = c[i] * e;
c[d - 1] = 0;
e++;
}
}
}
}
int main() {
printf("Enter number as an limit:\n");
scanf("%d", &n);
for (int i = 0; i < n; i++) {
c[i] = i + 1;
}
sort();
printf("Prime numbers between 1 and %d are:\n", n);
for (int i = 1; i < b; i++) {
printf("%d ", a[i]);
}
return 0;
}
Here is output for 25:
Enter number as an limit:
25
2 1 1
2 2 2
2 3 4
2 4 6
2 5 10
2 6 12
2 7 16
2 8 18
2 9 22
Prime numbers between 1 and 25 are:
2 3 5 7 11 13 17 19 23
But for 83 is:
Enter number as an limit:
83
2 1 1
2 2 2
2 3 4
2 4 6
2 5 10
2 6 12
2 7 16
2 8 18
2 9 22
2 10 28
2 11 30
2 12 36
2 13 40
2 14 42
2 15 46
2 16 52
0 17 58
0 18 60
0 19 66
0 20 70
0 21 72
0 22 78
0 23 82
Prime numbers between 1 and 83 are:
0 3 5 7 11 0 17 19 23 29 31 37 0 43 47 53 0 61 67 71 73 79 83
Blank spots always spots after 17th prime number. And always the blank numbers are the same. Can you help me please what is the problem?
The loop setting entries in c for multiples of c[i] runs too far: you should compute the next d before comparing against n:
for (d = c[i] * 2; d <= n; d += c[i]) {
c[d - 1] = 0;
}
As a matter of fact you could start at d = c[i] * c[i] because all lower multiples have already been seen during the previous iterations of the outer loop.
Also note that it is confusing to store i + 1 into c[i]: the code would be simpler with an array of booleans holding 1 for prime numbers and 0 for composite.
Here is a modified version:
#include <stdio.h>
int main() {
unsigned char c[101];
int a[50];
int n, b = 0;
printf("Enter number as a limit:\n");
if (scanf("%d", &n) != 1 || n < 0 || n > 100) {
printf("invalid input\n");
return 1;
}
for (int i = 0; i < n; i++) {
c[i] = 1;
}
for (int i = 2; i < n; i++) {
if (c[i] != 0) {
a[b] = i;
//printf("%d %d %d\n", a[0], b, i);
b++;
for (int d = i * i; d <= n; d += i) {
c[d] = 0;
}
}
}
printf("Prime numbers between 1 and %d are:\n", n);
for (int i = 0; i < b; i++) {
printf("%d ", a[i]);
}
printf("\n");
return 0;
}
Output:
chqrlie$ ./sieve4780
Enter number as a limit:
25
Prime numbers between 1 and 25 are:
2 3 5 7 11 13 17 19 23
chqrlie$ ./sieve4780
Enter number as a limit:
83
Prime numbers between 1 and 83 are:
2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79
Your problem seems to be caused by the fact that you have declared an array with size 50, but in fact it goes further than that: imagine you want to use Eratosthenes' procedure to find the first 10,000 prime numbers. Does this mean that you need to declare an array of size 10,000 first (or even bigger), risking to blow up your memory?
No: best thing to do is to work with collections where you don't need to set the maximum size at declaration time, like a linked list, a vector, ..., like that you can make your list grow as much as you like during runtime.
Have tried few basic pattern
trying to get pattern
1
2 4
3 6 12
4 8 16 32
SO far trying to find the proper sequence, my idea is that need another variable lets say num, and need to create a sequence for num to print num eventually
#include <stdio.h>
int main()
{
int rows = 0 , i, j , num,num2;
do{
printf("please enter the number of rows: ");
scanf("%d",&rows);
}while(rows <=2 );
printf("printing a half pyramid of %d rows", rows);
printf("\n");
for( i = 1; i <=rows; ++i) {
for (j = 1; j <= i; ++j ) {
printf("%d ", );
}
printf("\n");
}
return 0;
}
Not being able to figure out a sequence
The code you were given literally contains all the parts necessary. All that remains for you is to fill out this line inside the nested loop:
printf("%d ", ‹what goes here?›);
To find the answer you need to find how the value relates to the current row and column (give by i and j, respectively).
You don’t need an additional variable num (to be clear, you can create one, but it’s not necessary to solve this problem).
We, beginners, should help each other.:)
Here you are.
#include <stdio.h>
int main(void)
{
while ( 1 )
{
const unsigned int Base = 10;
printf( "Enter the height of a pyramid (0 - exit): " );
unsigned int n;
if ( ( scanf( "%u", &n ) != 1 ) || ( n == 0 ) ) break;
int width = 0;
unsigned int tmp = n * n;
do { ++width; } while ( tmp /= Base );
putchar( '\n' );
for ( unsigned int i = 0; i < n; i++ )
{
unsigned int value = i + 1;
for ( unsigned int j = 0; j++ <= i; )
{
printf( "%*u ", width, value * j );
}
putchar( '\n' );
}
putchar( '\n' );
}
return 0;
}
The program output might look like
Enter the height of a pyramid (0 - exit): 1
1
Enter the height of a pyramid (0 - exit): 2
1
2 4
Enter the height of a pyramid (0 - exit): 3
1
2 4
3 6 9
Enter the height of a pyramid (0 - exit): 4
1
2 4
3 6 9
4 8 12 16
Enter the height of a pyramid (0 - exit): 5
1
2 4
3 6 9
4 8 12 16
5 10 15 20 25
Enter the height of a pyramid (0 - exit): 6
1
2 4
3 6 9
4 8 12 16
5 10 15 20 25
6 12 18 24 30 36
Enter the height of a pyramid (0 - exit): 7
1
2 4
3 6 9
4 8 12 16
5 10 15 20 25
6 12 18 24 30 36
7 14 21 28 35 42 49
Enter the height of a pyramid (0 - exit): 8
1
2 4
3 6 9
4 8 12 16
5 10 15 20 25
6 12 18 24 30 36
7 14 21 28 35 42 49
8 16 24 32 40 48 56 64
Enter the height of a pyramid (0 - exit): 9
1
2 4
3 6 9
4 8 12 16
5 10 15 20 25
6 12 18 24 30 36
7 14 21 28 35 42 49
8 16 24 32 40 48 56 64
9 18 27 36 45 54 63 72 81
Enter the height of a pyramid (0 - exit): 0
The loops in the program can also look like
for ( unsigned int i = 0; i++ < n; )
{
unsigned int value = i;
for ( unsigned int j = 0; j < i; j++ )
{
printf( "%*u ", width, value );
value += i;
}
putchar( '\n' );
}
or without introducing the intermediate variable value like
for ( unsigned int i = 0; i < n; i++ )
{
for ( unsigned int j = 0; j++ <= i; )
{
printf( "%*u ", width, j * ( i + 1 ) );
}
putchar( '\n' );
}
You yourself can add a check to the program that n * n is not greater than UINT_MAX.
Edit: As you changed the displayed values in the pattern then the program can look for example the following way
#include <stdio.h>
#include <math.h>
int main(void)
{
while ( 1 )
{
const unsigned int Base = 10;
printf( "Enter the height of a pyramid (0 - exit): " );
unsigned int n;
if ( ( scanf( "%u", &n ) != 1 ) || ( n == 0 ) ) break;
int width = 0;
unsigned long long int tmp = n * ( long long unsigned )pow( 2, ( n - 1 ) );
do { ++width; } while ( tmp /= Base );
putchar( '\n' );
for ( unsigned int i = 0; i++ < n; )
{
unsigned int value = i;
for ( unsigned int j = 0; j < i; j++ )
{
printf( "%*u ", width, value );
value *= 2;
}
putchar( '\n' );
}
putchar( '\n' );
}
return 0;
}
The program output might look like
Enter the height of a pyramid (0 - exit): 10
1
2 4
3 6 12
4 8 16 32
5 10 20 40 80
6 12 24 48 96 192
7 14 28 56 112 224 448
8 16 32 64 128 256 512 1024
9 18 36 72 144 288 576 1152 2304
10 20 40 80 160 320 640 1280 2560 5120
Enter the height of a pyramid (0 - exit): 0
Tricky Pattern. Here, is a logic for that pattern with implementation.
'n' is the number of rows.
#include <stdio.h>
int main(void) {
int n = 4;
for(int i=1; i<=n; i++) {
int k=i;
printf("%d%s",k," ");
for(int j=1; j<i; j++) {
k = k*2;
printf("%d%s",k," ");
}
printf("\n");
}
return 0;
}
The spacing is off in my code, can anyone help. I have attempted it (shown below)
#include <stdio.h>
int factorial(int n){
int fact = 1;
if(n == 0){
return 1;
} else {
for(int i = 1; i <= n; i++){
fact = fact * i;
}
return fact;
}
}
int choose(int n, int r)
{
int ans;
ans = (factorial(n))/((factorial(r))*(factorial(n-r)));
return ans;
}
void triangle(int numOfRows){
for(int n=0; n<numOfRows; n++)
{
for(int i=1; i<=numOfRows-n; i++){
printf(" "); // Note the extra space
}
for(int r=0; r<=n; r++)
{
printf("%5d ",choose(n,r)); // Changed to %3d
}
printf("\n");
}
}
int main(){
int rows;
printf("Enter the number of rows: ");
scanf("%d", &rows);
while(rows > 0 && rows <=13){
triangle(rows);
printf("Enter the number of rows: ");
scanf("%d", &rows);
}
return 0;
}
The expected output should be:
Thanks i'd appreciate it (this is also my first time using this site, so sorry for bad format stuff).
The program needs to work up to 13 rows (which is shown in my while loop in my main functions).
You need to make a couple of changes to have the triangle aligned to the left as in the expected output.
First, you are adding 3 extra spaces in the first loop with the printf(" "), that is fixed using < instead of <= in the loop condition.
Second, there are 4 extra chars added due to the "%5d " in the second printf call, you need to avoid that for the first iteration (when r == 0) using just "%d ".
Here's how the triangle() function will look like after the changes:
void triangle(int numOfRows) {
for(int n = 0; n < numOfRows; n++) {
for(int i = 1; i < numOfRows-n; i++) {
printf(" ");
}
for(int r = 0; r <= n; r++) {
printf(r == 0 ? "%d " : "%5d ", choose(n, r));
}
printf("\n");
}
}
And some example output (works up to 13 without a problem, at least on my 64-bit Linux with both gcc and clang):
Enter the number of rows: 3
1
1 1
1 2 1
Enter the number of rows: 4
1
1 1
1 2 1
1 3 3 1
Enter the number of rows: 5
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
Enter the number of rows: 13
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
1 6 15 20 15 6 1
1 7 21 35 35 21 7 1
1 8 28 56 70 56 28 8 1
1 9 36 84 126 126 84 36 9 1
1 10 45 120 210 252 210 120 45 10 1
1 11 55 165 330 462 462 330 165 55 11 1
1 12 66 220 495 792 924 792 495 220 66 12 1
So this is my code for printing pascal triangle using 2d arrays but its not giving me the desired output and I cannot determine what's wrong with the logic/code.
#include <stdio.h>
int main()
{
int num, rows, col, k;
printf("Enter the number of rows of pascal triangle you want:");
scanf("%d", &num);
long a[100][100];
for (rows = 0; rows < num; rows++)
{
for (col = 0; col < (num - rows - 1); col++)
printf(" ");
for (k = 0; k <= rows; k++)
{
if (k == 0 || k == rows)
{
a[rows][k] = 1;
printf("%ld", a[rows][k]);
}
else
a[rows][k] = (a[rows - 1][k - 1]) + (a[rows - 1][k]);
printf("%ld", a[rows][k]);
}
printf("\n");
}
return 0;
}
You don't have curly braces around the statements after the else, so it looks like you'll double-printf() when the condition of the if-statement is true.
I copied the source into codechef.com/ide and changed the io for num to be just assigned to 6 which produced the following output:
Enter the number of rows of pascal triangle you want:
11
1111
11211
113311
1146411
1151010511
It looks like your close, but you want 1, 11, 121, 1331 etc right?
Wraping the else case produced the following output:
if (k == 0 || k == rows)
{
a[rows][k] = 1;
printf("(%ld)", a[rows][k]);
}
else{// START OF BLOCK HERE
a[rows][k] = (a[rows - 1][k - 1]) + (a[rows - 1][k]);
printf("(%ld)", a[rows][k]);
}//END OF BLOCK HERE, NOTE THAT IT INCLUDES THE PRINT IN THE ELSE CASE NOW
OUTPUT:
Enter the number of rows of pascal triangle you want:
(1)
(1)(1)
(1)(2)(1)
(1)(3)(3)(1)
(1)(4)(6)(4)(1)
(1)(5)(10)(10)(5)(1)
But i added () to make it clearer to me. I also added a "/n" to the end of the first printf that asks for the value of num, so that the first line is on a new line.
printf("Enter the number of rows of pascal triangle you want:\n");
You can do that without using any arrays:
#include <stdlib.h>
#include <stdio.h>
int num_digits(int number)
{
int digits = 0;
while (number) {
number /= 10;
++digits;
}
return digits;
}
unsigned max_pascal_value(int row)
{
int result = 1;
for (int num = row, denom = 1; num > denom; --num, ++denom)
result = (int)(result * (double)num / denom );
return result;
}
int main()
{
printf("Enter the number of rows of pascals triangle you want: ");
int rows;
if (scanf("%d", &rows) != 1) {
fputs("Input error. Expected an integer :(\n\n", stderr);
return EXIT_FAILURE;
}
int max_digits = num_digits(max_pascal_value(rows));
for (int i = 0; i <= rows; ++i) {
for (int k = 0; k < (rows - i) * max_digits / 2; ++k)
putchar(' ');
int previous = 1;
printf("%*i ", max_digits, previous);
for (int num = i, denom = 1; num; --num, ++denom) {
previous = (int)(previous * (double)num / denom );
printf("%*i ", max_digits, previous);
}
putchar('\n');
}
}
Output:
Enter the number of rows of pascals triangle you want: 15
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
1 6 15 20 15 6 1
1 7 21 35 35 21 7 1
1 8 28 56 70 56 28 8 1
1 9 36 84 126 126 84 36 9 1
1 10 45 120 210 252 210 120 45 10 1
1 11 55 165 330 462 462 330 165 55 11 1
1 12 66 220 495 792 924 792 495 220 66 12 1
1 13 78 286 715 1287 1716 1716 1287 715 286 78 13 1
1 14 91 364 1001 2002 3003 3432 3003 2002 1001 364 91 14 1
1 15 105 455 1365 3003 5005 6435 6435 5005 3003 1365 455 105 15 1
I need to write a program which prints the conversion table from feet and inches to centimetres. The numbers printed in row i (counting from zero), column j (counting from zero) of the table should be the cm equivalent of i feet and j inches. i should go from 0 to 7, and j from 0 to 11. Each column should be five characters wide, and the cm figures should be rounded to the nearest integer.
The example of required output is given below:
0 3 5 8 10 13
30 33 36 38 41
61 64 66 69 71
91 94 97 99 102
The code I have prints only one row of inches and column of feet but I don't know how to make into table without producing lots of irrelevant repetitions.
The code is:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main()
{
int i,j;
int cm,p;
for (i=0; i<= 11; i++) {
cm =round(i * 2.54);
printf ("%5d",cm);
}
for (j=0; j<=7; j++) {
p =round(j* 12.0 * 2.54);
printf ("%5d\n",p);
}
return 0;
}
This produces:
0 3 5 8 10 13 15 18 20 23 25 28 0
30
61
91
122
152
183
213
What am I doing wrong?
You have one loop after the other. What you need to do is run through the inches loop every iteration of your feet loop. What you get is nested loops:
#include <stdio.h>
int main()
{
for (int feet = 0; feet <= 7; ++feet) {
for (int inches = 0; inches < 12; ++inches) {
int microns = (feet * 12 + inches) * 25400;
int rounded_cm = (microns + 5000) / 10000;
printf("%5d", rounded_cm);
}
puts("");
}
}
I've made some other changes in my version; you're encouraged to study it and understand why it does what it does (read the man page for puts(), for example). Don't just copy it and hand it in - it will be obvious it isn't your code.
An alternative approach is to use a single loop (in inches), and insert a newline when we reach the 11th inch in each foot:
#include <stdio.h>
int main()
{
for (int i = 0; i < 96; ++i) {
printf("%4d%s",
(i * 25400 + 5000) / 10000,
i%12==11 ? "\n" : " ");
}
}
(You'll want to give meaningful names to your constants; the above is written in a "code-golf" style).
Whatever you do, don't be tempted to avoid multiplying by instead adding 2.54 repeatedly in the loop. Floating-point numbers are not exact, and addition will accumulate the error.
OP needs to put the "inches" loop inside the "foot" loop as well answered by others. #Toby Speight #VHS
Code could do its "round to nearest" via the printf() statement by using "%5.0f" to control the output width and rounding.
Let code use foot/inch instead of i/j #KevinDTimm for clarity.
#include <stdio.h>
#define INCH_PER_FOOT 12
#define CM_PER_INCH 2.54
int main(void) {
// go from 0 to 7, and ...
for (int foot = 0; foot <= 7; foot++) {
// from 0 to 11
// for (int inch = 0; inch < INCH_PER_FOOT; inch++) { is more idiomatic
for (int inch = 0; inch <= 11; inch++) {
printf("%5.0f", (foot * INCH_PER_FOOT + inch) * CM_PER_INCH);
}
puts("");
}
}
Output
0 3 5 8 10 13 15 18 20 23 25 28
...
213 216 218 221 224 226 229 231 234 236 239 241
You are running your loops backwards. First you need to run through feet and then through inches. But you are having it the other way round. Check the following snipped and compare it with your code and try to understand what's wrong.
#include <stdio.h>
#include <stdlib.h>
#include <math.h> // for rounding of a number
int main()
{
int i,j;
int cm,p;
for(i=0; i<=7;i++) {
for(j=0;j<=11;j++) {
cm = round(i*30.48 + j*2.54);
printf ("%5d",cm);
}
printf("\n");
}
return 0;
}