30+ Most Asked Pattern Programs in C, C++ and Java | FACE Prep

In this article, we will be discussing various Pattern programs in C, C++ and Java. Pattern programs are nothing but patterns consisting of numbers, alphabets or symbols in a particular form. These kinds of pattern programs can be solved easily using for loop condition. Here are some 30 most asked pattern programs in C, C++ and Java.

Most Asked Pattern Printing Programs

#1 – Rectangle Pattern Programs in C, C++, Java

To print a solid and hollow rectangle using stars. An example of the same is shown below. Click here to see the solution in all the languages.

pattern programs in c

#2 – Pyramind Pattern Programs in C, C++, Java using stars

To print the various below shown Pyramid pattern programs using stars. Click here to see the solutions of all the below mentioned patterns.

pyramid pattern printing in c

#3 – Pyramid Pattern Programs in C, C++, Java using numbers

To print the various below shown Pyramid pattern programs using numbers. Click here to see the solutions of all the below mentioned patterns.

pyramid pattern printing programs in c, c++, java

#4 – Palindrome Pyramid Pattern Programs in C, C++, Java using numbers & Alphabets

To print the various below given palindromic pyramid pattern programs using numbers and alphabets. Click here to see the solutions of all the below mentioned diamond patterns.

palindromic pattern programs in c, c++, java

#5 – Diamond Pattern Programs in C, C++, Java using stars

To print the various below given Diamond pattern programs using stars. Click here to see the solutions of all the below mentioned diamond patterns.

diamond pattern programs in c, c++, java using stars

#6 – Diamond Pattern Programs in C, C++, Java using numbers and stars

To print the various below given Diamond pattern programs using numbers and stars.

diamond pattern programs in c, c++, java using numbers and stars

#7 – Floyd’s Triangle Pattern Program in C, C++, Java, Python

To know the solution to print the below shown Floyd’s triangle, click here.

floyd's triangle pattern program in c

#8 – Pascal’s Triangle Pattern Program in C, C++, Java, Python

Click here to know the detailed solution of the below shown Pascal’s triangle.

row 0 =1
row 1 = (0+1), (1+0) = 1, 1
row 2 = (0+1), (1+1), (1+0) = 1, 2, 1
row 3 = (0+1), (1+2), (2+1), (1+0) = 1, 3, 3, 1
row 4 = (0+1), (1+3), (3+3), (3+1), (1+0) = 1, 4, 6, 4, 1
row 5 = (0+1), (1+4), (4+6), (6+4), (4+1),(1+0) = 1, 5, 10, 10, 5, 1
row 6 = (0+1), (1+5), (5+10), (10+10), (10+5), (5+1), (1+0) = 1, 6, 15, 20, 15, 6, 1

pascals triangle pattern program in c

#9 – Hollow Diamond Inscribed in a Rectangle

Input: 5
Output: 

pattern programs in c

Solution for diamond inscribed inside rectangle:

#include <stdio.h>
int main()
{
    int i, j, n;
    scanf("%d", &n);
// upper half of the pattern
    for(i = 0; i < n; i++)
    {
        for(j = 0; j < (2 * n); j++)
        {
            if(i + j <= n - 1) // upper left triangle
                printf("*");
            else
                printf(" ");
            if((i + n) <= j) // upper right triangle
                printf("*");
            else
                printf(" ");
        }
        printf("\n");
    }
// bottom half of the pattern
    for(i = 0; i < n; i++)
    {
        for(j = 0; j < (2 * n); j++)
        {
            if(i >= j) //bottom left triangle
                printf("*");
            else
                printf(" ");
            if(i >= (2 * n - 1) - j) // bottom right triangle
                printf("*");
            else
                printf(" ");
        }
        printf("\n");
    }
    return 0;
}
#include <iostream>
using namespace std; 
int main()
{
    int i, j, n;
    cin >> n;
// upper half of the pattern
    for(i = 0; i < n; i++)
    {
        for(j = 0; j < (2 * n); j++)
        {
            if(i + j <= n - 1) // upper left triangle
                cout << "*";
            else
                cout << " ";
            if((i + n) <= j) // upper right triangle
                cout << "*";
            else
                cout << " ";
        }
        cout << "\n";
    }
// bottom half of the pattern
    for(i = 0; i < n; i++)
    {
        for(j = 0; j < (2 * n); j++)
        {
            if(i >= j) // bottom left triangle
                cout << "*";
            else
                cout << " ";
            if(i >= (2 * n - 1) - j) // bottom right triangle
                cout << "*";
            else
                cout << " ";
        }
        cout << "\n";
    }
    return 0;
}
import java.util.Scanner;
public class Main
{
    public static void main(String args[])
    {
        Scanner sc = new Scanner(System.in);
        int i, j;
        int n = sc.nextInt();
// upper half of the pattern
        for(i = 0; i < n; i++)
        {
            for(j = 0; j < (2 * n); j++)
            {
                if(i + j <= n - 1) // upper left triangle
                    System.out.print("*");
                else
                    System.out.print(" ");
                if((i + n) <= j) // upper right triangle
                    System.out.print("*");
                else
                    System.out.print(" ");
            }
            System.out.println();
        }
// bottom half of the pattern
        for(i = 0; i < n; i++)
        {
            for(j = 0; j < (2 * n); j++)
            {
                if(i >= j) // bottom left triangle
                    System.out.print("*");
                else
                    System.out.print(" ");
                if(i >= (2 * n - 1) - j) // bottom right triangle
                    System.out.print("*");
                else
                    System.out.print(" ");
            }
            System.out.println();
        }
    }
}

