C Program To Find nPr Factorial

Lets write a C Program to Find nPr Factorial for the user input values of n and r.

In this video tutorial we are showing iterative logic, recursive logic and how to write single line of code for getting factorial using recursion and ternary/conditional operator.

Formula To Calculate nPr Factorial

nPr = n! / (n-r)!;

Definition:

The number of possibilities for choosing an ordered set of r objects(a permutation) from a total of n objects.

Very Important Note: n value should always be greater than value of r. Because, for example, we can get 2 out of 6. But we can’t get 6 out of 2. i.e., the main set should always be greater than the subset.

Example:

Lets assume that there are 3 people in a park. Let their names be A, B and C. There are only 2 seats available to sit. So the possible ways for people to sit over the seat using permutation are {AB, AC, BA, BC, CA, CB}. In our example, we have 3 people, so n = 3. And we have 2 seats available, so r = 2. Finally we get 6 objects/permutation in a set, so nPr is 6, when n = 3 and r = 2.

Factorial Definition: Factorial of a positive integer n, denoted by n!, is the product of all positive integers less than or equal to n.

Important Note: By convention, Factorial of 0 is 1. i.e., 0! = 1.

Example: 5! = 5 x 4 x 3 x 2 x 1 which is equal to 120. i.e., 5! = 120.

Related Read:
C Program To Find nCr Factorial

Video Tutorial: C Program To Find nPr Factorial


