How To Impress I To 100 Without Using Loop Inward Java

This is i of the interesting problems  I accept seen on coding interviews  for beginners. The programme has surprise chemical component past times maxim that y'all demand to impress 1 to 100 without using a loop e.g. for, forEach or enhanced for loop. This brings out or therefore innovative solutions from candidates which tell a lot most their approach, knowledge, together with attitude. I remember, bespeak this interrogation during campus recruitment to at to the lowest degree five candidates. The starting fourth dimension i took five minutes to intend together with and therefore come upward up amongst the animate beingness forcefulness agency where he but copies pasted 100 System.out.println() together with printed expose 1 to 100 but when nosotros asked to meliorate that, he couldn't likewise that. Unfortunately, he didn't intend that recursion tin supersede an iterative algorithm without using a loop.

The 2d candidate was to a greater extent than talented together with had expert noesis of figurer fundamentals, he direct away brings the recursion inward house together with seat the 2d solution described inward this article. We inquire this interrogation to three to a greater extent than candidates together with they took a lot of fourth dimension to come upward up amongst a recursive solution which nosotros are looking forward.

Unfortunately, nosotros didn't honor anyone who brings a completely novel solution to the problem, i of our goal was to honor such programmers, who tin intend out-of-box together with tin offering inventiveness to the team. At last, nosotros goal upward hiring the 2d guy who was expert at figurer fundamentals together with information construction together with algorithms. Good for us, he turns out to live a star developer inward our squad together with reinforced our belief inward hiring individual amongst expert noesis of basics together with coding skills ever pays off.



How to impress 1 to 100 without using loop inward Java

Now, let's shift our focus to the work inward hand, how are y'all going to impress expose 1 to 100 without using a loop. As I said, recursion is the choice solution because both recursion together with iteration allows y'all to repeat a task. Iteration execute the code within loop piece inward recursion a method transcend on calling itself, until base of operations instance is reached.




Java Program to impress 1 to 100 without using loop 

Here is my sample programme to impress 1 to 100 without using whatever loop e.g. for, piece or do-while loop. For the sake of brevity, I accept solely printed expose 1 to 10, but y'all tin operate the same technique to impress every bit many numbers y'all want. Though, y'all mightiness live careful because I accept never tested a variable arguments method amongst to a greater extent than than 10 arguments.

Btw, If y'all are comfortable amongst recursion together with fighting to write recursive algorithm,  then I propose y'all to read a expert mass on Algorithms e.g. Grokking Algorithms which explains key algorithms amongst easy-to-understand existent the world example.

 This is i of the interesting problems How to impress 1 to 100 without using loop inward Java


Printing 1 to 100 inward Java without using a loop.
import java.util.ArrayList; import java.util.HashMap; import java.util.Map;   /**  * Java Program to impress 1 to 100 without using loop  *   * @author WINDOWS 8  */  public class Test {      public static void main(String args[]) {                  System.out.println("1st agency to impress 1 to 100 without loops");         usingSysOut();                  System.out.println("2nd agency to impress 1 to 100 without using loops");         usingSysPrint();                  System.out.println("3rd agency to impress 1 to 100 without loops");         usingRecursion(10);             }                   public static void usingSysOut(){         System.out.println(1);         System.out.println(2);         System.out.println(3);         System.out.println(4);         System.out.println(5);         System.out.println(6);         System.out.println(7);         System.out.println(8);         System.out.println(9);         System.out.println(10);     }          public static void usingSysPrint(){         System.out.printf("%s%n%s%n%s%n%s%n%s%n%s%n"                 + "%s%n%s%n%s%n%s%n", 1, 2, 3, 4, 5, 6, 7, 8, 9, 10);     }          public static void usingRecursion(int number){         if(number > 1){             usingRecursion(number-1);         }         System.out.println(number);     }     }  Output 1st agency to impress 1 to 100 without loops 1 2 3 4 5 6 7 8 9 10 2nd agency to impress 1 to 100 without using loops 1 2 3 4 5 6 7 8 9 10 third agency to impress 1 to 100 without loops 1 2 3 4 5 6 7 8 9 10



That's all most how to impress 1 to 100 without using a loop inward Java. The full general dominion of pollex is that most of the things which tin live done using a loop tin also live done using recursion. The loop version of the algorithm is known every bit iterative algorithm piece the version which uses recursion is known every bit a recursive algorithm.

Further Learning
The Coding Interview Bootcamp: Algorithms + Data Structures
Data Structures together with Algorithms: Deep Dive Using Java
solution)
  • How to transpose a matrix inward Java? (solution)
  • How to implement binary search using recursion inward Java? (solution)
  • How to contrary a String inward house inward Java? (solution)
  • Java Program to impress Prime numbers from 1 to 100? (program)
  • How to implement Linear Search inward Java? (solution)
  • How to honor largest prime factors of a given integer inward Java? (program)
  • How to contrary words inward a given String inward Java? (solution)
  • How to honor all prime factors of a expose inward Java? (solution)
  • How to banking concern lucifer if 2 given Strings are Anagram inward Java? (solution)
  • How to generate prime numbers upward to 100 using Sieve of Eratosthenes Algorithm(program)
  • How to remove duplicate characters from String inward Java? (solution)
  • How to banking concern lucifer if a twelvemonth is a boundary twelvemonth inward Java? (solution)
  • How to honor the foursquare origin of a expose without using a library constituent inward Java? (program)
  • How to remove duplicate elements from the array inward Java? (solution)
  • How to banking concern lucifer if given expose is prime inward Java (solution)
  • How to banking concern lucifer if a expose is binary inward Java? (algorithm)
  • How to calculate Area of Triangle inward Java? (program)
  • How to impress Fibonacci serial inward Java (solution)
  • How to calculate the foursquare origin of a given expose inward Java? (solution)
  • How to honor the highest occurring give-and-take from a given file in Java? (solution)
  • How to banking concern lucifer if given String is palindrome or non inward Java? (solution)
  • How to banking concern lucifer if 2 rectangles intersect amongst each other inward Java? (solution)
  • How to honor all permutations of a given String inward Java? (solution)
  • How to banking concern lucifer if a String contains duplicate characters inward Java? (solution)
  • How to calculate the total of all elements of an array inward Java? (program)
  • How to contrary an array inward house inward Java? (solution)
  • How to impress prime expose upward to a given expose inward Java? (solution)
  • How to honor if given Integer is Palindrome inward Java? (solution)
  • How to calculate the average of all numbers of an array inward Java? (program)
  • Thanks for reading this article therefore far. If y'all similar this article together with therefore delight portion amongst your friends together with colleagues. If y'all accept whatever question, feedback or proposition together with therefore delight drib a comment. 

    Sumber https://javarevisited.blogspot.com/

    0 Response to "How To Impress I To 100 Without Using Loop Inward Java"

    Post a Comment

    Iklan Atas Artikel

    Iklan Tengah Artikel 1

    Iklan Tengah Artikel 2

    Iklan Bawah Artikel