6.17.2013

Number Pyramid/Triangle

Q. Write a C program to print the following number triangle as:

0
01
012
0123
01234
0123
012
01
0

Ans.

/*c program for number triangle*/
#include<stdio.h>
int main()
{
 int num,n,r,c,z;
 printf("Enter pyramid max number: ");
 scanf("%d", &num);
 for(r=0; r<=num; r++)
 {
  for(c=0,z=0; c<=r; c++,z++)
    printf("%d",z);
  printf("\n");
 }
 for(n=num-1; n>=0; n--)
 {
  for(c=0,z=0; c<=n; c++,z++)
    printf("%d",z);
  printf("\n");
 }
 getch();
 return 0;
}

The output of above program would be:

output of number pyramid C program
Figure: Screen shot for number pyramid C program


1 comment:

  1. hi dude how about this problem

    Q.ifference Triangle
    Introduction
    Similar to Pascal’s triangle, the difference triangle has some interesting properties that find applications in various fields of the natural and applied sciences. In simple terms, a difference triangle is a set of integers arranged in an inverted triangle where each inverted triangle triad has its lower element equal to the difference (absolute value) of the two elements in the upper row.
    A difference triangle can be created from a sequence of integers forming the uppermost row by iteratively taking differences between consecutive terms to form the next row until a single-element row is created.
    Example
    Consider the sequence 5, 8, 13, 21, 34, 55 from the Fibonacci series as the uppermost row of the difference triangle. The difference between successive elements form a new set: 3 (= 8 – 5), 5 (= 13 – 8), 8 (= 21 – 13), 13 (= 34 – 21), and 21 (= 55 – 34). The process can then be repeated until there is only one element left giving the following difference triangle:
    5 8 13 21 34 55
    3 5 8 13 21
    2 3 5 8
    1 2 3
    1 1
    0
    Problem
    Write a program that forms a difference triangle using a given series of numbers as topmost row.
    Input
    The input shall be a single line containing a sequence of positive integers separated by spaces. There shall be a maximum of 10 numbers, each less than 100000.
    Output
    The program shall output the difference triangle formed using the input series as topmost row, properly formatted (see sample runs).
    Input Validation

    ReplyDelete