If you’re a computer science student enrolled in University, your syllabus may not include in-depth learning of preprocessors. But when it comes to competitive exams and real-time application programming you’ll have to use preprocessors a lot. So better learn it now.
In upcoming videos we’ll cover preprocessor concepts in detail with simple example programs to explain the concepts/topics.
So stay tuned, stay subscribed to our YouTube channel and blog.
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.
#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:
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.
#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.
Lets write a C program to count the number of occurrences of digit k in user input positive integer number n, using iterative logic. We will be using while loop to iterate in this program.
For Example:
If user inputs n = 1550, and k = 5, then our C program should find how many times digit 5 is present in number 1550.
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.
Source Code: C Program To Count Digit k in Number n
#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)
{
int count = 0;
while(num)
{
if(num%10 == k)
count++;
num = num / 10;
}
return count;
}
Output: Enter a positive integer number 1550 Which digits occurrence you want to check for? 5
5 has appeared 2 times in 1550.
Logic To Count Digit k in Number n
We ask the user to input values for variable n and k. We need to find the number of occurrences of digit k in number n. We pass both these variables to a function called occurrence().
Inside occurrence() function Inside occurrence() function, we keep iterating the while loop until num value is 0. Btw, n value is copied into variable num. Now we check if (num%10) is equal to value of k. (num%10) fetches the last digit in the num. If (num%10) is equal to k, then we increment the value of count by 1. For each iteration of while loop we decrement the value of num by 1 digit, from right, by dividing num by 10 i.e., (num/10).
Once num is 0, control exits while loop and value of count will be returned to main method.
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.
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
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;