Showing posts with label Recursive Function. Show all posts
Showing posts with label Recursive Function. Show all posts

Sunday, March 10, 2024

C Language Recursive Program To Check Palindrome

 #include <stdio.h>
#include <string.h>

int rev(char [], int,int);

int main()
{
   char ch[100];
   printf("Enter a string:");
   scanf("%s",ch);
   printf("\nEntered string is %s",ch);
   int x=rev(ch,0,strlen(ch)-1);
   if(x==1)
   printf("\nPalindrome");
   else
   printf("\nNot Palindrome");
    return 0;
}

int rev(char ch[],int start,int end)
{
   
    if(ch[start]!=ch[end])
    return 0;
    else if(start==end)
    return 1;
    start++;
    end--;
    rev(ch,start,end);
}

C Language Recursive Function Program To Reverse A String

 #include <stdio.h>
#include <string.h>

void rev(char [],int, int);

int main()
{   char ch[100];
   printf("Enter a string:");
   scanf("%s",ch);
   printf("\nEntered string is %s",ch);
 rev(ch,0,strlen(ch)-1); 
      return 0;
}

void rev(char ch[],int i,int end)
{
    char c;
   if(end<=i)
   {
      printf("\nReverse=%s",ch);
       return ;
   }
      c=ch[i];
       ch[i]=ch[end];
       ch[end]=c;
   i++;
   end--;
   rev(ch,i,end);    
}

Sunday, October 15, 2023

C Language Program Finding Factorial Value Using Recursive Function

 #include<stdio.h>

long int fact(int n);

int main() {

    int n;

    printf("Enter a positive integer: ");

    scanf("%d",&n);

    printf("Factorial of %d = %ld", n, fact(n));

    return 0;

}

long int fact(int n) {

    if (n==1)

    return 1;

       int y=fact(n-1);

       return n*y;    

}

Sunday, March 27, 2022

Sum Of Array Elements Using Recursive Function

 Compute and returns the sum of all elements in an array using recursion function where array and the size is given as parameter

import java.io.*;
public class ArrayRecursion
{
    BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int arr[]=new int[10];
    void accept() throws Exception
    {
          for(int i=0;i<10;i++)
          {
              System.out.print("\nEnter Value: ");
              arr[i]=Integer.parseInt(br.readLine());
            }
            System.out.print("\nValues as follows: ");
            for(int i=0;i<10;i++)
            System.out.print(" "+arr[i]);
           int x=sumArray(arr,9);
           System.out.print("\nSum of all elements in the array: "+x);
        }
           private int sumArray(int arr[], int n)
       {
            if(n<0)
            return 0;
            int s=arr[n];            
            return s+sumArray(arr,--n);            
        }
            
public static void main(String args[])throws Exception
{
    
ArrayRecursion ob=new ArrayRecursion();
    ob.accept();
}
}

Check any string for palindrome using recursive function

 import java.io.*;
public class PalinRecursive
 {
    BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
String str;
    void accept() throws Exception
    {
          System.out.print("\nEnter Sentence: ");
           str=br.readLine();
           int x=palin(str,0,str.length()-1);
           if(x==0)
           System.out.print("\nThe string "+str +" Palindrome");
           else
           System.out.print("\nThe string "+str +" is not Palindrome");
        }
           private int palin(String str,int start, int end)
       {
            if(start>=end)
            return 0;
            if(str.charAt(start)==str.charAt(end))
            return palin(str,++start,--end);
            else
            return 1;
        }
            
public static void main(String args[])throws Exception
{
    
PalinRecursive ob=new PalinRecursive();
    ob.accept();
}
}

Saturday, July 3, 2021

Decimal To Octal Conversion Using Recursive Function


A class DeciOct has been defined to convert a decimal number into its equivalent octal number. Some of the members of the class are given below:

Class name: DeciOct
Data members/instance variables:
n: stores the decimal number
oct: stores the octal equivalent number

Member functions:

DeciOct(): constructor to initialize the data members
n = 0, oct = 0.

void getnum(int nn): assign nn to n

void deci_oct(): calculates the octal equivalent of ‘n’ and stores it in oct using the recursive technique

void show(): displays the decimal number ‘n’, calls the function deci_oct() and displays its octal equivalent.

(a) Specify the class DeciOct, giving details of the constructor( ), void getnum(int), void deci_oct( ) and void show(). Also define a main() function to create an object and call the functions accordingly to enable the task.

 
import java.util.*;
class DeciOct
{
    int n;
    String oct;
    DeciOct()
    {
        n=0;
        oct="";
    }
    private void getnum(int nn)
    {
        n=nn;
    }
    void deci_oct(int a)
    {
        String s="";
        int d=a/8;
        int r=a%8;
        if(r==0)
        return;
        oct=r+oct;
        deci_oct(d);
    }
    void show()
    {
        System.out.println("The Original Number :"+n);
        deci_oct(n);
        System.out.println("Its Octal Equivalent :"+oct);
    }
    public static void main(String args[])
    {
        Scanner sc=new Scanner(System.in);
        System.out.println("Enter the Number :");
        int num=sc.nextInt();
        DeciOct ob=new DeciOct();
        ob.getnum(num);
        ob.show();
    }
}

