# Using functions to implement several classic codes

Source: Internet
Author: User

1. Use a function to determine whether a number is prime

`#include <stdio.h> #include <math.h>void fun (int x) {int. i;if (x<=2) printf ("The number is prime\n"); else{ For (i=2;i<sqrt (x); i+=2) {if (x%i==0) break;}        if (i<sqrt (x)) printf ("The number is not prime\n"); else printf ("The number is prime\n");}} int main () {int num;printf ("Please input a number:\n"); scanf ("%d", &num); (num); return 0;}`

Implement a function to determine if year is not a leap year.

`#include <stdio.h>void Fun (int x) {if (x%4==0) && (x%100!=0) | | (x%400) ==0) printf ("%d years is a leap year \ n", x); elseprintf ("%d years is not a leap year \ n", x);} int main () {int year;printf ("Please input year:\n"); scanf ("%d", &year); Fun (year); return 0;}`

3. Create an array,

The implementation function init () initializes the array,

Implement empty () to empty the array,

Implements the reverse () function to complete the inverse of the array element.

`#include <stdio.h>void init (int a1[],int len1) {int j;int a2[10]={0,1,2,3,4,5,6,7,8,9};for (j=0;j<len1;j++) { A1[J]=A2[J];p rintf ("%d", A1[j]);}}    void reverse (int a2[], int len2) {int k;int a3[10]={0};for (k=0;k<len2;k++) {a3[k]=a2[len2-1-k]; printf ("%d", A3[k]);}} void empty (int a3[],int len3) {int h;int a4[10]={0};for (h=0;h<len3;h++) {a3[h]=a4[h];p rintf ("%d", A3[h]);}} int main () {int i;int a[10]={0};int len=10;init (a,len);p rintf ("\ n"), reverse (A,len);p rintf ("\ n"); empty (A,len);p rintf ("\ n");}`

Using functions to implement several classic codes

Related Keywords:

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.