Codes 01
Codes 01
#include <stdio.h>
int factorial(int n) {
if (n == 0 || n == 1) {
return 1;
} else {
int coefficient;
printf(" ");
printf("%4d", coefficient);
printf("\n");
printf("\n");
int main() {
int rows;
scanf("%d", &rows);
displayPascalsTriangle(rows);
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>
double factorial(int n) {
if (n == 0 || n == 1) {
return 1;
} else {
double sum = 0;
sum += term;
return sum;
int main() {
int x, n;
scanf("%d", &x);
printf("Enter the number of terms (n): ");
scanf("%d", &n);
// Convert x to radians
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>
scanf("%d", &matrix[i][j]);
printf("Matrix:\n");
printf("%d\t", matrix[i][j]);
printf("\n");
result[i][j] = matrix[j][i];
result[i][j] = 0;
int main() {
int choice;
do {
printf("d. Exit\n");
switch (choice) {
case 'a':
inputMatrix(matrix1);
inputMatrix(matrix2);
displayMatrix(result);
break;
case 'b':
// Transpose of a matrix
inputMatrix(matrix1);
transposeMatrix(matrix1, result);
displayMatrix(result);
break;
case 'c':
inputMatrix(matrix1);
inputMatrix(matrix2);
displayMatrix(result);
break;
case 'd':
// Exit
break;
default:
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>
if (len1 != len2) {
return 0; // Strings are not equal if lengths are different
}
return 1; // Palindrome
}
int main() {
char inputString[100];
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");
}
reverseWithoutFunctions(inputString);
printf("Reversed string: %s\n", inputString);
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>
void generateFibonacci(int n) {
printf("Fibonacci Series:\n");
first = second;
second = next;
printf("\n");
int main() {
int n;
scanf("%d", &n);
generateFibonacci(n);
return 0;
}
10)WAP to search an elements in an array using function.
#include <stdio.h>
if (arr[i] == target) {
int main() {
scanf("%d", &size);
int arr[size];
scanf("%d", &arr[i]);
scanf("%d", &target);
if (result != -1) {
} else {
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>
int main() {
int n;
printf("Name: ");
scanf("%s", employees[i].name);
printf("Designation: ");
scanf("%s", employees[i].designation);
printf("Salary: ");
scanf("%f", &employees[i].salary);
}
return 0;
}
12)
a) #include <stdio.h>
int temp = a;
a = b;
b = temp;
int main() {
scanf("%d", &num1);
scanf("%d", &num2);
printf("\nBefore swapping:\n");
swap(num1, num2);
return 0;
}
b)Write a function to swap two integers using call by reference
#include <stdio.h>
*a = *b;
*b = temp;
int main() {
scanf("%d", &num1);
scanf("%d", &num2);
printf("\nBefore swapping:\n");
swap(&num1, &num2);
return 0;
}
c)Write a program to display contents of array using pointers
#include <stdio.h>
printf("Array Contents:\n");
printf("%d ", *(arr + i)); // Using pointer arithmetic to access array elements
printf("\n");
int main() {
int size;
scanf("%d", &size);
// Declare an array
int arr[size];
scanf("%d", &arr[i]);
displayArray(arr, size);
return 0;