当前位置:网站首页>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; } |
边栏推荐
- How to open a video number?
- 为什么虚拟机ping的通主机,主机ping不通虚拟机
- qt -- QTabWidget 中支持拖拽TabBar项
- Install wpr Exe command
- [deep learning][pytorch][original]crnn trains loss on the higher version of pytorch as a solution for Nan
- 把騰訊搬到雲上,治愈了他們的技術焦慮
- How to use arbitrarygen code generator what are the characteristics of this generator
- H5 video conference, camera monitoring, web streaming and live broadcast integration scheme
- [technical tutorial] national standard protocol platform easygbs cascading supports customized national standard channels
- Audio knowledge (III) -- MFCCs code implementation
猜你喜欢

Maui的学习之路 -- 开篇
![[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?

Programmers spend most of their time not writing code, but...

Turn 2D photos into 3D models to see NVIDIA's new AI "magic"!

“一次编写,运行各端”,高通重磅发布 AI 软件栈!

《opencv学习笔记》-- 图像的载入和保存

链接器 --- Linker

图片的可视化呈现有效增强大屏吸引力

【数字IC/FPGA】Booth乘法器

《opencv学习笔记》-- 分离颜色通道、多通道混合
随机推荐
Code is really - omnipotent! Refuse to fight
GLOG从入门到入门
Nacos source code - Interface read configuration
How to use arbitrarygen code generator what are the characteristics of this generator
[the lottery in May has ended, and the list of winners has been announced] special session of techo youth university open course database
程序员大部分时间不是写代码,而是。。。
math_ Summation and derivation of proportional series & derivation of sum and difference of equal powers / difference between two nth power numbers/
“一个优秀程序员可抵五个普通程序员!”
《opencv学习笔记》-- 离散傅里叶变换
软件测试 对前一日函数的基本路径测试
Programmers spend most of their time not writing code, but...
First acquaintance with string+ simple usage (I)
《opencv学习笔记》-- 分离颜色通道、多通道混合
万名校园开发者花式玩AI,亮点看这张图就够啦!
Basic path test of software test on the function of the previous day
Tools and methods - use code formatting tools in source insight
How to open a video number?
Influence of DEX optimization on arouter lookup path
It's so difficult for me. Have you met these interview questions?
[deep learning][pytorch][original]crnn trains loss on the higher version of pytorch as a solution for Nan