Draw Triangles

 Here,the code of the draw two triangles by taking input of numbers of rows from the user.

Code:

/*This program can correctly print the both triangles till 10 rows. 

And in this program all the variable names are appropriate and can be easily understand*/

triangle-pattren

#include <stdio.h>

int factorial(int); //This is factorial function.

int main(){


int singlerow = 0; //This variable is for single row. And it will print each row continuously.

int noofrows = 0; //This variable is for user input. Means what no of rows want to print?

int beforetrianglespaces = 0;                                     //This variable used to print spaces before each line of triangle. 

int n = 0;                                            //As in formula, it is same variable. And it is for rows number. 

int k = 0;                             //As in formula, it is same variable. And it is for each row and it start from 0 and end to number of row which is presently continue.


int c = 0;                                //As in formula, it is same variable. And it is answer after calculation of n and k.

int factn, factk, nminusk, factnminusk;                                                     //These variables used for taking factorials of n, k and (n-k).

printf("This program gives two unique triangles and these triangles have number of rows that user want for triangle !");

printf("\nEnter the no of rows:   ");

scanf("%d", &noofrows); //user input

//First triangle: which has shape as c(n,k)

for (singlerow = 0; singlerow < noofrows; singlerow++) //for no of rows

{

for (beforetrianglespaces = 1; beforetrianglespaces <= (noofrows - singlerow); beforetrianglespaces++)                        // for spaces before each line

{

printf("%3c", ' ');

}

for (n = singlerow, k = 0; k <= singlerow; k++)                // for printing the linese of triangle one by one. 

{

printf("c(%d,%d) ", n, k);

}


printf("\n");

}

printf("\n\n");

//Second triangle: which used formula of combination

int trianglespaces;                             //this variable used to print number of spaces before each line of triangle. 

trianglespaces = 4 * noofrows;                                            //I do this to increase the number of spaces. 

for (singlerow = 0; singlerow < noofrows; singlerow++)                                     //for number of rows

{

trianglespaces = trianglespaces - 3;                                             //i did it to decrease the spaces before each line of triangle.  

for (beforetrianglespaces = 1; beforetrianglespaces <= trianglespaces; beforetrianglespaces++)                                        //for spaces before each line of triangle

{

printf("%c", ' ');

}

for (n = singlerow, k = 0; k <= singlerow; k++)                                                         //In this loop formula of combination is used. And it will print each line of triangle.

{

//Here, formula is using.

factn = factorial(n);

factk = factorial(k);

nminusk = n - k;

factnminusk = factorial(nminusk);

c = factk*factnminusk;

c = factn / c;

//c is answer (of n and k) after calculation.

if (c<10)                                 //For number less than 10. And it is to adjust the spaces in triangle

printf("%d     ", c);

else if (c<100)                            //For number less than 100. And it is to adjust the spaces in triangle

printf("%d    ", c);

else if (c<1000)                        //For number less than 1000. And it is to adjust the spaces in triangle

printf("%d   ", c);

}

printf("\n");

}

getch();

return 0;

}

int factorial(int num)                              //Factorial function defination.

{

int y;

int x;

if (num >= 1)

{

x = 1;

for (y = 1; y <= num; y++)

x = x*y;

}

else if (num == 0)

x = 1;

return x;

}


Comments

Popular posts from this blog

how to cout in cpp

input by cin in c++

Flutter layout