当前位置:网站首页>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; } |
边栏推荐
- 为什么虚拟机ping的通主机,主机ping不通虚拟机
- 如何只导出word文档中的标题?(即将正文内容都删除,只保留标题)B站牛逼
- [net action!] Cos data escort helps SMEs avoid content security risks!
- Functions of document management what functions does the document management software have
- Use the process monitor tool to monitor process operations on registries and files
- Maui's way of learning -- Opening
- [Flink source code practice (I)] add a rest API to Flink
- About the unsupported instruction set SSE 4.2 of CPU in virtualization
- How to open a video number?
- Which is a good CAD drawing software? How to select good software
猜你喜欢

Qt: judge whether the string is in numeric format

万名校园开发者花式玩AI,亮点看这张图就够啦!

齐次坐标的理解
![[graduation season · attacking technology Er] three turns around the tree, what branch can we rely on?](/img/0a/0ebfa1e5c1bea6033b538528242252.png)
[graduation season · attacking technology Er] three turns around the tree, what branch can we rely on?

GLOG从入门到入门
![[activities this Saturday] NET Day in China](/img/33/c0e8eeb8f673232a7c27bbaf5e713f.jpg)
[activities this Saturday] NET Day in China

FreeRTOS概述与体验

2D 照片变身 3D 模型,来看英伟达的 AI 新“魔法”!

“一个优秀程序员可抵五个普通程序员!”

TP-LINK 1208 router tutorial (2)
随机推荐
工具及方法 - 在Source Insight中使用代码格式化工具
How does wechat and QQ chat work? So simple!!!
qt -- QTabWidget 中支持拖拽TabBar项
RPM installation percona5.7.34
I pushed my younger brother into Tencent. Look at his benchmark resume!
把腾讯搬到云上,治愈了他们的技术焦虑
Cloud vendor secondary virtualization restrictions
Google ranging for PHP wechat development
It's so difficult for me. Have you met these interview questions?
math_ Summation and derivation of proportional series & derivation of sum and difference of equal powers / difference between two nth power numbers/
Any and typevar make the automatic completion of IDE better
Opencv optical flow prediction and remap remapping function usage
AXI低功耗接口
Google Earth engine (GEE) - how to add a legend in the map panel
Attribute observer didset and willset in swift of swiftui swift internal skill
【数字IC/FPGA】Booth乘法器
如何开发短信通知和语音功能医院信息系统(HIS系统)
Which is a good CAD drawing software? How to select good software
Ppt drawing related, shortcut keys, aesthetics
Centripetalnet: more reasonable corner matching, improved cornernet | CVPR 2020 in many aspects