Author: admin

  • Neon Number in C++

    In this tutorial, we will write a neon number program in Cpp. We will write two different programs.

    1. Check for neon number
    2. Display the neon number within the range

    Neon Numbers

    A number is said to be a neon number if the sum of the digits of a square of the number is equal t the original number.

    Example: 9 is a Neon number (see explanation in the diagram).

    Neon Number in C

    Neon Number in C++

    C++ Program to check for neon numbers.

    //Cpp Program to check whether a given number is a Neon number or not
    #include <iostream>
    using namespace std;
    
    int main()
    {
      int num;
    
      cout << "Enter the number: ";
      cin >> num;
    
      int square = num * num;
      int sum = 0;
    
      while (square > 0)
      {
        int lastDigit = square % 10;
        sum = sum + lastDigit;
        square = square / 10;
      }
    
      if (sum == num)
        cout << num << " is a Neon number";
      else
        cout << num << " is NOT a Neon number";
    
      return 0;
    }

    Output:

    Enter the number: 9
    9 is a Neon number


    C++ program to display all the neon number within a given range

    //Cpp Program to check whether a given number is a Neon number or not
    #include <iostream>
    using namespace std;
    
    int checkForNeon(int x)
    {
      int square = x * x;
      int sumOfDigits = 0;
    
      while (square != 0)
      {
        sumOfDigits = sumOfDigits + square % 10;
        square /= 10;
      }
    
      return (sumOfDigits == x);
    }
    
    // Main function
    int main(void)
    {
      int lowestNum, highestNum;
    
      cout << "ENTER THE STARTING AND FINAL NUMBER FOR THE RANGE TO CHECK\n";
    
      cout << "\nEnter the lowest number: ";
      cin >> lowestNum;
    
      cout << "Enter the Highest number: ";
      cin >> highestNum;
    
      cout << "\nNeon Number between them are:";
      for (int i = lowestNum; i <= highestNum; i++)
      {
        if (checkForNeon(i))
          cout << i << " ";
      }
    }

    Output:

    ENTER THE STARTING AND FINAL NUMBER FOR THE RANGE TO CHECK

    Enter the lowest number: 1
    Enter the Highest number: 2000

    Neon Number between them are:1 9


  • Half Diamond number pattern in C

    In this tutorial, we will write a C Program to display full hourglass patterns using numbers. Before that, you may go through the following topic in C.

    Half Diamond number pattern in C

    We will write various programs for the left half and right half diamond with different kinds of number patterns. These programs can also be called the arrowhead pattern programs in C.

    Right Half Diamond number patter

    1. Increasing number pattern

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

    Output:

    Enter the no. of rows: 5
    1
    1 2
    1 2 3
    1 2 3 4
    1 2 3 4 5
    1 2 3 4
    1 2 3
    1 2
    1


    2. Mirror number pattern

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

    Output:

    Enter the no of rows: 5
    1
    121
    12321
    1234321
    123454321
    1234321
    12321
    121
    1


    3. Increment in two numbers

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

    Output:

    Enter the no. of rows: 5
    1
    123
    12345
    1234567
    123456789
    1234567
    12345
    123
    1


    Left half diamond pattern in C

    Pattern 1:

    #include <stdio.h>
    
    int main()
    {
      int i, j, rows;
    
      printf("Enter the no of rows: ");
      scanf("%d", &rows);
    
      // upper half
      for (i = 1; i <= rows; i++)
      {
        for (j = rows - 1; j >= i; j--)
          printf(" ");
    
        for (j = 1; j <= i; j++)
          printf("%d", j);
    
        printf("\n");
      }
    
      //lower part
      for (i = rows - 1; i >= 1; i--)
      {
        for (j = i; j < rows; j++)
          printf(" ");
    
        for (j = 1; j <= i; j++)
          printf("%d", j);
    
        printf("\n");
      }
    
      return 0;
    }

    Output:

    Enter the no of rows: 5
        1
       12
      123
     1234
    12345
     1234
      123
       12
        1

    Pattern 2:

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

    Output

    Enter the no. of rows: 5
            1
          123
        12345
      1234567
    123456789
      1234567
        12345
          123
            1

  • Half Hourglass Number pattern in C

    In this tutorial, we will write a C Program to display half hourglass patterns using numbers. Before that, you may go through the following topic in C.

    Half Hourglass number pattern in C

    1. Left half hourglass number pattern

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

    Output:

    Enter the number of rows: 5
    12345
     2345
      345
       45
        5
       45
      345
     2345
    12345

    2. Right half hourglass number pattern

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

    Output:

    Enter the no. of rows: 5
    12345
    1234
    123
    12
    1
    12
    123
    1234
    12345


  • Left Half Pyramids in C using Numbers

    In this tutorial, we will learn and write codes for various half pyramids number patterns in C programming language. However, in this tutorial, we will create a numeric pattern in C using for loop. So you may go through the following topic in C.

    • for loop in C : loops plays a key role to achieve various patterns in any programming language. Mastering patterns means having excellent grasp at loops.

    Left Half Pyramids in C using Numbers

    We will look at the following topic of pyramid patterns in C. Each of the programs is consists of different number patterns.

    • Left half pyramid
    • Inverted left half pyramid

    Left half pyramid pattern of numbers in C

    Pattern 1:

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

    Output:

    Enter the no. of rows: 5
             1
           1 2
         1 2 3
       1 2 3 4
     1 2 3 4 5

    Inverted left half pyramid patterns of Numbers in C

    Pattern 1:

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

    Output:

    Enter the no. of rows: 5
       1 2 3 4 5
         2 3 4 5
           3 4 5
             4 5
               5

  • Half Pyramid Number Pattern in C

    In this tutorial, we will learn and write codes for various half pyramids number patterns in C programming language. However, in this tutorial, we will create a numeric pattern in C using for loop. So you may go through the following topic in C.

    • for loop in C : loops plays a key role to achieve various patterns in any programming language. Mastering patterns means having excellent grasp at loops.

    Half Pyramid Number Pattern in C

    We will look at the following topic of pyramid patterns in C. Each of the programs is consists of different number patterns.

    • Right half pyramid
    • Inverted right half pyramid

    Right half Pyramid Pattern

    Pattern 1: Also called the right triangle pattern.

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

    Enter the no. of rows: 5
    1
    1 2
    1 2 3
    1 2 3 4
    1 2 3 4 5


    Pattern 2:

    #include <stdio.h>
    
    int main()
    {
      int rows, count = 1;
    
      printf("Enter the number of rows: ");
      scanf("%d", &rows);
    
      for (int i = 1; i <= rows; i++)
      {
        for (int j = 1; j <= i; j++)
        {
          printf("%d ", count);
          ++count;
        }
    
        printf("\n");
      }
    
      return 0;
    }

    Enter the number of rows: 4
    1
    2 3
    4 5 6
    7 8 9 10


    Pattern 3: Pattern with a binary number in C

    #include <stdio.h>
    
    int main()
    {
      int i, j, rows;
    
      printf("Enter the number of rows: ");
      scanf("%d", &rows);
    
      for (i = 1; i <= rows; i++)
      {
        for (j = 1; j <= i; j++)
        {
          if (j % 2 == 0)
            printf("0");
          else
            printf("1");
        }
    
        printf("\n");
      }
    
      return 0;
    }

    Enter the number of rows: 6
    1
    10
    101
    1010
    10101
    101010


    Pattern 4: Right-angled triangle pattern

    The patterns have a similar number in each row. It can also be called half pyramid number patterns in C.

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

    Enter the no. of rows: 5
    1
    2 2
    3 3 3
    4 4 4 4
    5 5 5 5 5


    Pattern 5:

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

    Enter the no. of rows: 10
    1
    2 4
    3 6 9
    4 8 12 16
    5 10 15 20 25
    6 12 18 24 30 36
    7 14 21 28 35 42 49
    8 16 24 32 40 48 56 64
    9 18 27 36 45 54 63 72 81
    10 20 30 40 50 60 70 80 90 100


    Pattern 6:

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

    Enter the no. of rows: 5
    5
    5 4
    5 4 3
    5 4 3 2
    5 4 3 2 1


    Pattern 7:

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

    Enter the no. of rows: 5
    5
    4 4
    3 3 3
    2 2 2 2
    1 1 1 1 1


    Inverted right half Pyramid Pattern

    Pattern 1:

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

    Enter the no. of rows: 6
    1 2 3 4 5 6
    1 2 3 4 5
    1 2 3 4
    1 2 3
    1 2
    1


    Pattern 2:

    Half inverted right-angled triangle with decreasing order.

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

    Enter the no. of rows: 5
    5 4 3 2 1
    4 3 2 1
    3 2 1
    2 1
    1


    Pattern 3:

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

    Enter the no. of rows: 5
    5 5 5 5 5
    4 4 4 4
    3 3 3
    2 2
    1


    Pattern 4:

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

    Enter the no. of rows: 5
    5 4 3 2 1
    5 4 3 2
    5 4 3
    5 4
    5


    Pattern 5:

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

    Enter the no. of rows: 5
    1 2 3 4 5
    2 3 4 5
    3 4 5
    4 5
    5


    Pattern 6:

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

    Enter the no. of rows: 5
    1 1 1 1 1
    2 2 2 2
    3 3 3
    4 4
    5


  • Full Pyramid Number Pattern in C

    In this tutorial, we will write a C Program to display Pyramid patterns using numbers. Before that, you may go through the following topic in C.

    Full Pyramid Number Pattern in C

    1. Full pyramid with mirror pattern of numbers.

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

    2. Full pyramid with repeating pattern of numbers.

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

    3. Full pyramid pattern of numbers.

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

  • Hourglass number Pattern in C

    In this tutorial, we will write a C Program to display full hourglass patterns using numbers. Before that, you may go through the following topic in C.

    Hourglass number Pattern in C

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

    Output:

    Enter the number of rows: 6
    1 2 3 4 5 6 
     2 3 4 5 6 
      3 4 5 6 
       4 5 6 
        5 6 
         6 
        5 6 
       4 5 6 
      3 4 5 6 
     2 3 4 5 6 
    1 2 3 4 5 6

  • Inverted hollow Triangle Star pattern in C

    In this tutorial, we will write a C program to display a hollow inverted triangle star pattern. Before that, you may go through the following topics in C.

    Inverted hollow Triangle Star pattern in C

    It is also called Hollow Pyramid Star Pattern in C.

    #include <stdio.h>
    
    int main()
    {
      int i, j, k, m = 1, rows;
    
      printf("Enter the no. of rows: ");
      scanf("%d", &rows);
    
      for (i = rows; i >= 1; i--)
      {
        for (j = 1; j < m; j++)
          printf(" ");
    
        for (k = 1; k <= 2 *i - 1; k++)
        {
          if (k == 1 || k == 2 *i - 1 || i == rows)
            printf("*");
          else
            printf(" ");
        }
        m++;
    
        printf("\n");
      }
    
      return 0;
    }

    Output:

    inverted hollow triangle pattern in c

  • Diagonal Number Pattern in C

    In this tutorial, we will learn and write a program in C to display the diagonal number pattern. So you may go through the following topic in C.

    Diagonal Number Pattern in C

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

    Output:

    Diagonal Number Pattern in C 1

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

    Output:

    Diagonal Number Pattern in C 2

  • Diagonal Star Pattern in C

    In this tutorial, we will write a program in C to display the diagonal star pattern. So you may go through the following topic in C.

    Diagonal Star Pattern in C

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

    Output:

    Diagonal Star Pattern in C