samedi 11 juillet 2020

Write a short program that prints each number from 1 to 100 on a new line. 1. For each multiple of 5, print "Foo" instead of the number [closed]

Write a short program that prints each number from 1 to 100 on a new line. 1. For each multiple of 5, print "Foo" instead of the number. 2. For each multiple of 7, print "Bar" instead of the number. 3. For nos. which are multiples of both 5 and 7, print "FooBar" instead of no. using recursion

public class Test
{
  public static int callRecursively(int n)
  {
    if (n <= 100)
    {
        if (n % 5 == 0)
        {
            System.out.println("Foo");
        }
        else if(n%7==0)
        {
            System.out.println("Bar");
        }   
        else if (n%5 == 0 && n % 7 == 0)
        {
            System.out.println("FooBar");
        }
    }

     if(n>100)
     {
         return 0;
     }
     
     return callRecursively(n+1);
  }

  public static void main(String[] args)
  {
    int n = 1;
    callRecursively(n);
  }
}

Aucun commentaire:

Enregistrer un commentaire