#10 – Butterfly Pattern Printing

Input: 5
Output: 

pattern programs in c 

Solution for Butterfly Pattern:

#include <stdio.h>
int main()
{
    int i, j, n;
    scanf("%d", &n);
// upper half of the pattern
    for(i = 0; i < n; i++)
    {
        for(j = 0; j < (2 * n); j++)
        {
            if(i >= j) // upper left triangle
                printf("*");
            else
                printf(" ");
            if(i >= (2 * n - 1) - j) // upper right triangle
                printf("*");
            else
                printf(" ");
        }
        printf("\n");
    }
// bottom half of the pattern
    for(i = 0; i < n; i++)
    {
        for(j = 0; j < (2 * n); j++)
        {
            if(i + j <= n - 1) // bottom left triangle
                printf("*");
            else
                printf(" ");
            if((i + n) <= j) // bottom right triangle
                printf("*");
            else
                printf(" ");
        }
        printf("\n");
    }
    return 0;
}
#include <iostream>
using namespace std; 
int main()
{
    int i, j, n;
    cin >> n;
// upper half of the pattern
    for(i = 0; i < n; i++)
    {
        for(j = 0; j < (2 * n); j++)
        {
            if(i >= j) // upper left triangle
                cout << "*";
            else
                cout << " ";
            if(i >= (2 * n - 1) - j) // upper right triangle
                cout << "*";
            else
                cout << " ";
        }
        cout << "\n";
    }
// bottom half of the pattern
    for(i = 0; i < n; i++)
    {
        for(j = 0; j < (2 * n); j++)
        {
            if(i + j <= n - 1) // bottom left triangle
                cout << "*";
            else
                cout << " ";
            if((i + n) <= j) // bottom right triangle
                cout << "*";
            else
                cout << " ";
        }
        cout << "\n";
    }
    return 0;
}
import java.util.Scanner;
public class Main
{
    public static void main(String args[])
    {
        Scanner sc = new Scanner(System.in);
        int i, j;
        int n = sc.nextInt();
// upper half of the pattern
        for(i = 0; i < n; i++)
        {
            for(j = 0; j < (2 * n); j++)
            {
                if(i >= j) // upper left triangle
                    System.out.print("*");
                else
                    System.out.print(" ");
                if(i >= (2 * n - 1) - j) // upper right triangle
                    System.out.print("*");
                else
                    System.out.print(" ");
            }
            System.out.println();
        }
// bottom half of the pattern
        for(i = 0; i < n; i++)
        {
            for(j = 0; j < (2 * n); j++)
            {
                if(i + j <= n - 1) // bottom left triangle
                    System.out.print("*");
                else
                    System.out.print(" ");
                if((i + n) <= j) // bottom right triangle
                    System.out.print("*");
                else
                    System.out.print(" ");
            }
            System.out.println();
        }
    }
}

#11 – Diagonal & Sides of a Rectangle

Input: 7 (input should be an odd number only, else the desired output will not be obtained)
Output: 

pattern programs in c

Solution for Printing the diagonal and sides of a rectangle:

