"Learning the path of iOS: C-language" loop questions

Source: Internet
Author: User
Tags greatest common divisor

1. Print the graphic shown below:

*

*  *

* * *

The code is as follows:

    for (int i = 0, i < 3; i++) {for            (int j = 2; j > i; j--) {            printf ("");        }       for (int k = 0; k < i + 1; k++) {            printf ("*");        }        printf ("\ n");            }

2. Print out the shape:

*

*   *

*  *  *

*   *

*

The code is as follows:

    for (int i = 0; i < 5; i++) {                if (I < 3) {             //first three lines, that is, first half of the graph is printed out for            (int j = 0; J < 2-i, J + +) {                pri NTF ("");            }            for (int  k = 0; k < i + 1; k++) {                 printf ("*");            }            printf ("\ n");        } else {                       //After two lines, the lower half of the graph for            (int j = 0; J < I-2; J + +) {                printf ("");            }            for (int k = 0; k < 5-i; k++) {                printf ("*");            }            printf ("\ n");}         }     

3. Enter an integer from the console to determine if it is a prime number.

     <span style= "FONT-SIZE:14PX;" >  int n = 0, m = 0;    printf ("Please input a int number:\n");    scanf ("%d", &n);    for (M = 2; m < n; m++) {        if (n% m = = 0  ) {            printf ("Not Prime:%d", n);            break;//end of this layer loop, loop over.        }    } After the loop is over.    if (M = = N) {        printf ("is prime number \ n");    } </span>

Add:

How to remove random numbers in C language:

Stochastic formula: [A, b] a random number in the range of a-a
int n3 = Arc4random ()% (b-a + 1) + A;


4. Randomly generate 20 positive integers [10, 100], output these numbers and the maximum number of them

<span style= "FONT-SIZE:14PX;" >int max = 0;int temp = 0;    for (Int J =0; J <; J + +) {        temp = arc4random ()% +10;                if (Max < temp) {            max = temp;        }            }    printf ("The Max  is%d\n", max);</span>    

5. Program to print out all "daffodils" and print the total number of them. "Narcissus number" is a three-digit number of individual instituted and equal to that integer.

<span style= "FONT-SIZE:14PX;" >   int x = 0;//hundred    int y = 0;//10 digits    int z = 0;//single digit for    (int num = n; num <= 999; num++) {        x = n um/100;        y = num/10%;        z = num%;        if (num = = (x*x*x + y*y*y + z*z*z)) {            printf ("Number of daffodils:%d\n", num);}    } </span>

6. Enter two numbers for greatest common divisor and least common multiple. (In two ways: the Euclidean method and the common method)

<span style= "FONT-SIZE:14PX;" >    int a = 0;    int b = 0;    int Yushu = 0;    printf ("Please intput-numbers: \ n");    scanf ("%d%d", &a, &b);        The Euclidean method    int max = 0, min = 0;    max = a > B? a:b;//Judge ab size and give Max the big value. Most dividend    min = a > b? b:a;//the most divisor.    Yushu = max% min;    while (Yushu! = 0) {        max = min;        min = Yushu;        Yushu = max% min;    }    printf ("Greatest common divisor:%d\n", min);    printf ("Maximum Common multiple:%d\n", (A * b)/min);   Common method for  (int i = 1; I <= min; i++) {if (max% i = = 0 && min% i = = 0) {yue=i;    }} printf ("Greatest common divisor:%d\n", Yue); printf ("Minimum number of conventions:%d\n", (a*b)/yue);</span>

7.find the minimum value in n random numbers

<span style= "FONT-SIZE:14PX;" >    int min = 0, temp = 0, n =0;    printf ("Please intput a number n:\n");    scanf ("%d", &n);    for (int j = 0; J < N; j + +) {        temp = arc4random ()% (15-10 + 1) +;        if (j = = 0) {            min = temp;        } else if (min > Temp) {            min = temp;        }    }    printf ("The Min is%d\n", min);</span>



"Learning the path of iOS: C-language" loop questions

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.