Lab Assignment: Programming in C
Lab Assignment: Programming in C
Lab Assignment
Bachelor of Technology
Submitted by
Submitted to
Dr. Mahendra Kumar Srivas
UPES
Bidholi, Via Prem Nagar, Dehradun, Uttarakhand
July-Dec – 2024
INDEX
Experiment 2: Operators
1. WAP a C program to calculate the area and perimeter of a rectangle based on its length and width.
2. WAP a C program to Convert temperature from Celsius to Fahrenheit using the formula: F = (C *
9/5) + 32.
1. WAP to take check if the triangle is valid or not. If the validity is established, do check if the
triangle is isosceles, equilateral, right angle, or scalene. Take sides of the triangle as input from a
user.
2. WAP to compute the BMI Index of the person and print the BMI values as per the following ranges.
You can use the following formula to compute BMI= weight(kgs)/Height(Mts)*Height(Mts).
BMI
Starvation <15
Anorexic 5.1 to 17.5
Underweight 17.6 to 18.5
Ideal 18.6 to 24.9
Overweight 25 to 25.9
Obese 30 to 39.9
Morbidity Obese 40.0 above
3. WAP to check if three points (x1,y1), (x2,y2) and (x3,y3) are collinear or not.
4. According to the Gregorian calendar, it was Monday on the date 01/01/01. If Any year is input
through the keyboard write a program to find out what is the day on 1st January of this year.
5. WAP using ternary operator, the user should input the length and breadth of a rectangle, one has to
find out which rectangle has the highest perimeter. The minimum number of rectangles should be
three.
Experiment 3.2: Loops
1. WAP to enter numbers till the user wants. At the end, it should display the count of positive,
negative, and Zeroes entered.
2. WAP to print the multiplication table of the number entered by the user. It should be in the correct
formatting. Num * 1 = Num
3. WAP to generate the following set of output.
a.
1
23
456
b.
1
11
121
1331
14641
4. The population of a town is 100000. The population has increased steadily at the rate of 10% per
year for the last 10 years. Write a program to determine the population at the end of each year in the
last decade.
5. Ramanujan Number is the smallest number that can be expressed as the sum of two cubes in two
different ways. WAP to print all such numbers up to a reasonable limit.
1. Declare a global variable outside all functions and use it inside various functions to understand its
accessibility.
2. Declare a local variable inside a function and try to access it outside the function. Compare this with
accessing the global variable from within the function.
3. Declare variables within different code blocks (enclosed by curly braces) and test their accessibility
within and outside those blocks.
4. Declare a static local variable inside a function. Observe how its value persists across function
calls.
Experiment 5: Array
1. WAP to read a list of integers and store it in a single dimensional array. Write a C program to print
the second largest integer in a list of integers.
2. WAP to read a list of integers and store it in a single dimensional array. Write a C program to count
and display positive, negative, odd, and even numbers in an array.
3. WAP to read a list of integers and store it in a single dimensional array. Write a C program to find
the frequency of a particular number in a list of integers.
4. WAP that reads two matrices A (m x n) and B (p x q) and computes the product A and B. Read matrix
A and matrix B in row major order respectively. Print both the input matrices and resultant matrix
with suitable headings and output should be in matrix format only. Program must check the
compatibility of orders of the matrices for multiplication. Report appropriate message in case of
incompatibility.
Experiment 6: Functions
1. Develop a recursive and non-recursive function FACT(num) to find the factorial of a number, n!,
defined by FACT(n) = 1, if n = 0. Otherwise, FACT(n)
2. = n * FACT(n-1). Using this function, write a C program to compute the binomial coefficient.
Tabulate the results for different values of n and r with suitable messages.
3. Develop a recursive function GCD (num1, num2) that accepts two integer arguments. Write a C
program that invokes this function to find the greatest common divisor of two given integers.
4. Develop a recursive function FIBO (num) that accepts an integer argument. Write a C program that
invokes this function to generate the Fibonacci sequence up to num.
5. Develop a C function ISPRIME (num) that accepts an integer argument and returns 1 if the argument
is prime, a 0 otherwise. Write a C program that invokes this function to generate prime numbers
between the given ranges.
6. Develop a function REVERSE (str) that accepts a string argument. Write a C program that invokes
this function to find the reverse of a given string.
2. Write a C program to compute the monthly pay of 100 employees using each employee‗s name, basic
pay. The DA is computed as 52% of the basic pay. Gross-salary (basic pay + DA). Print the
employees name and gross salary.
3. Create a Book structure containing book_id, title, author name and price. Write a C program to pass a
structure as a function argument and print the book details.
4. Create a union containing 6 strings: name, home_address, hostel_address, city, state and zip. Write a
C program to display your present address.
Experiment 8: Pointers
1. Declare different types of pointers (int, float, char) and initialize them with the addresses of
variables. Print the values of both the pointers and the variables they point to.
2. Perform pointer arithmetic (increment and decrement) on pointers of different data types. Observe
how the memory addresses change and the effects on data access.
3. Write a function that accepts pointers as parameters. Pass variables by reference using pointers and
modify their values within the function.
1. Write a program to create a new file and write text into it.
2. Open an existing file and read its content character by character, and then close the file.
3. Open a file, read its content line by line, and display each line on the console.
1. Write a program to create a simple linked list in C using pointer and structure.
2. Write a program to insert item in middle of the linked list.
Experiment 11: Bitwise Operator
1. Write a program to apply bitwise OR, AND and NOT operators on bit level.
2. Write a program to apply left shift and right shift operator.
Code
#include<stdio.h>
int main()
{
printf("Hello World");
return 0;
}
Output
Output
3. Write a C program that prompts the user to enter their name and age.
Code
#include <stdio.h>
int main()
{
char name[50];
int age;
return 0;
}
Output
4. Write a C program to add two numbers, take number from user.
Code
#include<stdio.h>
#include<math.h>
int main()
{
int a, b, c;
c = a + b;
printf("The required number is: %d\n", c);
return 0;
}
Output
Experiment 2: Operators
1. WAP a C program to calculate the area and perimeter of a rectangle based on its length and width.
Code
#include <stdio.h>
int main()
{
printf("Enter the length of the rectangle: ");
scanf("%f", &length);
Output
2. WAP a C program to Convert temperature from Celsius to Fahrenheit using the formula: F = (C *
9/5) + 32.
Code
#include <stdio.h>
int main()
{
float celsius, fahrenheit;
Output
Experiment 3.1: Conditional Statements
1. WAP to take check if the triangle is valid or not. If the validity is established, do check if the
triangle is isosceles, equilateral, right angle, or scalene. Take sides of the triangle as input from a
user.
Code
#include <stdio.h>
#include <math.h>
int main()
{
float a,b,c;
Output
2. WAP to compute the BMI Index of the person and print the BMI values as per the following ranges.
You can use the following formula to compute BMI= weight(kgs)/Height(Mts)*Height(Mts).
BMI
Starvation <15
Anorexic 5.1 to 17.5
Underweight 17.6 to 18.5
Ideal 18.7 to 24.9
Overweight 25 to 25.9
Obese 30 to 39.9
Morbidity Obese 40.0 above
Code
#include<stdio.h>
int main ()
{ float a, w, h;
printf("Weight (kg):");
scanf("%f", &w);
printf("Height (meter):");
scanf("%f", &h);
a = w / (h*h);
printf("BMI: %f\n",a);
if (a <= 15)
{printf("Starvation");}
else if (a >= 15.1 && a <=17.5)
{printf("Anorexic");}
else if (a >= 17.6 && a <= 18.5)
{printf("Underweight");}
else if (a >= 18.6 && a <= 24.9)
{printf("Ideal");}
else if (a >= 25 && a <= 25.9)
{printf("Overweight");}
else if (a >= 30 && a <= 39.9)
{printf("Obese");}
else
{printf("Morbidity Obese");}
return 0; }
Output
3. WAP to check if three points (x1,y1), (x2,y2) and (x3,y3) are collinear or not.
Code
#include<stdio.h>
int main()
{
int x1,x2,x3,y1,y2,y3;
printf("ENTER x1,x2,x3,y1,y2,y3:");
scanf("%d%d%d%d%d%d",&x1,&x2,&x3,&y1,&y2,&y3);
if(x1*y2-x1*y3-y1*x2+y1*x3+x2*y3-x3*y2 ==0)
{
printf("THE POINTS ARE COLLINEAR");
}
else
{
printf("THE POINTS ARE NOT COLLINEAR");
}
return 0;
}
Output
4. According to the Gregorian calendar, it was Monday on the date 01/01/01. If Any year is input
through the keyboard write a program to find out what is the day on 1st January of this year.
Code
#include <stdio.h>
int month = 1;
int day = 1;
if (month == 1 || month == 2) {
month += 12;
year -= 1; }
int k = year % 100;
int j = year / 100;
return day_of_week; }
int main() {
int year;
printf("Enter a year: ");
scanf("%d", &year);
return 0;
}
Output
5. WAP using ternary operator, the user should input the length and breadth of a rectangle, one has to
find out which rectangle has the highest perimeter. The minimum number of rectangles should be
three.
Code
#include <stdio.h>
int main() {
float length1, breadth1, length2, breadth2, length3, breadth3;
float perimeter1, perimeter2, perimeter3;
return 0;
}
Output
Experiment 3.2: Loops
1. WAP to enter numbers till the user wants. At the end, it should display the count of positive,
negative, and Zeroes entered.
Code
#include <stdio.h>
int main() {
int number, positive_count = 0, negative_count = 0, zero_count = 0;
char choice;
do {
printf("Enter a number: ");
scanf("%d", &number);
if (number > 0) {
positive_count++;
} else if (number < 0) {
negative_count++;
} else {
zero_count++;
}
return 0;
}
Output
2. WAP to print the multiplication table of the number entered by the user. It should be in the correct
formatting. Num * 1 = Num
Code
#include <stdio.h>
int main() {
int number;
return 0;
}
Output
3. WAP to generate the following set of output.
a.
1
23
456
Code
#include <stdio.h>
int main() {
int num = 1;
return 0;
}
Output
b.
1
11
121
1331
1 4641
Code
#include <stdio.h>
int main() {
int rows = 5;
int coef = 1;
return 0;
}
Output
4. The population of a town is 100000. The population has increased steadily at the rate of 10% per
year for the last 10 years. Write a program to determine the population at the end of each year in the
last decade.
Code
#include <stdio.h>
int main()
{
int population = 100000;
float rate = 0.10;
int year;
Output
5. Ramanujan Number is the smallest number that can be expressed as the sum of two cubes in two
different ways. WAP to print all such numbers up to a reasonable limit.
Code
#include <stdio.h>
int main() {
int limit = 2000; // You can change this limit as needed
int found = 0;
if (!found) {
printf("No Ramanujan numbers found up to %d.\n", limit);
}
return 0;
}
Output
Experiment 4: Variable and Scope of Variable
1. Declare a global variable outside all functions and use it inside various functions to understand its
accessibility.
Code
#include <stdio.h>
void functionA() {
printf("Function A: Global variable = %d\n", globalVar);
}
void functionB() {
globalVar += 50;
printf("Function B: Global variable after modification = %d\n", globalVar);
}
void functionC() {
printf("Function C: Global variable = %d\n", globalVar);
}
int main() {
printf("Main: Initial global variable = %d\n", globalVar);
functionA();
functionB();
functionC();
return 0;
}
Output
2. Declare a local variable inside a function and try to access it outside the function. Compare this with
accessing the global variable from within the function.
Code
#include <stdio.h>
void myFunction() {
int globalVar = 25;
printf("Inside myFunction: Local variable = %d\n", globalVar);
printf("Inside myFunction: Global variable = %d\n", globalVar);
}
int main() {
myFunction();
printf("In main: Global variable = %d\n", globalVar);
printf("In main: Trying to access local variable = %d\n", globalVar);
return 0;
}
Output
3. Declare variables within different code blocks (enclosed by curly braces) and test their accessibility
within and outside those blocks.
Code
#include <stdio.h>
int main() {
{
int blockVar1 = 10;
printf("Inside first block: blockVar1 = %d\n", blockVar1);
}
{
int blockVar2 = 20;
printf("Inside second block: blockVar2 = %d\n", blockVar2);
}
return 0;
}
Output
4. Declare a static local variable inside a function. Observe how its value persists across function
calls.
Code
#include <stdio.h>
void myFunction() {
static int count = 0;
count++;
printf("Function called %d times\n", count);
}
int main() {
myFunction();
myFunction();
myFunction();
return 0;
}
Output
Experiment 5: Array
1. WAP to read a list of integers and store it in a single dimensional array. Write a C program to print
the second largest integer in a list of integers.
CODE
#include <stdio.h>
int main() {
int n, i, largest, second_largest;
printf("Enter the number of integers: ");
scanf("%d", &n);
int arr[n];
printf("Enter %d integers: ", n);
for(i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
Output
2. WAP to read a list of integers and store it in a single dimensional array. Write a C program to count
and display positive, negative, odd, and even numbers in an array.
CODE
#include <stdio.h>
int main() {
int n, i, positive = 0, negative = 0, odd = 0, even = 0;
printf("Enter the number of integers: ");
scanf("%d", &n);
int arr[n];
printf("Enter %d integers: ", n);
for(i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
if(arr[i] % 2 == 0) {
even++;
} else {
odd++;
}
}
return 0;
}
Output
3. WAP to read a list of integers and store it in a single dimensional array. Write a C program to find the
frequency of a particular number in a list of integers
CODE
#include <stdio.h>
int main() {
int n, i, num, count = 0;
printf("Enter the number of integers: ");
scanf("%d", &n);
int arr[n];
printf("Enter %d integers: ", n);
for(i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
Output
4. WAP that reads two matrices A (m x n) and B (p x q) and computestheproduct A and B. Read matrix
A and matrix B in row major order respectively. Print both the input matrices and resultant matrix
with suitable headingsandoutput should be in matrix format only. Program must check the
compatibilityof orders of the matrices for multiplication. Report appropriate messageincase of
incompatibility
CODE
#include <stdio.h>
int main() {
int m, n, p, q;
printf("Enter the number of rows and columns for matrix A (m x n): ");
scanf("%d %d", &m, &n);
printf("Enter the number of rows and columns for matrix B (p x q): ");
scanf("%d %d", &p, &q);
if (n != p) {
printf("Matrices cannot be multiplied. Incompatible dimensions.\n");
return 0;
}
printf("\nMatrix A:\n");
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
printf("%d ", A[i][j]);
}
printf("\n");
}
printf("\nMatrix B:\n");
for (int i = 0; i < p; i++) {
for (int j = 0; j < q; j++) {
printf("%d ", B[i][j]);
}
printf("\n");
}
return 0;
}
Output
Experiment 5: Array
1. WAP to read a list of integers and store it in a single dimensional array. Write a C program to print
the second largest integer in a list of integers.
Code
#include <stdio.h>
#include <limits.h>
int main() {
int n, i;
int arr[100];
int largest, second_largest;
if (n < 2) {
printf("Please enter at least 2 integers.\n");
return 1; }
if (second_largest == INT_MIN) {
printf("There is no second largest element.\n");
} else {
printf("The second largest integer is: %d\n", second_largest); }
return 0;
}
Output
2. WAP to read a list of integers and store it in a single dimensional array. Write a C program to count
and display positive, negative, odd, and even numbers in an array.
Code
#include <stdio.h>
int main() {
int n, i;
int arr[100];
int positive_count = 0, negative_count = 0, odd_count = 0, even_count = 0;
return 0;
}
Output
3. WAP to read a list of integers and store it in a single dimensional array. Write a C program to find the
frequency of a particular number in a list of integers.
Code
#include <stdio.h>
int main() {
int n, i, num, count = 0;
int arr[100];
return 0;
}
Output
4. WAP that reads two matrices A (m x n) and B (p x q) and computes the product A and B. Read matrix
A and matrix B in row major order respectively. Print both the input matrices and resultant matrix
with suitable headings and output should be in matrix format only. Program must check the
compatibility of orders of the matrices for multiplication. Report appropriate message in case of
incompatibility.
Code
#include <stdio.h>
int main() {
int m, n, p, q;
int A[10][10], B[10][10], C[10][10];
printf("Enter the number of rows and columns for matrix A (m n): ");
scanf("%d %d", &m, &n);
printf("Enter the number of rows and columns for matrix B (p q): ");
scanf("%d %d", &p, &q);
if (n != p) {
printf("Matrices cannot be multiplied. Incompatible dimensions.\n");
return 0;
}
printf("\nMatrix A:\n");
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
printf("%d ", A[i][j]);
}
printf("\n");
}
printf("\nMatrix B:\n");
for (int i = 0; i < p; i++) {
for (int j = 0; j < q; j++) {
printf("%d ", B[i][j]);
}
printf("\n");
}
return 0;
}
Output
Experiment 6: Functions
1. Develop a recursive and non-recursive function FACT(num) to find the factorial of a number, n!,
defined by FACT(n) = 1, if n = 0. Otherwise, FACT(n)
Code
#include <stdio.h>
int main() {
int num;
printf("Enter a non-negative integer: ");
scanf("%d", &num);
if (num < 0) {
printf("Factorial is not defined for negative numbers.\n");
return 1;
}
return 0;
}
Output
2. = n * FACT(n-1). Using this function, write a C program to compute the binomial coefficient.
Tabulate the results for different values of n and r with suitable messages.
Code
#include <stdio.h>
int main() {
int max_n, r;
printf("Enter the maximum value of n: ");
scanf("%d", &max_n);
return 0;
}
Output
3. Develop a recursive function GCD (num1, num2) that accepts two integer arguments. Write a C
program that invokes this function to find the greatest common divisor of two given integers.
Code
#include <stdio.h>
int factorial(int n) {
if (n == 0) {
return 1;
}
return n * factorial(n - 1);
}
int main() {
int n, r;
printf("Enter the maximum value of n: ");
scanf("%d", &n);
return 0;
}
Output
4. Develop a recursive function FIBO (num) that accepts an integer argument. Write a C program that
invokes this function to generate the Fibonacci sequence up to num.
Code
#include <stdio.h>
int factorial(int n) {
if (n == 0) {
return 1;
}
return n * factorial(n - 1);
}
int main() {
int n, r;
printf("Enter the maximum value of n: ");
scanf("%d", &n);
return 0;
}
Output
5. Develop a C function ISPRIME (num) that accepts an integer argument and returns 1 if the argument
is prime, a 0 otherwise. Write a C program that invokes this function to generate prime numbers
between the given ranges.
Code
#include <stdio.h>
int main() {
int start, end;
printf("Enter the range (start and end): ");
scanf("%d %d", &start, &end);
printf("\n");
return 0;
}
Output
6. Develop a function REVERSE (str) that accepts a string argument. Write a C program that invokes
this function to find the reverse of a given string.
Code
#include <stdio.h>
#include <string.h>
int main() {
char str[100];
str[strcspn(str, "\n")] = '\0'; // Remove the newline character from the string
REVERSE(str);
return 0;
}
Output
Experiment 7: Structures and Union
Code
#include <stdio.h>
struct Complex {
float real;
float imag;
};
int main() {
struct Complex c1, c2, sum, difference;
printf("Sum: ");
writeComplex(sum);
printf("Difference: ");
writeComplex(difference);
return 0;
}
Output
2. Write a C program to compute the monthly pay of 100 employees using each employee‗s name, basic
pay. The DA is computed as 52% of the basic pay. Gross-salary (basic pay + DA). Print the
employees name and gross salary.
Code
#include <stdio.h>
struct Employee {
char name[50];
float basicPay;
float grossSalary;
};
int main() {
struct Employee employees[MAX_EMPLOYEES];
int i;
return 0;
}
Output
3. Create a Book structure containing book_id, title, author name and price. Write a C program to pass a
structure as a function argument and print the book details.
Code
#include <stdio.h>
#define MAX_TITLE_LENGTH 50
#define MAX_AUTHOR_NAME_LENGTH 50
struct Book {
int book_id;
char title[MAX_TITLE_LENGTH];
char author_name[MAX_AUTHOR_NAME_LENGTH];
float price;
};
int main() {
struct Book myBook = {1, "The Catcher in the Rye", "J.D. Salinger", 9.99};
printBookDetails(myBook);
return 0;
}
Output
4. Create a union containing 6 strings: name, home_address, hostel_address, city, state and zip. Write a
C program to display your present address.
Code
#include <stdio.h>
#include <string.h>
struct Address {
char name[MAX_LENGTH];
char home_address[MAX_LENGTH];
char hostel_address[MAX_LENGTH];
char city[MAX_LENGTH];
char state[MAX_LENGTH];
char zip[MAX_LENGTH];
};
int main() {
struct Address address;
return 0;
}
Output
Experiment 8: Pointers
1. Declare different types of pointers (int, float, char) and initialize them with the addresses of
variables. Print the values of both the pointers and the variables they point to.
Code
#include<stdio.h>
int main() {
int intVar = 16;
float floatVar = 12.5;
char charVar = 'X';
return 0;
}
Output
2. Perform pointer arithmetic (increment and decrement) on pointers of different data types. Observe
how the memory addresses change and the effects on data access.
Code
#include <stdio.h>
int main() {
int intVar = 16;
float floatVar = 12.5;
char charVar = 'X';
intPtr++;
floatPtr++;
charPtr++;
return 0;
}
Output
3. Write a function that accepts pointers as parameters. Pass variables by reference using pointers and
modify their values within the function.
Code
#include <stdio.h>
int main() {
int intVar = 16;
float floatVar = 12.5;
char charVar = 'X';
printf("Before modification:\n");
printf("intVar: %d, \nfloatVar: %.2f, \ncharVar: %c\n", intVar, floatVar, charVar);
printf("\nAfter modification:\n");
printf("intVar: %d, \nfloatVar: %.2f, \ncharVar: %c\n", intVar, floatVar, charVar);
return 0;
}
Output
Experiment 9: File Handling in C
1. Write a program to create a new file and write text into it.
Code
#include <stdio.h>
int main() {
FILE *file;
file = fopen("example.txt", "w");
if (file == NULL) {
return 1;
}
fprintf(file, "Hello, this is a text file.\n");
fclose(file);
return 0;
}
Output
2. Open an existing file and read its content character by character, and then close the file.
Code
#include <stdio.h>
int main() {
FILE *file;
char ch;
fclose(file);
return 0;
}
Output
3. Open a file, read its content line by line, and display each line on the console.
Code
#include <stdio.h>
int main() {
FILE *file;
char line[256];
fclose(file);
return 0;
}
Output
Experiment 10: Dynamic Memory Allocation
1. Write a program to create a simple linked list in C using pointer and structure.
Code
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* next;
};
int main() {
struct Node* head = NULL;
appendNode(&head, 10);
appendNode(&head, 20);
appendNode(&head, 30);
appendNode(&head, 40);
return 0;
}
Output
2. Write a program to insert item in middle of the linked list.
Code
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* next;
};
int main() {
struct Node* head = NULL;
insertMiddle(&head, 10);
insertMiddle(&head, 20);
insertMiddle(&head, 30);
insertMiddle(&head, 40);
insertMiddle(&head, 25);
return 0;
}
Output
Experiment 11: Bitwise Operator
1. Write a program to apply bitwise OR, AND and NOT operators on bit level.
Code
#include <stdio.h>
int main() {
int a = 12; // 1100 in binary
int b = 10; // 1010 in binary
int bitwise_or = a | b;
int bitwise_and = a & b;
int bitwise_not_a = ~a;
int bitwise_not_b = ~b;
return 0;
}
Output
2. Write a program to apply left shift and right shift operator.
Code
#include <stdio.h>
int main() {
int num = 5;
return 0;
}
Output
Experiment 12: Preprocessor and Directives in C
Code
#include <stdio.h>
#define PI 3.14
#define MAX_SIZE 100
#define GREETING "Hello, World!"
int main() {
float area;
int radius = 5;
printf("%s\n", GREETING);
printf("Area of the circle with radius %d: %.2f\n", radius, area);
printf("Maximum size allowed: %d\n", MAX_SIZE);
return 0;
}
Output
2. Write a program to define a function in directives.
Code
#include <stdio.h>
int main() {
int num = 4;
int result = SQUARE(num);
return 0;
}
Output
Experiment 13: Macros in C
Code
#include <stdio.h>
int main() {
int a = 10;
int b = 5;
printf("Arithmetic Operations:\n");
printf("%d + %d = %d\n", a, b, ADD(a, b));
printf("%d - %d = %d\n", a, b, SUBTRACT(a, b));
printf("%d * %d = %d\n", a, b, MULTIPLY(a, b));
printf("%d / %d = %d\n", a, b, DIVIDE(a, b));
return 0;
}
Output
Experiment 14: Static Library in C
Code
#include <stdio.h>
int main() {
int a = 10, b = 5;
return 0;
}
Output
2. Write a program to use static library in other program.
Code
#include <stdio.h>
#include <stdlib.h>
int main() {
int a = 10, b = 5;
return 0;
}
Output
Experiment 15: Shared Library in C
Code
#include <stdio.h>
#include <stdlib.h>
int main() {
int a = 10, b = 5;
return 0;
}
Output
2. Write a program to use shared library in other program.
Code
#include <stdio.h>
#include <stdlib.h>
int main() {
int a = 10, b = 5;
return 0;
}
Output
Experiment 16: Multithreading in C
Code
#include <stdio.h>
#include <pthread.h>
#include <unistd.h>
#define NUM_PRINTS 5
#define MAX_NUMBER 10
usleep(100000);
}
return NULL;
}
int main() {
pthread_t thread1, thread2;
pthread_join(thread1, NULL);
pthread_join(thread2, NULL);
return 0;
}
Output
Experiment 17: Socket Programming in C
Code
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <pthread.h>
address.sin_family = AF_INET;
address.sin_addr.s_addr = INADDR_ANY;
address.sin_port = htons(8080);
close(new_socket);
close(server_fd);
return NULL;
}
pthread_join(server_thread, NULL);
pthread_join(client_thread, NULL);
return 0;
}
Output
Experiment 18: Testing and Debugging
Code
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <pthread.h>
address.sin_family = AF_INET;
address.sin_addr.s_addr = INADDR_ANY;
address.sin_port = htons(8080);
if (listen(server_fd, 3) < 0) {
perror("Listen failed");
exit(EXIT_FAILURE);
}
close(new_socket);
close(server_fd);
return NULL;
}
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(8080);
int main() {
pthread_t server_thread, client_thread;
pthread_join(server_thread, NULL);
pthread_join(client_thread, NULL);
return 0;
}
Output