Print digit pattern
The task is to write a program that prints a digit pattern based on the given input number. The input number represents a sequence of digits, and each digit determines the number of asterisks (*) to be printed on a new line. The program uses recursion to extract each digit from the number and iteratively prints asterisks based on the digit value.
Example:
Let's understand the problem with an example. Consider the input number 53235.
- The first digit is 5, so we print "*****" (five asterisks).
- The second digit is 3, so we print "***" (three asterisks).
- The third digit is 2, so we print "**" (two asterisks).
- The fourth digit is 3, so we print "***" (three asterisks).
- The fifth digit is 5, so we print "*****" (five asterisks).
The final output for the number 53235 would be:
#***** #*** #** #*** #*****
Algorithm:
- Define a function
printValue
that takes an integer parameternum
. - If
num
is greater than zero, perform the following steps: - Call
printValue(num / 10)
recursively to process the next digit. - Calculate the digit value by finding the remainder of
num
divided by 10. - Print the initial "#" symbol.
- Use a loop to print "*" based on the digit value.
- Define another function
printPattern
that takes an integer parameternum
. - If
num
is less than or equal to zero, return. - Print the given number using
printf
. - Call
printValue(num)
to print the digit pattern. - Write the
main
function: - Call
printPattern
with test cases. - Return 0 to indicate successful program execution.
Pseudocode:
function printValue(num):
if num > 0:
printValue(num / 10)
digit = num % 10
print "#"
for i from 0 to digit:
print "*"
function printPattern(num):
if num <= 0:
return
print "Given num: " + num
printValue(num)
function main():
printPattern(53235)
printPattern(84033)
Explanation:
The program defines two functions: printValue
and printPattern
.
The printValue
function is a recursive function that takes an integer num
. It first divides num
by 10 and calls itself recursively to process the next digit. It then calculates the remainder to obtain the current digit value. The function then prints the "#" symbol and uses a loop to print the required number of asterisks based on the digit value.
The printPattern
function takes an integer num
as input. If num
is less than or equal to zero, it returns immediately. Otherwise, it prints the given number and calls the printValue
function to print the digit pattern.
In the main
function, we call the printPattern
function with two test cases: 53235 and 84033.
Code Solution
// C program for
// Print digit pattern
#include <stdio.h>
void printValue(int num)
{
if (num > 0)
{
// Recursive function calling
printValue(num / 10);
int digit = num % 10;
// Include initial value
printf("\n#");
// Print the *by given digit
for (int i = 0; i < digit; ++i)
{
printf("*");
}
}
}
void printPattern(int num)
{
if (num <= 0)
{
return;
}
// Display given number
printf("\nGiven num : %d ", num);
printValue(num);
}
int main(int argc, char
const *argv[])
{
// Test
printPattern(53235);
printPattern(84033);
return 0;
}
Output
Given num : 53235
#*****
#***
#**
#***
#*****
Given num : 84033
#********
#****
#
#***
#***
/*
Java Program
Print digit pattern
*/
public class Pattern
{
public void printValue(int num)
{
if (num > 0)
{
// Recursive function calling
printValue(num / 10);
int digit = num % 10;
// Include initial value
System.out.print("\n#");
// Print the *by given digit
for (int i = 0; i < digit; ++i)
{
System.out.print("*");
}
}
}
public void printPattern(int num)
{
if (num <= 0)
{
return;
}
// Display given number
System.out.print("\nGiven num : " + num);
printValue(num);
}
public static void main(String[] args)
{
Pattern task = new Pattern();
// Test
task.printPattern(53235);
task.printPattern(84033);
}
}
Output
Given num : 53235
#*****
#***
#**
#***
#*****
Given num : 84033
#********
#****
#
#***
#***
// Include header file
#include <iostream>
using namespace std;
/*
C++ Program
Print digit pattern
*/
class Pattern
{
public: void printValue(int num)
{
if (num > 0)
{
// Recursive function calling
this->printValue(num / 10);
int digit = num % 10;
// Include initial value
cout << "\n#";
// Print the *by given digit
for (int i = 0; i < digit; ++i)
{
cout << "*";
}
}
}
void printPattern(int num)
{
if (num <= 0)
{
return;
}
// Display given number
cout << "\nGiven num : " << num;
this->printValue(num);
}
};
int main()
{
Pattern *task = new Pattern();
// Test
task->printPattern(53235);
task->printPattern(84033);
return 0;
}
Output
Given num : 53235
#*****
#***
#**
#***
#*****
Given num : 84033
#********
#****
#
#***
#***
// Include namespace system
using System;
/*
Csharp Program
Print digit pattern
*/
public class Pattern
{
public void printValue(int num)
{
if (num > 0)
{
// Recursive function calling
this.printValue(num / 10);
int digit = num % 10;
// Include initial value
Console.Write("\n#");
// Print the *by given digit
for (int i = 0; i < digit; ++i)
{
Console.Write("*");
}
}
}
public void printPattern(int num)
{
if (num <= 0)
{
return;
}
// Display given number
Console.Write("\nGiven num : " + num);
this.printValue(num);
}
public static void Main(String[] args)
{
Pattern task = new Pattern();
// Test
task.printPattern(53235);
task.printPattern(84033);
}
}
Output
Given num : 53235
#*****
#***
#**
#***
#*****
Given num : 84033
#********
#****
#
#***
#***
package main
import "fmt"
/*
Go Program
Print digit pattern
*/
type Pattern struct {}
func getPattern() * Pattern {
var me *Pattern = &Pattern {}
return me
}
func(this Pattern) printValue(num int) {
if num > 0 {
// Recursive function calling
this.printValue(num / 10)
var digit int = num % 10
// Include initial value
fmt.Print("\n#")
// Print the *by given digit
for i := 0 ; i < digit ; i++ {
fmt.Print("*")
}
}
}
func(this Pattern) printPattern(num int) {
if num <= 0 {
return
}
// Display given number
fmt.Print("\nGiven num : ", num)
this.printValue(num)
}
func main() {
var task * Pattern = getPattern()
// Test
task.printPattern(53235)
task.printPattern(84033)
}
Output
Given num : 53235
#*****
#***
#**
#***
#*****
Given num : 84033
#********
#****
#
#***
#***
<?php
/*
Php Program
Print digit pattern
*/
class Pattern
{
public function printValue($num)
{
if ($num > 0)
{
// Recursive function calling
$this->printValue((int)($num / 10));
$digit = $num % 10;
// Include initial value
echo("\n#");
// Print the *by given digit
for ($i = 0; $i < $digit; ++$i)
{
echo("*");
}
}
}
public function printPattern($num)
{
if ($num <= 0)
{
return;
}
// Display given number
echo("\nGiven num : ".$num);
$this->printValue($num);
}
}
function main()
{
$task = new Pattern();
// Test
$task->printPattern(53235);
$task->printPattern(84033);
}
main();
Output
Given num : 53235
#*****
#***
#**
#***
#*****
Given num : 84033
#********
#****
#
#***
#***
/*
Node JS Program
Print digit pattern
*/
class Pattern
{
printValue(num)
{
if (num > 0)
{
// Recursive function calling
this.printValue(parseInt(num / 10));
var digit = num % 10;
// Include initial value
process.stdout.write("\n#");
// Print the *by given digit
for (var i = 0; i < digit; ++i)
{
process.stdout.write("*");
}
}
}
printPattern(num)
{
if (num <= 0)
{
return;
}
// Display given number
process.stdout.write("\nGiven num : " + num);
this.printValue(num);
}
}
function main()
{
var task = new Pattern();
// Test
task.printPattern(53235);
task.printPattern(84033);
}
main();
Output
Given num : 53235
#*****
#***
#**
#***
#*****
Given num : 84033
#********
#****
#
#***
#***
# Python 3 Program
# Print digit pattern
class Pattern :
def printValue(self, num) :
if (num > 0) :
# Recursive function calling
self.printValue(int(num / 10))
digit = num % 10
# Include initial value
print("\n#", end = "")
i = 0
# Print the *by given digit
while (i < digit) :
print("*", end = "")
i += 1
def printPattern(self, num) :
if (num <= 0) :
return
# Display given number
print("\nGiven num : ", num, end = "")
self.printValue(num)
def main() :
task = Pattern()
# Test
task.printPattern(53235)
task.printPattern(84033)
if __name__ == "__main__": main()
Output
Given num : 53235
#*****
#***
#**
#***
#*****
Given num : 84033
#********
#****
#
#***
#***
# Ruby Program
# Print digit pattern
class Pattern
def printValue(num)
if (num > 0)
# Recursive function calling
self.printValue(num / 10)
digit = num % 10
# Include initial value
print("\n#")
i = 0
# Print the *by given digit
while (i < digit)
print("*")
i += 1
end
end
end
def printPattern(num)
if (num <= 0)
return
end
# Display given number
print("\nGiven num : ", num)
self.printValue(num)
end
end
def main()
task = Pattern.new()
# Test
task.printPattern(53235)
task.printPattern(84033)
end
main()
Output
Given num : 53235
#*****
#***
#**
#***
#*****
Given num : 84033
#********
#****
#
#***
#***
/*
Scala Program
Print digit pattern
*/
class Pattern()
{
def printValue(num: Int): Unit = {
if (num > 0)
{
// Recursive function calling
printValue(num / 10);
var digit: Int = num % 10;
// Include initial value
print("\n#");
var i: Int = 0;
// Print the *by given digit
while (i < digit)
{
print("*");
i += 1;
}
}
}
def printPattern(num: Int): Unit = {
if (num <= 0)
{
return;
}
// Display given number
print("\nGiven num : " + num);
printValue(num);
}
}
object Main
{
def main(args: Array[String]): Unit = {
var task: Pattern = new Pattern();
// Test
task.printPattern(53235);
task.printPattern(84033);
}
}
Output
Given num : 53235
#*****
#***
#**
#***
#*****
Given num : 84033
#********
#****
#
#***
#***
/*
Swift 4 Program
Print digit pattern
*/
class Pattern
{
func printValue(_ num: Int)
{
if (num > 0)
{
// Recursive function calling
self.printValue(num / 10);
let digit: Int = num % 10;
// Include initial value
print("\n#", terminator: "");
var i: Int = 0;
// Print the *by given digit
while (i < digit)
{
print("*", terminator: "");
i += 1;
}
}
}
func printPattern(_ num: Int)
{
if (num <= 0)
{
return;
}
// Display given number
print("\nGiven num : ", num, terminator: "");
self.printValue(num);
}
}
func main()
{
let task: Pattern = Pattern();
// Test
task.printPattern(53235);
task.printPattern(84033);
}
main();
Output
Given num : 53235
#*****
#***
#**
#***
#*****
Given num : 84033
#********
#****
#
#***
#***
/*
Kotlin Program
Print digit pattern
*/
class Pattern
{
fun printValue(num: Int): Unit
{
if (num > 0)
{
// Recursive function calling
this.printValue(num / 10);
val digit: Int = num % 10;
// Include initial value
print("\n#");
var i: Int = 0;
// Print the *by given digit
while (i < digit)
{
print("*");
i += 1;
}
}
}
fun printPattern(num: Int): Unit
{
if (num <= 0)
{
return;
}
// Display given number
print("\nGiven num : " + num);
this.printValue(num);
}
}
fun main(args: Array < String > ): Unit
{
val task: Pattern = Pattern();
// Test
task.printPattern(53235);
task.printPattern(84033);
}
Output
Given num : 53235
#*****
#***
#**
#***
#*****
Given num : 84033
#********
#****
#
#***
#***
Time Complexity:
The time complexity of this code depends on the number of digits in the input number. Let's denote the number of digits as n
.
- In the
printValue
function, for each digit, we perform a constant number of operations (calculations and printing asterisks). Since we are recursively calling the function for each digit, the total number of operations is proportional to the number of digits, which isn
. - In the
printPattern
function, we call theprintValue
function, which has a time complexity ofO(n)
. Therefore, the overall time complexity of theprintPattern
function is alsoO(n)
.
Hence, the time complexity of the program is O(n)
, where n
is the number of digits in the input number.
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