Showing posts with label pattern programs. Show all posts
Showing posts with label pattern programs. Show all posts

Sunday, 31 January 2016

Cpp Program to print triangle with numbers

This is a simple Cpp Program which prints out a triangle with respect to the input given , here the obtained triangle is filled with numbers.

Triangle pattern with numbers

Download Code

PROGRAM :

#include <iostream>

using namespace std;

int main()
{
    int i, j, n,k;
    cout << "Enter number of lines[height of triangle] : \t";
    cin >> n;
    for(i=1; i<=n; i++)
    {
        for(j=i; j<n; j++)
        {
            cout << " ";
        }
        for(k=1; k<(i*2); k++)
        {
            cout << k;
        }
        cout << "\n";
    }
    return 0;
}

OUTPUT :

Cpp Program - Triangle with numbers

Cpp Program to print a triangle with stars

This is a simple Cpp Program which prints out a triangle with respect to the input given , the obtained triangle is  filled with stars.

Triangle pattern with stars

Download Code

PROGRAM :

#include <iostream>

using namespace std;

int main()
{
    int i, j, n,k;
    cout << "Enter number of lines[height of triangle] : \t";
    cin >> n;
    for(i=1; i<=n; i++)
    {
        for(j=i; j<n; j++)
        {
            cout << " ";
        }
        for(k=1; k<i*2; k++)
        {
            cout << "*";
        }
        cout << "\n";
    }
    return 0;
}


OUTPUT : 

Cpp Program to print a triangle with stars

Saturday, 30 January 2016

Cpp Program to print a Right angled triangle

This is a simple Cpp Program which prints out a Right angled triangle with respect to the input given.

Right angled triangle pattern program
Download Code
PROGRAM :


#include <iostream>

using namespace std;

int main()
{
    int i, j, n,k;
    cout << "Enter number of lines[height of triangle] : \t";
    cin >> n;
    for(i=1; i<=n; i++)
    {
        for(j=i; j<n; j++)
        {
            cout << " ";
        }
        for(k=1; k<(i*2); k++)
        {
            cout << k++;
        }
        cout << "\n";
    }
    return 0;
}


Download Code
OUTPUT :
Cpp Program - Right angled triangle pattern

Cpp Program to print Pascals triangle

This is a Cpp Program which prints pascals triangle based on the input given.

Pascals Triangle


Download Code
PROGRAM : 

#include <iostream>

using namespace std;

int main()
{
     int i,j,x,n,s;
    cout << "Enter number of lines[height of triangle] : \t";
    cin >> n;
    for(i=0; i<=n; i++)
    {
        x=1;
        for(s=1; s<=n-i; s++)
            cout << " ";
        for(j=1; j<=i+1; j++)
        {
            cout << " " << x;
            x=x*(i-j+1)/j;
        }
        cout << "\n";
    }
    return 0;
}

Download Code
OUTPUT : 
Cpp Program to print pascals triangle


Tuesday, 26 January 2016

Cpp Program to print palindrome triangle

This is a Cpp Program which prints thee palindrome triangle based on the input given.

Here , check each row elements , you can see a palindrome sequence.
Palindrome triangle

Download Code

PROGRAM :

#include <iostream>

using namespace std;

int main()
{
    int i,j,n;
    cout << "Enter number of lines[height of triangle] : \t";
    cin >> n;
    for(i=1; i<=n; i++)
    {
        for(j=i; j<n; j++)
            cout << " ";
        for(j=1; j<=i; j++)
            cout << j;
        for(j=i-1; j>=1; j--)
            cout << j;
        cout << "\n";
    }
    return 0;
}

OUTPUT :

Cpp program - Palindrome triangle

Download Code

Cpp Program to print Floyds triangle

This is a Cpp program which prints the Floyd's triangle with respect to the input given.

Floyds triangle


Download Code

PROGRAM :

#include <iostream>

using namespace std;

int main()
{
    int i, j, k = 1,n;
    cout << "Enter the range: ";
    cin >> n;
    cout << "\nFLOYD'S TRIANGLE : \n";
    for (i = 1; i <= n; i++)
    {
        for (j = 1; j <= i; j++, k++)
        {
            cout << "\t" << k;
        }
        cout << "\n" ;
    }
    return 0;
}

OUTPUT :

Cpp Program - Floyds Triangle

Cpp program to print diamond with stars

This is a  Cpp Program which prints out the diamond with stars pattern.

Diamond with stars ( pattern)
Download Code
PROGRAM :

#include <iostream>

using namespace std;

int main()
{
    int i, j, k,n;
    cout << "Enter number of lines[height of diamond] : \t" ;
    cin >> n;
    for(i=1; i<=n; i++)
    {
        for(j=i; j<n; j++)
        {
            cout << " ";
        }
        for(k=1; k<(i*2); k++)
        {
            cout << "*";
        }
        cout << "\n";
    }
    for(i=n-1; i>=1; i--)
    {
        for(j=n; j>i; j--)
        {
            cout << " ";
        }
        for(k=1; k<(i*2); k++)
        {
            cout << "*";
        }
        cout << "\n";
    }

    return 0;
}


OUTPUT :
Cpp Program  - Diamond pattern with stars

Download Code

Tuesday, 20 January 2015

Java Program to print a triangle filled with stars



This is a Java Program which  prints out a triangle filled with stars.

Java Program to print a triangle filled with stars

PROGRAM :
package codingcorner.in;

import java.util.Scanner;

public class TriangeWithStars {
 public static void main(String[] args) {
  int i, j, n, k;
  Scanner scan = new Scanner(System.in);
  System.out.print("Enter number of lines[height of triangle] : \t");
  n = scan.nextInt();
  scan.close();
  for (i = 1; i <= n; i++) {
   for (j = i; j < n; j++) {
    System.out.print(" ");
   }
   for (k = 1; k < i * 2; k++) {
    System.out.print("*");
   }
   System.out.print("\n");
  }
 }
}

OUTPUT :
Java Program to print a triangle filled with stars


Saturday, 17 January 2015

C Program to print a triangle filled with stars


This is a simple C Program which prints out a triangle with respect to the input given , the obtained triangle is  filled with stars.

Triangle pattern

PROGRAM :
#include <stdio.h>
#include <stdlib.h>

int main()
{
    int i, j, n,k;
    printf("Enter number of lines[height of triangle] : \t");
    scanf("%d",&n);
    for(i=1; i<=n; i++)
    {
        for(j=i; j<n; j++)
        {
            printf(" ");
        }
        for(k=1; k<i*2; k++)
        {
            printf("*");
        }
        printf("\n");
    }
    return 0;
}

OUTPUT : 
C Program to print a triangle