Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions Practical2/Conversion.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#include <stdio.h>
#include <math.h>

int main(void) {

/* Declare variables */
int i,inum,tmp,numdigits;
float fnum;
char binnum[60];

/* Intialise 4-byte integer */
inum = 33554431;
/* Convert to 4-byte float */
fnum = (float) inum;


/* Convert to binary number (string)*/
i = 0; tmp = inum;
while (tmp > 0) {
sprintf(&binnum[i],"%1d",tmp%2);
tmp = tmp/2;
i++;
}

/* Terminate the string */
binnum[i] = '\0';


/* Complete the expression */
numdigits = (int) ceil(log2(inum + 1));
printf("The number of digits is %d\n",numdigits);



printf("inum=%d, fnum=%f, inum in binary=%s\n",
inum,fnum,binnum);

}
7 changes: 7 additions & 0 deletions Practical2/REAMDE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Command to run Conversion.c
gcc -o conversion Conversion.c -lm
./conversion

Command to run sum.c
gcc -o sum sum.c -lm
./sum
34 changes: 34 additions & 0 deletions Practical2/Sum.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#include <stdio.h>


int main(void) {
/* Declare variables */
int i;
float sum1, sum2, diff;


/* First sum */
sum1 = 0.0;
for (i=1; i<=1000; i++) {
/* Insert here */
sum1 += 1.0 / i;
}


/* Second sum */
sum2 = 0.0;
for (i=1000; i>0; i--) {
/* Insert the same line as above except use sum2 */
sum2 += 1.0 / i;

}

printf(" Sum1=%f\n",sum1);
printf(" Sum2=%f\n",sum2);

/* Find the difference */
diff = sum1 - sum2;

printf(" Difference between the two is %f\n",diff);

}
28 changes: 28 additions & 0 deletions Practical3/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# Trapezoidal Rule Approximation for f(x) = tan(x)

This C program approximates the integral of f(x) = tan(x) from 0 to π/3 using the Trapezoidal Rule. The code calculates the numerical approximation and compares it with the actual value obtained from log(2).

## Compilation and Running Instructions

1. **Compile the Program:**
- Open a terminal.
- Navigate to the directory containing the C file (`trapezoidal_rule.c`).
- Compile the program using a C compiler (e.g., gcc):
```bash
gcc -o trapezoidal_rule trapezoidal_rule.c -lm
```
Note: The `-lm` flag is used to link the math library.

2. **Run the Program:**
- Execute the compiled binary:
```bash
./trapezoidal_rule
```

3. **Expected Output:**
- The program will display the approximation of the integral using the Trapezoidal Rule and the actual value of the integral.





35 changes: 35 additions & 0 deletions Practical3/trapezoidal_rule.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#include <stdio.h>
#include <math.h>

int main() {
// Define variables
int n = 12;
double a = 0;
double b = M_PI / 3;
double w = (b - a) / (double)n;

// Initialize the sum with the values at the endpoints
double sum = tan(a) + tan(b);
int i;

// Iterate through equidistant points and update the sum
for (i = 1; i < n; i++) {
sum += 2 * tan(a + w * i);
}

// Finalize the approximation using the Trapezoidal Rule
sum = sum * w * 0.5;

// Print the approximation result
printf("The Approximation of f(x) = tan(x) from 0 to pi/3 is %.3f\n", sum);

// Calculate the actual value using log(2)
double actual_value = log(2.0);

// Print the actual value
printf("The actual value of f(x) = tan(x) from 0 to pi/3 is %.3f\n", actual_value);

return 0;
}


23 changes: 23 additions & 0 deletions Practical4/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Trapezoidal Rule Approximation for tan(x)

This C program approximates the integral of tan(x) from 0 to 60 degrees using the Trapezoidal Rule. The code calculates the numerical approximation and compares it with the actual value obtained from log(2).

## Compilation and Running Instructions

1. **Compile the Program:**
- Open a terminal.
- Navigate to the directory containing the C file (`practical4.c`).
- Compile the program using gcc:
```bash
gcc -o prog filename.c -lm
```
Note: The `-lm` flag is used to link the math library.

2. **Run the Program:**
- Execute the compiled binary:
```bash
./prog
```

3. **Expected Output:**
- The program will display the values of tan(x) for each degree, the approximation of the integral using the Trapezoidal Rule, and the actual value of the integral.
46 changes: 46 additions & 0 deletions Practical4/practical4.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#include <stdio.h>
#include <math.h>

double converttoradians(double degree);
double calarea(int n);

int main() {
int n = 12;
double area;
area = calarea(n);
int i;
double arr[n+1];
for(i=0;i<=n;i++){
arr[i]=tan(converttoradians(5.0*i));
printf("The value of tan(x) when x=%d is %.2f ",i*5,arr[i]);
printf("\n");

}

printf("The approximation of tan(x) from 0-60 degrees using trapezoidal rule is %.3f", area);
printf("\n");
printf("The actual value of tan(x) from 0-60 degrees is:%.3f",log(2.0));

return 0;
}

double converttoradians(double degree) {
return (degree * M_PI) / 180.0;
}

