Number pattern programs in C/C++

Number pattern programs in C/C++

number pattern programs output

Pattern programs are widely asked questions in university exams and interviews. These type of questions test logic and concept of a programmer. Every pattern requires different logic. All type of patterns use nested for loops. Some of the pattern uses more than two for loops. Let’s see some number pattern programs in C/C++.

Also read:
1. Star pattern programs in C – 1
2. Star pattern programs in C/C++ – 2
3. Alphabet pattern programs in C/C++.

1. Number triangle -1

Output-

pattern programs in C/C++

Program in C :

/*
 1
 1 2
 1 2 3
 1 2 3 4
*/
#include<stdio.h>
int main() {
  int i, j, n, x;
  printf("\n Enter no of rows: ");
  scanf("%d", & n);
  for (i = 1; i <= n; i++) {
    for (x = 1, j = n - i; j < n; j++, x++)
      printf(" %d", x);
    printf("\n");
  }
  return 0;
}

Program in C++ :

/*
 1
 1 2
 1 2 3
 1 2 3 4
*/
#include<iostream>
using namespace std;
int main() {
  int i, j, n, x;
  cout << "\n Enter no of rows: ";
  cin >> n;
  for (i = 1; i <= n; i++) {
    for (x = 1, j = n - i; j < n; j++, x++)
      cout << " " << x;
    cout << "\n";
  }
  return 0;
}

2. Number triangle- 2

Output-

pattern programs in C/C++

Program in C :

/*
 1
 2 2
 3 3 3
 4 4 4 4
*/
#include<stdio.h>
int main() {
  int i, j, n, x = 1;
  printf("\n Enter no of rows: ");
  scanf("%d", & n);
  for (i = 1; i <= n; i++, x++) {
    for (j = n - i; j < n; j++)
      printf(" %d", x);
    printf("\n");
  }
  return 0;
}

Program in C++ :

/*
 1
 2 2
 3 3 3
 4 4 4 4
*/
#include<iostream>
using namespace std;
int main() {
  int i, j, n, x = 1;
  cout <<"\n Enter no of rows: ";
  cin>>n;
  for (i = 1; i <= n; i++, x++) {
    for (j = n - i; j < n; j++)
      cout << " "<<x;
    cout <<"\n";
  }
  return 0;
}

3. Floyd’s triangle

Output-

pattern programs in C/C++

Program in C :

/*
 1
 2 3
 4 5 6
 7 8 9 10
*/
#include<stdio.h>
int main() {
  int i, j, n, x = 1;
  printf("\n Enter no of rows: ");
  scanf("%d", & n);
  for (i = 1; i <= n; i++) {
    for (j = n - i; j < n; j++, x++)
      printf(" %d", x);
    printf("\n");
  }
  return 0;
}

Program in C++ :

/*
 1
 2 3
 4 5 6
 7 8 9 10
*/
#include<iostream>
using namespace std;
int main() {
  int i, j, n, x = 1;
  cout <<"\n Enter no of rows: ";
  cin>>n;
  for (i = 1; i <= n; i++) {
    for (j = n - i; j < n; j++, x++)
      cout << " "<<x;
    cout <<"\n";
  }
  return 0;
}

4. Pascal’s triangle

Output-

Program in C :

/*
             1
           1   1
         1   2   1
       1   3   3   1
     1   4   6   4   1
*/
#include <stdio.h>
int main() {
  int r, n = 1, s, i, j;
  printf("\n Enter number of rows: ");
  scanf("%d", & r);
  for (i = 0; i < r; i++) {
    for (s = 1; s <= r - i; s++)
      printf("  ");
    for (j = 0; j <= i; j++) {
      if (j == 0 || i == 0)
        n = 1;
      else
        n = n * (i - j + 1) / j;
      printf("%4d", n);
    }
    printf("\n");
  }
  return 0;
}

Program in C++ :

/*
             1
           1   1
         1   2   1
       1   3   3   1
     1   4   6   4   1
*/
#include <iostream>
#include <iomanip>
using namespace std;
int main() {
  int r, n = 1, s, i, j;
  cout << "\n Enter number of rows: ";
  cin >> r;
  for (i = 0; i < r; i++) {
    for (s = 1; s <= r - i; s++)
      cout << "  ";
    for (j = 0; j <= i; j++) {
      if (j == 0 || i == 0)
        n = 1;
      else
        n = n * (i - j + 1) / j;
      cout << setw(4) << n;
    }
    cout << "\n";
  }
  return 0;
}

