当前位置:网站首页>Introduction to C language circular statements (foe, while, do... While)

Introduction to C language circular statements (foe, while, do... While)

2022-06-24 11:32:00 Hua Weiyun

This article mainly introduces the usage of circular sentence in the form of case title , Use scenarios . By way of while loop 、for loop 、do..while loop , this 3 Complete the same exercise in two different ways , Compare 3 The difference between circular grammar , Deepen the understanding .

1.1 Explanation of exercise topics ( practice :for)

1.​  Output all 200~400 Endogenic quilt 3 Divide by integer and the single digit number is 6 The integer of .

Example :

#include <stdio.h>

/*1. Output all 200~400 Endogenic quilt 3 Divide by integer and the single digit number is 6 The integer of */

int main()

{

int i,data;

for(i=200;i<=400;i++)

{

data=i%10;

if(i%3==0 && data==6)

{

printf("%d ",i);

}

}

return 0;

}


2.​  Judge 10~100 Direct all prime numbers . ( prime number : Can only be 1 And divisible by itself . such as : 2,3,7)

Example :

#include <stdio.h>

/*2. Judge 10~100 Direct all prime numbers .*/

int main()

{

int i,j,stat=0,cnt=0;

for(i=10;i<100;i++) //4 7

{

for(j=2;j<i;j++)

{

if(i%j==0)

{

stat=1;

}

cnt++;

}


if(stat==0)

{

printf("%d ",i);

}

else

{

stat=0;

}

}

printf("\n");

printf(" The total number of times : %d\n",cnt); //4725

return 0;

}


3.​  Output all Narcissus numbers .( The number of splashes is a three digit number , The sum of the cubes on the tens is equal to the number itself 123)

Example :

#include <stdio.h>

/*3. Output all Narcissus numbers .*/

int main()

{

int i;

int a,b,c;

//123

for(i=100;i<=999;i++)

{

a=i/100;

b=i%100/10;

c=i%10/1;

if((a*a*a+b*b*b+c*c*c) == i)

{

printf("%d ",i);

}

}

printf("\n");

return 0;

}


4.​  Output 100~500 Direct number of all palindromes

Example :

#include <stdio.h>

/*4. Output 100~500 Direct number of all palindromes */

int main()

{

int i;

int a,b; //123

for(i=100;i<=500;i++)

{

a=i/100;

b=i%10;

if(a==b)

{

printf("%d ",i);

}

}

printf("\n");

return 0;

}


5.​  Programming calculation 1*2*3+4*5*6+....+97*98*99 Value .

Example :

#include <stdio.h>


/*1*2*3+4*5*6+....+97*98*99*/

int main()

{

int i;

int sum=0;

for(i=1;i<=99;i+=3) //i+=3 =i=1+3

{

sum=sum+(i)*(i+1)*(i+2);

}

printf("%d\n",sum);

return 0;

}


6.​  Programming calculation 1!+2!+3!+..+10! Value .( Factorial )

Example :

#include <stdio.h>

/*6. Programming calculation 1!+2!+3!+..+10! Value .( Factorial )*/

int main()

{

int i,sum=0,tmp=1;

for(i=1;i<=10;i++)

{

tmp=tmp*i;

sum+=tmp;

}

printf("%d\n",sum);

return 0;

}


7.​  Output 9*9 Multiplication table

Example :

#include <stdio.h>

/*7. Output 9*9 Multiplication table */

int main()

{

int i,j;

for(i=1;i<=9;i++)

{

for(j=1;j<=i;j++)

{

printf("%d*%d=%d ",j,i,i*j);

}

printf("\n");

}

return 0;

}

1.2 Exercises related to circular sentences

1.​  Cumulative sum :1+2+3+…+n

Example :


#include <stdio.h>

/*1. Cumulative sum :1+2+3+…+n */

int main()

{

int n,sum=0,i;

printf(" Please enter an integer : \n");

scanf("%d",&n);

for(i=1;i<=n;i++)

{

sum=sum+i;

}

printf("sum=%d\n",sum);

return 0;

}


2.​  The problem of buying 100 chickens for 100 yuan : Each cock 5 element , Each hen 3 element , Chick 3 Only one yuan , There are several ways to buy 100 chickens for 100 yuan .

( First, calculate how many chickens each type of chicken can buy )

Example :

#include<stdio.h>

int main()

{

int m,g,x;

int m_max;

int g_max;

int x_max;

int q,cnt; //cnt= Number q: money

printf(" Please enter the quantity of chicken and RMB :\n");

scanf("%d%d",&cnt,&q);// 100 100


/*1. Judge whether the input data is reasonable */

if(q>0&&cnt>0)

{

m_max=cnt/3; // hen 100 /3 =33

g_max=cnt/5; // cock 100 /5 =20

x_max=(cnt/1)*3; // Chick 100 /1*3 =300


for(g=0;g<g_max;g++) // Circular judgment may

{

for(m=0;m<m_max;m++)

{

x=cnt-g-m; // Total quantity - Number of cocks - The number of hens = The number of chickens

if(x+g+m==cnt && x/3+g*5+m*3==q)

// If the total number of chickens is equal to the total number of chickens and the total amount of money is equal to the amount of money to buy chickens, it will be output

{

printf(" cock =%d\t",g);

printf(" hen =%d\t",m);

printf(" Chick =%d\t\n",x);

}

}

}

}

else

printf(" Incorrect input \n");

return 0;

}


3.​  seek n Of m Power .(n and m Type... From the keyboard )

Example :

#include <stdio.h>

/*3. seek n Of m Power .(n and m Type... From the keyboard )*/

int main()

{

int n,m,i,data=1; // such as : n=3 m=2

printf(" Calculation n Of m Power , Please enter n and m Value :");

scanf("%d%d",&n,&m);

for(i=0;i<m;i++)

{

data=data*n;

}

printf("%d\n",data);

return 0;

}

1.3 Loop statement

1.3.1 for loop

for(< initialization >;< Conditional expression >;< Self increasing / Self reduction >)

{

// sentence ;

}

// If the above 3 Conditional expressions are not written ( You can't have less semicolons ),for It's a dead cycle

Only 0 Is false . Not 0 It's all true .

for For known cycles .

1.3.2 while loop

while(< Conditional expression >)

{

< sentence >;

}

while For unknown loop .

Example :

#include<stdio.h>

int main()

{

int i=0;

while(i<5)

{

i++;// 1 2 3 4 5

}

printf("%d",i); //

return 0;

}

1.3.3 do..while loop

do

{

< sentence >;

}while(< Conditional expression >);

1.3.4 goto Jump statements

goto < label >;


label :

goto Jump statements . You can jump to any position in a function . ( It is not commonly used )

Label naming rules : Same as variable name .

Example :

#include<stdio.h>

int main()

{

int i=0;

printf("1\n");

n:

printf("2\n"); //5 Time

i++;

if(i==5)goto k;

goto n;

k:

printf("3\n");

return 0;

}

1.3.5 break sentence

Format : break;

function :1. Jump out of the nearest loop . 2. End switch Execution of statements

1.3.6 continue sentence

Format :continue;

function : End the current cycle .

#include<stdio.h>

int main()

{

int i=0,j=0,cnt=0;

for(i=0;i<5;i++)

{

cnt++;

if(i==3)continue;

printf("8888\n"); // Print 4 Time

}

printf("%d\n",cnt);//

return 0;

}

原网站

版权声明
本文为[Hua Weiyun]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/175/202206241003460244.html