Bubble sort
#include <stdio.h>int main () { int i,j,n,temp,a[10]; scanf ("%d", &n); printf ("The original numbers:\n"); for (i=0;i<n;i++) scanf ("%d", &a[i]); for (i=0;i<n-1;i++) for (j=1;j<n-1;j++) if (A[j-1]>a[j]) { temp=a[j-1];a[j-1]=a[j];a[j]= temp; } printf ("\nthe sorted number:\n"); for (i=0;i<n;i++) printf ("%d", A[i]); return 0;}
Judging primes:
If a number can be decomposed by factoring, then the two number that is obtained when decomposing must be a less than or equal to sqrt (n), a greater than or equal to sqrt (n),
If the sqrt (n) is not found on the left, then the right side must not find the approximate.
#include <stdio.h>void main () {int i,n;printf ("Please enter N:"), scanf ("%d", &n), I=2;while (i<n) {if (n%i==0) break; i++;} if (i==n) printf ("%d is prime\n", N), elseprintf ("%d is no prime\n", n);}
The iterative formula for finding the square root is: x n+1 = (xn + a/xn)/2
#include <stdio.h> #include <math.h>int main () {float A; float x0, X1; printf ("Input a positive number:\n"); scanf ("%f", &a); x0 = A/2; X1 = (x0 + a/x0)/2; while (Fabs (x1-x0) >= 1e-5) {x0 = x1; X1 = (x0 + a/x0)/2; } printf ("The square root of%5.2f is%8.5f\n", A, X1); return 0;}
C: Bubble Sort & Determine whether a number is a prime & an iterative formula for square root