double calarea(int n) {
int i;
double arr[n + 1], rad, w;
for (i = 0; i < n + 1; i++) {
rad = tan(converttoradians(5.0 * i));
arr[i] = rad;
}
double area = arr[0] + arr[n];
for (i = 1; i < n; i++) {
area = area + 2.0 * arr[i];
}
w = converttoradians((60.0 - 0) / (2.0 * n));
area = area * w;
return area;
}

44 changes: 44 additions & 0 deletions Practical5/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# Fibonacci Sequence Program

## Overview

This C program generates the Fibonacci sequence up to a specified value of `n`. It uses a function to calculate the next Fibonacci number based on the previous two. The program includes comments for clarity and readability.

## Instructions

### 1. Compilation

Compile the program using a C compiler. For example:

```bash
gcc -o fibonacci fibonacci.c -lm

### 2. Execution
Run the compiled program:
./fibonacci

### 3. Usage
Enter the desired value of n when prompted. The program will then display the Fibonacci sequence up to the specified value.

# Arctangent Approximation Program

## Overview

This C program calculates and compares two approximations of the arctangent function for a range of input values. The approximations are implemented in `archtanx1` and `archtanx2` functions. The program includes comments for clarity and readability.

## Instructions

### 1. Compilation

Compile the program using a C compiler. For example:

```bash
gcc -o arctan_approx arctan_approx.c -lm

### 2. Execution
./arctan_approx

### 3. Usage
Enter the desired value of delta when prompted. The program will then display the difference between the arctangent approximations for a range of input values.


52 changes: 52 additions & 0 deletions Practical5/arctan_approx.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#include <stdlib.h>
#include <stdio.h>
#include <math.h>

// Function prototypes
double archtanx1(const double x, const double delta);
double archtanx2(const double x);

int main() {
double delta, x, tan1[1000], tan2[1000];
printf("Enter value of delta: ");
scanf("%lf", &delta);
x = -0.9;
int j;

for (j = 0; x <= 0.9 && j < 1000; j++) {
// Calculate arctangent approximations
tan1[j] = archtanx1(x, delta);
tan2[j] = archtanx2(x);

// Print the difference between the approximations for each x
printf("The difference between functions archtanx1 and archtanx2 is %.10f for x=%f\n", fabs(tan1[j] - tan2[j]), x);

x = x + 0.1;
}

return 0;
}

// Approximation of arctangent using a power series
double archtanx1(const double x, const double delta) {
double sum = 0;
double current = x;
int i = 0;
int exponent;

// Calculate terms of the power series until convergence
while (fabs(current) > delta) {
exponent = 2 * i + 1;
current = pow(x, exponent) / exponent;
sum += current;
i++;
}

return sum;
}

// Approximation of arctangent using logarithmic functions
double archtanx2(const double x) {
return (log(1 + x) - log(1 - x)) / 2;
}

46 changes: 46 additions & 0 deletions Practical5/fibonacci.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#include<stdio.h>
#include<stdlib.h>

// Function to calculate the next Fibonacci number
void fib(int *fn_1, int *fn_2);

int main() {
int n;
printf("Enter the value of n: ");
scanf("%d", &n);

int f0, f1, next, i;
f1 = 1;
f0 = 0;

// Print Fibonacci sequence for n=1
if (n == 1) {
printf("The Fibonacci sequence for n=%d is %d", n);
printf("\n");
}
// Print Fibonacci sequence for n=2
else if (n == 2) {
printf("The Fibonacci sequence for n=%d is %d", n, 1);
printf("\n");
} else {
printf("The Fibonacci sequence is %d ", f1, 1);

// Loop to calculate and print Fibonacci sequence up to n
for (i = 1; i <= n - 1; i++) {
fib(&f0, &f1);
printf("%d", f1);
printf(" ");
}
}

printf("\n");
return 0;
}

// Function to calculate the next Fibonacci number
void fib(int *a, int *b) {
int next;
next = *a + *b;
*a = *b;
*b = next;
}
8 changes: 8 additions & 0 deletions Practical6/.Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
CC =gcc
CCFLAGS=-03
matmult: main.o matmul.o
$(CC) -o matmult main.o matmul.o
matmul.o: matmul.c
$(CC) -c $(CCFLAGS) matmul.c
main.o: main.c
$(CC) -c $(CCFLAGS) main.c
8 changes: 8 additions & 0 deletions Practical6/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
CC =gcc
CCFLAGS=-O3
matmult: main.o matmul.o
$(CC) -o matmult main.o matmul.o
matmul.o: matmul.c
$(CC) -c $(CCFLAGS) matmul.c
main.o: main.c
$(CC) -c $(CCFLAGS) main.c
12 changes: 12 additions & 0 deletions Practical6/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Matrix Multiplication Program
This program performs matrix multiplication of two matrices, Cn×q = An×pBp×q, where n = 5, p = 3, and q = 4. It initializes three arrays A, B, and C of type double, and populates them according to specified rules.

# Compilation:
1. make
This will compile the main.c and matmult.c files and generate the executable named matmult.

2. ./matmult
This will execute the program, performing matrix multiplication and printing the results.



Loading