Other Programs On Numbers: CLICK HERE

Recursive Function And Case Change Of Letters In A Sentence

 
Design a class Change to perform string related operations. The details of the class are given below:

Class name: Change
Data Members/instance variables:
str: stores the word
newstr: stores the changed word
len: store the length of the word

Member functions:

Change(): default constructor

void inputword( ): to accept a word

char caseconvert (char ch): converts the case of the character and returns it

void recchange (int): extracts characters using recursive technique and changes its case using caseconvert () and forms a new word

void display (): displays both the words

(a) Specify the class Change, giving details of the Constructor ( ), member functions void inputword (), char caseconvert (char ch), void recchange (int) and void display (). Define the main () function to create an object and call the functions accordingly to enable the above change in the given word.

import java.io.*;
class change
{
    String str;
    static String newstr;
    int len;
    BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
    private change()
    {
        str="";
        newstr="";
        len=0;
    }
    private void inputword()throws IOException
    {
        System.out.println("Enter the word :");
        str=br.readLine();
        len=str.length();
        rechange(0);
    }
    private char caseconvert(char ch)
    {
        if(Character.isUpperCase(ch))
        ch=Character.toLowerCase(ch);
        else if(Character.isLowerCase(ch))
        ch=Character.toUpperCase(ch);
      return ch;  
    }
    private void rechange(int n)
    {
        char x=' ';
       if(n==len)
       return;   
        char ch=str.charAt(n);
        x=caseconvert(ch);
        newstr=newstr+x;
       n++;
       rechange(n);
    }
    public static void main(String args[])throws IOException
    {
        change ob=new change();
        ob.inputword();
        System.out.println(newstr);
    }
}

Sunday, June 6, 2021

C Array And Recursive Function To Check Prime Number And Largest Number

 Details of the program: Write a C program that takes a 1-dimensional array arr as input (of size 10) and check if each element in arr is prime or not using recursion, and store the prime numbers in another array arr2

Subtract 2 from each element in arr2
Use recursion to find the largest element in arr2

   
   
   #include<stdio.h>
   int x;
   void main()
   {
    int arr1[10],arr2[10];
    int i;
    for(i=0;i<10;i++)
    {
    printf("\nElement No %d: ",(i+1));
    scanf("%d",&arr1[i]);
    }
    for(i=0;i<10;i++)
{

if(isPrime(arr1[i],arr1[i]/2))

arr2[x++]=arr1[i];
}
printf("\nPrime numbers are\n");
for(i=0;i<x;i++)
printf("%d ",arr2[i]);
for(i=0;i<x;i++)
arr2[i]=arr2[i]-2;

printf("\nAfter Deduction\n");
for(i=0;i<x;i++)
printf("%d ",arr2[i]);
i=getMax(arr2);
printf("\nMax Value=%d",i);
getch();
   }
  int isPrime(int n, int i)
{
    if(i == 1)
        return 1;   
    else
    {
        if(n%i == 0)
            return 0;
        else
            isPrime(n, i-1);    
    }
}
  int getMax(int a[])
{
    static int i = 0, max =- 9999;  
    if(i < x)   
    {
        if(max < a[i])
        max = a[i];
        i++;    
        getMax(a);   
    }
    return max;
}

Saturday, May 2, 2015

Program to display a line of text in reverse order using recursive function


#include< stdio.h>
 #include< string.h>
 void rec(char *,int);
 void main()
 {
  char ch[100];
  int len;
  clrscr();
  printf("\nEnter the string:");
  gets(ch);
  len=strlen(ch);
  printf("\nSentence in reverse direction=");
  rec(ch,len-1);
  getch();
  }
  void rec(char *p,int n)
  {
   if(n==-1)
   return;
   printf("%c",*(p+n));
   n--;
   rec(p,n);
   }


   Write a recursive program to compute x^n

   long power (int, int);

void main()
{
    int p, n;
    clrscr();
    long result;

    printf("Enter 'n' and 'p': ");
    scanf("%d%d", &n, &p);
   result = power(n, p);
    printf("%d^%d is %ld", n, p, result);
    getch();
}

long power (int n, int p)
{
    int y,x;
    if (p==0)
    return 1;
    x=p-1;
   y=power(n,x);
   return n*y;
}

Wednesday, August 24, 2011

Decimal to binary conversion using recursive function


In this program a decimal number will be entered by the user and it will be converted in it’s equivalent binary number. The codes of the program is given below:

#include< stdio.h>
int i=0;
int arr[20];
void rec(int n)
{
 int c= n%2;
 arr[i++]=c;
 n=n/2;
 if(n==1)
 {
 arr[i++]=n;
 return;
 }
 rec(n);
 }
  void main()
   {
   int j,n;
   clrscr();
   printf("\nEnter the number:");
   scanf("%d",&n);
   rec(n);
   printf("\nThe binary equivalent is :");
   for(j=0;j< i;j++)
   printf("%d",arr[j]);
   getch();
   }

In this program of converting decimal number in its’ equivalent binary number, the function ‘rec ()’ is used for converting and this function is called recursively. The one dimensional array and it’s index variable are declared as external variables. The one dimensional array will hold the binary digits. 

Subscribe via email

Enter your email address:

Delivered by FeedBurner