Professional Documents
Culture Documents
CSE Lab Report (1706045)
CSE Lab Report (1706045)
int main ()
{
int arr[] = {2, 4, 6, 8, 10};
int size = sizeof(arr) / sizeof(arr[0]);
int sum = 0;
for (int i = 0; i < size; ++i) {
sum += arr[i];
}
printf("Sum of elements in the array: %d\n", sum);
return 0;
}
if (isPrime(num)) {
printf("%d is a prime number.\n", num);
}
else
{
printf("%d is not a prime number.\n", num);
}
return 0;
}
3. Create a C program to calculate the factorial of a given number.
#include <stdio.h>
unsigned long long factorial(int num) {
if (num == 0 || num == 1) {
return 1;
} else {
return num * factorial(num - 1);
}
}
int main() {
int num;
printf("Enter a number: ");
scanf("%d", &num);
if (num < 0) {
printf("Factorial is not defined for negative numbers.\n");
} else {
return 0;
}
4. Develop a program to reverse a string in C.
#include <stdio.h>
#include <string.h>
int main() {
char str[100];
printf("Enter a string: ");
fgets(str, sizeof(str), stdin);
if (str[strlen(str) - 1] == '\n') {
str[strlen(str) - 1] = '\0';
}
reverseString(str);
printf("Reversed string: %s\n", str);
return 0;
}
5. Write a C function to swap two numbers without using a temporary variable.
#include <stdio.h>
void swapNumbers(int *a, int *b) {
*a = *a ^ *b;
*b = *a ^ *b;
*a = *a ^ *b;
}
int main() {
int num1, num2;
printf("Enter the first number: ");
scanf("%d", &num1);
return 0;
}
6. Implement a C program to find the largest element in an array.
#include <stdio.h>
int main() {
int arr[] = {10, 5, 7, 18, 23, 12, 15};
int size = sizeof(arr) / sizeof(arr[0]);
int max = arr[0];
return 0;
}
7. Create a program to compute the Fibonacci series in C.
#include <stdio.h>
int main() {
int n, first = 0, second = 1, next;
printf("Enter the number of terms: ");
scanf("%d", &n);
printf("Fibonacci Series: ");
for (int i = 0; i < n; ++i) {
printf("%d, ", first);
next = first + second;
first = second;
second = next;
}
return 0;
}
8. Write a C function to check if a string is a palindrome.
#include <stdio.h>
#include <string.h>
int isPalindrome(char str[]) {
int length = strlen(str);
int start = 0;
int end = length - 1;
while (start < end) {
if (str[start] != str[end]) {
return 0; // Not a palindrome
}
start++;
end--;
}
return 1;
}
int main() {
char str[100];
printf("Enter a string: ");
scanf("%s", str);
if (isPalindrome(str)) {
printf("The string is a palindrome.\n");
} else {
printf("The string is not a palindrome.\n");
}
return 0;
}
9. Develop a program to convert temperature from Celsius to Fahrenheit.
#include <stdio.h>
int main() {
float celsius, fahrenheit;
printf("Enter temperature in Celsius: ");
scanf("%f", &celsius);
return 0;
}
10. Implement a C program for binary search on a sorted array.
#include <stdio.h>
int binarySearch(int arr[], int size, int target) {
int left = 0, right = size - 1;
if (arr[mid] == target) {
return mid;
}
return count;
}
int main() {
char str[100];
return 0;
}
12. Create a program to calculate the area of a circle in C.
#include <stdio.h>
#define PI 3.14159
int main() {
float radius, area;
printf("Enter the radius of the circle: ");
scanf("%f", &radius);
area = PI * radius * radius;
printf("Area of the circle: %.2f\n", area);
return 0;
}
13. Implement a function to sort an array using the bubble sort algorithm in C.
#include <stdio.h>
int main() {
int arr[] = {64, 34, 25, 12, 22, 11, 90};
int size = sizeof(arr) / sizeof(arr[0]);
return 0;
}
14. Write a C program to find the factorial of a number using recursion.
#include <stdio.h>
int main() {
int num;
printf("Enter a number: ");
scanf("%d", &num);
return 0;
}
15. Develop a program to find the GCD (Greatest Common Divisor) of two numbers in C.
#include <stdio.h>
int main() {
int num;
str1[i] = '\0';
}
int main() {
char str1[100], str2[50];
concatenateStrings(str1, str2);
return 0;
}
int main() {
int year;
return 0;
}
18. Write a C program to reverse a linked list.
#include <stdio.h>
struct Node {
int data;
struct Node* next;
};
*head = prev;
}
int main() {
struct Node* head = NULL;
insertAtBeginning(&head, 3);
insertAtBeginning(&head, 2);
insertAtBeginning(&head, 1);
return 0;
}
19. Develop a C program to find the power of a number using recursion.
#include <stdio.h>
int main() {
double base;
int exponent;
return 0;
}
20. Create a program to calculate the average of an array of numbers in C.
#include <stdio.h>
int main() {
int n;
if (n <= 0) {
printf("Invalid input for the number of elements.\n");
return 1;
}
int arr[n];
int sum = 0;
printf("Enter the array elements:\n");
for (int i = 0; i < n; ++i) {
scanf("%d", &arr[i]);
sum += arr[i];
}
double average = (double)sum / n;
printf("Average of the array: %.2lf\n", average);
return 0;
}
21. Write a C function to remove duplicate elements from an array.
#include <stdio.h>
int temp[n];
int j = 0;
return j;
}
int main() {
int arr[] = {1, 2, 2, 3, 4, 4, 5};
int n = sizeof(arr) / sizeof(arr[0]);
n = removeDuplicates(arr, n);
printf("Array after removing duplicates: ");
for (int i = 0; i < n; ++i) {
printf("%d ", arr[i]);
}
return 0;
}
22. Implement a program to perform matrix multiplication in C.
#include <stdio.h>
#define MAX_SIZE 10
int main() {
int firstMatrix[MAX_SIZE][MAX_SIZE], secondMatrix[MAX_SIZE][MAX_SIZE],
result[MAX_SIZE][MAX_SIZE];
int rowFirst, columnFirst, rowSecond, columnSecond;
int main() {
double a, b, c, discriminant, root1, root2;
if (discriminant > 0) {
root1 = (-b + sqrt(discriminant)) / (2 * a);
root2 = (-b - sqrt(discriminant)) / (2 * a);
printf("Roots are real and different. Root1 = %.2lf, Root2 = %.2lf\n", root1, root2);
} else if (discriminant == 0) {
root1 = root2 = -b / (2 * a);
printf("Roots are real and the same. Root1 = Root2 = %.2lf\n", root1);
} else {
double realPart = -b / (2 * a);
double imaginaryPart = sqrt(-discriminant) / (2 * a);
printf("Roots are complex and different. Root1 = %.2lf + %.2lfi, Root2 = %.2lf - %.2lfi\n",
realPart, imaginaryPart, realPart, imaginaryPart);
}
return 0;
}
24. Write a program to check if a number is Armstrong or not in C.
#include <stdio.h>
#include <math.h>
originalNumber = number;
while (originalNumber != 0) {
originalNumber /= 10;
++n;
}
originalNumber = number;
while (originalNumber != 0) {
remainder = originalNumber % 10;
result += pow(remainder, n);
originalNumber /= 10;
}
if (result == number) {
return 1; // Armstrong number
} else {
return 0; // Not an Armstrong number
}
}
int main() {
int number;
if (isArmstrong(number)) {
printf("%d is an Armstrong number.\n", number);
} else {
printf("%d is not an Armstrong number.\n", number);
}
return 0;
}
25. Create a C function to convert a decimal number to binary.
#include <stdio.h>
printf("\n");
}
int main() {
int decimal;
return 0;
}
26. Implement a program to count the occurrence of a character in a string in C.
#include <stdio.h>
return count;
}
int main() {
char str[100];
char ch;
switch (operator) {
case '+':
printf("%.2lf + %.2lf = %.2lf\n", num1, num2, num1 + num2);
break;
case '-':
printf("%.2lf - %.2lf = %.2lf\n", num1, num2, num1 - num2);
break;
case '*':
printf("%.2lf * %.2lf = %.2lf\n", num1, num2, num1 * num2);
break;
case '/':
if (num2 != 0) {
printf("%.2lf / %.2lf = %.2lf\n", num1, num2, num1 / num2);
} else {
printf("Error! Division by zero.\n");
}
break;
default:
printf("Error! Invalid operator.\n");
}
return 0;
}
28. Develop a program to find the LCM (Least Common Multiple) of two numbers in C.
#include <stdio.h>
int main() {
int num1, num2;
return 0;
}
30. Create a program to check if a string is an anagram of another string in C.
#include <stdio.h>
#include <string.h>
if (len1 != len2) {
return 0; // Not anagrams if lengths are different
}
if (areAnagrams(str1, str2)) {
printf("The strings are anagrams.\n");
} else {
printf("The strings are not anagrams.\n");
}
return 0;
}