Saturday, 24 December 2022

Print the triangle pattern with * from 1 to n lines using Time Complexity of O(n)=n*(n+1)/2.

 

#include<stdio.h>

#include<conio.h>

int main(void)

{

    int i=1,n,t,j;

    //clrscr();    //use clrscr to clean console screen in TurboC++ 

    printf("Pick a number: \n");

    scanf("%d",&n);

    printf("Printing the triangle with * from 1 to %d line\n",n);

    for (i=0,t=1,j=1;i<(n*(n+1)/2);i++)

    {

        if(i==t*(t+1)/2)

        {

            printf("\n");

            t++;

        }

        printf("* ");

    }

    //getch();    //use getch() to hold console in TurboC++ 

}


No comments:

Post a Comment