#include<stdio.h>
int main()
{
int num = 4, row, col, space, count = 1;
float i = -7;
for(row = 1; row <= num; row++)
{
for(space = i; space <= (num-row); space++)
{
printf(" ");
}
for(col = 1; col <= row; col++)
{
printf(" %d ", count++);
}
i += 0.8;
printf("\n");
}
return 0;
}
Output
1
2 3
4 5 6
7 8 9 10
Logic To Draw Pyramid of Numbers, using For Loop
Here we already know that the Pyramid we need to print has 4 rows and is formed of numbers 1 to 10.
Outer for loop selects the row number and the number of elements to be printed for that particular selected row. For example, 1st row has 1 element. 2nd row has 2 elements. 3rd row has 3 elements and so on. So the row number and the number of elements in that particular row are always the same.
First inner for loop prints the adequate spacing required for the pyramid. Second inner for loop prints the actual numbers.
Logic To Display Right Angled Triangle With Alphabets, using For Loop
If we take a closer look at the right angled triangle, we can see that the number of rows and the number of elements in that row are same. Example, row number 5 has 5 elements. Row number 6 has 6 elements etc. So we store this number in a variable called row. Since row starts from 1, we initialize the variable row to 1.
Outer For Loop
We ask the user to enter the maximum number of rows for the right angled triangle, and store it in address of variable num. Outer for loop selects the row. Outer for loop executes until row value is less than or equal to the user entered number.
Inner For Loop
Inner For loop prints the Alphabets. Printing starts from 1st position. So we initialize variable col to 1. And inner for loop iterates until col is less than or equal to the value present in variable row.
Inside inner for loop we check if the value of variable count is 91(ASCII Value of Z is 91). If true, we resent the value of count to 65(ASCII Value of A is 65).
Note: Row number is equal to the number of elements present in that row.
Source Code: C Program To Display Right Angled Triangle With Alphabets, using For Loop
We ask the user to input the number of rows for Floyd’s Triangle, we store it inside variable num.
Outer For loop In our C program, row number and the total numbers to be printed for that row is present inside variable row. So the outer for loop selects the row number and the number of elements to be printed in that row.
We initialize the row to 1 and iterate through the outer for loop until row is less than or equal to user entered number. For each iteration of the outer for loop we increment the value of row by 1. That way selecting the next row for each iteration.
Inner For loop Inner for loop prints natural numbers in each selected row. Variable col is assigned to 1 for each iteration of outer for loop, so that the numbers gets printed from the first position in any selected row.
Inner for loop prints the natural number from the first position till the selected row number of times.
For Example, if user enters num = 5, the following Triangle will be printed:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
Note that the Triangle printed is a right angled Triangle and has 5 rows of natural numbers.
Important Note: Also note that first row has 1 number. Second row has 2 numbers. Third row has 3 numbers and so on. So row number and total numbers in that particular row are always equal in any Floyd’s Triangle.
Source Code: C Program To Print Floyd’s Triangle using For Loop
According to a study, the approximate level of intelligence of a person can be calculated using the following formula:
i = 2 + ( y + 0.5x )
Write a C program that will produce a table of values of i, y and x, where y varies from 1 to 6, and, for each value of y, x varies from 5.5 to 12.5 in steps 0.5.
1. Problem statement snippet: “..y varies from 1 to 6”. That means, we can accomplish it using a looping statement. So we can take a for loop and initialize variable y to 1 and iterate the for loop until y is less than or equal to 6.
2. Problem statement snippet: “..for each value of y, x varies from”. That means we should write a nested for loop here.
3. Problem statement snippet: “..for each value of y, x varies from 5.5 to 12.5 in steps 0.5”. That means, we need to write a nested for loop and initialize x value to 5.5 and iterate through the for loop until x value is less than or equal to 12.5 And for each iteration of the inner for loop we need to increment the value of x by 0.5
Video Tutorial: C Program To Calculate Approximate Level of Intelligence of a Person
printf("%d. i = %0.2f,\t x = %0.2f,\t y = %d\n", count++,i, x, y);
}
}
return 0;
}
#include<stdio.h>
int main()
{
int y, count = 1;
float i, x;
for(y = 1; y <= 6; y++)
{
for(x = 5.5; x <= 12.5; x += 0.5)
{
i = 2 + (y + 0.5 * x);
printf("%d. i = %0.2f,\t x = %0.2f,\t y = %d\n", count++,i, x, y);
}
}
return 0;
}
Output:
1. i = 5.75, x = 5.50, y = 1
2. i = 6.00, x = 6.00, y = 1
3. i = 6.25, x = 6.50, y = 1
4. i = 6.50, x = 7.00, y = 1
5. i = 6.75, x = 7.50, y = 1
6. i = 7.00, x = 8.00, y = 1
7. i = 7.25, x = 8.50, y = 1
8. i = 7.50, x = 9.00, y = 1
9. i = 7.75, x = 9.50, y = 1
10. i = 8.00, x = 10.00, y = 1
11. i = 8.25, x = 10.50, y = 1
12. i = 8.50, x = 11.00, y = 1
13. i = 8.75, x = 11.50, y = 1
14. i = 9.00, x = 12.00, y = 1
15. i = 9.25, x = 12.50, y = 1
16. i = 6.75, x = 5.50, y = 2
17. i = 7.00, x = 6.00, y = 2
18. i = 7.25, x = 6.50, y = 2
19. i = 7.50, x = 7.00, y = 2
20. i = 7.75, x = 7.50, y = 2
21. i = 8.00, x = 8.00, y = 2
22. i = 8.25, x = 8.50, y = 2
23. i = 8.50, x = 9.00, y = 2
24. i = 8.75, x = 9.50, y = 2
25. i = 9.00, x = 10.00, y = 2
26. i = 9.25, x = 10.50, y = 2
27. i = 9.50, x = 11.00, y = 2
28. i = 9.75, x = 11.50, y = 2
29. i = 10.00, x = 12.00, y = 2
30. i = 10.25, x = 12.50, y = 2
31. i = 7.75, x = 5.50, y = 3
32. i = 8.00, x = 6.00, y = 3
33. i = 8.25, x = 6.50, y = 3
34. i = 8.50, x = 7.00, y = 3
35. i = 8.75, x = 7.50, y = 3
36. i = 9.00, x = 8.00, y = 3
37. i = 9.25, x = 8.50, y = 3
38. i = 9.50, x = 9.00, y = 3
39. i = 9.75, x = 9.50, y = 3
40. i = 10.00, x = 10.00, y = 3
41. i = 10.25, x = 10.50, y = 3
42. i = 10.50, x = 11.00, y = 3
43. i = 10.75, x = 11.50, y = 3
44. i = 11.00, x = 12.00, y = 3
45. i = 11.25, x = 12.50, y = 3
46. i = 8.75, x = 5.50, y = 4
47. i = 9.00, x = 6.00, y = 4
48. i = 9.25, x = 6.50, y = 4
49. i = 9.50, x = 7.00, y = 4
50. i = 9.75, x = 7.50, y = 4
51. i = 10.00, x = 8.00, y = 4
52. i = 10.25, x = 8.50, y = 4
53. i = 10.50, x = 9.00, y = 4
54. i = 10.75, x = 9.50, y = 4
55. i = 11.00, x = 10.00, y = 4
56. i = 11.25, x = 10.50, y = 4
57. i = 11.50, x = 11.00, y = 4
58. i = 11.75, x = 11.50, y = 4
59. i = 12.00, x = 12.00, y = 4
60. i = 12.25, x = 12.50, y = 4
61. i = 9.75, x = 5.50, y = 5
62. i = 10.00, x = 6.00, y = 5
63. i = 10.25, x = 6.50, y = 5
64. i = 10.50, x = 7.00, y = 5
65. i = 10.75, x = 7.50, y = 5
66. i = 11.00, x = 8.00, y = 5
67. i = 11.25, x = 8.50, y = 5
68. i = 11.50, x = 9.00, y = 5
69. i = 11.75, x = 9.50, y = 5
70. i = 12.00, x = 10.00, y = 5
71. i = 12.25, x = 10.50, y = 5
72. i = 12.50, x = 11.00, y = 5
73. i = 12.75, x = 11.50, y = 5
74. i = 13.00, x = 12.00, y = 5
75. i = 13.25, x = 12.50, y = 5
76. i = 10.75, x = 5.50, y = 6
77. i = 11.00, x = 6.00, y = 6
78. i = 11.25, x = 6.50, y = 6
79. i = 11.50, x = 7.00, y = 6
80. i = 11.75, x = 7.50, y = 6
81. i = 12.00, x = 8.00, y = 6
82. i = 12.25, x = 8.50, y = 6
83. i = 12.50, x = 9.00, y = 6
84. i = 12.75, x = 9.50, y = 6
85. i = 13.00, x = 10.00, y = 6
86. i = 13.25, x = 10.50, y = 6
87. i = 13.50, x = 11.00, y = 6
88. i = 13.75, x = 11.50, y = 6
89. i = 14.00, x = 12.00, y = 6
90. i = 14.25, x = 12.50, y = 6
Note: Inner for loop executes from 5.5 to 12.5 with a 0.5 step increment for each loop. So its 15 iterations. Outer for loop executes 6 times. So total iterations equals 15 x 6 = 90 times. So 90 values will be printed to the console window.