#include <stdio.h>
int main()
{
    int i, j, n;
    scanf("%d", &n); // 'n' must be odd
    for(i = 0; i < n; i++)
    {
        for(j = 0; j < n; j++)
        {
// left diagonal, right diagonal, top horizontal, bottom horizontal, left vertical, right vertical
            if(i == j || i + j == n - 1 || i == 0 || i == n - 1 || j == 0 || j == n - 1)
                printf("*");
            else
                printf(" ");
        }
        printf("\n");
    }
    return 0;
}
#include <iostream>
using namespace std;
int main()
{
int i, j, n;
cin >> n; // 'n' must be odd
for(i = 0; i < n; i++)
{
for(j = 0; j < n; j++)
{
// left diagonal, right diagonal, top horizontal, bottom horizontal, left vertical, right vertical
if(i == j || i + j == n - 1 || i == 0 || i == n - 1 || j == 0 || j == n - 1)
cout << "*";
else
cout << " ";
}
cout << "\n";
}
return 0;
}
import java.util.Scanner;
public class Main
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
int i, j;
int n = sc.nextInt(); // 'n' must be odd
for(i = 0; i < n; i++)
{
for(j = 0; j < n; j++)
{
// left diagonal, right diagonal, top horizontal, bottom horizontal, left vertical, right vertical
if(i == j || i + j == n - 1 || i == 0 || i == n - 1 || j == 0 || j == n - 1)
System.out.print("*");
else
System.out.print(" ");
}
System.out.println();
}
}
}

#12 – Diagonal & Sides of a Rhombus/Diamond

Input: 9 (input should be an odd number only, else the desired output will not be obtained)
Output: 

pattern programs in c, c++, java

Solution for Printing the diagonal and sides of a Rhombus/Diamond:

#include <stdio.h>
int main()
{
int i, j, n;
scanf("%d", &n); // ‘n’ must be odd
int num1 = n / 2 * 3;
for(i = 0; i < n; i++)
{
for(j = 0; j < n; j++)
{
// center horizontal, center vertical, upper left diagonal, bottom left diagonal, upper right diagonal, bottom right diagonal
if(i == n / 2 || j == n / 2 || i + j == n / 2 || i - j == n / 2 || j - i == n / 2 || i + j == num1)
printf("*");
else
printf(" ");
}
printf("\n");
}
return 0;
}
#include <iostream>
using namespace std;
int main()
{
int i, j, n;
cin >> n; // ‘n’ must be odd
int num1 = n / 2 * 3;
for(i = 0; i < n; i++)
{
for(j = 0; j < n; j++)
{
// center horizontal, center vertical, upper left diagonal, bottom left diagonal, upper right diagonal, bottom right diagonal
if(i == n / 2 || j == n / 2 || i + j == n / 2 || i - j == n / 2 || j - i == n / 2 || i + j == num1)
cout << "*";
else
cout << " ";
}
cout << "\n";
}
return 0;
}
import java.util.Scanner;
public class Main
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
int i, j;
int n = sc.nextInt(); // ‘n’ must be odd
int num1 = n / 2 * 3;
for(i = 0; i < n; i++)
{
for(j = 0; j < n; j++)
{
// center horizontal, center vertical, upper left diagonal, bottom left diagonal, upper right diagonal, bottom right diagonal
if(i == n / 2 || j == n / 2 || i + j == n / 2 || i - j == n / 2 || j - i == n / 2 || i + j == num1)
System.out.print("*");
else
System.out.print(" ");
}
System.out.println();
}
}
}

#13 – Left and Right Arrows

Input: 7 (Here n is the height and width of pattern to be printed)
Output: 

pattern programs in c, c++, java

Solution for Printing the left arrow & right arrow:

