diff options
Diffstat (limited to 'assignments')
24 files changed, 1216 insertions, 0 deletions
diff --git a/assignments/01-grading-system.c b/assignments/01-grading-system.c new file mode 100644 index 0000000..3c15f24 --- /dev/null +++ b/assignments/01-grading-system.c @@ -0,0 +1,63 @@ +/* Grading system */ +#include <stdio.h> + +int main(void) +{ + int n1, n2, n3, n4; + float avg; + printf("Grading system\n\n"); + printf("Enter marks obtained in four subjects: "); + scanf("%d%d%d%d", &n1, &n2, &n3, &n4); + avg = (float)(n1 + n2 + n3 + n4)/4; + printf("The average of the marks is %f\n", avg); + if (avg >= 80) + printf("The student has grade A.\n"); + if (avg >= 60 && avg < 80) + printf("The student has grade B.\n"); + if (avg >= 40 && avg < 60) + printf("The student has grade C.\n"); + if (avg < 40) + printf("The student has failed.\n"); + return 0; +} + +/* +Output: +Set 1: +Grading system + +Enter marks obtained in four subjects: 81 +85 +86 +91 +The average of the marks is 85.750000 +The student has grade A. + +Set 2: +Grading system + +Enter marks obtained in four subjects: 86 75 71 42 +The average of the marks is 68.500000 +The student has grade B. + +Set 3: +Grading system + +Enter marks obtained in four subjects: 45 +55 +51 +42 +The average of the marks is 48.250000 +The student has grade C. + +Set 4: +Grading system + +Enter marks obtained in four subjects: 25 +31 +40 32 +The average of the marks is 32.000000 +The student has failed. + +*/ + diff --git a/assignments/02-leapyear.c b/assignments/02-leapyear.c new file mode 100644 index 0000000..f281dc2 --- /dev/null +++ b/assignments/02-leapyear.c @@ -0,0 +1,47 @@ +/* Leap year */ +#include <stdio.h> + +int main(void) +{ + int year; + printf("Enter a year: "); + scanf("%d", &year); + if (year <= 1200 || year >= 9999) { + printf("Invalid year.\n"); + return 0; + } + if ((year % 400 == 0) || (year % 100 != 0 && year % 4 == 0)) + printf("%d is a leap year.\n", year); + else + printf("%d is not a leap year.\n", year); + return 0; +} + +/* +Output: +Set 1: +Enter a year: 1300 +1300 is not a leap year. + +Set 2: +Enter a year: 1600 +1600 is a leap year. + +Set 3: +Enter a year: 2008 +2008 is a leap year. + +Set 4: +Enter a year: 2003 +2003 is not a leap year. + +Set 5: +Enter a year: 1100 +Invalid year. + +Set 6: +Enter a year: 10000 +Invalid year. + +*/ + diff --git a/assignments/03-quadratic-equation.c b/assignments/03-quadratic-equation.c new file mode 100644 index 0000000..d7000df --- /dev/null +++ b/assignments/03-quadratic-equation.c @@ -0,0 +1,79 @@ +/* Quadratic equation and the nature of the roots */ + +#include <stdio.h> +#include <math.h> + +int main(void) +{ + float a, b, c, discr, x1, x2; + printf("Finding the roots and the nature of them of quadratic equation\n" + "\t\tax^2+bx+c=0\n\n"); + printf("Enter a: "); + scanf("%f", &a); + printf("Enter b: "); + scanf("%f", &b); + printf("Enter c: "); + scanf("%f", &c); + if (a == 0) { + printf("In a quadratic equation, a can not be zero.\n"); + return 0; + } + discr = b * b - 4 * a * c; + if (discr < 0) { + printf("The roots of this equation are imaginary.\n"); + return 0; + } + if (discr == 0) { + printf("The roots of this equation are real and equal.\n"); + } + if (discr > 0) { + printf("The roots of this equation are real and distinct.\n"); + } + x1 = (-b + sqrt(discr)) / (2 * a); + x2 = (-b - sqrt(discr)) / (2 * a); + printf("The roots are: %f and %f\n", x1, x2); + return 0; +} + +/* +Output: +Set 1: +Finding the roots and the nature of them of quadratic equation + ax^2+bx+c=0 + +Enter a: 0 +Enter b: -1 +Enter c: 2 +In a quadratic equation, a can not be zero. + +Set 2: +Finding the roots and the nature of them of quadratic equation + ax^2+bx+c=0 + +Enter a: -1 +Enter b: 4 +Enter c: -5 +The roots of this equation are imaginary. + +Set 3: +Finding the roots and the nature of them of quadratic equation + ax^2+bx+c=0 + +Enter a: 2.5 +Enter b: -5 +Enter c: 2.5 +The roots of this equation are real and equal. +The roots are: 1.000000 and 1.000000 + +Set 4: +Finding the roots and the nature of them of quadratic equation + ax^2+bx+c=0 + +Enter a: -1.23 +Enter b: 45.6 +Enter c: 789 +The roots of this equation are real and distinct. +The roots are: -12.849214 and 49.922382 + +*/ + diff --git a/assignments/04-sum-product-of-digits.c b/assignments/04-sum-product-of-digits.c new file mode 100644 index 0000000..d49acd3 --- /dev/null +++ b/assignments/04-sum-product-of-digits.c @@ -0,0 +1,49 @@ +/* Sum and product of the digits of given number */ + +#include <stdio.h> + +int main(void) +{ + int n, m, r, sum, prod; + printf("Sum and product of the digits of a given number\n\n"); + printf("Enter a number: "); + scanf("%d", &n); + m = n; + sum = 0; + prod = 1; + do { + r = m % 10; + m = m / 10; + sum = sum + r; + prod = prod * r; + } while (m > 0); + printf("Sum of digits of %d is %d\n", n, sum); + printf("Product of digits of %d is %d\n", n, prod); + return 0; +} + +/* +Output: +Set 1: +Sum and product of the digits of a given number + +Enter a number: 0 +Sum of digits of 0 is 0 +Product of digits of 0 is 0 + +Set 2: +Sum and product of the digits of a given number + +Enter a number: 123456 +Sum of digits of 123456 is 21 +Product of digits of 123456 is 720 + +Set 3: +Sum and product of the digits of a given number + +Enter a number: 123456789 +Sum of digits of 123456789 is 45 +Product of digits of 123456789 is 362880 + +*/ + diff --git a/assignments/05-reverse-digits.c b/assignments/05-reverse-digits.c new file mode 100644 index 0000000..05fadd7 --- /dev/null +++ b/assignments/05-reverse-digits.c @@ -0,0 +1,43 @@ +/* Print the digits of given number in reverse order */ + +#include <stdio.h> + +int main(void) +{ + int n, m, r, sum, prod; + printf("Print the digits of given number in reverse order\n\n"); + printf("Enter a number: "); + scanf("%d", &n); + m = n; + printf("%d printed in reverse order is: ", n); + do { + r = m % 10; + m = m / 10; + printf("%d", r); + } while (m > 0); + printf("\n"); + return 0; +} + +/* +Output: +Set 1: +Print the digits of given number in reverse order + +Enter a number: 0 +0 printed in reverse order is: 0 + +Set 2: +Print the digits of given number in reverse order + +Enter a number: 123456 +123456 printed in reverse order is: 654321 + +Set 3: +Print the digits of given number in reverse order + +Enter a number: 541563249 +541563249 printed in reverse order is: 942365145 + +*/ + diff --git a/assignments/06-check-palindrome-number.c b/assignments/06-check-palindrome-number.c new file mode 100644 index 0000000..d1a10d8 --- /dev/null +++ b/assignments/06-check-palindrome-number.c @@ -0,0 +1,47 @@ +/* Check whether a given number is palindromic or not */ + +#include <stdio.h> + +int main(void) +{ + int n, m, p, r; + printf("Check whether a given number is palindromic\n\n"); + printf("Enter a number: "); + scanf("%d", &n); + m = n; + p = 0; + while (m > 0) { + r = m % 10; + m = m / 10; + p = p * 10 + r; + } + if (p == n) { + printf("%d is a palindromic number.\n", n); + } else { + printf("%d is not a palindromic number.\n", n); + } + return 0; +} + +/* +Output: +Set 1: +Check whether a given number is palindromic + +Enter a number: 0 +0 is a palindromic number. + +Set 2: +Check whether a given number is palindromic + +Enter a number: 123456 +123456 is not a palindromic number. + +Set 3: +Check whether a given number is palindromic + +Enter a number: 1234321 +1234321 is a palindromic number. + +*/ + diff --git a/assignments/07-check-prime.c b/assignments/07-check-prime.c new file mode 100644 index 0000000..3a492ef --- /dev/null +++ b/assignments/07-check-prime.c @@ -0,0 +1,55 @@ +#include <stdio.h> +#include <stdbool.h> + +bool is_prime(int n) +{ + int i; + for (i = 2; i < n; i++) { + if (n % i == 0) return false; + } + return true; +} + +int main(void) +{ + int i, n; + printf("Check whether a given number is prime or not\n\n"); + printf("Enter a number: "); + scanf("%d", &n); + if (n < 2) { + printf("Invalid input, it must be an integer greater than 2\n"); + return 0; + } + printf("%d is%s a prime number.\n", n, is_prime(n) ? "" : " not"); + return 0; +} + +/* +Output: +Set 1: +Check whether a given number is prime or not + +Enter a number: 100 +100 is not a prime number. + +Set 2: +Check whether a given number is prime or not + +Enter a number: 97 +97 is a prime number. + +Set 3: +Check whether a given number is prime or not + +Enter a number: 2 +2 is a prime number. + +Set 4: +Check whether a given number is prime or not + +Enter a number: 1 +Invalid input, it must be an integer greater than 2 + + +*/ + diff --git a/assignments/08-prime-factors.c b/assignments/08-prime-factors.c new file mode 100644 index 0000000..93b027d --- /dev/null +++ b/assignments/08-prime-factors.c @@ -0,0 +1,46 @@ +/* Print the prime factorization of a given number */ + +#include <stdio.h> + +int main(void) +{ + int n, m, i; + printf("Print the prime factorization of a given number\n\n"); + printf("Enter a number: "); + scanf("%d", &n); + printf("The prime factors of %d are: ", n); + m = n; + while (m > 1) { + for (i = 2; i <= m; i++) { + while (m % i == 0) { + printf("%d ", i); + m = m / i; + } + } + } + printf("\n"); + return 0; +} + +/* +Output: +Set 1: +Print the prime factorization of a given number + +Enter a number: 2 +The prime factors of 2 are: 2 + +Set 2: +Print the prime factorization of a given number + +Enter a number: 120 +The prime factors of 120 are: 2 2 2 3 5 + +Set 3: +Print the prime factorization of a given number + +Enter a number: 97 +The prime factors of 97 are: 97 + +*/ + diff --git a/assignments/09-check-perfect-number.c b/assignments/09-check-perfect-number.c new file mode 100644 index 0000000..33b575e --- /dev/null +++ b/assignments/09-check-perfect-number.c @@ -0,0 +1,53 @@ +/* Check whether a given number is a perfect number */ + +#include <stdio.h> + +int main(void) +{ + int n, sum, i, j, isprime; + printf("Check whether a given number is a perfect number\n\n"); + printf("Enter a number: "); + scanf("%d", &n); + sum = 0; + for (i = 1; i < n; i++) { + if (n % i == 0) { + sum += i; + } + } + if (n == sum) { + printf("%d is a perfect number.\n", n); + } else { + printf("%d is not a perfect number.\n", n); + } + return 0; +} + + +/* +Output: +Set 1: +Check whether a given number is a perfect number + +Enter a number: 6 +6 is a perfect number. + +Set 2: +Check whether a given number is a perfect number + +Enter a number: 10 +10 is not a perfect number. + +Set 3: +Check whether a given number is a perfect number + +Enter a number: 28 +28 is a perfect number. + +Set 4: +Check whether a given number is a perfect number + +Enter a number: 100 +100 is not a perfect number. + +*/ + diff --git a/assignments/10-hcf-lcm.c b/assignments/10-hcf-lcm.c new file mode 100644 index 0000000..4a10e3d --- /dev/null +++ b/assignments/10-hcf-lcm.c @@ -0,0 +1,54 @@ +/* Calculate the highest common factor and lowest common multiplier of two numbers */ + +#include <stdio.h> + +int main(void) +{ + int a, b, m, n, t; + printf("Calculate the HCF and LCM of two numbers\n\n"); + printf("Enter two numbers: "); + scanf("%d%d", &a, &b); + m = a; + n = b; + while (n > 0) { + t = n; + n = m % n; + m = t; + } + printf("The HCF of %d and %d is %d\n", a, b, m); + printf("The LCM of %d and %d is %d\n", a, b, (a*b)/m); + return 0; +} + +/* +Output: +Set 1: +Calculate the HCF and LCM of two numbers + +Enter two numbers: 12 16 +The HCF of 12 and 16 is 4 +The LCM of 12 and 16 is 48 + +Set 2: +Calculate the HCF and LCM of two numbers + +Enter two numbers: 112 67 +The HCF of 112 and 67 is 1 +The LCM of 112 and 67 is 7504 + +Set 3: +Calculate the HCF and LCM of two numbers + +Enter two numbers: 1 2 +The HCF of 1 and 2 is 1 +The LCM of 1 and 2 is 2 + +Set 4: +Calculate the HCF and LCM of two numbers + +Enter two numbers: 123 456 +The HCF of 123 and 456 is 3 +The LCM of 123 and 456 is 18696 + +*/ + diff --git a/assignments/11-fibonacci.c b/assignments/11-fibonacci.c new file mode 100644 index 0000000..4019a7c --- /dev/null +++ b/assignments/11-fibonacci.c @@ -0,0 +1,44 @@ +/* Print first n fibonacci numbers */ + +#include <stdio.h> + +int main(void) +{ + int a, b, i, n, t; + printf("Print first n fibonacci number\n\n"); + printf("Enter a number: "); + scanf("%d", &n); + a = 1; b = 0; + printf("The first %d fibonacci numbers are: ", n); + for (i = 1; i <= n; i++) { + printf("%d ", b); + t = a; + a = a + b; + b = t; + } + printf("\n"); + return 0; +} + +/* +Output: +Set 1: +Print first n fibonacci number + +Enter a number: 5 +The first 5 fibonacci numbers are: 0 1 1 2 3 + +Set 2: +Print first n fibonacci number + +Enter a number: 10 +The first 10 fibonacci numbers are: 0 1 1 2 3 5 8 13 21 34 + +Set 3: +Print first n fibonacci number + +Enter a number: 20 +The first 20 fibonacci numbers are: 0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 + +*/ + diff --git a/assignments/12-armstrong-range.c b/assignments/12-armstrong-range.c new file mode 100644 index 0000000..cc57fb4 --- /dev/null +++ b/assignments/12-armstrong-range.c @@ -0,0 +1,62 @@ +#include <math.h> +#include <stdio.h> +#include <stdbool.h> + +bool is_armstrong(int n) +{ + int m = n, r, s = 0; + float c = ceil(log10(n)); + while (m > 0) { + r = m % 10; + m /= 10; + s += pow(r, c); + } + return n == s; +} + +int main(void) +{ + int i, l, h; + printf("Print Armstrong Numbers in a given range\n\n"); + printf("Enter the range: "); + scanf("%d%d", &l, &h); + if (h < l) { + printf("Invalid range\n"); + return 0; + } + printf("Armstrong numbers between %d and %d are: ", l, h); + for (i = l; i <= h; i++) { + if (is_armstrong(i)) printf("%d ", i); + } + printf("\n"); + return 0; +} + +/* +Output: +Set 1: +Print Armstrong Numbers in a given range + +Enter the range: 100 1 +Invalid range + +Set 2: +Print Armstrong Numbers in a given range + +Enter the range: 1 100 +Armstrong numbers between 1 and 100 are: 1 2 3 4 5 6 7 8 9 + +Set 3: +Print Armstrong Numbers in a given range + +Enter the range: 100 10000 +Armstrong numbers between 100 and 10000 are: 153 370 371 407 1634 8208 9474 + +Set 4: +Print Armstrong Numbers in a given range + +Enter the range: 10000 1000000 +Armstrong numbers between 10000 and 1000000 are: 54748 92727 93084 548834 + +*/ + diff --git a/assignments/13-binary-pattern.c b/assignments/13-binary-pattern.c new file mode 100644 index 0000000..5b3e4a9 --- /dev/null +++ b/assignments/13-binary-pattern.c @@ -0,0 +1,65 @@ +#include <stdio.h> + +int main(void) +{ + int i, j, h, f; + printf("Enter a height: "); + scanf("%d", &h); + for (i = 1; i <= h; i++) { + f = i % 2; + for (j = 1; j <= i; j++) { + printf("%d", f); + f = !f; + } + printf("\n"); + } + return 0; +} + +/* +Output: +Set 1: +Enter a height: 5 +1 +01 +101 +0101 +10101 + +Set 2: +Enter a height: 6 +1 +01 +101 +0101 +10101 +010101 + +Set 3: +Enter a height: 8 +1 +01 +101 +0101 +10101 +010101 +1010101 +01010101 + +Set 4: +Enter a height: 11 +1 +01 +101 +0101 +10101 +010101 +1010101 +01010101 +101010101 +0101010101 +10101010101 + + +*/ + diff --git a/assignments/14-star-pattern-90t.c b/assignments/14-star-pattern-90t.c new file mode 100644 index 0000000..2a285d2 --- /dev/null +++ b/assignments/14-star-pattern-90t.c @@ -0,0 +1,47 @@ +#include <stdio.h> + +int main(void) +{ + int i, j, h; + printf("Enter a height: "); + scanf("%d", &h); + for (i = 1; i <= h; i++) { + for (j = 1; j <= 2 * (h - i); j++) { + printf(" "); + } + for (j = 1; j <= 2 * i - 1; j++) { + printf("*"); + } + printf("\n"); + } + return 0; +} + +/* +Output: +Set 1: +Enter a height: 3 + * + *** +***** + +Set 2: +Enter a height: 5 + * + *** + ***** + ******* +********* + +Set 3: +Enter a height: 7 + * + *** + ***** + ******* + ********* + *********** +************* + +*/ + diff --git a/assignments/15-star-pattern-eql.c b/assignments/15-star-pattern-eql.c new file mode 100644 index 0000000..e32b745 --- /dev/null +++ b/assignments/15-star-pattern-eql.c @@ -0,0 +1,47 @@ +#include <stdio.h> + +int main(void) +{ + int i, j, h; + printf("Enter a height: "); + scanf("%d", &h); + for (i = 1; i <= h; i++) { + for (j = 1; j <= h - i; j++) { + printf(" "); + } + for (j = 1; j <= 2 * i - 1; j++) { + printf("*"); + } + printf("\n"); + } + return 0; +} + +/* +Output: +Set 1: +Enter a height: 3 + * + *** +***** + +Set 2: +Enter a height: 5 + * + *** + ***** + ******* +********* + +Set 3: +Enter a height: 7 + * + *** + ***** + ******* + ********* + *********** +************* + +*/ + diff --git a/assignments/16-star-pattern-extreme.c b/assignments/16-star-pattern-extreme.c new file mode 100644 index 0000000..97d4d8d --- /dev/null +++ b/assignments/16-star-pattern-extreme.c @@ -0,0 +1,50 @@ +#include <stdio.h> + +int main(void) +{ + int i, j, h; + printf("Enter a height: "); + scanf("%d", &h); + for (i = 1; i <= h; i++) { + for (j = 1; j <= i; j++) { + printf("*"); + } + for (j = 1; j <= 2 * h - 2 * i; j++) { + printf(" "); + } + for (j = 1; j <= i; j++) { + printf("*"); + } + printf("\n"); + } + return 0; +} + +/* +Output: +Set 1: +Enter a height: 4 +* * +** ** +*** *** +******** + +Set 2: +Enter a height: 5 +* * +** ** +*** *** +**** **** +********** + +Set 3: +Enter a height: 6 +* * +** ** +*** *** +**** **** +***** ***** +************ + +*/ + diff --git a/assignments/17-pascal-triangle.c b/assignments/17-pascal-triangle.c new file mode 100644 index 0000000..24047c6 --- /dev/null +++ b/assignments/17-pascal-triangle.c @@ -0,0 +1,52 @@ +#include <math.h> +#include <stdio.h> +#include <stdlib.h> + +static int count_digits(double n) +{ + return floor(log10(n)) + 1; +} + +static double factorial(double n) +{ + return tgamma(n + 1); +} + +static double comb(long n, long r) +{ + return factorial(n) / (factorial(r) * factorial(n - r)); +} + +int main(void) +{ + int i, j, k, h, l, *arr, *p; + size_t n; + printf("To Print the Pascal's Triangle\n\n"); + printf("Enter a height: "); + scanf("%d", &h); + l = count_digits(comb(h, h / 2)); + arr = malloc((h + 3) * sizeof(int)); + if (arr == NULL) { + printf("Insufficient memory.\n"); + return 0; + } + arr[0] = 1; + arr[1] = 0; + n = 1; + for (i = 1; i <= h + 1; i++) { + for (k = 1; k <= (h - i + 1) * l; k++) { + printf(" "); + } + for (k = 0; k < n; k++) { + printf("%- *d ", l + 1, arr[k]); + } + printf("\n"); + arr[k+1] = 0; + for (n++; k > 0; k--) { + arr[k] += arr[k-1]; + } + } + free(arr); + return 0; +} + diff --git a/assignments/18-harmonic-series.c b/assignments/18-harmonic-series.c new file mode 100644 index 0000000..4fc62b1 --- /dev/null +++ b/assignments/18-harmonic-series.c @@ -0,0 +1,45 @@ +#include <stdio.h> + +int main(void) +{ + int i, n; + float sum; + printf("Calculate the sum of 1/i from i to a given number.\n\n"); + printf("Enter a number: "); + scanf("%d", &n); + sum = 0; + for (i = 1; i <= n; i++) { + sum += 1/(float)i; + } + printf("The sum is %f\n", sum); + return 0; +} + +/* +Output: +Set 1: +Calculate the sum of 1/i from i to a given number. + +Enter a number: 1 +The sum is 1.000000 + +Set 2: +Calculate the sum of 1/i from i to a given number. + +Enter a number: 2 +The sum is 1.500000 + +Set 3: +Calculate the sum of 1/i from i to a given number. + +Enter a number: 10 +The sum is 2.928968 + +Set 4: +Calculate the sum of 1/i from i to a given number. + +Enter a number: 100 +The sum is 5.187378 + +*/ + diff --git a/assignments/19-int-series.c b/assignments/19-int-series.c new file mode 100644 index 0000000..7403517 --- /dev/null +++ b/assignments/19-int-series.c @@ -0,0 +1,46 @@ +#include <stdio.h> + +int main(void) +{ + int i, sum, n, a; + printf("Calculate the sum of (-1)^(n+1) * i for i from 1 to given number.\n\n"); + printf("Enter a number: "); + scanf("%d", &n); + a = 1; + sum = 0; + for (i = 1; i <= n; i++) { + sum += a * i; + a = -a; + } + printf("The sum is %d\n", sum); + return 0; +} + +/* +Output: +Set 1: +Calculate the sum of (-1)^(n+1) * i for i from 1 to given number. + +Enter a number: 1 +The sum is 1 + +Set 2: +Calculate the sum of (-1)^(n+1) * i for i from 1 to given number. + +Enter a number: 2 +The sum is -1 + +Set 3: +Calculate the sum of (-1)^(n+1) * i for i from 1 to given number. + +Enter a number: 10 +The sum is -5 + +Set 4: +Calculate the sum of (-1)^(n+1) * i for i from 1 to given number. + +Enter a number: 123 +The sum is 62 + +*/ + diff --git a/assignments/20-exp.c b/assignments/20-exp.c new file mode 100644 index 0000000..b9a4721 --- /dev/null +++ b/assignments/20-exp.c @@ -0,0 +1,54 @@ +#include <stdio.h> + +#define STEPS 100 + +double my_exp(double x) +{ + int i; + double n = 1; + double p = 1; + for (i = 1; i <= STEPS; i++) { + p *= x / i; + n += p; + } + return n; +} + +int main(void) +{ + double x; + printf("To calculate exp(x) for a given number x\n\n"); + printf("Enter a number: "); + scanf("%lf", &x); + printf("exp(%.14g) = %.14g\n", x, my_exp(x)); + return 0; +} + +/* +Output: +Set 1: +To calculate exp(x) for a given number x + +Enter a number: 1 +exp(1) = 2.718281828459 + +Set 2: +To calculate exp(x) for a given number x + +Enter a number: 2 +exp(2) = 7.3890560989306 + +Set 3: +To calculate exp(x) for a given number x + +Enter a number: 0.1 +exp(0.1) = 1.1051709180756 + +Set 4: +To calculate exp(x) for a given number x + +Enter a number: -1 +exp(-1) = 0.36787944117144 + +*/ + diff --git a/assignments/21-sin-cos.c b/assignments/21-sin-cos.c new file mode 100644 index 0000000..c8b2896 --- /dev/null +++ b/assignments/21-sin-cos.c @@ -0,0 +1,71 @@ +#include <stdio.h> + +#define STEPS 1000 + +double my_sin(double x) +{ + int i; + double n = x; + double p = x; + for (i = 2; i <= STEPS * 2; i += 2) { + p *= -1 * x * x / (i * (i + 1)); + n += p; + } + return n; +} + +double my_cos(double x) +{ + int i; + double n = 1; + double p = 1; + for (i = 2; i <= STEPS * 2; i += 2) { + p *= -1 * x * x / (i * (i - 1)); + n += p; + } + return n; +} + +int main(void) +{ + double x; + printf("To calculate the sine and cosine of a given number\n\n"); + printf("Enter a number: "); + scanf("%lf", &x); + printf("sin(%.14g) = %.14g\n", x, my_sin(x)); + printf("cos(%.14g) = %.14g\n", x, my_cos(x)); + return 0; +} + +/* +Output: +Set 1: +To calculate the sine and cosine of a given number + +Enter a number: 0 +sin(0) = 0 +cos(0) = 1 + +Set 2: +To calculate the sine and cosine of a given number + +Enter a number: 1.5707963267948966 +sin(1.5707963267949) = 1 +cos(1.5707963267949) = 4.2647940513479e-17 + +Set 3: +To calculate the sine and cosine of a given number + +Enter a number: 3.141592653579893 +sin(3.1415926535799) = 9.9005553937036e-12 +cos(3.1415926535799) = -1 + +Set 4: +To calculate the sine and cosine of a given number + +Enter a number: -0.13720941543579 +sin(-0.13720941543579) = -0.13677929342038 +cos(-0.13720941543579) = 0.99060154698618 + +*/ + diff --git a/assignments/22-dec-to-roman.c b/assignments/22-dec-to-roman.c new file mode 100644 index 0000000..05a9553 --- /dev/null +++ b/assignments/22-dec-to-roman.c @@ -0,0 +1,53 @@ +#include <stdio.h> + +void roman_part(int n, int l, char u, char h, char p) +{ + int i, k; + if (n >= l) { + k = n / l; + if (k == 9) { + printf("%c%c", u, p); + } else if (k >= 5) { + printf("%c", h); + for (i = 6; i <= k; i++) { + printf("%c", u); + } + } else if (k == 4) { + printf("%c%c", u, h); + } else { + for (i = 1; i <= k; i++) { + printf("%c", u); + } + } + } +} + +void decimal_to_roman(int n) +{ + int i, k; + if (n >= 1000) { + k = n / 1000; + for (i = 1; i <= k; i++) { + printf("M"); + } + } + roman_part(n % 1000, 100, 'C', 'D', 'M'); + roman_part(n % 100, 10, 'X', 'L', 'C'); + roman_part(n % 10, 1, 'I', 'V', 'X'); + printf("\n"); +} + +int main(void) +{ + int n; + printf("To convert a decimal number to roman number\n\n"); + printf("Enter a number: "); + scanf("%d", &n); + if (n < 1 || n > 3999) { + printf("Invalid input: it must be between 1 and 3999, inclusive.\n"); + return 0; + } + decimal_to_roman(n); + return 0; +} + diff --git a/assignments/23-basic-calculator.c b/assignments/23-basic-calculator.c new file mode 100644 index 0000000..203fdf8 --- /dev/null +++ b/assignments/23-basic-calculator.c @@ -0,0 +1,23 @@ +#include <stdio.h> + +int menu_option(char *choice) +{ + printf( + "Basic Calculator\n" + "================\n" + "Menu:\n" + " + Add\n" + " - Subtract\n" + " * Multiply\n" + " / Divide\n" + " q Quit\n\n" + ); +} + +int main(void) +{ + char *choice; + menu_option(&choice); + return 0; +} + diff --git a/assignments/24-value-vs-ref.c b/assignments/24-value-vs-ref.c new file mode 100644 index 0000000..8640792 --- /dev/null +++ b/assignments/24-value-vs-ref.c @@ -0,0 +1,21 @@ +#include <stdio.h> + +void swap_by_value(int a, int b) +{ + int c = a; + b = a; + b = c; +} + +void swap_by_ref(int *a, int *b) +{ + int c = *a; + *a = *b; + *b = c; +} + +int main(void) +{ + return 0; +} + |