[youtube https://www.youtube.com/watch?v=KPPHJ8yZRdk]

YouTube Link: https://www.youtube.com/watch?v=KPPHJ8yZRdk [Watch the Video In Full Screen.]

Source Code: C Program To Find nPr Factorial

#include<stdio.h>

int factorial(int);

int main()
{
    int n, r;
    float npr;

    printf("Enter a positive value for n and r\n");
    scanf("%d%d", &n, &r);

    npr = factorial(n) / factorial(n - r);

    printf("\nnPr Factorial of %d and %d is %0.2f.\n", n, r, npr);

    return 0;
}

int factorial(int num)
{
    int fact = 1, count;

    for(count = 1; count <= num; count++)
    {
        fact = fact * count;
    }
    return(fact);
}

Output 1:
Enter positive integer value for n and r
3
2

nPr Factorial of 3 and 2 is 6.00

Output 2:
Enter positive integer value for n and r
6
2

nPr Factorial of 6 and 2 is 30.00

Source Code: C Program To Find nPr Factorial using Recursion

#include<stdio.h>

int factorial(int);

int main()
{
    int n, r;
    float npr;

    printf("Enter positive integer value for n and r\n");
    scanf("%d%d", &n, &r);

    npr = factorial(n) / factorial(n-r);

    printf("\nnPr Factorial of %d and %d is %0.2f\n", n, r, npr);

    return 0;
}

int factorial(int num)
{
    if(num)
        return(num * factorial(num - 1));
    else
        return 1;
}

Output 1:
Enter positive integer value for n and r
3
2

nPr Factorial of 3 and 2 is 6.00

Output 2:
Enter positive integer value for n and r
6
2

nPr Factorial of 6 and 2 is 30.00

Source Code: C Program To Find nPr Factorial using Recursion and Ternary or Conditional Operator

#include<stdio.h>

int factorial(int);

int main()
{
    int n, r;
    float npr;

    printf("Enter positive integer value for n and r\n");
    scanf("%d%d", &n, &r);

    npr = factorial(n) / factorial(n-r);

    printf("\nnPr Factorial of %d and %d is %0.2f\n", n, r, npr);

    return 0;
}

int factorial(int num)
{
  return( (num != 0) ? (num * factorial(num - 1)) : 1);
}

Output 1:
Enter positive integer value for n and r
3
2

nPr Factorial of 3 and 2 is 6.00

Output 2:
Enter positive integer value for n and r
6
2

nPr Factorial of 6 and 2 is 30.00

Logic To Find nPr Factorial of n and r

We ask the user to input positive integer value for n and r. We then make use of nPr formula to get the nPr value.

To find Factorial logic, please visit the links present below in “Related Read” section below:

Related Read:
C Program To Find Factorial of a Number using Function
C Program To Find Factorial of a Number using Recursion

We have separate short videos explaining the iterative logic, recursive logic to find factorial of a user input number.

For list of all c programming interviews / viva question and answers visit: C Programming Interview / Viva Q&A List

For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert

C Program To Find nCr Factorial

Lets write a C Program to Find nCr Factorial for the user input values of n and r.

In this video tutorial we are showing iterative logic, recursive logic and how to write single line of code for getting factorial using recursion and ternary/conditional operator.

Formula To Calculate nCr Factorial

nCr = n! / (r! * (n-r)!);

Definition:

The number of different, unordered combination of r objects from a set of n objects is called nCr Factorial of n and r.

Very Important Note: n value should always be greater than value of r. Because, for example, we can get 2 out of 6. But we can’t get 6 out of 2. i.e., the main set should always be greater than the subset.

Example:

Lets assume that there are 3 people in a park. Let their names be A, B and C. There are only 2 seats available to sit. So the possible ways for people to sit over the seat using combination are {AB, AC, BC}. Since nCr is unordered combination AB and BA are considered same, so we use only one of them in our set. So in our example, we have 3 people, so n = 3. And we have 2 seats available, so r = 2. Finally we get 3 objects/combination in a set, so nCr is 3, when n = 3 and r = 2.

Factorial Definition: Factorial of a positive integer n, denoted by n!, is the product of all positive integers less than or equal to n.

Important Note: By convention, Factorial of 0 is 1. i.e., 0! = 1.

Example: 5! = 5 x 4 x 3 x 2 x 1 which is equal to 120. i.e., 5! = 120.

Video Tutorial: C Program To Find nCr Factorial


[youtube https://www.youtube.com/watch?v=s9fF8aSfQwg]

YouTube Link: https://www.youtube.com/watch?v=s9fF8aSfQwg [Watch the Video In Full Screen.]

Source Code: C Program To Find nCr Factorial

#include<stdio.h>

int factorial(int);

int main()
{
    int n, r;
    float ncr;

    printf("Enter a positive value for n and r\n");
    scanf("%d%d", &n, &r);

    ncr = factorial(n) / ( factorial(r) * factorial(n - r) );

    printf("\nnCr Factorial of %d and %d is %0.2f.\n", n, r, ncr);

    return 0;
}

int factorial(int num)
{
    int fact = 1, count;

    for(count = 1; count <= num; count++)
    {
        fact = fact * count;
    }
    return(fact);
}

Output 1:
Enter a positive value for n and r
3
2

nCr Factor of 3 and 2 is 3.00.

Output 2:
Enter a positive value for n and r
6
2

nCr Factor of 6 and 2 is 15.00.

Source Code: C Program To Find nCr Factorial using Recursion

#include<stdio.h>

int factorial(int);

int main()
{
    int n, r;
    float ncr;

    printf("Enter a positive value for n and r\n");
    scanf("%d%d", &n, &r);

    ncr = factorial(n) / ( factorial(r) * factorial(n - r) );

    printf("\nnCr Factorial of %d and %d is %0.2f.\n", n, r, ncr);

    return 0;
}

int factorial(int num)
{
    if(num)
        return(num * factorial(num-1));
    else
        return 1;
}

Output 1:
Enter a positive value for n and r
3
2

nCr Factor of 3 and 2 is 3.00.

Output 2:
Enter a positive value for n and r
6
2

nCr Factor of 6 and 2 is 15.00.

Source Code: C Program To Find nCr Factorial using Recursion and Ternary or Conditional Operator

#include<stdio.h>

int factorial(int);

int main()
{
    int n, r;
    float ncr;

    printf("Enter a positive value for n and r\n");
    scanf("%d%d", &n, &r);

    ncr = factorial(n) / ( factorial(r) * factorial(n - r) );

    printf("\nnCr Factorial of %d and %d is %0.2f.\n", n, r, ncr);

    return 0;
}

int factorial(int num)
{
  return( (num != 0) ? num * factorial(num-1) : 1);
}

Output 1:
Enter a positive value for n and r
3
2

nCr Factor of 3 and 2 is 3.00.

Output 2:
Enter a positive value for n and r
6
2

nCr Factor of 6 and 2 is 15.00.

Logic To Find nCr Factorial of n and r

We ask the user to input positive integer value for n and r. We then make use of nCr formula to get the nCr value.

To find Factorial logic, please visit the links present below in “Related Read” section.

Related Read:
C Program To Find Factorial of a Number using Function
C Program To Find Factorial of a Number using Recursion

We have separate short videos explaining the iterative logic, recursive logic to find factorial of a user input number.

For list of all c programming interviews / viva question and answers visit: C Programming Interview / Viva Q&A List

For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert

C Program To Count Digit k in Number n using Recursion

Lets write a C program to count the number of occurrences of digit k in user input positive integer number n, using recursion.

For Example:

If user inputs n = 12235, and k = 2, then our C program should find how many times digit 2 is present in number 12235.

Note: (num % 10) fetches last digit in num. If num = 1234; (num%10) fetches the last digit from right, which is 4.

(num/10) removes the last number from right. If num = 1234; (num/10) removes last digit i.e., 4 and the result of (num/10) will be 123.

Related Read:
C Program To Count Digit k in Number n

Video Tutorial: C Program To Count Digit k in Number n using Recursion


[youtube https://www.youtube.com/watch?v=sZhIRYXkquk]

YouTube Link: https://www.youtube.com/watch?v=sZhIRYXkquk [Watch the Video In Full Screen.]

Source Code: C Program To Count Digit k in Number n using Recursion

#include<stdio.h>

int occurrence(int, int);

int main()
{
    int n, k;

    printf("Enter a positive integer number\n");
    scanf("%d", &n);

    printf("Which digits occurrence you want to check for?\n");
    scanf("%d", &k);

    printf("\n%d has appeared %d times in %d.\n", k, occurrence(n, k), n);

    return 0;
}

int occurrence(int num, int k)
{
    if(num == 0)
        return 0;
    else if(k == (num%10))
        return(1 + occurrence(num/10, k));
    else
        return(occurrence(num/10, k));
}

Output:
Enter a positive integer number
12555
Which digits occurrence you want to check for?
5

5 has appeared 3 times in 12555.

Source Code: C Program To Count Digit k in Number n using Recursion and Ternary or Conditional Operator

#include<stdio.h>

int occurrence(int, int);

int main()
{
    int n, k;

    printf("Enter a positive integer number\n");
    scanf("%d", &n);

    printf("Which digits occurrence you want to check for?\n");
    scanf("%d", &k);

    printf("\n%d has appeared %d times in %d.\n", k, occurrence(n, k), n);

    return 0;
}

int occurrence(int num, int k)
{
    return( (num == 0)? 0 :
            (k == (num%10)) ?
            (1 + occurrence(num/10, k)) :
            (occurrence(num/10, k)));
}

Output:
Enter a positive integer number
155555061
Which digits occurrence you want to check for?
5

5 has appeared 5 times in 155555061.

To know more about Ternary or Conditional Operator visit:
Ternary Operator / Conditional Operator In C.

Logic To Count Digit k in Number n using Recursion

First we write the base condition i.e., if num is 0, then our function returns 0 to the calling function. Else if num%10 is equal to the value of k, then we return (1 + occurrence(num/10, k)). That way we count or increment by 1 for each time we found a digit which is equal to k, and then we reduce the num by one digit from right by doing num/10. k value will remain same throughout the program execution.

If num%10 is not equal to k, then we simply divide the num by 10, and reduce the value of num by one digit from right.

Example:

Lets assume that user input value for n and k:
n = 1223; k = 2;

n(n%10) == kcountn/10
1223False122
122True112
12True1+1=21
1False1+1=20

So, 2 appeared 2 times in 1223.

For list of all c programming interviews / viva question and answers visit: C Programming Interview / Viva Q&A List

For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert

C Program To Find Sum of Squares of Digits using Recursion

Write a C program to find sum of squares of digits of a positive integer number input by the user, using recursive function.

Example:

If user inputs num value as 123. Then we fetch the individual digits present in 123 i.e., 3, 2 and 1, square it and add it to get the final result.

i.e., (3 x 3) + (2 x 2) + (1 x 1) = 14.

So, sum of squares of digits of 123 is 14.

Video Tutorial: C Program To Find Sum of Squares of Digits using Recursion


[youtube https://www.youtube.com/watch?v=7YaJ2pIkzKc]

YouTube Link: https://www.youtube.com/watch?v=7YaJ2pIkzKc [Watch the Video In Full Screen.]

Source Code: C Program To Find Sum of Squares of Digits using Recursion

#include<stdio.h>

int square(int num)
{
    if(num == 0)
        return 0;
    else
        return( (num%10) * (num%10) + square(num/10) );
}

int main()
{
    int num;

    printf("Enter a positive integer number:\n");
    scanf("%d", &num);

    printf("Sum of squares of digits of %d is %d.\n", num, square(num));

    return 0;
}

Output:
Enter a positive integer number:
123
Sum of squares of digits of 123 is 14.

Source Code: C Program To Find Sum of Squares of Digits using Recursion and pow() method

#include<stdio.h>
#include<math.h>

int square(int num)
{
    if(num == 0)
        return 0;
    else
        return( pow((num%10), 2) + square(num/10) );
}

int main()
{
    int num;

    printf("Enter a positive integer number:\n");
    scanf("%d", &num);

    printf("Sum of squares of digits of %d is %d.\n", num, square(num));

    return 0;
}

Output:
Enter a positive integer number:
123
Sum of squares of digits of 123 is 14.

Here we are making use of pow() method present inside math.h library file. pow() takes base value as first argument and exponent value as its second argument.

Source Code: C Program To Find Sum of Squares of Digits using Recursion, Ternary/Conditional Operator and pow() method

#include<stdio.h>
#include<math.h>

int square(int);

int main()
{
    int num;

    printf("Enter a positive integer number:\n");
    scanf("%d", &num);

    printf("Sum of squares of digits of %d is %d.\n", num, square(num));

    return 0;
}

int square(int num)
{
    return( (num == 0) ? 0 : ( pow((num % 10), 2) + square(num/10) ));
}

Output 1:
Enter a positive integer number:
123
Sum of squares of digits of 123 is 14.

Output 2:
Enter a positive integer number:
2103
Sum of squares of digits of 2103 is 14.

Output 3:
Enter a positive integer number:
456
Sum of squares of digits of 456 is 77.

Output 4:
Enter a positive integer number:
2020
Sum of squares of digits of 2020 is 8.

Output 5:
Enter a positive integer number:
2021
Sum of squares of digits of 2021 is 9.

To know more about Ternary or Conditional Operator visit:
Ternary Operator / Conditional Operator In C.

Dry Run: Example

Lets assume that user has input num value as 123.

num(num%10)2num/10(num%10)2+square(num/10)
123(3)2129+square(12)
12(2)214+square(1)
1(1)201+square(0)

Value Returning – Control Shifting back.

Return ValueToResult
return 0;square(0)1+0=1
1square(1)4+1=5
5square(12)9+5=14

So, sum of squares of digits of 123 is 14.

For list of all c programming interviews / viva question and answers visit: C Programming Interview / Viva Q&A List

For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert

C Program To Convert Decimal To Binary Number using Recursion

A positive integer is entered through the keyboard, write a function to find the Binary equivalent of this number:

(1) Without using recursion.
(2) Using recursion.

Analyze The Problem Statement

We need to convert the user input Decimal number to its equivalent Binary number using iterative logic as well as recursive logic.

In this video tutorial, we’ll write 2 functions. One for iterative logic and another for recursive logic.

Expected Input/Output

Enter a Decimal number
14

Iterative Logic
Binary Equivalent of 14 is 1110.

Recursive Logic
Binary Equivalent of 14 is 11110.

Note: Binary number system can be derived by base 2 to the power of whole numbers.

Binary Number System

Explanation:

If user enters num = 14

We keep on dividing and modulo dividing the number by 2.

14 / 2 = 7, reminder 0.
07 / 2 = 3, reminder 1.
03 / 2 = 1, reminder 1.
01 / 2 = 0

So Binary equivalent of 14 is 1110.

Video Tutorial: C Program To Convert Decimal To Binary Number using Recursion


[youtube https://www.youtube.com/watch?v=hBaesKngoMY]

YouTube Link: https://www.youtube.com/watch?v=hBaesKngoMY [Watch the Video In Full Screen.]

Source Code: C Program To Convert Decimal To Binary Number using Recursion

#include<stdio.h>

int binary_rec(int);
int binary(int);

int main()
{
    int num;

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

    printf("Binary Equivalent (Iterative) of %d is %d\n", num, binary(num));
    printf("Binary Equivalent (Recursive) of %d is %d\n", num, binary_rec(num));

    return 0;
}

int binary_rec(int num)
{
    if(num == 0)
        return 0;
    else
        return((num % 2) + 10 * binary_rec(num/2));
}

int binary(int num)
{
    int rem, bin = 0, place = 1;
    while(num)
    {
        rem   = num % 2;
        num   = num / 2;
        bin   = bin + (rem * place);
        place = place * 10;
    }
    return(bin);
}

Output 1:
Enter a Decimal number
14

Iterative Logic
Binary Equivalent of 14 is 1110

Recursive Logic
Binary Equivalent of 14 is 1110

Output 2:
Enter a Decimal number
41

Iterative Logic
Binary Equivalent of 41 is 101001

Recursive Logic
Binary Equivalent of 41 is 101001

Logic To Convert Decimal Number To Binary Number using Recursion

For iterative logic, please check the video tutorial C Program To Convert Decimal Number To Binary Number, using While Loop.

Recursive Function Logic
Assume that user inputs num value as 14.

numnum % 2(num % 2) + 10 * binary_rec(num/2)
1414 % 2(0) + 10 * binary_rec(7)
77 % 2(1) + 10 * binary_rec(3)
33 % 2(1) + 10 * binary_rec(1)
11 % 2(1) + 10 * binary_rec(0)

Value Returning – Control Shifting back.

Return ValueToResult
return 0;(1) + 10 * binary_rec(0)(1) + 10 * 0 = 1
1(1) + 10 * binary_rec(1)(1) + 10 * 1 = 11
11(1) + 10 * binary_rec(3)(1) + 10 * 11 = 111
111(0) + 10 * binary_rec(7)(0) + 10 * 111 = 1110

Binary Equivalent of Decimal Number 14 is 1110.

Source Code: C Program To Convert Decimal To Binary Number using Recursion and Ternary or Conditional Operator

#include<stdio.h>

int binary_rec(int);
int binary(int);

int main()
{
    int num;

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

    printf("\nIterative Logic\n");
    printf("Binary Equivalent of %d is %d\n\n", num, binary(num));

    printf("\nRecursive Logic\n");
    printf("Binary Equivalent of %d is %d\n\n", num, binary_rec(num));

    return 0;
}

int binary_rec(int num)
{
    return( (num == 0) ? 0 : (num % 2) + 10 * binary_rec(num / 2));
}

int binary(int num)
{
    int rem, bin = 0, place = 1;

    while(num != 0)
    {
        rem   = num % 2;
        num   = num / 2;
        bin   = bin + (rem * place);
        place = place * 10;
    }
    return(bin);
}

Output 1:
Enter a Decimal number
14

Iterative Logic
Binary Equivalent of 14 is 1110

Recursive Logic
Binary Equivalent of 14 is 1110

Output 2:
Enter a Decimal number
41

Iterative Logic
Binary Equivalent of 41 is 101001

Recursive Logic
Binary Equivalent of 41 is 101001

To know more about Ternary or Conditional Operator visit:
Ternary Operator / Conditional Operator In C

Source Code: C Program To Convert Decimal To Binary Number using Recursion

Another Method

#include<stdio.h>

void binary_rec(int);
int binary(int);

int main()
{
    int num;

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

    printf("\nIterative Logic\n");
    printf("Binary Equivalent of %d is %d\n\n", num, binary(num));

    printf("\nRecursive Logic\n");
    printf("Binary Equivalent of %d is ", num);
    binary_rec(num);
    
    printf("\n");

    return 0;
}

void binary_rec(int num)
{
    if(num == 1)
        printf("1");
    else
    {
        binary_rec(num/2);
        printf("%d", num%2);
    }
}

int binary(int num)
{
    int rem, bin = 0, place = 1;

    while(num != 0)
    {
        rem   = num % 2;
        num   = num / 2;
        bin   = bin + (rem * place);
        place = place * 10;
    }
    return(bin);
}

Output:
Enter a Decimal number
14

Iterative Logic
Binary Equivalent of 14 is 1110

Recursive Logic
Binary Equivalent of 14 is 1110

Here we simply divide the number by 2 and keep passing it as new value of num to binary_rec() function, and we print num%2 once num = 1 and it returns the value 1.

Number Systems

number systems

1. Binary Number System uses base 2 and digits 01.
2. Octal Number System uses base 8 and digits 01234567.
3. Decimal Number System uses base 10 and digits 0123456789.
4. Hexadecimal Number System uses base 16 and digits 0123456789ABCDEF.

For list of all c programming interviews / viva question and answers visit: C Programming Interview / Viva Q&A List

For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert