Adsense 728 * 15

Search

Custom Search

C Program to Print Factorial of a Number using Recursion

Write a C program to print factorial of a number using recursion.  In this program, we have to write a code which takes an input number and print factorial of a number using recursion.

Before solving this problem, let's first understand what is recursion? And the difference between recursion and iteration.

What is Recursion?


In Recursion, A function calls itself until the base condition is reached. Using recursion, we can write much cleaner and shorter as compared to iterative code. You can learn more about recursion using following tutorials.


MCQ on Recursion for Practice

In this tutorial, we use recursion to solve this problem. In my previous post, i have explained how to solve this problem using iteration.

C program to print factorial of a number

Java program to print factorial of a number

C++ program to print factorial of a number

C Program to Print Factorial of a Number using Recursion


C Program to Find Factorial of a Number using Recursion



Let's write a c code to print factorial of a number using recursive approach. In this program, first we take input number from a user then we call a calculateFactorial() method to calculate factorial of a number.


#include <stdio.h>

int calculateFactorial (int num) {
 
  /* If number is zero or 1 then return 1 */

  if ( num == 0 || num == 1) {
   
     return 1;
   
   } 
  
   /* Recursive function call */

   return num * calculateFactorial(num-1);
}

int main() {
 
  int num, fact = 0;
 
  printf ("Enter a number \n");
  scanf ("%d", &num);
 
  /* Function call */

  fact = calculateFactorial(num);
 
  printf ("Factorial of a number %d is %d ",num,fact);
  return 0;
}


Output :

Enter number :  5

Factorial of a number 5 is 120


Explanation :

Suppose you have entered 5 so how this program is going to be executed.

5 * calculateFactorial (5-1)
5 * 4 * calculateFactorial (4-1)
5 * 4 * 3 * calculateFactorial (3-1)
5 * 4 * 3 * 2 * calculateFactorial (2-1)
5 * 4 * 3 * 2 * 1 

C++ Program to Check Whether a Number is Palindrome or Not

Write a c++ program to check whether a number is palindrome or not. In this program, we write a code to take an input number from a user and check whether a number is palindrome or not.

Let's first understand what is a palindrome number?

A palindrome number is a number that remains same when it's digits are reversed.

For example -

i) 121 is a palindrome number.

ii) 12321 is a palindrome number.


In my previous post, I have explained what is palindrome and how to check whether a number

C program to check whether a number is palindrome or not

How to check whether an input number is palindrome or not?


i) Take an input number from a user.

ii) Reverse an input number.

iii) Compare input number and reverse of an input number. If both are equal then entered number is a palindrome number.


C++ Program to Check Whether a Number is Palindrome or Not


C++ Program to Check Whether a Number is Palindrome or Not


#include<iostream>
using namespace std;
  
int main(){

    int num, temp, rev = 0, digit;
    
    //Input number
    cout << "Enter a number \n";
    cin  >> num;
    
    temp = num;
    
    //Reverse a number  
    while (temp > 0) {
        
        digit = temp % 10;   
        rev  = rev * 10 + digit; 
        temp = temp / 10; 
    }
   
    if ( num == rev) {
        cout << num << " is a palindrome number";
    } else {
        cout << num << " is not a palindrome number";
    }
               
    return 0;     
}





C++ program to delete an element from an array

Programming questions on strings

Programming questions on recursion


C Program to Check Whether a Number is Palindrome or Not

Write a C Program to check whether a number is palindrome or not. In this program, we take an input number from a user and check whether an input number is palindrome or not.

What is a Palindrome Number?


A palindrome number is a number that remains same when it's digits are reversed.

For example - 11, 22, 121, 12321, 16461 etc.

C program to check whether a number is Armstrong or not

How to check whether a number is palindrome or not?


i) Take an input number from a user.

ii) Reverse an input number.

iii) Compare input number and reverse of an input number. If both are equal then entered number is a palindrome number.

C program to check whether a string is palindrome or not

C Program to Check Whether a Number is Palindrome or Not



C Program to Check Whether a Number is Palindrome or Not


In this programming example, first step is to take an input from a user then find the reverse of an input number. If input number and reverse of an input number is same then it's a palindrome number.

#include<stdio.h>

  
int main(){

   int num, temp, rev, digit;

   //Input number
   printf("Enter a number \n");
   scanf("%d", &num);
    

   temp = num;
   
   //Reverse a number
   while( temp > 0) {
       
       digit = temp % 10;
       rev   = rev * 10 + digit;
       temp  = temp / 10;
   }

   /*
   If original number and reverse of a number
   is same, then it's palindrome
   */
   if( num == rev) {
       printf(" %d is a Palindrome number", num);
   } else {
       printf(" %d is not a Palindrome number", num);
   }

    return 0;
}


