0% found this document useful (0 votes)
107 views30 pages

CSE Lab Report (1706045)

The document contains 18 programming assignments for a C programming course. The assignments cover topics like arrays, prime number checking, recursion, strings, sorting, linked lists, and more. Each assignment includes sample code to implement the given task.

Uploaded by

Shaad sayed
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)
107 views30 pages

CSE Lab Report (1706045)

The document contains 18 programming assignments for a C programming course. The assignments cover topics like arrays, prime number checking, recursion, strings, sorting, linked lists, and more. Each assignment includes sample code to implement the given task.

Uploaded by

Shaad sayed
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/ 30

CSE assignment

Shaadnan Bin Syed


Roll: 1706045
Department of Glass & Ceramic Engineering.
17 Series.

1. Write a C program to find the sum of elements in an array.


#include <stdio.h>

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;
}

2. Implement a program to check if a number is prime or not.


#include <stdio.h>

int isPrime(int num)


{
if (num <= 1) {
return 0;
}
for (int i = 2; i * i <= num; ++i)
{
if (num % i == 0) {
return 0;
}
}
return 1;
}
int main()
{
int num;
printf("Enter a number: ");
scanf("%d", &num);

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 {

printf("Factorial of %d = %llu\n", num, factorial(num));


}

return 0;
}
4. Develop a program to reverse a string in C.
#include <stdio.h>
#include <string.h>

void reverseString(char str[]) {


int length = strlen(str);
int start = 0;
int end = length - 1;

while (start < end) {


char temp = str[start];
str[start] = str[end];
str[end] = temp;
start++;
end--;
}
}

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);

printf("Enter the second number: ");


scanf("%d", &num2);
printf("Original numbers: num1 = %d, num2 = %d\n", num1, num2);
swapNumbers(&num1, &num2);
printf("Numbers after swapping: num1 = %d, num2 = %d\n", num1, num2);

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];

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


if (arr[i] > max) {
max = arr[i];
}
}
printf("Largest element in the array: %d\n", max);

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);

fahrenheit = (celsius * 9 / 5) + 32;


printf("Temperature in Fahrenheit: %.2f\n", fahrenheit);

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;

while (left <= right) {


int mid = left + (right - left) / 2;

if (arr[mid] == target) {
return mid;
}

if (arr[mid] < target) {


left = mid + 1;
} else {
right = mid - 1;
}
}
return -1;
}
int main() {
int arr[] = {2, 4, 6, 8, 10, 12, 14, 16, 18, 20};
int size = sizeof(arr) / sizeof(arr[0]);
int target;
printf("Enter the element to search: ");
scanf("%d", &target);
int result = binarySearch(arr, size, target);
if (result != -1) {
printf("Element %d found at index %d.\n", target, result);
} else {
printf("Element %d not found in the array.\n", target);
}
return 0;
}
11. Write a C program to count the number of vowels in a string.
#include <stdio.h>
#include <string.h>

int countVowels(char str[]) {


int count = 0;
int length = strlen(str);

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


char currentChar = tolower(str[i]);

if (currentChar == 'a' || currentChar == 'e' || currentChar == 'i' || currentChar == 'o' ||


currentChar == 'u') {
count++;
}
}

return count;
}

