C Program To Draw Pyramid of Numbers, using For Loop

Lets write a C program to draw / display / print a four row pyramid formed from numbers 1 to 10.

Related Read:
Nested For Loop In C Programming Language
C Program To Draw Pyramid of Numbers, using While Loop

Video Tutorial: C Program To Draw Pyramid of Numbers, using For Loop


[youtube https://www.youtube.com/watch?v=Z-uVxVnXqgE]

YouTube Link: https://www.youtube.com/watch?v=Z-uVxVnXqgE [Watch the Video In Full Screen.]

Source Code: C Program To Draw Pyramid of Numbers, using For Loop

#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.

For list of all c programming interviews / viva question and answers visit: C Programming Interview / Viva Q&A List

For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert

C Program To Draw Pyramid of Stars, using For Loop

Lets write a C program to draw / print / display a pyramid / triangle formed from stars, using nested for loop.

Related Read:
Nested For Loop In C Programming Language
C Program To Draw Pyramid of Stars, using While Loop

Expected Output for the Input

User Input:
Enter no of rows of Pyramid
5

Output:

    
     *
    ***
   *****
  *******
 *********

Pyramid With 20 Rows
pyramid of stars

Video Tutorial: C Program To Draw Pyramid of Stars, using For Loop


[youtube https://www.youtube.com/watch?v=omDJLEDBZNw]

YouTube Link: https://www.youtube.com/watch?v=omDJLEDBZNw [Watch the Video In Full Screen.]

Logic To Draw Pyramid of Stars, using For Loop

If num = 5;

First Iteration of outer for loop
row = 1;

1st inner for loop
condition:
num – row;
= 5 – 1
= 4
Four spaces gets printed.

2st inner for loop
condition:
2 * row – 1;
= 2 * 1 – 1;
= 2 – 1
= 1
One star gets printed.

Second Iteration of outer for loop
row = 2;

1st inner for loop
condition:
num – row;
= 5 – 2
= 3
Three spaces gets printed.

2st inner for loop
condition:
2 * row – 1;
= 2 * 2 – 1;
= 4 – 1
= 3
Three stars gets printed.

Third Iteration of outer for loop
row = 3;

1st inner for loop
condition:
num – row;
= 5 – 3
= 2
Two spaces gets printed.

2st inner for loop
condition:
2 * row – 1;
= 2 * 3 – 1;
= 6 – 1
= 5
Five stars gets printed.

Forth Iteration of outer for loop
row = 4;

1st inner for loop
condition:
num – row;
= 5 – 4
= 1
One space gets printed.

2st inner for loop
condition:
2 * row – 1;
= 2 * 4 – 1;
= 8 – 1
= 7
Seven stars gets printed.

Fifth Iteration of outer for loop
row = 5;

1st inner for loop
condition:
num – row;
= 5 – 5
= 0
Zero space gets printed.

2st inner for loop
condition:
2 * row – 1;
= 2 * 5 – 1;
= 10 – 1
= 9
Nine stars gets printed.

Source Code: C Program To Draw Pyramid of Stars, using For Loop

#include<stdio.h>

int main()
{
    int num, row, col, space;

    printf("Enter no of rows of Pyramid\n");
    scanf("%d", &num);

    for(row = 1; row <= num; row++)
    {
        for(space = 1; space <= (num-row); space++)
        {
            printf(" ");
        }
        for(col = 1; col <= (2*row-1); col++)
        {
            printf("*");
        }
        printf("\n");
    }

    return 0;
}

Output 1:
Enter no of rows of Pyramid
5

     *
    ***
   *****
  *******
 *********

For list of all c programming interviews / viva question and answers visit: C Programming Interview / Viva Q&A List

For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert

C Program To Display Right Angled Triangle With Alphabets, using For Loop

Lets write a C program to display/print/output a right angled triangle pattern formed with Capital Letter English Alphabets, using nested for loop.

Related Read:
Nested For Loop In C Programming Language
C Program To Display Right Angled Triangle With Alphabets, using While Loop

Expected Output for the Input

If user enters number of rows as 5. Then our C program prints a Right angled Triangle with 5 rows of Capital Letter English Alphabets in it.

User Input:
num = 5;

Output:

A
B C
D E F
G H I J
K L M N O

Video Tutorial: C Program To Display Right Angled Triangle With Alphabets, using For Loop


[youtube https://www.youtube.com/watch?v=WYDm6zrPhkg]

YouTube Link: https://www.youtube.com/watch?v=WYDm6zrPhkg [Watch the Video In Full Screen.]

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

#include<stdio.h>

int main()
{
    int num, row, col, count = 65;

    printf("Enter no of rows\n");
    scanf("%d", &num);

    printf("\n");
    for(row = 1; row <= num; row++)
    {
        for(col = 1; col <= row; col++)
        {
            if(count == 91)
                count = 65;

            printf("%c  ", count++);
        }
        printf("\n");
    }

    return 0;
}

Output 1:
Enter no of rows
5

A
B C
D E F
G H I J
K L M N O

Output 2:
Enter no of rows
10

A
B  C
D  E  F
G  H  I  J
K  L  M  N  O
P  Q  R  S  T  U
V  W  X  Y  Z  A  B
C  D  E  F  G  H  I  J
K  L  M  N  O  P  Q  R  S
T  U  V  W  X  Y  Z  A  B  C

Related Read:
C Program To Print All ASCII Characters and Value using For Loop

ASCII value of A is 65

ASCII value of B is 66

ASCII value of C is 67

ASCII value of D is 68

ASCII value of E is 69

ASCII value of F is 70

ASCII value of G is 71

ASCII value of H is 72

ASCII value of I is 73

ASCII value of J is 74

ASCII value of K is 75

ASCII value of L is 76

ASCII value of M is 77

ASCII value of N is 78

ASCII value of O is 79

ASCII value of P is 80

ASCII value of Q is 81

ASCII value of R is 82

ASCII value of S is 83

ASCII value of T is 84

ASCII value of U is 85

ASCII value of V is 86

ASCII value of W is 87

ASCII value of X is 88

ASCII value of Y is 89

ASCII value of Z is 90

For list of all c programming interviews / viva question and answers visit: C Programming Interview / Viva Q&A List

For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert

C Program To Print Floyd’s Triangle using For Loop

Lets write C program to print Floyd’s Triangle, using nested for loop.

Floyd’s Triangle: is a right angled Triangle formed with natural numbers.

Related Read:
For Loop In C Programming Language
Nested For Loop In C Programming Language
C Program To Print Floyd’s Triangle

Video Tutorial: C Program To Print Floyd’s Triangle using For Loop


[youtube https://www.youtube.com/watch?v=cfN_uSXJW_w]

YouTube Link: https://www.youtube.com/watch?v=cfN_uSXJW_w [Watch the Video In Full Screen.]

Logic To Print Floyd’s Triangle 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

#include<stdio.h>

int main()
{
    int num, row, col, count = 1;

    printf("Enter number of rows for Floyd's Triangle\n");
    scanf("%d", &num);

    printf("\n");
    for(row = 1; row <= num; row++)
    {
        for(col = 1; col <= row; col++)
        {
            printf("%d  ", count++);
        }
        printf("\n");
    }

    return 0;
}

Output 1:
Enter number of rows for Floyd’s Triangle
5

1
2 3
4 5 6
7 8 9 10
11 12 13 14 15

Output 2:
Enter number of rows for Floyd’s Triangle
14

1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31 32 33 34 35 36
37 38 39 40 41 42 43 44 45
46 47 48 49 50 51 52 53 54 55
56 57 58 59 60 61 62 63 64 65 66
67 68 69 70 71 72 73 74 75 76 77 78
79 80 81 82 83 84 85 86 87 88 89 90 91
92 93 94 95 96 97 98 99 100 101 102 103 104 105

For list of all c programming interviews / viva question and answers visit: C Programming Interview / Viva Q&A List

For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert

C Program To Calculate Approximate Level of Intelligence of a Person

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.

Related Read:
Basic Arithmetic Operations In C
Compound Assignment Operators in C
Nested For Loop In C Programming Language

Logic To understanding the problem statement

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


[youtube https://www.youtube.com/watch?v=wRfgDzMMIvY]

YouTube Link: https://www.youtube.com/watch?v=wRfgDzMMIvY [Watch the Video In Full Screen.]


Source Code: C Program To Calculate Approximate Level of Intelligence of a Person

#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.

For list of all c programming interviews / viva question and answers visit: C Programming Interview / Viva Q&A List

For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert