Skip to content

Commit

Permalink
added questions using recursion
Browse files Browse the repository at this point in the history
  • Loading branch information
Rishabh Balaiwar committed Oct 2, 2020
1 parent 5bbb172 commit 263596e
Show file tree
Hide file tree
Showing 9 changed files with 233 additions and 0 deletions.
24 changes: 24 additions & 0 deletions C++/LeapYearORNot.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#include<iostream>
using namespace std;

void leapYear(int year)
{
if((year % 4 == 0 && year % 100 != 0 ) || (year % 400 == 0))
{
cout<<"Yes it's a leap year"<<endl;
}
else
{
cout<<"No it's not a leap year"<<endl;
}

}
int main()
{
int year;
cin>>year;
{
leapYear(year);
}
return 0;
}
33 changes: 33 additions & 0 deletions C++/Questions using Recursion/checkPalindrome.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#include <iostream>
#include <cstring>
using namespace std;

bool isPalRec(char input[],int s,int e)
{
if(s==e)
return true;
if(input[s]!=input[e])
return false;
if(s<e+1)
return isPalRec(input,s+1,e-1);
return true;
}
bool checkPalindrome(char input[]) {
int n=strlen(input);
if(n==0)
return true;
return isPalRec(input,0,n-1);
}
int main() {
char input[50];
cin >> input;

if(checkPalindrome(input)) {
cout << "true" << endl;
}
else {
cout << "false" << endl;
}
}


22 changes: 22 additions & 0 deletions C++/Questions using Recursion/countZeroes.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#include <iostream>

using namespace std;

int main() {
int n;
cin >> n;
cout << countZeros(n) << endl;
}

int countZeros(int n) {
// Write your code here
if(n==0)
return 1;
if(n<10)
return 0;
else if(n%10==0)
return 1+countZeros(n/10);
return countZeros(n/10);
}


41 changes: 41 additions & 0 deletions C++/Questions using Recursion/firstIndexOfNumber.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#include<iostream>
using namespace std;

int firstIndex(int input[], int size, int x) {
if(size==1){
if(input[0]==x)
return 0;
else
return -1;
}

if(input[0]==x)
return 0;
else{
int index=firstIndex(input+1,size-1,x);
if(index != -1)
return index+1;
else
return -1;
}

}
int main(){
int n;
cin >> n;

int *input = new int[n];

for(int i = 0; i < n; i++) {
cin >> input[i];
}

int x;

cin >> x;

cout << firstIndex(input, n, x) << endl;

}


21 changes: 21 additions & 0 deletions C++/Questions using Recursion/geometricSum.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#include <iostream>
#include <math.h>
#include <iomanip>
using namespace std;

int main() {
int k;
cin >> k;
cout << fixed << setprecision(5);
cout << geometricSum(k) << endl;
}

double geometricSum(int k) {
// Write your code here
if(k==0)
return 1;
double ans = 1 / (double)pow(2, k) + geometricSum(k - 1);
return ans;
}


17 changes: 17 additions & 0 deletions C++/Questions using Recursion/multiplication.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#include <iostream>
using namespace std;

int multiplyNumbers(int m, int n) {
// Write your code here
if(m==0)
{
return 0;
}
int result=(m*n);
return result;
}
int main() {
int m, n;
cin >> m >> n;
cout << multiplyNumbers(m, n) << endl;
}
23 changes: 23 additions & 0 deletions C++/Questions using Recursion/removeX.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#include <iostream>
using namespace std;

void removeX(char input[]) {
// Write your code here
if(input[0]=='\0')
return;
if(input[0]=='x'){
int i=1;
for(;input[i]!='\0';i++)
input[i-1]=input[i];
input[i-1]=input[i];
removeX(input);
}
removeX(input+1);
}
int main() {
char input[100];
cin.getline(input, 100);
removeX(input);
cout << input << endl;
}

30 changes: 30 additions & 0 deletions C++/Questions using Recursion/replacePi.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#include <iostream>
#include <cstring>
using namespace std;

void replacePi(char input[]) {
// Write your code here
if(input[0]=='\0')
return;
if(input[0]=='p' && input[1]=='i')
{
input[0]='3';
input[1]='.';

int size=strlen(input);
for(int i=size+2;i>1;i--)
{
input[i]=input[i-2];
}
input[2]='1';
input[3]='4';
}
replacePi(input+1);
}
int main() {
char input[10000];
cin.getline(input, 10000);
replacePi(input);
cout << input << endl;
}

22 changes: 22 additions & 0 deletions C++/Questions using Recursion/stringToInteger.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#include <iostream>
#include<cmath>
#include<cstring>
using namespace std;

int stringToNumber(char input[]) {
// Write your code her
if (input[0]=='\0')
return 0;
int ans=stringToNumber(input+1);

int size=strlen(input);
int d=(input[0]-48)*pow(10,size-1);
return ans+d;

}

int main() {
char input[50];
cin >> input;
cout << stringToNumber(input) << endl;
}

0 comments on commit 263596e

Please sign in to comment.