C Program : To Print Triangle & Pyramid by using for loops


To Print Triangle & Pyramid by using for loops


C language To Print Triangle & Pyramid by using for loops

a.)Program for Triangle

 #include
 main()
  {
   int n, i, j;
   printf("Enter how many lines you want : ");
   scanf("%d", &n);
   for(i = 1; i < = n; i++)
    {
      for(j = 1; j < = i; j++)
       printf("%d \t", i);
       printf(" \n ");       
    }
  }
Input : Enter how many lines you want : 4

b.) Program for Pyramid

 #include
 main()
  {
   int n, i, j, k;
   clrscr();
   printf("Enter how many lines you want : ");
   scanf("%d", &n);
   for(i = 1; i < = n; i++)
    {
       for(k = 0; k < = n - i; k++)
       printf(" ");
      for(j = 1; j < = i; j++)
       printf(" %6d ", i);
       printf(" \n ");       
    }
  }
Input : Enter how many lines you want : 4

0 Komentar untuk "C Program : To Print Triangle & Pyramid by using for loops"

Back To Top