Output -

Enter a number:  121

121 is a palindrome number



C program to print fibonacci series using recursion

C program to reverse a number

C interview questions for practice

C program to calculate power of a number


C Program to Print Fibonacci Series Using Recursion

Write a c program to print Fibonacci Series using recursion. Given a positive integer n, we have to write a c code to print fibonacci series using recursion. This question is also very important in terms of technical interviews.

In my last post, I have already discussed C program to print fibonacci series using while, do while & for loop.  Let's first understand, what is Fibonacci series?

Fibonacci Series

In Fibonacci series, first two numbers are 0 and 1, and the remaining numbers are the sum of previous two numbers.

For example -

0  1  1  2  3  5  8 ......

You can clearly see first two numbers are 0 and 1 and the rest of the numbers are the sum of previous two numbers.

In this post, We will learn how to print Fibonacci series using recursion. So let's understand what is recursion.

C, C++ Interview Question

What is Recursion?


In simple words, Recursive function is a function that calls itself. Through recursion, We can write much cleaner and shorter code for complex problems.

In recursion program, Always specify the base condition(terminating condition) otherwise code will run infinitely until the program run out of memory.

Recursion vs iteration - Difference between recursion and iteration

C Program to Print Fibonacci Series Using Recursion


C Program to Print Fibonacci Series Using Recursion


In this program, we take a number of elements to print in a fibonacci series as an input from user. Then our recursive method print fibonacci series upto that number.


#include<stdio.h>


int fibonacci(int n){

  /**
   if number is less than 2,
   In case of 0 and 1
   */
  if(n < 2) {
      return n;
  }

  //Recursive call
  return fibonacci(n-1) + fibonacci(n-2);

}

  
int main(){

   int num;

   printf("Number of terms to print in a Fibonacci series");
   scanf("%d", &num);

   for(int i = 0; i < num; i++) {
       printf(" %d ", fibonacci(i));
   }

    return 0;
}



Output

Number of terms to print in a Fibonacci series 10

0  1  1  2  3  5  8  13  21  34


Another way to write this program using recursion.


#include<stdio.h>


void fibonacci(int n){

  static int first = 0, second = 1, third;

  //If n is greater than zero   
  if(n > 0){

    third = first + second;

    printf(" %d ",third);

    first  = second;
    second = third; 
 
   /* Function call itself. */

    fibonacci(n-1);

   }

}

  
int main(){

   int num;

   printf("Number of terms to print in a Fibonacci series");
   scanf("%d", &num);

   printf("%d %d",0,1);

  /* Frist two numbers (0,1) are printed, 
     so we passed n-2 to fibonacci function. */

   fibonacci(num-2);

    return 0;
}

 

C program to delete an element from an array

C program to check Armstrong number

C program to check whether a character is vowel or consonant

Questions on Recursion for Practice

C++ Program to Delete an Element from an Array

Write a C++ program to delete an element from an array at specified position.  Given an input array, Write a code to delete an element from a position input by a user.

To solve this problem, let's first understand what is the input parameter and what's our approach to solve this problem.


C++ Program to Delete an Element from an Array


In this program, we take an input array and position of an element to delete.


C program to delete an element from an array

Reverse an Array using Recursion


C++ Program to Delete an Element form an Array



In this program, we take an array and position of an element as an input. Then, we write a code to delete an element from an array. To delete an element at index i in array we have to shift all elements from index i+1(higher index) to i (previous index).


#include <iostream>
using namespace std;

int main() {
 
   
   int a[100], size, pos, i, count = 0;

   cout << "Enter the size of an array \n";
   cin  >> size;
   
   cout << "Enter the value in an array \n";
   
   // Take an input array
   for (i = 0; i < size; i++) {
    cin >> a[i];
   }
   
   //Input position where we delete an element 
   cout << "Enter the position \n";
   cin  >> pos;

   //Shift element from i+1 to i
   for(i = pos-1; i < size; i++) {

      arr[i] = arr[i+1];
   }

   // Reduce the size of an array
   size--;            

  // Print an array after deleting an element
  for(i = 0; i < size; i++) {
   
    cout<<"   "<<a[i];
    
  }

  return 0;
}


Output : 

Enter the size of an array  - 5

Enter the value in an array

2     4     6     1      2

Enter the position    -   2

 2    4   1    2



Programming questions on Strings

