6.
Display Employee Details using Structure
-------------------------------------------
Algorithm:
1. Define structure
2. Declare and initialize
3. Display values
Code:
#include <stdio.h>
struct Employee {
int id;
char name[20];
float salary;
};
int main() {
struct Employee emp = {101, "Ravi", 25000.50};
printf("ID: %d\nName: %s\nSalary: %.2f", emp.id, emp.name, emp.salary);
return 0;
}
Output:
ID: 101
Name: Ravi
Salary: 25000.50
7. Factorial Using Recursion
-----------------------------
Algorithm:
1. If n == 0 return 1
2. Else return n * factorial(n-1)
Code:
#include <stdio.h>
int factorial(int n) {
if (n == 0)
return 1;
else
return n * factorial(n - 1);
}
int main() {
int num = 5;
printf("Factorial of %d = %d", num, factorial(num));
return 0;
}
Output:
Factorial of 5 = 120
8. Binary Search Using Recursion
---------------------------------
Code:
#include <stdio.h>
int binarySearch(int a[], int low, int high, int key) {
if (low > high) return -1;
int mid = (low + high) / 2;
if (a[mid] == key) return mid;
else if (key < a[mid]) return binarySearch(a, low, mid - 1, key);
else return binarySearch(a, mid + 1, high, key);
}
int main() {
int a[] = {10, 20, 30, 40, 50};
int result = binarySearch(a, 0, 4, 40);
if (result != -1)
printf("Element found at index %d", result);
else
printf("Element not found");
return 0;
}
Output:
Element found at index 3
9. String Operations (Length and Concatenation)
-----------------------------------------------
Code:
#include <stdio.h>
#include <string.h>
int main() {
char str1[20] = "Hello", str2[] = "World";
printf("Length of str1: %lu\n", strlen(str1));
strcat(str1, str2);
printf("After concatenation: %s", str1);
return 0;
}
Output:
Length of str1: 5
After concatenation: HelloWorld
10. Internal Marks Using Structure and Function
-----------------------------------------------
Code:
#include <stdio.h>
struct Student {
char name[20];
int m1, m2, m3;
};
int totalMarks(struct Student s) {
return s.m1 + s.m2 + s.m3;
}
int main() {
struct Student s = {"Arun", 25, 20, 22};
int total = totalMarks(s);
printf("Student: %s\nTotal Marks: %d", s.name, total);
return 0;
}
Output:
Student: Arun
Total Marks: 67