0% found this document useful (0 votes)
2 views

PSP(EX6_8)

The document contains several C programming exercises demonstrating matrix operations, string functions, recursion for Fibonacci series, and the use of structures. It includes code snippets for summing and multiplying matrices, manipulating strings, displaying Fibonacci numbers, and illustrating nested structures and student information. Each exercise is presented with input prompts and output displays.

Uploaded by

abhi.dubey1827
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

PSP(EX6_8)

The document contains several C programming exercises demonstrating matrix operations, string functions, recursion for Fibonacci series, and the use of structures. It includes code snippets for summing and multiplying matrices, manipulating strings, displaying Fibonacci numbers, and illustrating nested structures and student information. Each exercise is presented with input prompts and output displays.

Uploaded by

abhi.dubey1827
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Ex 6:

1.Sum of two matrix.


#include <stdio.h>
int main () {
int m = 2, n = 2; // Dimensions for 2x2 matrices
int i, j;
int a [2][2], b [2][2], sum [2][2];

printf ("Enter the elements of matrix A:\n");


for (i = 0; i < m; ++i) {
for (j = 0; j < n; ++j) {
printf ("Enter element a[%d][%d]: ", i + 1, j +;
scanf ("%d", &a[i][j]);
}
}

printf ("Enter the elements of matrix B:\n");


for (i = 0; i < m; ++i) {
for (j = 0; j < n; ++j) {
printf ("Enter element b[%d][%d]: ", i + 1, j +;
scanf ("%d", &b[i][j]);
}
}

// Adding two matrices


for (i = 0; i < m; ++i) {
for (j = 0; j < n; ++j) {
sum[i][j] = a[i][j] + b[i][j];
}
}

// Printing the result


printf ("\nSum of two 2x2 matrices: \n");
for (i = 0; i < m; ++i) {
for (j = 0; j < n; ++j) {
printf ("%d ", sum[i][j]);
}
printf("\n");
}

return 0;
}
2.Product of two matrix.
#include <stdio.h>
int main () {
int m = 2, n = 2; // Dimensions for 2x2 matrices
int i, j, k;
int a [2][2], b [2][2], product [2][2];

printf ("Enter the elements of matrix A:\n");


for (i = 0; i < m; ++i) {
for (j = 0; j < n; ++j) {
printf ("Enter element a[%d][%d]: ", i + 1, j + 1);
scanf ("%d", &a[i][j]);
}
}

printf ("Enter the elements of matrix B:\n");


for (i = 0; i < m; ++i) {
for (j = 0; j < n; ++j) {
printf ("Enter element b[%d][%d]: ", i + 1, j + 1);
scanf ("%d", &b[i][j]);
}
}

// Multiplying two matrices


for (i = 0; i < m; ++i) {
for (j = 0; j < n; ++j) {
product[i][j] = 0;
for (k = 0; k < n; ++k) {
product[i][j] += a[i][j] * b[k][j];
}
}
}
// Printing the result
printf ("\nProduct of two 2x2 matrices: \n");
for (i = 0; i < m; ++i) {
for (j = 0; j < n; ++j) {
printf ("%d ", product[i][j]);
}
printf("\n");
}

return 0;
}
Ex 7:
1. WACP to ill urate four function of string.
#include <stdio.h>
#include <string.h>

int main () {
char str1[50], str2[50], str3[100];
int length, comparison;

// Input for first string


printf ("Enter the first string: ");
fgets (str1, sizeof(str1), stdin);
str1[strcspn (str1, "\n")] = 0; // Remove the newline character

// Input for second string


printf ("Enter the second string: ");
fgets (str2, size of(str2), stdin);
str2[strcspn (str2, "\n")] = 0; // Remove the newline character

// 1. strlen () - Calculate and display the length of the first string


length = strlen(str1);
printf ("\nLength of the first string: %d\n", length);

// 2. strcpy() - Copy the first string into another string


strcpy(str3, str1);
printf("String copied into str3: %s\n", str3);

// 3. strcat() - Concatenate the first and second strings


strcat(str3, str2);
printf("Concatenated string (str3): %s\n", str3);

// 4. strcmp() - Compare the first and second strings


comparison = strcmp(str1, str2);
if (comparison == 0) {
printf("The two strings are equal.\n");
} else if (comparison < 0) {
printf("The first string is lexicographically smaller than the second.\n");
} else {
printf("The first string is lexicographically greater than the second.\n");
}

return 0;
}
2. Write a c program to display fibonacci series using recursion

#include <stdio.h>
// Function to calculate Fibonacci numbers using recursion
int fibonacci(int n) {
if (n == 0)
return 0; // Base case: 0th Fibonacci number
else if (n == 1)
return 1; // Base case: 1st Fibonacci number
else
return fibonacci(n - 1) + fibonacci(n - 2); // Recursive case
}

int main() {
int n, i;

// Input the number of terms


printf("Enter the number of terms in the Fibonacci series: ");
scanf("%d", &n);

// Check if the input is valid


if (n <= 0) {
printf("Please enter a positive integer.\n");
return 1;
}

printf("Fibonacci Series: ");


for (i = 0; i < n; i++) {
printf("%d ", fibonacci(i)); // Call the recursive function
}

printf("\n");
return 0;
}
Ex8:
1. WACP to illustrate nested structures.

#include <stdio.h>
#include <string.h>

// Define a nested structure


struct Address {
char city[30];
char state[30];
int pincode;
};

struct Person {
char name[50];
int age;
struct Address; // Nested structure
};

int main() {
struct Person;

// Input details for the person


printf("Enter name: ");
fgets(person.name, sizeof(person.name), stdin);
person.name[strcspn(person.name, "\n")] = 0; // Remove newline character

printf("Enter age: ");


scanf("%d", &person.age);

printf("Enter city: ");


scanf("%s", person.address.city);

printf("Enter state: ");


scanf("%s", person.address.state);

printf("Enter pincode: ");


scanf("%d", &person.address.pincode);

// Display the information


printf("\nDisplaying Information:\n");
printf("Name: %s\n", person.name);
printf("Age: %d\n", person.age);
printf("City: %s\n", person.address.city);
printf("State: %s\n", person.address.state);
printf("Pincode: %d\n", person.address.pincode);

return 0;
}
2. WACP by defining a structure called Student and define members like id, name and
age and define two variables s1,s2 and display the values of all the
members using s1,s2.

#include <stdio.h>
#include <string.h>

// Define the Student structure


struct Student {
int id;
char name[50];
int age;
};

int main() {
struct Student s1, s2;

// Input details for student s1


printf("Enter details for student 1:\n");
printf("Enter ID: ");
scanf("%d", &s1.id);
printf("Enter Name: ");
getchar(); // Clear newline character from the buffer
fgets(s1.name, sizeof(s1.name), stdin);
s1.name[strcspn(s1.name, "\n")] = 0; // Remove newline character
printf("Enter Age: ");
scanf("%d", &s1.age);

// Input details for student s2


printf("\nEnter details for student 2:\n");
printf("Enter ID: ");
scanf("%d", &s2.id);
printf("Enter Name: ");
getchar(); // Clear newline character from the buffer
fgets(s2.name, sizeof(s2.name), stdin);
s2.name[strcspn(s2.name, "\n")] = 0; // Remove newline character
printf("Enter Age: ");
scanf("%d", &s2.age);

// Display details of student s1


printf("\nDetails of Student 1:\n");
printf("ID: %d\n", s1.id);
printf("Name: %s\n", s1.name);
printf("Age: %d\n", s1.age);

// Display details of student s2


printf("\nDetails of Student 2:\n");
printf("ID: %d\n", s2.id);
printf("Name: %s\n", s2.name);
printf("Age: %d\n", s2.age);

return 0;
}

You might also like