Print Pattern Without Using Loop

Arvind Dhakar
1 min readJul 19, 2019

--

Example: 1) Input N=10

Pattern : 10 5 0 5 10

2) Input N=13

Pattern: 13 8 3 -2 3 8 13

Solution: You have to subtract 5 till no. become 0 or negative & increase the count and then add 5 till count become zero.

C++ Code:

#include<bits/stdc++.h>
using namespace std;
void fun1(int n,int cnt){
if(cnt>=0){
cout<<n+5<<” “;
fun1(n+5,cnt-1);
}
}
void fun2(int n,int cnt){
if(n<=0){
cout<<n<<” “;
fun1(n,cnt-1);
return;
}
else{
cout<<n<<” “;
fun2(n-5,cnt+1);
}
}
int main(){
int n;
cin>>n;
fun2(n,0);
}

--

--

Arvind Dhakar
Arvind Dhakar

No responses yet