diff --git a/calc_nPr.c b/calc_nPr.c new file mode 100644 index 0000000..7c71fae --- /dev/null +++ b/calc_nPr.c @@ -0,0 +1,42 @@ +#include + +void main() +{ + int n, r, npr_var; + + printf("Enter the value of n:"); + scanf("%d", &n); + printf("\nEnter the value of r:"); + scanf("%d", &r); + + /* nPr is also known as P(n,r), the formula is: + * P(n,r) = n! / (n - r)! For 0 <= r <= n. + */ + npr_var = fact(n) / fact(n - r); + printf("\nThe value of P(%d,%d) is: %d",n,r,npr_var); +} +// Function for calculating factorial +int fact(int num) +{ + int k = 1, i; + // factorial of 0 is 1 + if (num == 0) + { + return(k); + } + else + { + for (i = 1; i <= num; i++) + { + k = k * i; + } + } + return(k); +} +Output: + +Enter the value of n: +5 +Enter the value of r: +2 +The value of P(6,2) is: 30 diff --git a/rootsofquad.c b/rootsofquad.c new file mode 100644 index 0000000..27e738c --- /dev/null +++ b/rootsofquad.c @@ -0,0 +1,40 @@ +#include +#include // This is needed to use sqrt() function + +int main() +{ + printf("\n\n\t\tStudytonight - Best place to learn\n\n\n"); + float a, b, c, determinant, r1, r2, real, imag; + printf("\n\nEnter coefficients a, b and c: \n\n\n"); + scanf("%f%f%f", &a, &b, &c); + + /* + mathematical formula to know the + nature of the roots + */ + determinant == b*b - 4*a*c; + + if(determinant > 0) // both roots are real + { + r1 = (-b + sqrt(determinant))/2*a; // Brackets are important + r2 = (-b - sqrt(determinant))/2*a; + printf("\n\n\nRoots are: %.2f and %.2f ", r1, r2); + } + else if(determinant == 0) // both roots are real and equal + { + r1 = r2 = -b/(2*a); // brackets are important + printf("\n\n\nRoots are: %.2f and %.2f ", r1, r2); + } + /* + Determinant < 0 - both roots are imaginary of the + form real + i*imaginary + */ + else + { + real = -b/(2*a); + imag = sqrt(-determinant)/(2*a); + printf("\n\n\nRoots are %.2f + i%.2f and %.2f - i%.2f ", real, imag, real, imag); + } + printf("\n\n\n\n\t\t\tCoding is Fun !\n\n\n"); + return 0; +}