Print N pattern
The task is to write a program that displays the N pattern of a given size. The N pattern is formed by printing asterisks (*) in a specific pattern that resembles the letter N.
Problem Statement
Given an integer size, the program should print the N pattern with the specified size. The pattern consists of asterisks (*) arranged in such a way that they form the shape of the letter N.
For example, for size = 5, the pattern looks like:
* * ** * * * * * ** * *
Similarly, for size = 7, the pattern looks like:
* * ** * * * * * * * * * * * ** * *
The task is to implement a C program that takes an integer size as input and prints the N pattern accordingly. The program should handle various test cases and display the output correctly.
Algorithm and Pseudocode
1. Start the program.
2. Define a function called "space" that takes an integer size as input.
3. Inside the "space" function, use a loop to print spaces based on the given size.
4. Define another function called "print_n" that takes an integer size as input.
5. Inside the "print_n" function, check if the size is less than or equal to 3. If so, return from the function as the pattern cannot be formed.
6. Print the size value as output to indicate the size of the pattern to be displayed.
7. Use a loop to iterate through each row of the pattern.
8. Inside the loop, print an asterisk (*) to start each row.
9. Check if the current row is not the first or last row. If so, print spaces and an asterisk (*) in the appropriate positions to form the N pattern.
10. If the current row is the first or last row, print spaces to maintain the pattern structure.
11. Print a new line after each row is printed.
12. Return to the loop and repeat steps 8-11 until all rows are printed.
13. Return to the main function.
14. Test the program by calling the "print_n" function with different test cases.
15. End the program.
The pseudocode for the program can be represented as:
function space(size): for i from 0 to size-1: print a space function print_n(size): if size <= 3: return print "Size: " + size for i from 0 to size-1: print an asterisk (*) if i is not 0 and i is not size-1: call space(i-1) print an asterisk (*) call space((size-2)-i) else: call space(size-2) print an asterisk (*) print a new line main: call print_n(5) call print_n(7) call print_n(4) call print_n(9)
Code Solution
// C Program
// Display N pattern
#include <stdio.h>
//include space
void space(int size)
{
for (int i = 0; i < size; ++i)
{
printf(" ");
}
}
void print_n(int size)
{
if (size <= 3)
{
return;
}
printf("Size : %d\n\n", size);
//Display Z pattern of given size
for (int i = 0; i < size; i++)
{
printf("*");
if (i != 0 && i != size - 1)
{
space(i - 1);
printf("*");
space((size - 2) - i);
}
else
{
space(size - 2);
}
printf("*");
printf("\n");
}
printf("\n");
}
int main()
{
//Test Case
print_n(5);
print_n(7);
print_n(4);
print_n(9);
return 0;
}
Output
Size : 5
* *
** *
* * *
* **
* *
Size : 7
* *
** *
* * *
* * *
* * *
* **
* *
Size : 4
* *
** *
* **
* *
Size : 9
* *
** *
* * *
* * *
* * *
* * *
* * *
* **
* *
// C++ program
// Display N pattern
#include<iostream>
using namespace std;
class MyPattern
{
public:
//include space
void space(int size)
{
for (int i = 0; i < size; ++i)
{
cout << " ";
}
}
void print_n(int size)
{
if (size <= 3)
{
return;
}
cout << "Size : " << size << "\n\n";
//Display Z pattern of given size
for (int i = 0; i < size; i++)
{
cout << "*";
if (i != 0 && i != size - 1)
{
this->space(i - 1);
cout << "*";
this->space((size - 2) - i);
}
else
{
this->space(size - 2);
}
cout << "*";
cout << "\n";
}
cout << "\n";
}
};
int main()
{
MyPattern obj = MyPattern();
//Test Case
obj.print_n(5);
obj.print_n(7);
obj.print_n(4);
obj.print_n(9);
return 0;
}
Output
Size : 5
* *
** *
* * *
* **
* *
Size : 7
* *
** *
* * *
* * *
* * *
* **
* *
Size : 4
* *
** *
* **
* *
Size : 9
* *
** *
* * *
* * *
* * *
* * *
* * *
* **
* *
// C# program
// Display N pattern
using System;
class MyPattern
{
//include space
public void space(int size)
{
for (int i = 0; i < size; i++)
{
Console.Write(" ");
}
}
public void print_n(int size)
{
if (size <= 3)
{
return;
}
Console.Write("Size : " + size + "\n\n");
//Display Z pattern of given size
for (int i = 0; i < size; i++)
{
Console.Write("*");
if (i != 0 && i != size - 1)
{
space(i - 1);
Console.Write("*");
space((size - 2) - i);
}
else
{
space(size - 2);
}
Console.Write("*");
Console.Write("\n");
}
Console.Write("\n");
}
public static void Main(String[] args)
{
MyPattern obj = new MyPattern();
//Test Case
obj.print_n(5);
obj.print_n(7);
obj.print_n(4);
obj.print_n(9);
}
}
Output
Size : 5
* *
** *
* * *
* **
* *
Size : 7
* *
** *
* * *
* * *
* * *
* **
* *
Size : 4
* *
** *
* **
* *
Size : 9
* *
** *
* * *
* * *
* * *
* * *
* * *
* **
* *
// Java program
// Display N pattern
class MyPattern
{
//include space
public void space(int size)
{
for (int i = 0; i < size; ++i)
{
System.out.print(" ");
}
}
public void print_n(int size)
{
if (size <= 3)
{
return;
}
System.out.print("Size : " + size + "\n\n");
//Display Z pattern of given size
for (int i = 0; i < size; i++)
{
System.out.print("*");
if (i != 0 && i != size - 1)
{
space(i - 1);
System.out.print("*");
space((size - 2) - i);
}
else
{
space(size - 2);
}
System.out.print("*");
System.out.print("\n");
}
System.out.print("\n");
}
public static void main(String[] args)
{
MyPattern obj = new MyPattern();
//Test Case
obj.print_n(5);
obj.print_n(7);
obj.print_n(4);
obj.print_n(9);
}
}
Output
Size : 5
* *
** *
* * *
* **
* *
Size : 7
* *
** *
* * *
* * *
* * *
* **
* *
Size : 4
* *
** *
* **
* *
Size : 9
* *
** *
* * *
* * *
* * *
* * *
* * *
* **
* *
<?php
// Php program
// Display N pattern
class MyPattern
{
//include space
public function space($size)
{
for ($i = 0; $i < $size; ++$i)
{
echo(" ");
}
}
public function print_n($size)
{
if ($size <= 3)
{
return;
}
echo("Size : ". $size ."\n\n");
//Display Z pattern of given size
for ($i = 0; $i < $size; $i++)
{
echo("*");
if ($i != 0 && $i != $size - 1)
{
$this->space($i - 1);
echo("*");
$this->space(($size - 2) - $i);
}
else
{
$this->space($size - 2);
}
echo("*");
echo("\n");
}
echo("\n");
}
}
function main()
{
$obj = new MyPattern();
//Test Case
$obj->print_n(5);
$obj->print_n(7);
$obj->print_n(4);
$obj->print_n(9);
}
main();
Output
Size : 5
* *
** *
* * *
* **
* *
Size : 7
* *
** *
* * *
* * *
* * *
* **
* *
Size : 4
* *
** *
* **
* *
Size : 9
* *
** *
* * *
* * *
* * *
* * *
* * *
* **
* *
// Node Js program
// Display N pattern
class MyPattern
{
//include space
space(size)
{
for (var i = 0; i < size; ++i)
{
process.stdout.write(" ");
}
}
print_n(size)
{
if (size <= 3)
{
return;
}
process.stdout.write("Size : " + size + "\n\n");
//Display Z pattern of given size
for (var i = 0; i < size; i++)
{
process.stdout.write("*");
if (i != 0 && i != size - 1)
{
this.space(i - 1);
process.stdout.write("*");
this.space((size - 2) - i);
}
else
{
this.space(size - 2);
}
process.stdout.write("*");
process.stdout.write("\n");
}
process.stdout.write("\n");
}
}
function main(args)
{
var obj = new MyPattern();
//Test Case
obj.print_n(5);
obj.print_n(7);
obj.print_n(4);
obj.print_n(9);
}
main();
Output
Size : 5
* *
** *
* * *
* **
* *
Size : 7
* *
** *
* * *
* * *
* * *
* **
* *
Size : 4
* *
** *
* **
* *
Size : 9
* *
** *
* * *
* * *
* * *
* * *
* * *
* **
* *
# Python 3 program
# Display N pattern
class MyPattern :
# include space
def space(self, size) :
i = 0
while (i < size) :
print(" ", end = "")
i += 1
def print_n(self, size) :
if (size <= 3) :
return
print("Size : ", size ,"\n\n", end = "")
# Display Z pattern of given size
i = 0
while (i < size) :
print("*", end = "")
if (i != 0 and i != size - 1) :
self.space(i - 1)
print("*", end = "")
self.space((size - 2) - i)
else :
self.space(size - 2)
print("*", end = "")
print("\n", end = "")
i += 1
print("\n", end = "")
def main() :
obj = MyPattern()
# Test Case
obj.print_n(5)
obj.print_n(7)
obj.print_n(4)
obj.print_n(9)
if __name__ == "__main__": main()
Output
Size : 5
* *
** *
* * *
* **
* *
Size : 7
* *
** *
* * *
* * *
* * *
* **
* *
Size : 4
* *
** *
* **
* *
Size : 9
* *
** *
* * *
* * *
* * *
* * *
* * *
* **
* *
# Ruby program
# Display N pattern
class MyPattern
# include space
def space(size)
i = 0
while (i < size)
print(" ")
i += 1
end
end
def print_n(size)
if (size <= 3)
return
end
print("Size : ", size ,"\n\n")
# Display Z pattern of given size
i = 0
while (i < size)
print("*")
if (i != 0 && i != size - 1)
self.space(i - 1)
print("*")
self.space((size - 2) - i)
else
self.space(size - 2)
end
print("*")
print("\n")
i += 1
end
print("\n")
end
end
def main()
obj = MyPattern.new()
# Test Case
obj.print_n(5)
obj.print_n(7)
obj.print_n(4)
obj.print_n(9)
end
main()
Output
Size : 5
* *
** *
* * *
* **
* *
Size : 7
* *
** *
* * *
* * *
* * *
* **
* *
Size : 4
* *
** *
* **
* *
Size : 9
* *
** *
* * *
* * *
* * *
* * *
* * *
* **
* *
// Scala program
// Display N pattern
class MyPattern
{
//include space
def space(size: Int): Unit = {
var i: Int = 0;
while (i < size)
{
print(" ");
i += 1;
}
}
def print_n(size: Int): Unit = {
if (size <= 3)
{
return;
}
print("Size : " + size + "\n\n");
//Display Z pattern of given size
var i: Int = 0;
while (i < size)
{
print("*");
if (i != 0 && i != size - 1)
{
space(i - 1);
print("*");
space((size - 2) - i);
}
else
{
space(size - 2);
}
print("*");
print("\n");
i += 1;
}
print("\n");
}
}
object Main
{
def main(args: Array[String]): Unit = {
var obj: MyPattern = new MyPattern();
//Test Case
obj.print_n(5);
obj.print_n(7);
obj.print_n(4);
obj.print_n(9);
}
}
Output
Size : 5
* *
** *
* * *
* **
* *
Size : 7
* *
** *
* * *
* * *
* * *
* **
* *
Size : 4
* *
** *
* **
* *
Size : 9
* *
** *
* * *
* * *
* * *
* * *
* * *
* **
* *
// Swift program
// Display N pattern
class MyPattern
{
//include space
func space(_ size: Int)
{
var i: Int = 0;
while (i < size)
{
print(" ", terminator: "");
i += 1;
}
}
func print_n(_ size: Int)
{
if (size <= 3)
{
return;
}
print("Size : ", size ,"\n\n", terminator: "");
//Display Z pattern of given size
var i: Int = 0;
while (i < size)
{
print("*", terminator: "");
if (i != 0 && i != size - 1)
{
self.space(i - 1);
print("*", terminator: "");
self.space((size - 2) - i);
}
else
{
self.space(size - 2);
}
print("*", terminator: "");
print("\n", terminator: "");
i += 1;
}
print("\n", terminator: "");
}
}
func main()
{
let obj: MyPattern = MyPattern();
//Test Case
obj.print_n(5);
obj.print_n(7);
obj.print_n(4);
obj.print_n(9);
}
main();
Output
Size : 5
* *
** *
* * *
* **
* *
Size : 7
* *
** *
* * *
* * *
* * *
* **
* *
Size : 4
* *
** *
* **
* *
Size : 9
* *
** *
* * *
* * *
* * *
* * *
* * *
* **
* *
Resultant Output Explanation
The program generates the following output for the given test cases:
Size : 5 * * ** * * * * * ** * * Size : 7 * * ** * * * * * * * * * * * ** * * Size : 4 * * ** * * ** * * Size : 9 * * ** * * * * * * * * * * * * * * * * * ** * *
The output shows the N pattern for each given size. Each asterisk (*) represents a character in the pattern, while spaces are used to align the asterisks in the desired shape. The pattern follows the logic explained in the algorithm section.
The time complexity of the code is O(n^2) because there are two nested loops. The outer loop iterates through each row, and the inner loop prints the spaces and asterisks in each row. The space function has a time complexity of O(n) as it simply prints spaces based on the given size.
Overall, the program successfully generates the N pattern for a given size and handles different test cases accordingly.
Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.
New Comment