1 7
12 67
123 567
1234567
123 567
12 67
1 7
Problem statement:- Program to print a butterfly shape Number pattern.
Data requirement:-
Input Data:- row_size
Output Data:- in or inn(for python)
Additional Data:-out, print_control_x
Program in C
Here is the source code of the C Program to print a butterfly shape Number pattern.
Code:
#include <stdio.h>int main(){    printf("Enter the row size(Odd Number):");    int row_size;    scanf("%d", &row_size);    int in, out;    int print_control_x = 1;
    for (out = 1; out <= row_size; out++)    {        for (in = 1; in <= row_size; in++)        {            if (in <= print_control_x || in >= row_size - print_control_x + 1)            {                printf("%d", in);            }            else            {                printf(" ");            }        }        if (out <= row_size / 2)            print_control_x++;        else            print_control_x--;        printf("\n");    }}Enter the row size(Odd Number):7
1          7
12      67
123  567
1234567
123  567
12      67
1          7
Program in C++
Here is the source code of the C++ Program to print a butterfly shape Number pattern.
Code:
#include <iostream>using namespace std;int main(){    cout << "Enter the row size(Odd Number):";    int row_size;    cin >> row_size;    int in, out;    int print_control_x = 1;    for (out = 1; out <= row_size; out++)    {        for (in = 1; in <= row_size; in++)        {            if (in <= print_control_x || in >= row_size - print_control_x + 1)            {                cout << in;            }            else            {                cout << " ";            }        }        if (out <= row_size / 2)            print_control_x++;        else            print_control_x--;        cout << "\n";    }}Enter the row size(Odd Number):5
1 5
12 45
12345
12 45
1 5
Program in Java
Here is the source code of the Java Program to print a butterfly shape Number pattern.
Code:
import java.util.Scanner;public class ButterflyNumberPattern {    public static void main(String[] args) {        Scanner cs = new Scanner(System.in);        System.out.println("Enter the row size(Odd Number):");        int out, in;        int row_size = cs.nextInt();        int print_control_x = 1;        for (out = 1; out <= row_size; out++) {            for (in = 1; in <= row_size; in++) {                if (in <= print_control_x || in >= row_size - print_control_x + 1) {                    System.out.print(in);                } else {                    System.out.printf(" ");                }            }            if (out <= row_size / 2)                print_control_x++;            else                print_control_x--;            System.out.println();        }        cs.close();    }}Enter the row size(Odd Number):7
1          7
12      67
123  567
1234567
123  567
12      67
1          7
Program in Python
Here is the source code of the Python Program to print a butterfly shape Number pattern.
Code:
row_size = int(input("Enter the row size:"))print_control_x = 1for out in range(1, row_size+1):    for inn in range(1, row_size+1):        if inn <= print_control_x or inn >= row_size-print_control_x+1:            print(inn, end="")        else:            print(" ", end="")    if out <= row_size // 2:        print_control_x += 1    else:        print_control_x -= 1    print("\r")Enter the row size:9
1 9
12 89
123 789
1234 6789
123456789
1234 6789
123 789
12 89
1 9
 

0 Comments
Please do not Enter any spam link in the comment box