6) print the even & odd numbers in separate line for n:
i/p:
20
o/p:
2 4 6 8 10 12 14 16 18 20
1 3 5 7 9 11 13 15 17 19
Solution:
using System;
class Program {
public static void printEven(int i,int n){
if(i>n) return; //condition to break the recursion
if(i!=0) Console.Write("{0} ",i);
printEven(i+=2,n);
}
public static void printOdd(int i,int n){
if(i>n) return; //condition to break the recursion
Console.Write("{0} ",i);
printEven(i+=2,n);
}
static void Main() {
printEven(0,20);
Console.WriteLine("");
printOdd(1,20);
}
}
7) print the number is prime or not:
i/p:
26
o/p:
Prime Number bcz its divisible by 2
i/p:
23
o/p:
Non Prime Number
Solution:
using System;
class Program {
public static void printPrime(int i,int n)
{
if(i>=n)
{
Console.WriteLine("Non Prime number");
return;
}
if(n%i==0 && i!=1){
Console.WriteLine("Prime number Bcz divisible by {0}",i);
return;
}
printPrime(i+=1,n);
}
static void Main() {
printPrime(1,27);
}
}
8) print the number is prime or not:
i/p:
MADAM
o/p:
Its Palindrome
i/p:
TATOO
o/p:
Its Not Palindrome
Solution:
using System;
class Program {
public static void checkPalindrome(char[] ch,int i,int j)
{
if(i>=j) { Console.Write("Its Palindrome"); return; }
if(ch[i]==ch[j]) { checkPalindrome(ch,i+=1,j-=1); }
else{ Console.Write("Its not Palindrome"); return; }
}
static void Main() {
string s="MADAM";
char[] ch=s.ToCharArray();
checkPalindrome(ch,0,s.Length-1);
}
}
9) print the factorial of the number n:
i/p:
5
o/p:
120
i/p:
3
o/p:
6
Solution:
using System;
class Program {
public static void printFactorialOfNumber(int fact,int n)
{
if(n==0) { Console.Write(fact); return; }
printFactorialOfNumber(fact*n,n-=1);
}
static void Main() {
int fact=3;
printFactorialOfNumber(fact,fact-1);
}
}
10) print the fibonnaci series of the number n:
i/p:
6
o/p:
0 1 1 2 3 5
Solution:
using System;
class Program {
public static void printFibonacciSeries(int n,int i,int j)
{
if(n==0){ return; }
if(i==0){ Console.Write("{0} ", i); }
Console.Write("{0} ", j);
int t=j;
j=i+j;
i=t;
printFibonacciSeries(n-=1,i,j);
}
static void Main() {
int fib=10;
printFibonacciSeries(fib-1,0,1);
}
}