Saturday, July 2, 2022

Print Pattern (Recurssion)

 Print Pattern (Recurssion)

Problem Statement
Print a sequence of numbers starting with N, without using loop, in which A[i+1] = A[i] - 5, if A[i]>0, else A[i+1] = A[i] + 5 repeat it until A[i]=N.

Note:- Once you change a operation you need to continue doing it.

static void printPattern(int n,int curr, boolean flag)
{
//Enter your code here
System.out.print(curr+" ");
if(flag==false && curr==n){
    return ;
}
if(curr<=0){flag=false;}
if(flag)//if(flag !=0) flag =true
{
    printPattern(n,curr-5,flag);
}
else
{
    printPattern(n,curr+5,flag);
}
}

No comments:

Post a Comment

ads vert

Basic HTML Tables - Layout, HTML Tables, Attributes, Aside, Footer, Tr tag, Td tag, Th tag, Tbody

  Basic HTML Tables - Layout, HTML Tables, Attributes, Aside, Footer, Tr tag, Td tag, Th tag, Tbody < table >      < thead >    ...