Programming questions on Recursion

Programming questions on Array

Programming questions on Linked List

C Program to Delete an Element from an Array

Write a c program to delete an element from an array at specified position. In this program, we take an input array and position to delete an array element from a user. Then we check whether a position enter by user is valid or not.

In array, all elements of an array are stored in contiguous memory location. To delete an element at index i in array we have to shift all elements from index i+1 to i to previous index.

For example - Take an array of length 5 and suppose we have to delete an element at index 3.

arr[] - {3, 8, 2, 7, 1}

position to delete an element : 3

 To delete an element, we have to move element at position 4 to 3 and position 5 to 4. Then we can decrement the size of an array.


Difference between array and linked list

C program to insert an element in an array


C Program to Delete an Element from an Array


C Program to Delete an Element from an Array


In this programming example, we take an input array elements and position to delete an element from a user.

#include <stdio.h>

int main() {
    
    int arr[100], n, i, pos;
    
    printf("Enter the size of an array( Max 100) \n");
    scanf("%d", &n);
    
    printf("Enter an array elements \n");
    
    //Take input values
    
    for(i = 0; i < n; i++) {
        scanf("%d", &arr[i]);
    }
    
    
    printf("Enter position to delete an element\n");
    scanf("%d", &pos);
    
    
    //Check valid delete positon
    
    if(pos < 0 || pos > n ) {
        printf("Invalid position");
        
    } else {
        
        //Traverse an array

        for(i = pos-1; i < n; i++) {

            arr[i] = arr[i+1];
        }

        //Decrement the size of an array
        n--;

        printf("Array after deleting an element\n");

        for(i = 0; i < n; i++) {

            printf("%d\n", arr[i]);
        }
    }
    
    return 0;
}



Output:

 Enter the size of an array 5

Enter an array elements


3
8
2
7
1

Enter position to delete an element 3

Array after deleting an element
3
8
7
1

C++ program to delete an element from an array

Programming questions on array

C program to check Armstrong number


Java Program to Find Largest Number in an Array

Write a java program to find largest number in an array. Given an unsorted array, we have to write a java code to find largest number in an array.

Algorithm to Find Largest Number in an Array


First Approach: Sort an array

First approach is to sort an array and pick the last element of an array. For sorting, we can use algorithm such as Bubble sortSelection sortInsertion sortQuick sort, Merge sort etc. to sort an array. After sorting, An array element at n-1 position is highest number of an array.

The time complexity of sorting an array is O(nlogn).

Bubble sort program in java

Sorting algorithms and their time complexity

Second Approach:

Traverse an array to find highest number of an array.

1. Take a variable and assign the minimum value.




2.  Traverse an array and compare each element of an array to the assigned variable. 

Java program to find second smallest number in an array


Java Program to Find Largest Number in an Array

Java Program to Find Largest Number in an Array


In this program, we take the size of an array and array element as input from a user. Then we declare a variable highest and assign a minimum integer value. After that, we traverse an array and compare each element of array to the value of a variable.


import java.util.Scanner;

public class Largest {

  public static void main(String[] args) {

     Scanner in = new Scanner(System.in);
     System.out.println("Enter the size of an array");

     // Size of an array
     int n = in.nextInt();

     // Array declaration
     int arr[] = new int[n];

     System.out.println("Enter an array elements");

     //Input array elements
     for (int i = 0; i < n; i++) {
 arr[i] = in.nextInt();
     }

     // Assign minimum value
     int highest = Integer.MIN_VALUE;

     for (int i = 0; i < n; i++) {
       // compare
       if (arr[i] > highest) {
          highest = arr[i];
       }
    }

     System.out.println("Highest number is " + highest);
   }

}


Output :

Output of largest number in an array

Java programs for practice

Java program to print factorial of a number

C Program to Find Highest Number in an Array

Write a c program to find highest number in an array. Given an input array, we have to write a code to find highest number in an array.

There are multiple approaches we can use to solve this problem.

i) Find highest number in an array using sorting algorithm.

We can use any sorting algorithm such as Bubble sort, Selection sort, Insertion sort, Quick sort, Merge sort etc. to sort an array. After sorting an array element at n-1 position is highest number in an array.

The time complexity of sorting an array is O(nlogn).

ii) Traverse an array to find highest number in array.

In this approach, first declare a variable highest and assign first element of an array. After that traverse an array, and compare each element of an array with the value of highest. If any element is greater than the highest then assign that value into highest variable.

The time complexity is O(n).

Let's implement the second approach.

C++ program to find largest number in an array

C Program to Find Highest Number in an Array


