0% found this document useful (0 votes)
31 views26 pages

Codes 01

The document contains C code to perform various operations on matrices and strings using functions. It includes functions to: 1) Input, add, transpose, and multiply 3x3 matrices using functions. A menu driven program is used to call these functions. 2) Perform string operations like finding length, reversing, comparing equality, and checking palindrome both with and without built-in string functions. 3) Accept a number from the user and generate/print the Fibonacci series up to that number using a function. 4) Search for an element in an array using a function that takes the array, size, and target element as parameters.

Uploaded by

Kamlesh Mali
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)
31 views26 pages

Codes 01

The document contains C code to perform various operations on matrices and strings using functions. It includes functions to: 1) Input, add, transpose, and multiply 3x3 matrices using functions. A menu driven program is used to call these functions. 2) Perform string operations like finding length, reversing, comparing equality, and checking palindrome both with and without built-in string functions. 3) Accept a number from the user and generate/print the Fibonacci series up to that number using a function. 4) Search for an element in an array using a function that takes the array, size, and target element as parameters.

Uploaded by

Kamlesh Mali
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/ 26

5) WAP to display pascal triangle and patterns using for loop.

#include <stdio.h>

// Function to calculate factorial

int factorial(int n) {

if (n == 0 || n == 1) {

return 1;

} else {

return n * factorial(n - 1);

// Function to display Pascal's Triangle

void displayPascalsTriangle(int rows) {

int coefficient;

for (int i = 0; i < rows; i++) {

for (int space = 1; space <= rows - i; space++) {

printf(" ");

for (int j = 0; j <= i; j++) {

coefficient = factorial(i) / (factorial(j) * factorial(i - j));

printf("%4d", coefficient);

printf("\n");

// Function to display a simple pattern

void displayPattern(int rows) {

for (int i = 1; i <= rows; i++) {

for (int j = 1; j <= i; j++) {


printf("* ");

printf("\n");

int main() {

int rows;

// Display Pascal's Triangle

printf("Enter the number of rows for Pascal's Triangle: ");

scanf("%d", &rows);

displayPascalsTriangle(rows);

// Display a simple pattern

printf("\nEnter the number of rows for the pattern: ");

scanf("%d", &rows);

displayPattern(rows);

return 0;

OUTPUT
6)WAP to accept the number of terms a finds the sum of sine series
#include <stdio.h>

#include <math.h>

// Function to calculate factorial

double factorial(int n) {

if (n == 0 || n == 1) {

return 1;

} else {

return n * factorial(n - 1);

// Function to calculate sine series

double calculateSineSeries(int x, int n) {

double sum = 0;

for (int i = 0; i < n; i++) {

// Calculate each term of the series

double term = pow(-1, i) * pow(x, 2 * i + 1) / factorial(2 * i + 1);

// Add the term to the sum

sum += term;

return sum;

int main() {

int x, n;

// Accept the values for x and n

printf("Enter the value of x (in degrees): ");

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

scanf("%d", &n);

// Convert x to radians

double xInRadians = x * M_PI / 180.0;

// Calculate and display the sum of the sine series

double result = calculateSineSeries(xInRadians, n);

printf("The sum of the sine series for %d terms is: %lf\n", n, result);

return 0;

}
7)Write a menu driven program to perform following operations:
a.Perform addition of two 3X3 matrices.
b.Obtain transpose of a given 3X3 matrix.
c.Multiplication of two 3X3 matrices.

#include <stdio.h>

// Function to input a 3x3 matrix

void inputMatrix(int matrix[3][3]) {

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

for (int i = 0; i < 3; i++) {

for (int j = 0; j < 3; j++) {

printf("Enter element at position (%d, %d): ", i + 1, j + 1);

scanf("%d", &matrix[i][j]);

// Function to display a 3x3 matrix

void displayMatrix(int matrix[3][3]) {

printf("Matrix:\n");

for (int i = 0; i < 3; i++) {

for (int j = 0; j < 3; j++) {

printf("%d\t", matrix[i][j]);

printf("\n");

// Function to add two 3x3 matrices

void addMatrices(int matrix1[3][3], int matrix2[3][3], int result[3][3]) {

for (int i = 0; i < 3; i++) {

for (int j = 0; j < 3; j++) {


result[i][j] = matrix1[i][j] + matrix2[i][j];

// Function to obtain the transpose of a 3x3 matrix

void transposeMatrix(int matrix[3][3], int result[3][3]) {

for (int i = 0; i < 3; i++) {

for (int j = 0; j < 3; j++) {

result[i][j] = matrix[j][i];

// Function to multiply two 3x3 matrices

void multiplyMatrices(int matrix1[3][3], int matrix2[3][3], int result[3][3]) {

for (int i = 0; i < 3; i++) {

for (int j = 0; j < 3; j++) {

result[i][j] = 0;

for (int k = 0; k < 3; k++) {

result[i][j] += matrix1[i][k] * matrix2[k][j];

int main() {

int choice;

int matrix1[3][3], matrix2[3][3], result[3][3];

do {

// Display the menu

printf("\nMatrix Operations Menu:\n");

printf("a. Add two matrices\n");


printf("b. Transpose a matrix\n");

printf("c. Multiply two matrices\n");

printf("d. Exit\n");

printf("Enter your choice: ");

scanf(" %c", &choice);

switch (choice) {

case 'a':

// Addition of two matrices

inputMatrix(matrix1);

inputMatrix(matrix2);

addMatrices(matrix1, matrix2, result);

printf("\nResult of matrix addition:\n");

displayMatrix(result);

break;

case 'b':

// Transpose of a matrix

inputMatrix(matrix1);

transposeMatrix(matrix1, result);

printf("\nTranspose of the matrix:\n");

displayMatrix(result);

break;

case 'c':

// Multiplication of two matrices

inputMatrix(matrix1);

inputMatrix(matrix2);

multiplyMatrices(matrix1, matrix2, result);

printf("\nResult of matrix multiplication:\n");

displayMatrix(result);

break;

case 'd':
// Exit

printf("Exiting the program.\n");

break;

default:

printf("Invalid choice. Please enter a valid option.\n");

} while (choice != 'd');

return 0;

}
8) WAP to accepts a string from user and perform string operations using inbuilt string function and
without using string funcations-
i) length of string
ii) String reversal
iii) Equality check of two strings
iv) Check palindrome
#include <stdio.h>

// Function to find the length of a string without using string functions


int findLengthWithoutFunctions(char str[]) {
int length = 0;
while (str[length] != '\0') {
length++;
}
return length;
}

// Function to reverse a string without using string functions


void reverseWithoutFunctions(char str[]) {
int length = findLengthWithoutFunctions(str);
int start = 0;
int end = length - 1;

while (start < end) {


// Swap characters at start and end positions
char temp = str[start];
str[start] = str[end];
str[end] = temp;

// Move towards the center


start++;
end--;
}
}

// Function to check equality of two strings without using string functions


int areEqualWithoutFunctions(char str1[], char str2[]) {
int len1 = findLengthWithoutFunctions(str1);
int len2 = findLengthWithoutFunctions(str2);

if (len1 != len2) {
return 0; // Strings are not equal if lengths are different
}

for (int i = 0; i < len1; i++) {


if (str1[i] != str2[i]) {
return 0; // Strings are not equal if any characters differ
}
}

return 1; // Strings are equal


}

// Function to check if a string is palindrome without using string functions


int isPalindromeWithoutFunctions(char str[]) {
int length = findLengthWithoutFunctions(str);
int start = 0;
int end = length - 1;

while (start < end) {


if (str[start] != str[end]) {
return 0; // Not a palindrome if characters at start and end positions are different
}
start++;
end--;
}

return 1; // Palindrome
}

int main() {
char inputString[100];

// Accept a string from the user


printf("Enter a string: ");
scanf("%s", inputString);

// Using inbuilt string functions


printf("Using inbuilt string functions:\n");
printf("Length of the string: %lu\n", strlen(inputString));

// Reverse the string without using strrev


char reversedString[100];
strcpy(reversedString, inputString);
reverseWithoutFunctions(reversedString);
printf("Reversed string: %s\n", reversedString);

char secondString[100];
printf("Enter another string for equality check: ");
scanf("%s", secondString);
if (strcmp(inputString, secondString) == 0) {
printf("Strings are equal.\n");
} else {
printf("Strings are not equal.\n");
}

if (strcmp(inputString, reversedString) == 0) {
printf("The original string is a palindrome.\n");
} else {
printf("The original string is not a palindrome.\n");
}

// Without using inbuilt string functions


printf("\nWithout using inbuilt string functions:\n");
printf("Length of the string: %d\n", findLengthWithoutFunctions(inputString));

reverseWithoutFunctions(inputString);
printf("Reversed string: %s\n", inputString);

printf("Enter another string for equality check: ");


scanf("%s", secondString);

if (areEqualWithoutFunctions(inputString, secondString)) {
printf("Strings are equal.\n");
} else {
printf("Strings are not equal.\n");
}

if (isPalindromeWithoutFunctions(inputString)) {
printf("The original string is a palindrome.\n");
} else {
printf("The original string is not a palindrome.\n");
}

return 0;
}

OUTPUT
9) WAP to accept from user the number of Fibonacci numbers to be generated and print the Fibonacci
series using
#include <stdio.h>

// Function to generate and print Fibonacci series

void generateFibonacci(int n) {

int first = 0, second = 1, next;

printf("Fibonacci Series:\n");

for (int i = 1; i <= n; ++i) {

printf("%d, ", first);

next = first + second;

first = second;

second = next;

printf("\n");

int main() {

int n;

// Input the number of Fibonacci numbers to generate

printf("Enter the number of Fibonacci numbers to generate: ");

scanf("%d", &n);

// Call the generateFibonacci function

generateFibonacci(n);

return 0;

}
10)WAP to search an elements in an array using function.
#include <stdio.h>

// Function to search for an element in an array

int searchElement(int arr[], int size, int target) {

for (int i = 0; i < size; i++) {

if (arr[i] == target) {

return i; // Return the index if the element is found

return -1; // Return -1 if the element is not found

int main() {

int size, target;

// Input the size of the array

printf("Enter the size of the array: ");

scanf("%d", &size);

int arr[size];

// Input the elements of the array

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

for (int i = 0; i < size; i++) {

printf("Enter element at position %d: ", i + 1);

scanf("%d", &arr[i]);

// Input the target element to search

printf("Enter the element to search: ");

scanf("%d", &target);

// Call the searchElement function


int result = searchElement(arr, size, target);

// Check and display the result

if (result != -1) {

printf("Element %d found at position %d in the array.\n", target, result + 1);

} else {

printf("Element %d not found in the array.\n", target);

return 0;

}
11)WAP to accept EMPLOYEE details (Name, Designation, Gender, DOJ and Salary). Define function
members to compute i)total number of employees in an organization ii) Employee with salary more than
20,000

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

// Define a structure for Employee details


struct Employee {
char name[50];
char designation[50];
char gender;
char doj[15];
float salary;
};

// Function to compute the total number of employees


int totalEmployees(struct Employee employees[], int n) {
return n;
}

// Function to find employees with a salary greater than 20,000


void employeesWithSalaryGreaterThan20K(struct Employee employees[], int n) {
printf("\nEmployees with Salary Greater than 20,000:\n");

for (int i = 0; i < n; i++) {


if (employees[i].salary > 20000.0) {
printf("Name: %s\n", employees[i].name);
printf("Designation: %s\n", employees[i].designation);
printf("Gender: %c\n", employees[i].gender);
printf("Date of Joining: %s\n", employees[i].doj);
printf("Salary: %.2f\n", employees[i].salary);
printf("--------------------\n");
}
}
}

int main() {
int n;

// Input the number of employees


printf("Enter the number of employees: ");
scanf("%d", &n);

// Create an array of Employee structures


struct Employee employees[n];

// Input details for each employee


for (int i = 0; i < n; i++) {
printf("\nEnter details for Employee %d:\n", i + 1);

printf("Name: ");
scanf("%s", employees[i].name);

printf("Designation: ");
scanf("%s", employees[i].designation);

printf("Gender (M/F): ");


scanf(" %c", &employees[i].gender);

printf("Date of Joining (DD-MM-YYYY): ");


scanf("%s", employees[i].doj);

printf("Salary: ");
scanf("%f", &employees[i].salary);
}

// Compute and display total number of employees


printf("\ni) Total Number of Employees: %d\n", totalEmployees(employees, n));

// Find and display employees with a salary greater than 20,000


employeesWithSalaryGreaterThan20K(employees, n);

return 0;
}
12)

a) #include <stdio.h>

// Function to swap two integers using call by value

void swap(int a, int b) {

int temp = a;

a = b;

b = temp;

int main() {

int num1, num2;

// Input two integers from the user

printf("Enter the first integer: ");

scanf("%d", &num1);

printf("Enter the second integer: ");

scanf("%d", &num2);

// Display the original values

printf("\nBefore swapping:\n");

printf("First integer: %d\n", num1);

printf("Second integer: %d\n", num2);

// Call the swap function

swap(num1, num2);

// Display the values after swapping

printf("\nAfter swapping (no change due to call by value):\n");

printf("First integer: %d\n", num1);

printf("Second integer: %d\n", num2);

return 0;
}
b)Write a function to swap two integers using call by reference
#include <stdio.h>

// Function to swap two integers using call by reference

void swap(int *a, int *b) {

int temp = *a;

*a = *b;

*b = temp;

int main() {

int num1, num2;

// Input two integers from the user

printf("Enter the first integer: ");

scanf("%d", &num1);

printf("Enter the second integer: ");

scanf("%d", &num2);

// Display the original values

printf("\nBefore swapping:\n");

printf("First integer: %d\n", num1);

printf("Second integer: %d\n", num2);

// Call the swap function

swap(&num1, &num2);

// Display the values after swapping

printf("\nAfter swapping using call by reference:\n");

printf("First integer: %d\n", num1);

printf("Second integer: %d\n", num2);

return 0;
}
c)Write a program to display contents of array using pointers
#include <stdio.h>

// Function to display the contents of an array using pointers

void displayArray(int *arr, int size) {

printf("Array Contents:\n");

for (int i = 0; i < size; i++) {

printf("%d ", *(arr + i)); // Using pointer arithmetic to access array elements

printf("\n");

int main() {

int size;

// Input the size of the array

printf("Enter the size of the array: ");

scanf("%d", &size);

// Declare an array

int arr[size];

// Input elements of the array

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

for (int i = 0; i < size; i++) {

printf("Enter element at position %d: ", i + 1);

scanf("%d", &arr[i]);

// Display the contents of the array using pointers

displayArray(arr, size);

return 0;

You might also like