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.
Logic To Draw Pyramid of Alphabets, using While Loop
We ask the user to enter the maximum number of rows for the pyramid and store it inside the address of variable num. We declare and initialize a variable count to 1(indicating first row of the pyramid).
In the outer while loop we check if count is less than or equal to num. For each iteration of the while loop we increment the value of count by 1. So the count value will have the row number i.e., for each iteration of the outer while loop we select row one by one (to print the alphabets).
Inside first inner while loop we print the adequate number of space for each row. Inside the second inner while loop we actually print the alphabets needed for each row.
At the end the result will be a pyramid with the number of rows as input by the user.
Note: Inside second nested while loop we are checking for the condition – if alphabet is equal to 90(ASCII value of Z), and then reinitializing it to 64(but ASCII value of A is 65), that is because after the if condition is executed we have alphabet++; statement which increments the value of variable alphabet from 64 to 65(which is the ASCII value of A).
Alphabets and ASCII Value We can even write the code in if else block as shown below:
We ask the user to enter the maximum number of rows for the pyramid and store it inside the address of variable num. We declare and initialize a variable count to 1(indicating first row of the pyramid).
In the outer while loop we check if count is less than or equal to num. For each iteration of the while loop we increment the value of count by 1. So the count value will have the row number i.e., for each iteration of the outer while loop we select row one by one (to print the stars).
Inside first inner while loop we print the adequate number of space for each row. Inside the second inner while loop we actually print the stars needed for each row.
At the end the result will be a pyramid with the number of rows as input by the user.
Source Code: C Program To Draw Pyramid of Stars, using While Loop
#include<stdio.h>
int main()
{
int num, count = 1, i;
printf("Enter no of rows of Pyramid\n");
scanf("%d", &num);
while(count <= num)
{
i = 0;
while( i <= (num - count) )
{
printf(" ");
i++;
}
i = 0;
while(i < (2*count-1))
{
printf("*");
i++;
}
printf("\n");
count++;
}
return 0;
}
Today lets learn how to use arcs and paths to draw circle.
Arcs are curves that are portions of a circle. A full circle is a 360 degree arc! A Path is a collection of some lines(or shapes) that once defined can be either filled or stroked.
index.html and myStyle.css files are kept same as illustrated in previous video tutorial: Canvas State: HTML5
Syntax arc(x, y, r, sA, eA, TF);
arc method takes 6 parameters. x and y are the x-axis and y-axis, which is the center of the circle. i.e., The x and y parameters determine where the center of the circle will be located on your canvas.
r is the radius of the circle, in pixels. i.e., the distance of the curve from the center of the circle.
sA is start angle of the circle. eA is the end angle of the circle.
TF is the direction – which can take 2 values i.e., True or False.
Note: 1. By default, direction of drawing the arc is clockwise/positive degrees(i.e., the value of last parameter is false). 2. PI value is 180. 3. 2 * PI is 360! 4. arc method should be passed with radian values and not degrees.