C Program to Find Highest Number in an Array


In this programming example, we first take an input array from user. Then we traverse an array to find highest number in an array.

#include <stdio.h>

int main() {
    
    int arr[100], n, i, highest;
    
    printf("Enter the size of an array( Max 100) \n");
    scanf("%d", &n);
    
    printf("Enter array elements \n");
    
    //Take input values
    
    for(i = 0; i < n; i++) {
        scanf("%d", &arr[i]);
    }
    
    
    //Assign first value 
    
    highest = arr[0];
    
    //Traverse an array
    
    for(i = 0; i < n; i++) {
        
        //If any value is greater than highest
        
        if(arr[i] > highest) {
            highest = arr[i];
        }
    }
    
    //Print largest number
    
    printf("Largest number is %d ", highest);
    
    return 0;
}



Output :

Enter the size of an array( Max 100)  5

Enter array elements
3
8
2
7
1

 Largest number is 8


Programming questions on arrays

C program to check whether a character is vowel or consonant

C program to find second smallest number in an array without sorting

C++ Program to Find Largest Number in an Array

Write a C++ program to find largest number in an array. Given an unsorted array, we have to write a C++ code to find the largest number in an array.

Let's think for a moment, how do you solve this problem. You can solve this problem using sorting algorithm, first sort an array and then pick the element at n-1 position. But, let's solve this problem without using any sorting algorithm.

Also, The time complexity of sorting an array is O(nlogn) but we can solve this problem in O(n) in a single traversal.

Algorithm to Find Largest Number in an Array


i) Take an input array from a user.

ii) Take a variable highest and assign first element of an array. After that traverse an array and compare each element of an array with highest. If any element is greater than the highest then assign that value into highest.


Sample snippet.

 /* Assign array first element to highest variable. */

   int highest = arr[0];

  /* Iterate till the end of an array. */
 
   for(i=1;i< arrcount; i++){
 
        /* Check if array element is higher
           than value in highest.*/
       
 if(arr[i]>highest){
   
        highest = arr[i];
     }
 }

Reverse a number using recursion in C, C++

C program to find highest number in an array


C++ Program to Find Largest Number in an Array


C++ Program to Find Largest Number in an Array


#include <iostream>
using namespace std;

int main() {
    
    int arr[100], n, i, highest;
    
    cout << "Enter the size of an array( Max 100) \n";
    cin >> n;
    
    cout << "Enter array elements \n";
    
    //Take input values
    
    for(i = 0; i < n; i++) {
        cin >> arr[i];
    }
    
    
    //Assign first value 
    
    highest = arr[0];
    
    //Traverse an array
    
    for(i = 0; i < n; i++) {
        
        //If any value is greater than highest
        
        if(arr[i] > highest) {
            highest = arr[i];
        }
    }
    
    //Print largest number
    
    cout << "Largest number " << highest;
    
    return 0;
}

Output :

Enter the size of an array( Max 100)  5

Enter array elements
3
8
2
7
1

 Largest number 8


Programming questions on arrays

C program to check whether a character is vowel or consonant

C program to find second smallest number in an array without sorting

C Program to Calculate the Sum of Digits of a Number

Write a c program to calculate the sum of digits of a number. In this program, we take an input number from a user and then calculate the sum of digits of a number.

Algorithm to calculate the sum of digits of a number


i) Take an input number from a user.

ii) Assign a input number in a variable.

iii) Get the last digit by performing modular division i.e. digit = num % 10.

iv) Take another variable sum and assign the last digit into it i.e. sum = sum + digit.

v) Remove the last digit from number by dividing the number by 10 (num = num / 10).

vi) Repeat step (iii to v) till number becomes 0. After that we will get the sum of the digits of a number.

Find sum of digits of a number using recursion

C Program to Calculate the Sum of Digits of a Number



C Program to Calculate the Sum of Digits of a Number


In this example, we take an input number and calculate the sum of digits of a number.


#include <stdio.h>

int main() {
 
    int num, sum = 0, remainder;
    
    //Input number
    
    printf("Enter a number \n");
    scanf("%d", &num);
    
    
    //If num is greater than zero
    
    while(num > 0) {
    
       /* Find remainder of a a number */
       remainder = num % 10;
    
       /* Add to a sum */
        
       sum = sum + remainder;
       
       /* remove the last digit */
        
       num = num / 10;
    }
    
    
    printf("Sum of digits is %d", sum);
    
    return 0;
}

Output

Enter a number : 234

Sum of digits is 9

Programming questions for beginners

Programming questions on arrays