Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create bubble_sort #14

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions binary_seach
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@

#include<stdio.h>
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;
}
24 changes: 24 additions & 0 deletions bubble_sort
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#include<stdio.h>
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;
}
36 changes: 36 additions & 0 deletions cont_vowel_consonant.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
//number of vowel and consonant
#include<stdio.h>
#include<string.h>
#include<ctype.h>
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;
}
24 changes: 24 additions & 0 deletions fibonacci.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
//TERM OF FIBONNACI SERIES
#include<stdio.h>
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;
}
28 changes: 28 additions & 0 deletions palidrome.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#include<stdio.h>
#include<string.h>
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;
}

24 changes: 24 additions & 0 deletions power.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
//POWER IN C
#include<stdio.h>
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;
}
34 changes: 34 additions & 0 deletions selection_sort
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#include<stdio.h>
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;
}