int main() {
char str[100];

printf("Enter a string: ");


scanf("%s", str);
int vowelCount = countVowels(str);
printf("Number of vowels in the string: %d\n", vowelCount);

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>

void bubbleSort(int arr[], int size) {


for (int i = 0; i < size - 1; ++i) {
for (int j = 0; j < size - i - 1; ++j) {
if (arr[j] > arr[j + 1]) {
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}

int main() {
int arr[] = {64, 34, 25, 12, 22, 11, 90};
int size = sizeof(arr) / sizeof(arr[0]);

printf("Original array: ");


for (int i = 0; i < size; i++) {
printf("%d ", arr[i]);
}
bubbleSort(arr, size);
printf("\nSorted array: ");
for (int i = 0; i < size; i++) {
printf("%d ", arr[i]);
}

return 0;
}
14. Write a C program to find the factorial of a number using recursion.
#include <stdio.h>

unsigned long long factorial(int n) {


if (n == 0 || n == 1) {
return 1;
} else {
return n * factorial(n - 1);
}
}

int main() {
int num;
printf("Enter a number: ");
scanf("%d", &num);

printf("Factorial of %d = %llu\n", num, factorial(num));

return 0;
}
15. Develop a program to find the GCD (Greatest Common Divisor) of two numbers in C.
#include <stdio.h>

unsigned long long factorial(int n) {


if (n == 0 || n == 1) {
return 1;
} else {
return n * factorial(n - 1);
}
}

int main() {
int num;

printf("Enter a number: ");


scanf("%d", &num);
printf("Factorial of %d = %llu\n", num, factorial(num));
return 0;
}
16. Create a C function to concatenate two strings without using the strcat function.
#include <stdio.h>

void concatenateStrings(char str1[], char str2[]) {


int i, j;
for (i = 0; str1[i] != '\0'; ++i);
for (j = 0; str2[j] != '\0'; ++j, ++i) {
str1[i] = str2[j];
}

str1[i] = '\0';
}
int main() {
char str1[100], str2[50];

printf("Enter the first string: ");


gets(str1);
printf("Enter the second string: ");
gets(str2);

concatenateStrings(str1, str2);

printf("Concatenated string: %s\n", str1);

return 0;
}

17. Implement a program to check if a given year is a leap year in C.


#include <stdio.h>

int isLeapYear(int year) {


if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) {
return 1;
} else {
return 0;
}
}

int main() {
int year;

printf("Enter a year: ");


scanf("%d", &year);
if (isLeapYear(year)) {
printf("%d is a leap year.\n", year);
} else {
printf("%d is not a leap year.\n", year);
}

return 0;
}
18. Write a C program to reverse a linked list.
#include <stdio.h>

struct Node {
int data;
struct Node* next;
};

void insertAtBeginning(struct Node** head, int value) {


struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = value;
newNode->next = *head;
*head = newNode;
}

void reverseLinkedList(struct Node** head) {


struct Node* prev = NULL;
struct Node* current = *head;
struct Node* next = NULL;
while (current != NULL) {
next = current->next;
current->next = prev;
prev = current;
current = next;
}

*head = prev;
}

void printList(struct Node* head) {


while (head != NULL) {
printf("%d ", head->data);
head = head->next;
}
printf("\n");
}

int main() {
struct Node* head = NULL;

insertAtBeginning(&head, 3);
insertAtBeginning(&head, 2);
insertAtBeginning(&head, 1);

printf("Original Linked List: ");


printList(head);
reverseLinkedList(&head);

printf("Reversed Linked List: ");


printList(head);

return 0;
}
19. Develop a C program to find the power of a number using recursion.
#include <stdio.h>

double power(double base, int exponent) {


if (exponent == 0) {
return 1;
} else if (exponent > 0) {
return base * power(base, exponent - 1);
} else {
return 1 / (base * power(base, -exponent - 1));
}
}

int main() {
double base;
int exponent;

printf("Enter the base: ");


scanf("%lf", &base);

printf("Enter the exponent: ");


scanf("%d", &exponent);
printf("Result: %.6lf\n", power(base, 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;

printf("Enter the number of elements: ");


scanf("%d", &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 removeDuplicates(int arr[], int n) {


if (n == 0 || n == 1) {
return n;
}

int temp[n];
int j = 0;

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


if (arr[i] != arr[i + 1]) {
temp[j++] = arr[i];
}
}

temp[j++] = arr[n - 1];

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


arr[i] = temp[i];
}

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

void multiplyMatrices(int firstMatrix[MAX_SIZE][MAX_SIZE], int


secondMatrix[MAX_SIZE][MAX_SIZE], int result[MAX_SIZE][MAX_SIZE], int rowFirst, int
columnFirst, int rowSecond, int columnSecond) {
for (int i = 0; i < rowFirst; ++i) {
for (int j = 0; j < columnSecond; ++j) {
result[i][j] = 0;
}
}

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


for (int j = 0; j < columnSecond; ++j) {
for (int k = 0; k < columnFirst; ++k) {
result[i][j] += firstMatrix[i][k] * secondMatrix[k][j];
}
}
}
}

void displayMatrix(int matrix[MAX_SIZE][MAX_SIZE], int row, int column) {


for (int i = 0; i < row; ++i) {
for (int j = 0; j < column; ++j) {
printf("%d\t", matrix[i][j]);
}
printf("\n");
}
}

int main() {
int firstMatrix[MAX_SIZE][MAX_SIZE], secondMatrix[MAX_SIZE][MAX_SIZE],
result[MAX_SIZE][MAX_SIZE];
int rowFirst, columnFirst, rowSecond, columnSecond;

printf("Enter rows and columns for the first matrix: ");


scanf("%d %d", &rowFirst, &columnFirst);

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


for (int i = 0; i < rowFirst; ++i) {
for (int j = 0; j < columnFirst; ++j) {
scanf("%d", &firstMatrix[i][j]);
}
}

printf("Enter rows and columns for the second matrix: ");


scanf("%d %d", &rowSecond, &columnSecond);

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


for (int i = 0; i < rowSecond; ++i) {
for (int j = 0; j < columnSecond; ++j) {
scanf("%d", &secondMatrix[i][j]);
}
}
if (columnFirst != rowSecond) {
printf("Error! Column of the first matrix not equal to row of the second matrix.\n");
} else {
multiplyMatrices(firstMatrix, secondMatrix, result, rowFirst, columnFirst, rowSecond,
columnSecond);
printf("Result of matrix multiplication:\n");
displayMatrix(result, rowFirst, columnSecond);
}
return 0;
}
23. Develop a C program to find the roots of a quadratic equation.
#include <stdio.h>
#include <math.h>

int main() {
double a, b, c, discriminant, root1, root2;

printf("Enter coefficients a, b, and c: ");


scanf("%lf %lf %lf", &a, &b, &c);
discriminant = b * b - 4 * a * c;

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>

int isArmstrong(int number) {


int originalNumber, remainder, n = 0, result = 0;

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;

printf("Enter a number: ");


scanf("%d", &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>

void decimalToBinary(int decimal) {


int binary[32];
int i = 0;

while (decimal > 0) {


binary[i] = decimal % 2;
decimal /= 2;
i++;
}

printf("Binary equivalent: ");

for (int j = i - 1; j >= 0; j--) {


printf("%d", binary[j]);
}

printf("\n");
}

int main() {
int decimal;

printf("Enter a decimal number: ");


scanf("%d", &decimal);
decimalToBinary(decimal);

return 0;
}
26. Implement a program to count the occurrence of a character in a string in C.
#include <stdio.h>

int countOccurrences(char str[], char ch) {


int count = 0;

for (int i = 0; str[i] != '\0'; ++i) {


if (str[i] == ch) {
count++;
}
}

return count;
}

int main() {
char str[100];
char ch;

printf("Enter a string: ");


scanf("%s", str);

printf("Enter a character to count: ");


scanf(" %c", &ch); // Add a space before %c to consume any whitespace characters
int occurrenceCount = countOccurrences(str, ch);
printf("Occurrences of '%c' in the string: %d\n", ch, occurrenceCount);
return 0;
}
27. Write a C program to implement a basic calculator.
#include <stdio.h>
int main() {
char operator;
double num1, num2;

printf("Enter an operator (+, -, *, /): ");


scanf(" %c", &operator); // Add a space before %c to consume any whitespace characters

printf("Enter two numbers: ");


scanf("%lf %lf", &num1, &num2);

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 findGCD(int a, int b) {


while (b != 0) {
int temp = b;
b = a % b;
a = temp;
}
return a;
}
int findLCM(int num1, int num2) {
return (num1 * num2) / findGCD(num1, num2);
}

int main() {
int num1, num2;

printf("Enter two numbers: ");


scanf("%d %d", &num1, &num2);

printf("LCM of %d and %d is %d\n", num1, num2, findLCM(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>

int areAnagrams(char str1[], char str2[]) {


int len1 = strlen(str1);
int len2 = strlen(str2);

if (len1 != len2) {
return 0; // Not anagrams if lengths are different
}

int count[256] = {0}; // Assuming ASCII characters

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


count[str1[i]]++;
count[str2[i]]--;
}

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


if (count[i] != 0) {
return 0; // Not anagrams if character frequencies are different
}
}

return 1; // Strings are anagrams


}
int main() {
char str1[100], str2[100];

printf("Enter the first string: ");


scanf("%s", str1);

printf("Enter the second string: ");


scanf("%s", str2);

if (areAnagrams(str1, str2)) {
printf("The strings are anagrams.\n");
} else {
printf("The strings are not anagrams.\n");
}

return 0;
}

You might also like