Monday 17 September 2012

c program to print Floyd's triangle



DESCRIPTION:
C program to print Floyd's triangle:- This program prints Floyd's triangle. Number of rows of Floyd's triangle to print is entered by the user. First four rows of Floyd's triangle are as follows :-
1
2 3
4 5 6
7 8 9 10
It's clear that in Floyd's triangle nth row contains n numbers.
code;
#include <stdio.h>

int main()
{
  int n, i,  c, a = 1;

  printf("Enter the number of rows of Floyd's triangle to print\n");
  scanf("%d", &n);

  for (i = 1; i <= n; i++)
  {
    for (c = 1; c <= i; c++)
    {
      printf("%d ",a);
      a++;
    }
    printf("\n");
  }

  return 0;
}
output:
enter the no of rows of floyds triangle to print:
5
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
case1:
#include<stdio.h>

main()
{
    int c, n, k;
    char ch = 'A';


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

    for ( c = 1 ; c <= n ; c++ )
    {
        for ( k = 1 ; k <= c ; k++)
            printf("%c ", ch);

        printf("\n");
        ch++;
    }

    return 0;
}
output:
A
BB
CCC
DDDD
EEEEEE
CASE2:
#include<stdio.h>

main()
{
   int n, c, k, space = 0;
   char ch, temp;

   scanf("%d", &n);

   ch = 'A'+n-1;
   temp = ch;

   for ( k = n ; k >= 1 ; k-- )
   {
      for ( c = 1 ; c <= space; c++)
         printf(" ");

      space++;

      for ( c = 1 ; c <= k ; c++ )
      {
         printf("%c",temp);
         temp--;
      }

      printf("\n");
      ch--;
      temp = ch;
   }

   return 0;
}
OUTPUT:
EDCBA
 DCBA
  CBA
   BA
    A

No comments:

Post a Comment