diff --git a/binary_seach b/binary_seach new file mode 100644 index 0000000..d344c48 --- /dev/null +++ b/binary_seach @@ -0,0 +1,35 @@ + +#include +int binary(int arr[], int left, int right, int target){ + while(left <= right) { + int mid = left + (right - left) / 2; + if(arr[mid] == target) { + return mid; + } + else if(arr[mid] < target) { + left = mid + 1; + } + else { + right = mid - 1; + } + return -1; + } +} +int main() { + int arr[10], target; + printf("enter the array: "); + for(int i = 0; i < 10; i++) { + scanf("%d", &target); + } + printf("Enter the element: "); + scanf("%d", &target); + int n = sizeof(arr) / sizeof(arr[0]); + int result = binary(arr, 0, n - 1, target); + if(result == -1) { + printf("Element not found."); + } + else { + printf("Element found in %d", result); + } + return 0; +} diff --git a/bubble_sort b/bubble_sort new file mode 100644 index 0000000..4ddbaf8 --- /dev/null +++ b/bubble_sort @@ -0,0 +1,24 @@ +#include +int main() { + int i, n, j, arr[100], t; + printf("Enter number of terms: "); + scanf("%d", &n); + printf("Enter values: "); + for(i = 0; i < n; i++) { + scanf("%d", &arr[i]); + } + for(i = 0; i < n; i++) { + for(j = 0; j < n -i; j++) { + if(arr[j] > arr[j + 1]) { + t = arr[j]; + arr[j] = arr[j + 1]; + arr[j + 1] = t; + } + } + } + printf("Values are: "); + for(i = 0; i < n; i++) { + printf("%d\t", arr[i]); + } +    return 0; +} diff --git a/cont_vowel_consonant.c b/cont_vowel_consonant.c new file mode 100644 index 0000000..bdda9ad --- /dev/null +++ b/cont_vowel_consonant.c @@ -0,0 +1,36 @@ +//number of vowel and consonant +#include +#include +#include +int is_vowel(char c) { + c = tolower(c); + if(c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u') { + return 1; + } + return 0; +} +void count(char *str, int *vowel, int *consonant) { + *vowel = 0; + *consonant = 0; + for(int i = 0; i < strlen(str); i++) { + char c = str[i]; + if(isalpha(c)) { + if(is_vowel(c)) { + (*vowel)++; + } + else { + (*consonant)++; + } + } + } +} +int main() { + char str[100]; + printf("Enter string in lowercase: "); + scanf("%s", str); + int vowel, consonant; + count(str, &vowel, &consonant); + printf("Vowel: %d\n", vowel); + printf("Consonant: %d\n", consonant); + return 0; +} \ No newline at end of file diff --git a/fibonacci.c b/fibonacci.c new file mode 100644 index 0000000..eacc9d3 --- /dev/null +++ b/fibonacci.c @@ -0,0 +1,24 @@ +//TERM OF FIBONNACI SERIES +#include +unsigned fibo(int num); +int main() { + int num; + unsigned fibonacci; + printf("Enter n: "); + scanf("%d", &num); + fibonacci = fibo(num); + printf("Term: %u", fibonacci); + return 0; +} +unsigned fibo(int num) { + if(num == 0) { + return 0; + } + else if(num == 1) { + return 1; + } + else { + return fibo(num - 1) + fibo(num - 2); + } + return 0; +} \ No newline at end of file diff --git a/palidrome.c b/palidrome.c new file mode 100644 index 0000000..1c675da --- /dev/null +++ b/palidrome.c @@ -0,0 +1,28 @@ +#include +#include +int palidrome(char *str, int start, int end) { + if(start >= end) { + return 1; + } + else if(str[start] == str[end]) { + return palidrome(str, start + 1, end - 1); + } + else { + return 0; + } +} +int main() { + char str[100]; + printf("Enter string: "); + scanf("%d", str); + int len = strlen(str); + int result = palidrome(str, 0, len - 1); + if(result == 1) { + printf("String is palidrome."); + } + else { + printf("String is not palidrome."); + } + return 0; +} + diff --git a/power.c b/power.c new file mode 100644 index 0000000..48a0618 --- /dev/null +++ b/power.c @@ -0,0 +1,24 @@ +//POWER IN C +#include +double power(double base, int exponent) { + if(exponent == 0) { + return 1; + } + else if(exponent > 0) { + return base * power(base, exponent - 1); + } +} +int main() { + double base; + int exponent; + printf("Enter base, exponent: "); + scanf("%lf %d", &base, &exponent); + if(exponent >= 0) { + double result = power(base, exponent); + printf("Result: %.1lf", result); + } + else { + printf("Exponent has to be greater than 0"); + } + return 0; +} diff --git a/selection_sort b/selection_sort new file mode 100644 index 0000000..107885a --- /dev/null +++ b/selection_sort @@ -0,0 +1,34 @@ +#include +void selection(int arr[], int n) { + for(int i = 0; i < n - 1; i++) { + int minIndex = 1; + for(int j = i + 1; j < n; j++) { + if(arr[j] < arr[minIndex]) { + minIndex = j; + } + } + if(minIndex != i) { + int temp = arr[i]; + arr[i] = arr[minIndex]; + arr[minIndex] = temp; + } + } +} +int main() { + int n; + printf("Enter number of elements of array: "); + scanf("%d", &n); + int arr[n]; + printf("Enter the elements of array: "); + for(int i = 0; i < n; i++) { + scanf("%d", &arr[i]); + } + printf("\n"); + selection(arr, n); + printf("Selection sort: "); + for(int i = 0; i < n; i++) { + printf("%d ", arr[i]); + } + printf("\n"); + return 0; +}