Facebook Twitter Instagram
    DeepCrazyWorld
    Facebook Twitter Instagram Pinterest YouTube
    • FLUTTER
      • FLUTTER APP
        • QRCode
        • Quiz App
        • Chat GPT
        • PDF App
        • News App
        • Fitness App
        • Weather App
        • BMI Calculator
        • GAME APP
        • Ecommerce App
        • wallpaper App
        • Finance app
        • Chat App
        • Wallet App
        • Taxi App
        • Quran app
        • Music player app
      • FLUTTER UI
        • Splash Screen
        • Onboarding Screen
        • Login Screen
        • Card Design
        • Drawer
    • PROJECT
      • Android Projects
      • College Projects
      • FLUTTER APP
      • Project Ideas
      • PHP Projects
      • Python Projects
    • SOURCE CODE
    • ANDROID
      • ANDROID APP
      • GAME APP
      • ANDROID STUDIO
    • MCQ
      • AKTU MCQ
        • RPA MCQ
        • COA MCQ
        • HPC MCQ
        • SPM MCQ
        • Renewable Energy All MCQ
        • Data Compression MCQ
        • Data Structure MCQ
        • Digital Image Processing MCQ
        • Software Engineering MCQ
        • Machine Learning MCQ
        • Artificial Intelligence MCQ
      • D PHARMA MCQ
        • Pharmaceutics – I MCQ
        • Pharmacognosy MCQ
        • Pharmaceutical Chemistry MCQ
        • Biochemistry and Clinical Pathology MCQ
        • Human Anatomy and Physiology MCQ
        • Heath Education and Community Pharmacy MCQ
    • INTERVIEW QUESTIONS
      • Flutter Interview Questions
      • INTERVIEW QUESTIONS
      • Python Interview Questions
      • Coding ninjas solution
    • MORE
      • WORDPRESS
        • SEO
        • TOP 10 WORDPRESS THEME
      • PRODUCTIVITY
      • Program
      • QUOTES
    DeepCrazyWorld
    Home»Coding Interview Questions»Rectangular Numbers Coding Ninjas question with solution
    Coding Interview Questions

    Rectangular Numbers Coding Ninjas question with solution

    DeepikaBy DeepikaOctober 8, 2023Updated:October 8, 2023No Comments5 Mins Read

    In this blog post we will Provide Coding Ninjas Basics of Java with Data Structures and Algorithms Solution. This will have solutions to all the problems that are included in Coding Ninjas Java Course.  Rectangular Numbers Coding Ninjas question with solution.

    Table of Contents

    Toggle
    • Rectangular Numbers Coding Ninjas question
    • Coding ninjas solutions
      • Java Language [Solutions 1]
      • Output
      • C++ [Solutions 2]
      • Python3 [Solutions 3]
      • C# [Solutions 4]
      • JavaScript [Solutions 5]
    • Conclusion
        • Related Articles:
      • READ MORE

    Rectangular Numbers Coding Ninjas question


    Rectangular numbers
    Send Feedback
    Print the following pattern for the given number of rows.


    Pattern for N = 4
    4444444
    4333334
    4322234
    4321234
    4322234
    4333334
    4444444


    Input format : N (Total no. of rows)
    Output format : Pattern in N lines


    Sample Input :
    3


    Sample Output :
    33333
    32223
    32123
    32223
    33333

    <img decoding=
    Rectangular Numbers Coding Ninjas question with solution

    Coding ninjas solutions

    Java Language [Solutions 1]

    public class RectangularNumbers { 
        // coding ninjas solution
        public static void print(int n) {
    
            // Write your code here
            int i, j, a;
            for (i = -(n - 1); i < n; i++) {
                a = n;
                for (j = -(n - 1); j < n; j++) {
                    if (Math.abs(i) < Math.abs(j)) {
                        if (j < 0) {
                            System.out.print(a);
                            a--;
                        } else {
                            a++;
                            System.out.print(a);
                        }
                    } else {
                        System.out.print(a);
                    }
                }
                System.out.println();
            }
        }
        //main driver code
        public static void main(String[] args) {
            print(4);
        }
    }

    Output

    java -cp /tmp/kY2Dxx35A3 RectangularNumbers
    4444444
    4333334
    4322234
    4321234
    4322234
    4333334
    4444444

    C++ [Solutions 2]

    // C++ Program to print rectangular
    // inner reducing pattern
    #include <bits/stdc++.h>
    using namespace std;
    
    #define max 100
    
    // function to Print pattern
    void print(int a[][max], int size) 
    {
    for (int i = 0; i < size; i++) {
    	for (int j = 0; j < size; j++) {
    	cout << a[i][j];
    	}
    	cout << endl;
    }
    }
    
    // function to compute pattern
    void innerPattern(int n) {
    	
    // Pattern Size
    int size = 2 * n - 1;
    int front = 0;
    int back = size - 1;
    int a[max][max];
    while (n != 0) 
    {
    	for (int i = front; i <= back; i++) {
    	for (int j = front; j <= back; j++) {
    		if (i == front || i == back ||
    			j == front || j == back)
    		a[i][j] = n;
    	}
    	}
    	++front;
    	--back;
    	--n;
    }
    print(a, size);
    }
    
    // Driver code
    int main()
    {
    	// Input
    	int n = 4; 
    	
    	// function calling
    	innerPattern(n);
    	
    return 0;
    }
    
    // This code is contributed by Anant Agarwal.
    

    Python3 [Solutions 3]

    # Python3 Program to print rectangular 
    # inner reducing pattern 
    MAX = 100
    
    # function to Print pattern 
    def prints(a, size): 
    	for i in range(size): 
    		for j in range(size):
    			print(a[i][j], end = '')
    		print()
    
    # function to compute pattern 
    def innerPattern(n): 
    	
    	# Pattern Size 
    	size = 2 * n - 1
    	front = 0
    	back = size - 1
    	a = [[0 for i in range(MAX)] 
    			for i in range(MAX)]
    	while (n != 0): 
    		for i in range(front, back + 1):
    			for j in range(front, back + 1):
    				if (i == front or i == back or
    					j == front or j == back):
    					a[i][j] = n
    		front += 1
    		back -= 1
    		n -= 1
    	prints(a, size); 
    
    # Driver code 
    
    # Input 
    n = 4
    
    # function calling 
    innerPattern(n)
    
    
    

    C# [Solutions 4]

    using System;
    
    public class Pattern {
    
    // function to compute pattern
    public static void InnerPattern(int n)
    {
    	
    	// Pattern Size
    	int size = 2 * n - 1;
    	int front = 0;
    	int back = size - 1;
    	int[, ] a = new int[size, size];
    
    	while (n != 0) {
    	for (int i = front; i <= back; i++) {
    		for (int j = front; j <= back; j++) {
    		if (i == front || i == back
    			|| j == front || j == back)
    			a[i, j] = n;
    		}
    	}
    	++front;
    	--back;
    	--n;
    	}
    	Print(a, size);
    }
    
    // function to Print pattern
    public static void Print(int[, ] a, int size)
    {
    	for (int i = 0; i < size; i++) {
    	for (int j = 0; j < size; j++) {
    		Console.Write(a[i, j]);
    	}
    	Console.WriteLine();
    	}
    }
    
    // Main Method
    public static void Main(string[] args)
    {
    	int n = 4; // Input
    	InnerPattern(n);
    }
    }
    

    JavaScript [Solutions 5]

    const MAX = 100;
    
    // function to Print pattern 
    function prints(a, size) {
    for (let i = 0; i < size; i++) {
    	let row = '';
    	for (let j = 0; j < size; j++) {
    	row += a[i][j];
    	}
    	console.log(row);
    }
    }
    
    // function to compute pattern 
    function innerPattern(n) {
    // Pattern Size 
    const size = 2 * n - 1;
    let front = 0;
    let back = size - 1;
    let a = new Array(MAX).fill(0).map(() => new Array(MAX).fill(0));
    while (n !== 0) {
    	for (let i = front; i <= back; i++) {
    	for (let j = front; j <= back; j++) {
    		if (i === front || i === back || j === front || j === back) {
    		a[i][j] = n;
    		}
    	}
    	}
    	front += 1;
    	back -= 1;
    	n -= 1;
    }
    prints(a, size);
    }
    
    // Driver code 
    
    // Input 
    const n = 4;
    
    // function calling 
    innerPattern(n);
    

    Conclusion

    Coding Ninja primarily targets students and professionals looking to enhance their coding and programming skills, prepare for technical interviews, or pursue careers in software development and related fields. It has gained a reputation for its quality courses and experienced instructors, making it a popular choice among those looking to improve their coding abilities.

    Related Articles:

    • About coding ninja questions
    • Rectangular Numbers Coding Ninjas questions with solution
    • How to Install Flutter in windows 10
    • How to Setup Space Between Elements In Flutter 
    • Flutter Card Widget with Example
    • Integrating an API into a Flutter – Working with REST APIs
    • Create Login Page UI Design and Animation For Flutter

    READ MORE

    Share. Facebook Twitter LinkedIn WhatsApp Telegram Pinterest Reddit Email
    Previous ArticleTell me about Coding Ninjas | Learn Coding, Programming Online
    Next Article Coding Ninjas question – Given a graph with N vertices

    Related Posts

    Given a NxM matrix containing Uppercase – Coding Ninjas question

    Coding ninjas solution 3 Mins Read

    Coding Ninjas question – Given a graph with N vertices

    Coding ninjas solution 3 Mins Read

    Tell me about Coding Ninjas | Learn Coding, Programming Online

    Coding ninjas solution 3 Mins Read

    Top 30+ Coding Interview Questions with solution 2023 (updated)

    Coding Interview Questions 3 Mins Read

    Leave A Reply Cancel Reply

    Recent Posts
    • Implementing a Dynamic FAQ Screen UI in Flutter Using ExpansionTile March 29, 2025
    • Creating an Instruction UI Screen in Flutter Application March 29, 2025
    • Animated Backgrounds in Flutter: A Complete Guide March 15, 2025
    • How to make Diary App using flutter stepwise using getx August 31, 2024
    • How to Create Music Player UI screen with fully functional in flutter August 30, 2024
    • How to make ListView Builder Ui in flutter with Source Code August 29, 2024
    • Create a TabBar View in flutter with fully functional stepwise August 28, 2024
    • How to create TabBar view in flutter with source code step wise August 27, 2024
    • How to make Heart rate measure app with Flutter stepwise August 26, 2024
    • How to make ChatGpt App in flutter with source code Stepwise August 25, 2024
    Facebook Twitter Instagram Pinterest YouTube
    • About
    • Contact
    • Disclaimer
    • Privacy Policy
    Copyright by DeepCrazyWorld © 2025

    Type above and press Enter to search. Press Esc to cancel.