Note: “%4d” in C and setw(4) in C++ are manipulators used to indicate that the value should be printed with a minimum field width of 4 characters. If it is less than 4 characters, the rest is filled with blanks to align the output. In C++, <iomanip> library must be included for this.

5. Number pyramid -1

Output-

Program in C :

/*
       1
     2 2 2
   3 3 3 3 3
 4 4 4 4 4 4 4
*/
#include <stdio.h>
int main() {
  int i, j, k, n, r;
  printf("\n Enter number of rows: ");
  scanf("%d", & r);
  n = r - 1;
  printf("\n");
  for (i = 1; i <= r; i++) {
    for (j = 1; j <= n; j++)
      printf("  ");
    n--;
    for (k = 1; k <= 2 * i - 1; k++)
      printf(" %d", i);
    printf("\n");
  }
  return 0;
}

Program in C++ :

/*
       1
     2 2 2
   3 3 3 3 3
 4 4 4 4 4 4 4
*/
#include <iostream>
using namespace std;
int main() {
  int i, j, k, n, r;
  cout << "\n Enter number of rows: ";
  cin >> r;
  n = r - 1;
  cout << "\n";
  for (i = 1; i <= r; i++) {
    for (j = 1; j <= n; j++)
      cout << "  ";
    n--;
    for (k = 1; k <= 2 * i - 1; k++)
      cout << " " << i;
    cout << "\n";
  }
}

6. Number pyramid -2

Output-

Program in C :

/*
   4 4 4 4 4 4 4
     3 3 3 3 3
       2 2 2
         1
*/
#include <stdio.h>
int main() {
  int i, j, k, n, r;
  printf("\n Enter number of rows: ");
  scanf("%d", & r);
  n = 1;
  for (i = 1; r >= i; r--) {
    for (j = 1; j <= n; j++)
      printf("  ");
    n++;
    for (k = 1; k <= 2 * (r - i) + 1; k++)
      printf(" %d", r);
    printf("\n");
  }
  return 0;
}

Program in C++ :

/*
   4 4 4 4 4 4 4
     3 3 3 3 3
       2 2 2
         1
*/
#include <iostream>
using namespace std;
int main() {
  int i, j, k, n, r;
  cout << "\n Enter number of rows: ";
  cin >> r;
  n = 1;
  for (i = 1; r >= i; r--) {
    for (j = 1; j <= n; j++)
      cout << "  ";
    n++;
    for (k = 1; k <= 2 * (r - i) + 1; k++)
      cout << " " << r;
    cout << "\n";
  }
  return 0;
}

7. Number pyramid -3

Output-

Program in C :

/*
           1
         2   2
       3   3   3
     4   4   4   4
*/
#include <stdio.h>
int main() {
  int r, n = 1, s, i, j;
  printf("\n Enter number of rows: ");
  scanf("%d", & r);
  for (i = 0; i < r; i++) {
    for (s = 1; s <= r - i; s++)
      printf("  ");
    for (j = 0; j <= i; j++)
      printf("%4d", i+1);
    printf("\n");
  }
  return 0;
}

Program in C++ :

/*
           1
         2   2
       3   3   3
     4   4   4   4
*/
#include <iostream>
#include <iomanip>
using namespace std;
int main() {
  int r, n = 1, s, i, j;
  cout << "\n Enter number of rows: ";
  cin >> r;
  for (i = 0; i < r; i++) {
    for (s = 1; s <= r - i; s++)
      cout << "  ";
    for (j = 0; j <= i; j++)
      cout << setw(4) << i+1;
    cout << "\n";
  }
  return 0;
}

Note: “%4d” in C and setw(4) in C++ are manipulators used to indicate that the value should be printed with a minimum field width of 4 characters. If it is less than 4 characters, the rest is filled with blanks to align the output. In C++, <iomanip> library must be included for this.

8. Number pyramid -4

Output-

Program in C :

/*
   4   4   4   4
     3   3   3
       2   2
         1
*/
#include <stdio.h>
int main() {
  int r, n = 1, s, i, j;
  printf("\n Enter number of rows: ");
  scanf("%d", & r);
  for (i = r; i > 0; i--) {
    for (s = 1; s <= r - i; s++)
      printf("  ");
    for (j = 0; j < i; j++)
      printf("%4d", i);
    printf("\n");
  }
  return 0;
}

Program in C++ :