#include 
int main()
{
int i, j, n;
scanf("%d", &n); // ‘n’ must be odd
int num1 = n / 2 * 3;
// right arrow
printf("Right Arrow\n");
for(i = 0; i < n; i++)
{
for(j = 0; j < n; j++)
{
// center horizontal, upper right diagonal, bottom right diagonal
if(i == n / 2 || j - i == n / 2 || i + j == num1)
printf("*");
else
printf(" ");
}
printf("\n");
}
// left arrow
printf("Left Arrow\n");
for(i = 0; i < n; i++)
{
for(j = 0; j < n; j++)
{
// center horizontal, bottom left diagonal, upper left diagonal
if(i == n / 2 || i - j == n / 2 || i + j == n / 2)
printf("*");
else
printf(" ");
}
printf("\n");
}
return 0;
}
#include 
using namespace std;
int main()
{
int i, j, n;
cin >> n;  // ‘n’ must be odd
int num1 = n / 2 * 3;
// right arrow
cout << "Right Arrow" << endl;
for(i = 0; i < n; i++)
{
for(j = 0; j < n; j++)
{
// center horizontal, upper right diagonal, bottom right diagonal
if(i == n / 2 || j - i == n / 2 || i + j == num1)
cout << "*";
else
cout << " ";
}
cout << "\n";
}
// left arrow
cout << "Left Arrow" << endl;
for(i = 0; i < n; i++)
{
for(j = 0; j < n; j++)
{
// center horizontal, bottom left diagonal, upper left diagonal
if(i == n / 2 || i - j == n / 2 || i + j == n / 2)
cout << "*";
else
cout << " ";
}
cout << "\n";
}
return 0;
}
import java.util.Scanner;
public class Main
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
int i, j;
int n = sc.nextInt(); // ‘n’ must be odd
int num1 = n / 2 * 3;
// right arrow
System.out.println("Right Arrow");
for(i = 0; i < n; i++)
{
for(j = 0; j < n; j++)
{
// center horizontal, upper right diagonal, bottom right diagonal
if(i == n / 2 || j - i == n / 2 || i + j == num1)
System.out.print("*");
else
System.out.print(" ");
}
System.out.println();
}
// left arrow
System.out.println("Left Arrow");
for(i = 0; i < n; i++)
{
for(j = 0; j < n; j++)
{
// center horizontal, bottom left diagonal, upper left diagonal
if(i == n / 2 || i - j == n / 2 || i + j == n / 2)
System.out.print("*");
else
System.out.print(" ");
}
System.out.println();
}
}
}

#14 – Rhombus Pattern Program in C, C++, Java

Input: 4 
Output: 

pattern programs in c, c++, java

Solution for Printing a solid and hollow Rhombus:

#include <stdio.h>
int main()
{
int i, j, n;
scanf("%d", &n);
// solid rhombus
printf("Solid Rhombus\n");
for(i = 0; i < n; i++)
{
for(j = 0; j < n - i; j++)
{
printf(" "); // leading spaces
}
for(j = 0; j < n; j++)
{
printf("*");
}
printf("\n");
}
// hollow rhombus
printf("Hollow Rhombus\n");
for(i = 0; i < n; i++)
{
for(j = 0; j < n - i; j++)
{
printf(" "); // leading spaces
}
for(j = 0; j < n; j++)
{
// upper horizontal, bottom horizontal, left diagonal, right diagonal
if(i == 0 || i == n - 1 || j == 0 || j == n - 1)
printf("*");
else
printf(" ");
}
printf("\n");
}
return 0;
}
#include <iostream>
using namespace std;
int main()
{
int i, j, n;
cin >> n;
// solid rhombus
cout << "Solid Rhombus" << endl;
for(i = 0; i < n; i++)
{
for(j = 0; j < n - i; j++)
{
cout << " "; // leading spaces
}
for(j = 0; j < n; j++)
{
cout << "*";
}
cout << "\n";
}
// hollow rhombus
cout << "Hollow Rhombus" << endl;
for(i = 0; i < n; i++)
{
for(j = 0; j < n - i; j++)
{
cout << " "; // leading spaces
}
for(j = 0; j < n; j++)
{
// upper horizontal, bottom horizontal, left diagonal, right diagonal
if(i == 0 || i == n - 1 || j == 0 || j == n - 1)
cout << "*";
else
cout << " ";
}
cout << "\n";
}
return 0;
}
import java.util.Scanner;
public class Main
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
int i, j;
int n = sc.nextInt();
// solid rhombus
System.out.println("Solid Rhombus");
for(i = 0; i < n; i++)
{
for(j = 0; j < n - i; j++)
{
System.out.print(" "); // leading spaces
}
for(j = 0; j < n; j++)
{
System.out.print("*");
}
System.out.println();
}
// hollow rhombus
System.out.println("Hollow Rhombus");
for(i = 0; i < n; i++)
{
for(j = 0; j < n - i; j++)
{
System.out.print(" "); // leading spaces
for(j = 0; j < n; j++)
{
// upper horizontal, bottom horizontal, left diagonal, right diagonal
if(i == 0 || i == n - 1 || j == 0 || j == n - 1)
System.out.print("*");
else
System.out.print(" ");
}
System.out.println();
}
}
}

Useful video on Pattern Programming for beginners