C: Bubble Sort & Determine whether a number is a prime & an iterative formula for square root

Source: Internet
Author: User
Tags square root

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.