diff --git a/README.md b/README.md index 41133ca..ddafcd6 100644 --- a/README.md +++ b/README.md @@ -4,7 +4,7 @@ ## Structured programming Journal programs -All C programs relevant to Semester I +All C programs relevant to Semester I Journal ## Table of Contents @@ -15,7 +15,12 @@ All C programs relevant to Semester I 5. [ Pascal triangle](/pascalsTriangle/explaination.md) 6. [Sine series](/sineSeries/explaination.md) 7. [Matrix operations](/matrices/explaination.md) -
+8. [String Operations](/stringManipulation/explaination.md) +9. [Fibonacci Series](fibonacci/explaination.md) +10. [Search](search/explaination.md) +11. [Employee Structure](employee/explaination.md) +12. [Swap Numbers](swap/explaination.md) +
### How to Compile diff --git a/armstrongNumber/explaination.md b/armstrongNumber/explaination.md index c02379e..3b0ef22 100644 --- a/armstrongNumber/explaination.md +++ b/armstrongNumber/explaination.md @@ -53,7 +53,10 @@ int main() ## Output -![Output](/src/output/armstrong.png) +![Output](/src/output/3-o_p.png) + +## Code +![Code](../src/output/3-code.png) + +## Question [11] + +WAP to accept EMPLOYEE details Name. Designation, Gender. DOJ and Salary)\ + Define function members to compute\ +i)total number of employees\ +ii)total number of employees with salaries greater than 20k + +## Code + +```c +#include +struct employee +{ + char name[30]; + char designation[30]; + char gender[10]; + int doj; + float salary; +}; + +int main() +{ + struct employee emp[10]; + int n, empCount = 0, highSalCount = 0; + printf("\nEnter the number of employees: "); + scanf("%d", &n); + for (int i = 0; i < n; i++) + { + printf("\nEMPLOYEE %d:- ", i + 1); + printf("\nEnter the name of employee: "); + scanf(" %[^\n]s", emp[i].name); + printf("Enter the Designation of employee: "); + scanf(" %[^\n]s", emp[i].designation); + printf("Enter the gender of employee: "); + scanf(" %[^\n]s", emp[i].gender); + printf("Enter the Date of joining of employee: "); + scanf("%d", &emp[i].doj); + printf("Enter the Salary of employee: "); + scanf("%f", &emp[i].salary); + empCount++; + if (emp[i].salary > 20000) + { + highSalCount++; + } + } + printf("\nTotal No of Employees: %d", empCount); + printf("\nTotal No of Employees with Salary greater than 20k: %d", highSalCount); + printf("\n"); +} +``` + +## Output + +![Output](/src/output/11-o_p.png) + +## Code + +![Code](../src/output/11-code.png) + + diff --git a/employee/main.c b/employee/main.c new file mode 100644 index 0000000..083b45c --- /dev/null +++ b/employee/main.c @@ -0,0 +1,39 @@ +#include +struct employee +{ + char name[30]; + char designation[30]; + char gender[10]; + int doj; + float salary; +}; + +int main() +{ + struct employee emp[10]; + int n, empCount = 0, highSalCount = 0; + printf("\nEnter the number of employees: "); + scanf("%d", &n); + for (int i = 0; i < n; i++) + { + printf("\nEMPLOYEE %d:- ", i + 1); + printf("\nEnter the name of employee: "); + scanf(" %[^\n]s", emp[i].name); + printf("Enter the Designation of employee: "); + scanf(" %[^\n]s", emp[i].designation); + printf("Enter the gender of employee: "); + scanf(" %[^\n]s", emp[i].gender); + printf("Enter the Date of joining of employee: "); + scanf("%d", &emp[i].doj); + printf("Enter the Salary of employee: "); + scanf("%f", &emp[i].salary); + empCount++; + if (emp[i].salary > 20000) + { + highSalCount++; + } + } + printf("\nTotal No of Employees: %d", empCount); + printf("\nTotal No of Employees with Salary greater than 20k: %d", highSalCount); + printf("\n"); +} \ No newline at end of file diff --git a/fibonacci/explaination.md b/fibonacci/explaination.md new file mode 100644 index 0000000..7edef2f --- /dev/null +++ b/fibonacci/explaination.md @@ -0,0 +1,41 @@ + + +## Question [9] + +WAP to display Fibonacci Series using Recursion + +## Code + +```c +#include +int fibonacci(int n) +{ + if (n <= 1) + return n; + return fibonacci(n - 1) + fibonacci(n - 2); +} +int main() +{ + int n, a = 0, b = 1, tmp; + printf("Enter N: "); + scanf("%d", &n); + for (int i = 0; i < n; i++) + { + printf("%d ", fibonacci(i)); + } + printf("\n"); + return 0; +} +``` + +## Output + +![Output](/src/output/9-o_p.png) + +## Code +![Code](../src/output/9-code.png) + diff --git a/fibonacci/main.c b/fibonacci/main.c new file mode 100644 index 0000000..2e7c581 --- /dev/null +++ b/fibonacci/main.c @@ -0,0 +1,19 @@ +#include +int fibonacci(int n) +{ + if (n <= 1) + return n; + return fibonacci(n - 1) + fibonacci(n - 2); +} +int main() +{ + int n, a = 0, b = 1, tmp; + printf("Enter N: "); + scanf("%d", &n); + for (int i = 0; i < n; i++) + { + printf("%d ", fibonacci(i)); + } + printf("\n"); + return 0; +} \ No newline at end of file diff --git a/grades/explaination.md b/grades/explaination.md index 259b0cd..bec91fe 100644 --- a/grades/explaination.md +++ b/grades/explaination.md @@ -52,8 +52,11 @@ int main() ## Output -![Output](/src/output/grades.png) +![Output](/src/output/2-o_p.png) + +## Code +![Code](../src/output/2-code.png) + +## Question [10] + +WAP to search for an Element in an Array using functions + +## Code + +```c +#include +int search(int ar[], int n, int key) +{ + for (int i = 0; i < n; i++) + { + if (ar[i] == key) + { + return 1; + break; + } + } + return 0; +} +int main() +{ + int ar[20], key, n; + printf("\nEnter Size of Array: "); + scanf("%d", &n); + printf("\nEnter Elements of Array: \n"); + for (int i = 0; i < n; i++) + { + scanf("%d", &ar[i]); + } + printf("\nEnter Element to be searched: "); + scanf("%d", &key); + if (search(ar, n, key)) + { + printf("\nElement found in Array\n"); + } + else + { + printf("\nElement not found in Array\n"); + } +} +``` + +## Output + +![Output](/src/output/10-o_p.png) + +## Code + +![Code](../src/output/10-code.png) + + diff --git a/search/main.c b/search/main.c new file mode 100644 index 0000000..0f7d1bf --- /dev/null +++ b/search/main.c @@ -0,0 +1,34 @@ +#include +int search(int ar[], int n, int key) +{ + for (int i = 0; i < n; i++) + { + if (ar[i] == key) + { + return 1; + break; + } + } + return 0; +} +int main() +{ + int ar[20], key, n; + printf("\nEnter Size of Array: "); + scanf("%d", &n); + printf("\nEnter Elements of Array: \n"); + for (int i = 0; i < n; i++) + { + scanf("%d", &ar[i]); + } + printf("\nEnter Element to be searched: "); + scanf("%d", &key); + if (search(ar, n, key)) + { + printf("\nElement found in Array\n"); + } + else + { + printf("\nElement not found in Array\n"); + } +} \ No newline at end of file diff --git a/sineSeries/explaination.md b/sineSeries/explaination.md index f013f0b..1efe046 100644 --- a/sineSeries/explaination.md +++ b/sineSeries/explaination.md @@ -53,7 +53,11 @@ int main() ## Output -![Output](/src/output/sine.png) +![Output](/src/output/6-o_p.png) + +## Code + +![Code](../src/output/6-code.png) + +## Question [8] + +WAP to accepts a string from user and perform string operations using inbuilt string function and without using string functions-\ +i) +length of string\ +ii) String reversal\ +iii) Equality check of two strings\ +iv) Check palindrome + +## Code + +```c +#include +#include +int main() +{ + char str[100]; + char str2[] = "HELLO"; + char str3[] = "ell"; + int j = 0; + printf("Enter a String: "); + scanf("%s", str); + char reversed[100]; + printf("\nOriginal String: %s", str); + printf("\nLength of String: %lu", strlen(str)); + for (int i = (strlen(str) - 1); i >= 0; i--) + { + reversed[j] = str[i]; + j++; + } + if (strcasecmp(str, reversed) == 0) + { + printf("\n%s is a Palindrome", str); + } + else + { + printf("\n%s is not a Palindrome", str); + } + + printf("\nReversed String: %s", reversed); + if (strcmp(str, str2) == 0) + { + printf("\n%s is equal to %s", str, str2); + } + else + { + printf("\n%s is not equal to %s", str, str2); + } + if (strstr(str, str3)) + { + printf("\n%s Substring Present", str3); + } + else + { + printf("\n%s Substring not Present", str3); + } + printf("\n"); + return 0; +} +``` + +## Output + +![Output](/src/output/8-o_p.png) + +## Code +![Code](../src/output/8-code.png) + diff --git a/stringManipulation/main.c b/stringManipulation/main.c new file mode 100644 index 0000000..053bc5a --- /dev/null +++ b/stringManipulation/main.c @@ -0,0 +1,47 @@ +#include +#include +int main() +{ + char str[100]; + char str2[] = "HELLO"; + char str3[] = "ell"; + int j = 0; + printf("Enter a String: "); + scanf("%s", str); + char reversed[100]; + printf("\nOriginal String: %s", str); + printf("\nLength of String: %lu", strlen(str)); + for (int i = (strlen(str) - 1); i >= 0; i--) + { + reversed[j] = str[i]; + j++; + } + if (strcasecmp(str, reversed) == 0) + { + printf("\n%s is a Palindrome", str); + } + else + { + printf("\n%s is not a Palindrome", str); + } + + printf("\nReversed String: %s", reversed); + if (strcmp(str, str2) == 0) + { + printf("\n%s is equal to %s", str, str2); + } + else + { + printf("\n%s is not equal to %s", str, str2); + } + if (strstr(str, str3)) + { + printf("\n%s Substring Present", str3); + } + else + { + printf("\n%s Substring not Present", str3); + } + printf("\n"); + return 0; +} \ No newline at end of file diff --git a/swap/explaination.md b/swap/explaination.md new file mode 100644 index 0000000..6f19b5d --- /dev/null +++ b/swap/explaination.md @@ -0,0 +1,65 @@ + + +## Question [12] + +a) Write a function to swap two integers using call by value.\ +b) Write a function to swap two integers using call by reference.\ +c) Write a program to display contents of array using pointers + +## Code + +```c +#include +void callValue(int a, int b) +{ + int tmp; + tmp = a; + a = b; + b = tmp; + printf("\nAfter Call by Value Swap:\n"); + printf("A: %d\nB: %d", a, b); +} +void referenceValue(int *m, int *n) +{ + int tmp; + tmp = *m; + *m = *n; + *n = tmp; + printf("\nAfter Call by Reference Swap:\n"); + printf("A: %d\nB: %d", *m, *n); +} +int main() +{ + int a, b; + int ar[] = {2, 4, 6, 8, 10}; + printf("Value of A:"); + scanf("%d", &a); + printf("Value of B:"); + scanf("%d", &b); + printf("\nBefore Call by Value Swap:\n"); + printf("A: %d\nB: %d", a, b); + callValue(a, b); + printf("\nBefore Call by Reference Swap:\n"); + printf("A: %d\nB: %d", a, b); + referenceValue(&a, &b); + printf("\nArray Displayed using pointers:\n"); + for (int i = 0; i < 5; i++) + { + printf("%d\n", *(ar + i)); + } + printf("\n"); +} +``` + +## Output + +![Output](/src/output/12-o_p.png) + +## Code +![Code](../src/output/12-code.png) + + diff --git a/swap/main.c b/swap/main.c new file mode 100644 index 0000000..5d8b2d9 --- /dev/null +++ b/swap/main.c @@ -0,0 +1,40 @@ +#include +void callValue(int a, int b) +{ + int tmp; + tmp = a; + a = b; + b = tmp; + printf("\nAfter Call by Value Swap:\n"); + printf("A: %d\nB: %d", a, b); +} +void referenceValue(int *m, int *n) +{ + int tmp; + tmp = *m; + *m = *n; + *n = tmp; + printf("\nAfter Call by Reference Swap:\n"); + printf("A: %d\nB: %d", *m, *n); +} +int main() +{ + int a, b; + int ar[] = {2, 4, 6, 8, 10}; + printf("Value of A:"); + scanf("%d", &a); + printf("Value of B:"); + scanf("%d", &b); + printf("\nBefore Call by Value Swap:\n"); + printf("A: %d\nB: %d", a, b); + callValue(a, b); + printf("\nBefore Call by Reference Swap:\n"); + printf("A: %d\nB: %d", a, b); + referenceValue(&a, &b); + printf("\nArray Displayed using pointers:\n"); + for (int i = 0; i < 5; i++) + { + printf("%d\n", *(ar + i)); + } + printf("\n"); +} \ No newline at end of file