wisdomatrix.com

Avatar

C Program to Implement Bubble Sort of Given Numbers

Bubble Sort is a simple sorting algorithm that repeatedly steps through the list, compares adjacent elements, and swaps them if they are in the wrong order. The pass through the list is repeated until the list is sorted. Here’s a simple C program to implement Bubble Sort Program : #include<stdio.h>void bubble_sort(long[],long);int main(){long arr[100],n,c,d,swap;printf(“Enter number of element : “);scanf(“%ld”,&n);printf(“Enter %ld Longegers : \n”,n);for(c=0;c<n;c++){scanf(“%ld”,&arr[c]);}bubble_sort(arr,n);printf(“Sorted listin ascending order : \n”);for(c=0;c<n;c++){printf(“%ld\n”,arr[c]);}return 0;}void bubble_sort(long list[], long n){long c,d,t;for(c=0;c<(n-1);c++){for(d=0;d<n-c-1;d++){if(list[d]>list[d+1]){t=list[d];list[d]=list[d+1];list[d+1]=t;}}}} Ouput :

C Program to Implement Bubble Sort of Given Numbers Read More »

C Program to Implement Shell Sort of a Given Numbers

Sure, I can provide you with a simple implementation of the Shell Sort algorithm in C. Shell Sort is an in-place comparison-based sorting algorithm that sorts elements far apart from each other and then progressively reduces the gap between elements to be compared. Here’s a basic implementation in C Program : Program : #include<stdio.h>#include<conio.h>void shellsort(int a[],int n){int j,i,m,mid;for(m=n/2;m>0;m/=2){for(j=m;j<n;j++){for(i=j-m;i>=0;i-=m){if(a[i+m]>=a[i])break;else{mid=a[i];a[i]=a[i+m];a[i+m]=mid;}}}}}void main(){int a[10],i,n;printf(“Enter the number of Elements : “);scanf(“%d”,&n);for(i=0;i<n;i++){printf(“Element%d : “,i+1);scanf(“%d”,&a[i]);}printf(“\nArray before sorting :”);for(i=0;i<n;i++){printf(“\t%d”,a[i]);}shellsort(a,n);printf(“\nArray after sorting : “);for(i=0;i<n;i++){printf(“\t%d”,a[i]);}getch();} Output :

C Program to Implement Shell Sort of a Given Numbers Read More »

C Program to Implement Heap Sort of a Given Numbers

Heap Sort is a comparison-based sorting algorithm that uses a binary heap data structure to build a min-heap or max-heap. The basic idea of the algorithm is to repeatedly remove the root of the heap (which is the minimum element in a min-heap or the maximum element in a max-heap) and swap it with the last element of the heap. After each removal, the heap property is restored by heapifying the remaining elements. Here’s a simple implementation of Heap Sort in C Program : #include <stdio.h>int main(){int arr[10], no, i, j, c, heap_root, temp;printf(“Input number of elements: “);scanf(“%d”, &no);printf(“\nInput array values one by one : “);for (i = 0; i < no; i++)scanf(“%d”, &arr[i]);for (i = 1; i < no; i++){c = i;do{heap_root = (c – 1) / 2;if (arr[heap_root] < arr[c]){temp = arr[heap_root];arr[heap_root] = arr[c];arr[c] = temp;}c = heap_root;} while (c != 0);}printf(“Heap array : “);for (i = 0; i < no; i++)printf(“%d\t “, arr[i]);for (j = no – 1; j >= 0; j–){temp = arr[0];arr[0] = arr[j];arr[j] = temp;heap_root = 0;do{c = 2 * heap_root + 1;if ((arr[c] < arr[c + 1]) && c < j-1)c++;if (arr[heap_root]<arr[c] && c<j){temp = arr[heap_root];arr[heap_root] = arr[c];arr[c] = temp;}heap_root = c;} while (c < j);} printf(“\nSorted array : “);for (i = 0; i < no; i++)printf(“%d\t”, arr[i]);printf(“\n”);} Output :

C Program to Implement Heap Sort of a Given Numbers Read More »

C program to Implement Selection Sort .

Selection Sort is a simple sorting algorithm that repeatedly selects the smallest (or largest, depending on the order) element from the unsorted part of the array and swaps it with the element at the beginning of the unsorted part. The algorithm divides the array into a sorted and an unsorted region. In each iteration, the smallest element from the unsorted region is selected and moved to the sorted region. Here’s a simple implementation of Selection Sort in the C programming language Program : #include<stdio.h>main(){int a[20],n,temp,i,j;printf(“Enter the Number to terms : “);scanf(“%d”,&n);printf(“\nEnter the Elements of the Array : “);for(i=1;i<=n;i++){scanf(“%d”,&a[i]);}for(i=1;i<=n-1;i++)for(j=i+1;j<=n;j++)if(a[i]>a[j]){temp=a[i];a[i]=a[j];a[j]=temp;}printf(“The Ascending Order list is : “);for(i=1;i<=n;i++)printf(“\n%d”,a[i]);} Output :

C program to Implement Selection Sort . Read More »

C Program to Use Trigonometric Functions

C programming, you can use trigonometric functions from the <math.h> header to perform various trigonometric calculations. Here’s a simple program that demonstrates the use of some common trigonometric functions. sin( ) : #include<stdio.h>#include<math.h>int main(){double result,x;printf(“Enter the Value : “);scanf(“%lf”,&x);result=sin(x);printf(“The sin() of %lf is %lf\n”,x,result);return 0;} Output : cos( ) : #include<stdio.h>#include<math.h>int main(){double result,x;printf(“Enter the Value : “);scanf(“%lf”,&x);result=cos(x);printf(“The cosine() of %lf is %lf\n”,x,result);return 0;} Output : tan( ) : #include<stdio.h>#include<math.h>int main(){double result,x;printf(“Enter the Value : “);scanf(“%lf”,&x);result=tan(x);printf(“The tan of %lf is %lf\n”,x,result);return 0;} Output : abs( ) : #include<stdio.h>#include<math.h>int main(){int result,x;printf(“Enter the Value : “);scanf(“%d”,&x);result=abs(x);printf(“The Absolute value of %d is %d\n”,x,result);return 0;} Output : sqrt( ) : #include<stdio.h>#include<math.h>int main(){double result,x;printf(“Enter the Value : “);scanf(“%lf”,&x);result=sqrt(x);printf(“The square root of %lf is %lf.\n”,x,result);return 0;} Output : log( ) : #include<stdio.h>#include<math.h>int main(){double result,x;printf(“Enter the Value : “);scanf(“%lf”,&x);result=log(x);printf(“The natural log of %lf is %lf.\n”,x,result);return 0;} Output :

C Program to Use Trigonometric Functions Read More »

C Program to Find the Length of a String

C program uses an array of characters (char str[100]) to store the input string. It uses fgets to read the string from the user, ensuring that it doesn’t overflow the array. The length of the string is then calculated using a while loop that iterates until it encounters the null character (‘\0’) or a newline character (‘\n’). The null character indicates the end of the string in C. Finally, the program prints the length of the string to the console. Method 1 : #include<stdio.h>void main(){int i=0;char input[20];printf(“Enter any String :”);gets(input);while(input[i]!=’\0′){i++;}printf(“Length of given string: %d”,i);} Output : Method 2 : #include<stdio.h>#include<conio.h>#include<string.h>void main(){char input[80];int i;printf(“Enter the string: “);gets(input);i=strlen(input);printf(“The length of the string is : %d”,i);} Output :

C Program to Find the Length of a String Read More »

Shopping Basket
Verified by MonsterInsights