diff options
32 files changed, 1721 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; +} + diff --git a/programs/add-2-nums-fns.c b/programs/add-2-nums-fns.c new file mode 100644 index 0000000..82a5bd9 --- /dev/null +++ b/programs/add-2-nums-fns.c @@ -0,0 +1,39 @@ +#include <stdio.h> + +int add(int a, int b) +{ + return a + b; +} + +int main(void) +{ + int a, b, sum; + printf("Enter first number: "); + scanf("%d", &a); + printf("Enter second number: "); + scanf("%d", &b); + sum = add(a, b); + printf("The sum of %d and %d is %d.\n", a, b, sum); + return 0; +} + +/* +Output: + +Set 1: +Enter first number: 10 +Enter second number: 20 +The sum of 10 and 20 is 30. + +Set 2: +Enter first number: 1 +Enter second number: 2 +The sum of 1 and 2 is 3. + +Set 3: +Enter first number: 33 +Enter second number: 44 +The sum of 33 and 44 is 77. + +*/ + diff --git a/programs/avg-fn.c b/programs/avg-fn.c new file mode 100644 index 0000000..da8ec19 --- /dev/null +++ b/programs/avg-fn.c @@ -0,0 +1,39 @@ +#include <stdio.h> + +float average(float a, float b, float c) +{ + return (a + b + c) / 3; +} + +int main(void) +{ + float a, b, c; + printf("Average of three numbers\n\n"); + printf("Enter three numbers: "); + scanf("%f%f%f", &a, &b, &c); + printf("The average is %f\n", average(a, b, c)); + return 0; +} + +/* +Output: +Set 1: +Average of three numbers + +Enter three numbers: 1 2 3 +The average is 2.000000 + +Set 2: +Average of three numbers + +Enter three numbers: 1.5 2.3 7.9 +The average is 3.900000 + +Set 3: +Average of three numbers + +Enter three numbers: 123 45.6 7.89 +The average is 58.829999 + +*/ + diff --git a/programs/leap-year-fn.c b/programs/leap-year-fn.c new file mode 100644 index 0000000..43842a9 --- /dev/null +++ b/programs/leap-year-fn.c @@ -0,0 +1,35 @@ +#include <stdio.h> +#include <stdbool.h> + +bool is_leap_year(int y) +{ + return y % 400 == 0 || (y % 100 != 0 && y % 4 == 0); +} + +int main(void) +{ + int year; + printf("To check whether a given year is leap year or not\n\n"); + printf("Enter a year: "); + scanf("%d", &year); + if (year <= 1200 || year >= 9999) { + printf("Invalid year.\n"); + return 0; + } + printf("%d is%s a leap year.\n", year, is_leap_year(year) ? "" : " not"); + return 0; +} + +/* +Output: +Set 1: + + +Set 2: + +Set 3: + +Set 4: + +*/ + diff --git a/programs/pre-post.c b/programs/pre-post.c new file mode 100644 index 0000000..7eab3a2 --- /dev/null +++ b/programs/pre-post.c @@ -0,0 +1,7 @@ +#include <stdio.h> + +int main(int argc, char **argv) +{ + printf("%d%d%d", argc++, argc, ++argc); +} + diff --git a/specials/bank.c b/specials/bank.c new file mode 100644 index 0000000..fd45d20 --- /dev/null +++ b/specials/bank.c @@ -0,0 +1,124 @@ +/* + +*/ + +#include <stdio.h> +#include <stdlib.h> + +#include <unistd.h> + +struct account { + int number; + double amount; +}; + +struct bank_state { + int accounts; + double total_amount; +}; + +void show_main_menu(void) +{ + printf( + "Menu:\n" + " 1 Create Account\n" + " 2 Check account balance\n" + " 3 Withdraw from account\n" + " 4 Add to account\n" + " 5 Send money\n" + " 6 Close account\n" + " 7 Check bank status\n" + " 8 Exit\n\n" + ); +} + +int are_you_sure(char *prompt) +{ + char c; + printf("%s\n", prompt); + for (;;) { + printf("Are you sure? (y/n): "); + scanf("%c", &c); + switch (c) { + case 'y': case 'Y': + return 1; + case 'n': case 'N': + return 0; + } + printf("Enter 'y' or 'n' to confirm your choice\n"); + } +} + +int menu_prompt(int *choice) +{ + printf("Choose an option from menu: "); + return scanf("%d", choice); +} + +void discard_input_line(void) +{ + int c; + do { + c = getchar(); + } while (c != '\n' && c != EOF); +} + +FILE *open_database(int *is_created) +{ + FILE *f; + if (access("bank.db", F_OK) != 0) { + // the file does not exist + f = fopen("bank.db", "w+b"); + *is_created = 1; + } else if (access("bank.db", R_OK | W_OK) != 0) { + printf("Fatal error: Can not open file for reading and writing: Permission Denied\n"); + exit(65); + } else { + f = fopen("bank.db", "r+b"); + if (f == NULL) { + printf("Fatal Error: Can not open bank database.\n"); + exit(64); + } + } + return f; +} + +void read_database(FILE *f, void **data, size_t *size) +{ + fseek(f, 0L, SEEK_END); + *size = ftell(f); + *data = malloc(*size); + fread(*data, 1, *size, f); +} + +void make_empty_database(FILE *f) +{ + struct bank_state st = { 0, 0.0 }; + fwrite(&st, 1, sizeof(st), f); +} + +int main(void) +{ + int choice, is_created = 0; + FILE *f; + void *data; + size_t size; + f = open_database(&is_created); + if (is_created) make_empty_database(f); + read_database(f, &data, &size); + printf( + "Bank System\n" + "===========\n\n" + ); + for (;;) { + show_main_menu(); + if (menu_prompt(&choice)) break; + printf("Invalid choice. Please enter a number to choose an option from the menu.\n"); + discard_input_line(); + } + printf("%d\n", choice); + free(data); + fclose(f); + return 0; +} + diff --git a/specials/file-replace.c b/specials/file-replace.c new file mode 100644 index 0000000..2e01367 --- /dev/null +++ b/specials/file-replace.c @@ -0,0 +1,119 @@ +#include <stdio.h> +#include <stdlib.h> + +/* for ftruncate */ +#include <unistd.h> +#include <sys/types.h> + +size_t str_length(char *s) +{ + size_t n = 0; + while (*s++ != '\0') { + n++; + } + return n; +} + +int str_equal(char *a, char *b) +{ + do { + if (*a++ != *b++) { + return 0; + } + } while (*b != '\0'); + return 1; +} + +long find_in_file(FILE *f, char *needle) +{ + size_t nlen = str_length(needle); + long pos = 0; + char *str = malloc(nlen + 1); + while (!feof(f) || !ferror(f)) { + if (fread(str, 1, nlen, f) != nlen) { + break; + } + str[nlen] = '\0'; + //printf("%ld: %s\n", ftell(f), str); + if (str_equal(str, needle)) { + return pos; + } + fseek(f, ++pos, SEEK_SET); + } + free(str); + return -1; +} + +void move_char(FILE *f, long src, long dst) +{ + int c; + fseek(f, src, SEEK_SET); + c = fgetc(f); + fseek(f, dst, SEEK_SET); + fputc(c, f); +} + +void file_move_contents(FILE *f, long from, long to, long end) +{ + int c; + long p, q; + if (from < to) { + // in case of moving to the right side, + // we start from right end and move backwards to the start + for (p = end, q = end + to - from; p >= from; p--, q--) { + move_char(f, p, q); + } + } else { + // otherwise, we start from the left and move towards + // the right + for (p = from, q = to; p <= end; p++, q++) { + move_char(f, p, q); + } + } +} + +int main(int argc, char **argv) +{ + FILE *f; + int fd; + long pos; + long olen, nlen, end; + if (argc < 4) { + printf("Usage: %s [file] [needle] [replacement]\n", argv[0]); + printf("Replaces needle in the contents of file with replacement.\n\n"); + return 2; + } + if (argv[2][0] == '\0') { + printf("%s: Invalid needle: needle can not be empty.\n", argv[0]); + return 3; + } + f = fopen(argv[1], "r+"); + if (f == NULL) { + printf("%s: Error: Can not open file `%s` for reading.\n", + argv[0], argv[1]); + return 1; + } + pos = find_in_file(f, argv[2]); + if (pos == -1) { + printf("The string \"%s\" is not found in file `%s`\n", + argv[2], argv[1]); + } else { + printf("The string \"%s\" is found in file `%s` at position %ld\n", argv[2], argv[1], pos); + fseek(f, 0L, SEEK_END); + end = ftell(f); + olen = str_length(argv[2]); + nlen = str_length(argv[3]); + file_move_contents(f, pos + olen, pos + nlen, end - 1); + // write down the replacement in the right place + fseek(f, pos, SEEK_SET); + fwrite(argv[3], 1, nlen, f); + fseek(f, 0L, SEEK_END); + end = ftell(f); + // truncate the file to the right size + fd = fileno(f); + ftruncate(fd, end + nlen - olen); + } + fclose(f); + return 0; +} + diff --git a/specials/file-search.c b/specials/file-search.c new file mode 100644 index 0000000..0644db7 --- /dev/null +++ b/specials/file-search.c @@ -0,0 +1,71 @@ +#include <stdio.h> +#include <stdlib.h> + +size_t str_length(char *s) +{ + size_t n = 0; + while (*s++ != '\0') { + n++; + } + return n; +} + +int str_equal(char *a, char *b) +{ + do { + if (*a++ != *b++) { + return 0; + } + } while (*b != '\0'); + return 1; +} + +long find_in_file(FILE *f, char *needle) +{ + size_t nlen = str_length(needle); + long pos = 0; + char *str = malloc(nlen + 1); + while (!feof(f) || !ferror(f)) { + if (fread(str, 1, nlen, f) != nlen) { + break; + } + str[nlen] = '\0'; + //printf("%ld: %s\n", ftell(f), str); + if (str_equal(str, needle)) { + return pos; + } + fseek(f, ++pos, SEEK_SET); + } + return -1; +} + +int main(int argc, char **argv) +{ + FILE *f; + long pos; + if (argc < 3) { + printf("Usage: %s [file] [needle]\n", argv[0]); + printf("Searches for needle in the contents of file.\n\n"); + return 2; + } + if (argv[2][0] == '\0') { + printf("%s: Invalid needle: needle can not be empty.\n", argv[0]); + return 3; + } + f = fopen(argv[1], "r"); + if (f == NULL) { + printf("%s: Error: Can not open file `%s` for reading.\n", + argv[0], argv[1]); + return 1; + } + pos = find_in_file(f, argv[2]); + if (pos == -1) { + printf("The string \"%s\" is not found in file `%s`\n", + argv[2], argv[1]); + } else { + printf("The string \"%s\" is found in file `%s` at position %ld\n", argv[2], argv[1], pos); + } + fclose(f); + return 0; +} + diff --git a/specials/string-search.c b/specials/string-search.c new file mode 100644 index 0000000..7fa0ec1 --- /dev/null +++ b/specials/string-search.c @@ -0,0 +1,71 @@ +#include <stdio.h> +#include <stdlib.h> + +size_t str_length(char *s) +{ + size_t n = 0; + while (*s++ != '\0') { + n++; + } + return n; +} + +int str_equal(char *a, char *b) +{ + do { + if (*a++ != *b++) { + return 0; + } + } while (*b != '\0'); + return 1; +} + +long find_in_file(FILE *f, char *needle) +{ + size_t nlen = str_length(needle); + long pos = 0; + char *str = malloc(nlen + 1); + while (!feof(f) || !ferror(f)) { + if (fread(str, 1, nlen, f) != nlen) { + break; + } + str[nlen] = '\0'; + printf("%ld: %s\n", ftell(f), str); + if (str_equal(str, needle)) { + return ftell(f); + } + fseek(f, ++pos, SEEK_SET); + } + return -1; +} + +int main(int argc, char **argv) +{ + FILE *f; + long pos; + if (argc < 3) { + printf("Usage: %s [file] [needle]\n", argv[0]); + printf("Searches for needle in the contents of file.\n\n"); + return 2; + } + if (argv[2][0] == '\0') { + printf("%s: Invalid needle: needle can not be empty.\n", argv[0]); + return 3; + } + f = fopen(argv[1], "r"); + if (f == NULL) { + printf("%s: Error: Can not open file `%s` for reading.\n", + argv[0], argv[1]); + return 1; + } + pos = find_in_file(f, argv[2]); + if (pos == -1) { + printf("The string \"%s\" is not found in file `%s`\n", + argv[2], argv[1]); + } else { + printf("The string \"%s\" is found in file `%s` at position %ld\n", argv[2], argv[1], pos); + } + fclose(f); + return 0; +} + |