/*
   4   4   4   4
     3   3   3
       2   2
         1
*/
#include <iostream>
#include <iomanip>
using namespace std;
int main() {
  int r, n = 1, s, i, j;
  cout << "\n Enter number of rows: ";
  cin >> r;
  for (i = r; i > 0; i--) {
    for (s = 1; s <= r - i; s++)
      cout << "  ";
    for (j = 0; j < i; j++)
      cout << setw(4) << i;
    cout << "\n";
  }
  return 0;
}

9. Number pyramid -5

Output-

Program in C :

/*
      1
    2 3 2
  3 4 5 4 3
4 5 6 7 6 5 4
*/
#include <stdio.h>
int main() {
  int r, i, j = 0, num = 0, k = 0, s;
  printf("\n Enter the number of r: ");
  scanf("%d", & r);
  for (i = 1; i <= r; i++) {
    for (s = 1; s <= r - i; s++, k++) {
      printf("  ");
    }
    while (j != 2 * i - 1) {
      if (k <= r - 1) {
        printf("%d ", i + j);
        k++;
      }
      else {
        num++;
        printf("%d ", (i + j - 2 * num));
      }
      j++;
    }
    num = k = j = 0;
    printf("\n");
  }
  return 0;
}

Program in C++ :

/*
      1
    2 3 2
  3 4 5 4 3
4 5 6 7 6 5 4
*/
#include <iostream>
using namespace std;
int main() {
  int r, i, j = 0, num = 0, k = 0, s;
  cout << "\n Enter the number of r: ";
  cin >> r;
  for (i = 1; i <= r; i++) {
    for (s = 1; s <= r - i; s++, k++) {
      cout << "  ";
    }
    while (j != 2 * i - 1) {
      if (k <= r - 1) {
        cout << i + j << " ";
        k++;
      } else {
        num++;
        cout << (i + j - 2 * num) << " ";
      }
      j++;
    }
    num = k = j = 0;
    cout << "\n";
  }
  return 0;
}

10. Number diamond pattern

Program in C :

/*
       1
     2 2 2
   3 3 3 3 3
 4 4 4 4 4 4 4
   3 3 3 3 3
     2 2 2
       1
*/
#include <stdio.h>
int main() {
  int i, j, k, n, r;
  printf("\n Enter number of rows: ");
  scanf("%d", & r);
  n = r - 1;
  printf("\n");
  for (i = 1; i <= r; i++) {
    for (j = 1; j <= n; j++)
      printf("  ");
    n--;
    for (k = 1; k <= 2 * i - 1; k++)
      printf(" %d", i);
    printf("\n");
  }
  n = 1;
  r--;
  for (i = 1; r >= i; r--) {
    for (j = 1; j <= n; j++)
      printf("  ");
    n++;
    for (k = 1; k <= 2 * (r - i) + 1; k++)
      printf(" %d", r);
    printf("\n");
  }
  return 0;
}

Program in C++ :

/*
       1
     2 2 2
   3 3 3 3 3
 4 4 4 4 4 4 4
   3 3 3 3 3
     2 2 2
       1
*/
#include <iostream>
using namespace std;
int main() {
  int i, j, k, n, r;
  cout << "\n Enter number of rows: ";
  cin >> r;
  n = r - 1;
  cout << "\n";
  for (i = 1; i <= r; i++) {
    for (j = 1; j <= n; j++)
      cout << "  ";
    n--;
    for (k = 1; k <= 2 * i - 1; k++)
      cout << " " << i;
    cout << "\n";
  }
  n = 1;
  r--;
  for (i = 1; r >= i; r--) {
    for (j = 1; j <= n; j++)
      cout << "  ";
    n++;
    for (k = 1; k <= 2 * (r - i) + 1; k++)
      cout << " " << r;
    cout << "\n";
  }
  return 0;
}

Parts of program-

1. #include is a pre-processor directive. It is used to include header files.
2. stdio/iostream is header file which has certain commands that c/c++ supports. E.g. return, main, etc.
3. .h is an extension for header file.
4. int is data type. It indicates that the program returns a value.
5. main() is main function that indicates the compiler that the user written programs starts from here.
6. print/cout is the keyword use to print a message.
7. scanf/cin is the keyword use to store values in variables.
8. return statement is use to return a value. If any error occurs the program will return 0.

Conclusion-

So that’s how we print different number pattern programs in C/C++. We hope it was clear to you. If you find it useful then do share it with your programming buddies and friends. Also if you have any doubt regarding any post do tell us in the comment section.

For daily updates do follow us on Instagram. Visit again!

ErrorFreeProgram.

1 COMMENT

Leave a Reply

Your email address will not be published. Required fields are marked *