Python Program for Print Number series without using any loop Last Updated : 23 Jul, 2025 Comments Improve Suggest changes Like Article Like Report Problem - Givens Two number N and K, our task is to subtract a number K from N until number(N) is greater than zero, once the N becomes negative or zero then we start adding K until that number become the original number(N). Note : Not allow to use any loop. Examples : Input : N = 15 K = 5 Output : 15 10 5 0 1 5 10 15 Input : N = 20 K = 6 Output : 20 14 8 2 -4 2 8 14 20 Explanation - We can do it using recursion idea is that we call the function again and again until N is greater than zero (in every function call we subtract N by K). Once the number becomes negative or zero we start adding K in every function call until the number becomes the original number. Here we use a single function for both addition and subtraction but to switch between addition or subtraction function we used a Boolean flag. Python3 # Python program to Print Number # series without using loop def PrintNumber(N, Original, K, flag): #print the number print(N, end = " ") # change flag if number # become negative if (N <= 0): if(flag==0): flag = 1 else: flag = 0 # base condition for # second_case (Adding K) if (N == Original and (not(flag))): return # if flag is true # we subtract value until # number is greater than zero if (flag == True): PrintNumber(N - K, Original, K, flag) return # second case (Addition ) if (not(flag)): PrintNumber(N + K, Original, K, flag); return N = 20 K = 6 PrintNumber(N, N, K, True) # This code is contributed by Mohit Gupta_OMG Output : 20 14 8 2 -4 2 8 14 20 The time complexity is O(N/K), where N is the input number and K is the step value. The auxiliary space is O(N/K) Approach Name: Recursive Mathematical Approach using Function Calls. Steps: Define a function print_series(N, K) that takes two parameters, N and K.Check if N is less than or equal to zero, if yes, print N and return.Print N and call the print_series(N-K, K) function recursively.Print N again. Python3 def print_series(N, K): if N <= 0: print(N, end=" ") return print(N, end=" ") print_series(N-K, K) print(N, end=" ") N = 20 K = 6 print_series(N, K) Output20 14 8 2 -4 2 8 14 20 It has a time complexity of O(log(N/K)) and an auxiliary space of O(log(N/K)). Please refer complete article on Print Number series without using any loop for more details! Comment More infoAdvertise with us Next Article Must Do Coding Questions - Topic-wise K kartik Follow Improve Article Tags : Python Programs DSA Similar Reads Interview PreparationInterview Preparation For Software DevelopersMust Coding Questions - Company-wise Must Do Coding Questions - Topic-wiseCompany-wise Practice ProblemsCompany PreparationCompetitive ProgrammingSoftware Design-PatternsCompany-wise Interview ExperienceExperienced - Interview ExperiencesInternship - Interview ExperiencesPractice @GeeksforgeeksProblem of the DayTopic-wise PracticeDifficulty Level - SchoolDifficulty Level - BasicDifficulty Level - EasyDifficulty Level - MediumDifficulty Level - HardLeaderboard !!Explore More...Data StructuresArraysLinked ListStackQueueBinary TreeBinary Search TreeHeapHashingGraphAdvance Data StructuresMatrixStringAll Data StructuresAlgorithmsAnalysis of AlgorithmsSearching AlgorithmsSorting AlgorithmsPattern SearchingGeometric AlgorithmsMathematical AlgorithmsRandomized AlgorithmsGreedy AlgorithmsDynamic ProgrammingDivide & ConquerBacktrackingBranch & BoundAll AlgorithmsProgramming LanguagesCC++JavaPythonC#Go LangSQLPHPScalaPerlKotlinWeb TechnologiesHTMLCSSJavaScriptBootstrapTailwind CSSAngularJSReactJSjQueryNodeJSPHPWeb DesignWeb BrowserFile FormatsComputer Science SubjectsOperating SystemsDBMSComputer NetworkComputer Organization & ArchitectureTOCCompiler DesignDigital Elec. & Logic DesignSoftware EngineeringEngineering MathematicsData Science & MLComplete Data Science CourseData Science TutorialMachine Learning TutorialDeep Learning TutorialNLP TutorialMachine Learning ProjectsData Analysis TutorialTutorial LibraryPython TutorialDjango TutorialPandas TutorialKivy TutorialTkinter TutorialOpenCV TutorialSelenium TutorialGATE CSGATE CS NotesGate CornerPrevious Year GATE PapersLast Minute Notes (LMNs)Important Topic For GATE CSGATE CoursePrevious Year Paper: CS exams Like