C--Partial loop excerpt

Source: Internet
Author: User
Tags greatest common divisor

1.99 Multiplication Table

#include <stdio.h>int main (int argc,const char *argv[]) {    //Outer loop--How many lines for    (int i=1; i<=9; i++) {       // Inner Loop-The specific entry for each line printed for        (int j=1; j<=i; J + +) {            printf ("%d*%d=%d", J, I, i*j);        }        printf ("\ n");    }    return 0;}

2. Alphabet Pyramid

Input: f////output://     f//    efe//   defed//  cdefedc//BCDEFEDCB//ABCDEFEDCBA
/*
Analysis:
     F
    EF  E   DEF  ED  cdef  EDC bcdef  edcbabcdef  EDCBA

*/int Main (int argc,const char *argv[]) { char ch; scanf ("%c", &ch);
Outer Loop--How many lines are printed for (int i=0; i<ch-' A ' +1; i++) {
Print spaces-note the relationship between the number of spaces and I for (int j=0; j<ch-' A '-i; j + +) { printf (""); }
Print left half character for (int j=i; j>=0; j--) { printf ("%c", ch-j); }
Prints a half-part character for (int j=0; j<i; J + +) { printf ("%c", ch-1-j); } printf ("\ n"); } return 0;}

    input: f//    output://    fedcba//    edcbab//    dcbabc//    cbabcd//    babcde//ABCDEF
/*
Analysis: In the same vein
*/ char ch; scanf ("%c", &ch); for (int i=0; i<ch-' a ' +1; i++) { for (int j=ch-' a '-I; j>=0; j--) { printf ("%c", ' a ' +j); } for (int j=0; j<i; J + +) { printf ("%c", ' A ' +j+1); } printf ("\ n"); }
Input: f////output:////       a//     aba//    abcba//  abcdcba//abcdedcba//abcdefedcbaint Main (int argc,const char * Argv[]) {    char ch;    scanf ("%c", &ch);        for (int i=0, i<ch-' a ' +1; i++) {for        (int j=0; j<ch-' a '-I; j + +) {            printf ("");        }        for (int j=0; j<=i; J + +) {            printf ("%c", ' A ' +j);        }        for (int j=i; j>0; j--) {            printf ("%c", ' A ' +j-1);        }        printf ("\ n");    }    return 0;}

Input: f////output:////fedcba//edcbab//  dcbabc//   cbabcd//    babcde//     abcdefint Main (int argc,const char * Argv[]) {    char ch;    scanf ("%c", &ch);    for (int i=0, i<ch-' A ' +1; i++) {for        (int j=i; j>0; j--) {            printf ("");        }        for (int j= ch-' a '-i;j>=0; j--) {            printf ("%c", ' a ' +j);        }        for (int j=0; j<i; J + +) {            printf ("%c", ' A ' +1+j);        }        printf ("\ n");    }        return 0;}

3. (Loop inversion)

eg. given a 5-bit integer, reverse the number according to the 10 binary bit, for example a given 12345 becomes 54321

    int m;    scanf ("%d", &m);    int n=0;    while (m) {
Remove the last digit and move the N=N*10 + m%10 to the high position ;
Remove the last m=m/10; } printf ("%d\n", n); return 0;

4. (Cycle through the figures)

eg. add the values of all bits of a 8-bit integer and output

    int m;    scanf ("%d", &m);    int n=0;    int sum=0;    while (m) {
Remove each bit of n=m%10;//printf ("%d", N) in turn; Sum=sum+n;
Remove the last m=m/10; } printf ("%d\n", sum);

5. (Loop Solve "end number")

eg. find all "end numbers" within 1000, the so-called end number is the sum of all its factors such as: 6 = 1+2+3;

for (int i = 1; I <=, i++)    {        int sum = 0;        for (int j = 1; j < I; j + +)        {
Find the number that matches, add if (i% J = = 0) sum = sum + j; }
Satisfies the condition if (i = = sum) printf ("%d\n", I); }

6. Greatest common divisor & least common multiple

Greatest common divisor, least common multiple   //least common multiple = Two integer multiplied by greatest common divisor//division//    ①a%b remainder c//    //    ② if c=0, then B is two number of greatest common divisor/    ///③ If c≠0, then a=b,b=c, then go back to execute ①    int m,n;    int a,b,c;    scanf ("%d%d", &a,&b);    M=a;    n=b;    Go    while (b!=0) {        c=a%b;        a=b;        b=c;//remainder Assignment    }    printf ("Greatest common divisor%d\n", a);    printf ("Least common multiple%d\n", m*n/a);

eg. enter two fractions and output the sum of two fractions (numerator required)

    int a, B;    int m,n;    scanf ("%d/%d", &a,&b);    scanf ("%d/%d", &m,&n);    int x, y;    X=a*n+m*b;    Y=b*n;    Convention number    int i,j,k;    i=x;    j=y;    while (j!=0) {        k=i%j;        I=j;        j=k;    }    printf ("\ n Convention number%d\n", i);    printf ("%d/%d\n", x/i,y/i);

C--Partial loop excerpt

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.