POINTERS and STRUCTURES Programs
POINTERS and STRUCTURES Programs
return 0;
}
int main() {
char *str;
str = (char *)malloc(100 * sizeof(char));
if (str == NULL) {
printf("Memory allocation failed!\n");
return 1;
}
printf("Enter a string: ");
fgets(str, 100, stdin);
int length = stringLength(str);
printf("Length of the string is: %d\n", length);
free(str);
return 0;
}
Sample Output:
Enter a string: Hello, world!
Length of the string is: 13
3. Program to Add Two Matrices Using Pointers
#include <stdio.h>
#include <stdlib.h>
int main() {
int **matrix1, **matrix2, **sum;
int rows, cols;
printf("Enter the number of rows and columns: ");
scanf("%d %d", &rows, &cols);
// Allocate memory for matrices
matrix1 = (int **)malloc(rows * sizeof(int *));
matrix2 = (int **)malloc(rows * sizeof(int *));
sum = (int **)malloc(rows * sizeof(int *));
// Free memory
for (int i = 0; i < rows; i++) {
free(matrix1[i]);
free(matrix2[i]);
free(sum[i]);
}
free(matrix1);
free(matrix2);
free(sum);
return 0;
}
Sample Output:
Enter the number of rows and columns: 2 2
Enter elements of matrix1:
12
34
Enter elements of matrix2:
56
78
Sum of the matrices:
68
10 12
4. Program to Reverse an Array Using Pointers
#include <stdio.h>
#include <stdlib.h>
void reverseArray(int *arr, int size) {
int *start = arr;
int *end = arr + size - 1;
int temp;
if (arr == NULL) {
printf("Memory allocation failed!\n");
return 1;
}
// Input values into the array
printf("Enter %d integers:\n", n);
for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
// Reverse the array
reverseArray(arr, n);
// Display the reversed array
printf("Reversed array:\n");
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("\n");
return 0;
}
Sample Output:
Enter the number of elements: 5
Enter 5 integers:
12345
Reversed array:
54321
5. Program to Calculate the Sum of an Array Using Pointers
#include <stdio.h>
#include <stdlib.h>
int sumArray(int *arr, int size) {
int sum = 0;
for (int i = 0; i < size; i++) {
sum += *(arr + i);
}
return sum;
}
int main() {
int *arr, n;
printf("Enter the number of elements: ");
scanf("%d", &n);
// Dynamically allocate memory for the array
arr = (int *)malloc(n * sizeof(int));
if (arr == NULL) {
printf("Memory allocation failed!\n");
return 1;
}
// Input values into the array
printf("Enter %d integers:\n", n);
for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
// Calculate and display the sum
int sum = sumArray(arr, n);
printf("Sum of the array elements is: %d\n", sum);
// Free the allocated memory
free(arr);
return 0;
}
Sample Output:
Enter the number of elements: 4
Enter 4 integers:
1234
Sum of the array elements is: 10
STRUCTURES
1. Program to Store and Display Student Information
#include <stdio.h>
#include <stdlib.h>
struct Student {
char name[50];
int roll_no;
float marks;
};
int main() {
struct Student student;
// Input student details
printf("Enter student's name: ");
fgets(student.name, sizeof(student.name), stdin);
printf("Enter roll number: ");
scanf("%d", &student.roll_no);
printf("Enter marks: ");
scanf("%f", &student.marks);
// Display student details
printf("\nStudent Information:\n");
printf("Name: %s", student.name);
printf("Roll Number: %d\n", student.roll_no);
printf("Marks: %.2f\n", student.marks);
return 0;
}
Sample Output:
Enter student's name: John Doe
Enter roll number: 101
Enter marks: 85.5
Student Information:
Name: John Doe
Roll Number: 101
Marks: 85.50
2. Program to Calculate Area of a Rectangle Using Structures
#include <stdio.h>
struct Rectangle {
float length;
float width;
};
float calculateArea(struct Rectangle rect) {
return rect.length * rect.width;
}
int main() {
struct Rectangle rect;
// Input rectangle dimensions
printf("Enter length of the rectangle: ");
scanf("%f", &rect.length);
printf("Enter width of the rectangle: ");
scanf("%f", &rect.width);
// Calculate and display area
float area = calculateArea(rect);
printf("The area of the rectangle is: %.2f\n", area);
return 0;
}
Sample Output:
Enter length of the rectangle: 5.5
Enter width of the rectangle: 4.2
The area of the rectangle is: 23.10
3. Program to Store and Display Date Using Structure
#include <stdio.h>
struct Date {
int day;
int month;
int year;
};
int main() {
struct Date date;
// Input date details
printf("Enter day: ");
scanf("%d", &date.day);
printf("Enter month: ");
scanf("%d", &date.month);
printf("Enter year: ");
scanf("%d", &date.year);
// Display date
printf("\nEntered date is: %02d/%02d/%d\n", date.day, date.month, date.year);
return 0;
}
Sample Output:
Enter day: 25
Enter month: 12
Enter year: 2024
Entered date is: 25/12/2024
4. Program to Store and Display Employee Information
#include <stdio.h>
struct Employee {
char name[50];
int id;
float salary;
};
int main() {
struct Employee emp;
// Input employee details
printf("Enter employee name: ");
fgets(emp.name, sizeof(emp.name), stdin);
printf("Enter employee ID: ");
scanf("%d", &emp.id);
printf("Enter employee salary: ");
scanf("%f", &emp.salary);
// Display employee details
printf("\nEmployee Information:\n");
printf("Name: %s", emp.name);
printf("ID: %d\n", emp.id);
printf("Salary: %.2f\n", emp.salary);
return 0;
}
Sample Output:
Enter employee name: Alice Smith
Enter employee ID: 12345
Enter employee salary: 55000.75
Employee Information:
Name: Alice Smith
ID: 12345
Salary: 55000.75
5. Program to Add Two Complex Numbers Using Structures
#include <stdio.h>
struct Complex {
float real;
float imag;
};
struct Complex addComplex(struct Complex c1, struct Complex c2) {
struct Complex result;
result.real = c1.real + c2.real;
result.imag = c1.imag + c2.imag;
return result;
}
int main() {
struct Complex c1, c2, sum;
// Input complex numbers
printf("Enter real and imaginary parts of first complex number: ");
scanf("%f %f", &c1.real, &c1.imag);
printf("Enter real and imaginary parts of second complex number: ");
scanf("%f %f", &c2.real, &c2.imag);
// Add complex numbers
sum = addComplex(c1, c2);
// Display the sum
printf("Sum of the complex numbers: %.2f + %.2fi\n", sum.real, sum.imag);
return 0;
}
Sample Output:
Enter real and imaginary parts of first complex number: 3.5 4.2
Enter real and imaginary parts of second complex number: 1.2 2.3
Sum of the complex numbers: 4.70 + 6.50i
6. Program to Find Largest of Three Numbers Using Structures
#include <stdio.h>
struct Numbers {
int num1;
int num2;
int num3;
};
int findLargest(struct Numbers n) {
if (n.num1 >= n.num2 && n.num1 >= n.num3) {
return n.num1;
} else if (n.num2 >= n.num1 && n.num2 >= n.num3) {
return n.num2;
} else {
return n.num3;
}
}
int main() {
struct Numbers n;
// Input three numbers
printf("Enter three numbers: ");
scanf("%d %d %d", &n.num1, &n.num2, &n.num3);
// Find and display the largest number
int largest = findLargest(n);
printf("The largest number is: %d\n", largest);
return 0;
}
Sample Output:
Enter three numbers: 10 25 15
The largest number is: 25
7. Program to Calculate Simple Interest Using Structure
#include <stdio.h>
struct SimpleInterest {
float principal;
float rate;
float time;
};
float calculateInterest(struct SimpleInterest si) {
return (si.principal * si.rate * si.time) / 100;
}
int main() {
struct SimpleInterest si;
// Input values
printf("Enter principal: ");
scanf("%f", &si.principal);
printf("Enter rate of interest: ");
scanf("%f", &si.rate);
printf("Enter time period: ");
scanf("%f", &si.time);
// Calculate and display simple interest
float interest = calculateInterest(si);
printf("Simple Interest: %.2f\n", interest);
return 0;
}
Sample Output:
Enter principal: 5000
Enter rate of interest: 5
Enter time period: 2
Simple Interest: 500.00