当前位置:网站首页>(C language) 3 small Codes: 1+2+3+ · · +100=? And judge whether a year is a leap year or a normal year? And calculate the circumference and area of the circle?
(C language) 3 small Codes: 1+2+3+ · · +100=? And judge whether a year is a leap year or a normal year? And calculate the circumference and area of the circle?
2022-07-02 12:06:00 【Study silently every day】
We know , A program contains a small algorithm , Program is the implementation of Algorithm .
So in real life , We can use some code to implement some calculations , In terms of the ability of our primary procedural apes , We can implement small algorithms through programs , such as 1+2+3+······+100, We all know Gauss Algorithm ,(1+100)*50=5050, Now we can realize it by program , As shown below :
#include<stdio.h>
int main()
{
int i,n=0;
for(i=0;i<=100;i++)
{
n+=i;
}
printf("1+2+3+···+100=%d",n);
}
The following is the operation interface :
We can contact from junior high school C Programming , When others are still taking calculators , We have started to write our own computing programs in code , Isn't it very COOL, With the development of technology , We have more and more learning channels , Such as network , We can take courses from the best universities in the world , Through video teaching , We can listen to the lessons we want at home , Learn what we want to learn .
therefore , Now is a world of self-study , Find your own learning content , Improve yourself , meanwhile , We can appropriately improve our hands-on ability , For example, make some small furniture , Or write code , Or learn SCM , Or learn some skills , however , Now China is also developing , Improve the quality and diversity of Education , For example, primary school students have a lot of housework and other classes , All very good , We don't just read , Recite , Problem. , We also need to do real things .
And today's topic is 2 The first code is to judge the average year , Leap year , We all know , A year , If it can be 4 to be divisible by , At the same time, it cannot be 100 to be divisible by , So this year is leap year , If a year can be 400 to be divisible by , So this year is leap year , The rest of the years are normal , So how do we implement this algorithm ?
#include<stdio.h> // The header file
int main() // Main function entry
{
int year; // Define an integer variable year
printf(" Please enter a year :"); // Print out : Please enter a year
scanf("%d",&year); // Enter a year , such as 2030
if((year%400 == 0)||(year%4==0&&year%100 !=0)) // Judge , Whether this year can be 400 to be divisible by , Or the concept can be 4 Divisible at the same time cannot be 100 to be divisible by
printf(" Leap year "); // If meet if Conditions , Print : Leap year
else
printf(" Ordinary year "); // If not satisfied if Conditions , Print : Ordinary year
return 0; // return 0
}
The code has been explained clearly through comments , I can't describe more here , This code is also a very simple code , By reading the code , Then change the code , I'll write the code myself when I have time , You can learn to write code slowly . Everyone said C Is the most basic language , If you learn C Language , Then you can learn other programming languages , meanwhile , There are specialized skills , At most, we are proficient in 1-2 Languages , Train yourself for a long time , Can become more and more powerful , Other languages , We can understand it , After all, our time and energy are limited .
The following is the operation interface :
OK, The above is the learning of this simple code , We are all training , Train your ability , When you have time, open the computer and type a code , Slowly , Will become more and more skilled , In the future, I can engage in software development .
Now let's share the first 3 Code :
#include <stdio.h>
// Definition pi by 3.14
#define pi 3.14
// Statement findCircumference() function
double findCircumference(double r);
double findArea(double r);
int main() {
double c; // Define the circumference of the circle
double r; // Define the radius of the circle
double s;
// Of course, it can also be written in the following form
//float c,r;
printf("Please input the radius:"); // Give a prompt for what you need to enter
scanf("%lf",&r); // Read the entered radius length from the keyboard
c=findCircumference(r);
s=findArea(r); // call findCircumference() function , Pass the value of radius into this function , Calculate the circumference of a circle , And assign the result to the variable c
printf("The circumference is: %.2lf\n",c); // Print the circumference of the circle on the screen
printf("The area is:%.2lf",s);
return 0; // End procedure
}
/**
* This function is used to calculate the circumference of a circle
* The parameter passed in is the radius of the circle
* The returned parameter is the length of the circle
*/
double findCircumference(double r){
double c; // Define the circumference of the circle , local variable , Only valid in this function
c=2*pi*r; // Calculate the circumference of a circle
return c; // Return the circumference of the circle
}
double findArea(double r){
double s; // Define the area of the circle , local variable , Only valid in this function
s=pi*r*r; // Calculate the area of a circle
return s; // Returns the area of the circle
}
Or that sentence , There are specialized skills , Our time and energy are limited , We need a long time of immersive programming , Some students may like programming very much , Some students don't like it very much , therefore , It still depends on personal interests , therefore , Find your own interests , Just study for decades , Take it as a skill of your own , You can even make your skills feel artistic .
OK, Thank you for your browsing , thank you all . I will continue to learn C Language , There is embedded software development , Try to make some interesting embedded systems to share with you , I am also constantly learning , meanwhile , I will insist on writing blog, Strive for better and better content , At the same time, writing skills continue to improve , Improve the excellence of the content and the readability of the document , Try to bring you different feelings .
边栏推荐
- Pytorch builds LSTM to realize clothing classification (fashionmnist)
- Leetcode739 每日温度
- Time format display
- Implementation of address book (file version)
- Le tutoriel F - String le plus facile à comprendre de l'histoire.
- YYGH-10-微信支付
- lombok常用注解
- PgSQL string is converted to array and associated with other tables, which are displayed in the original order after matching and splicing
- Take you ten days to easily finish the finale of go micro services (distributed transactions)
- Beautiful and intelligent, Haval H6 supreme+ makes Yuanxiao travel safer
猜你喜欢
数据分析 - matplotlib示例代码
PyTorch搭建LSTM实现服装分类(FashionMNIST)
Test shift left and right
HOW TO EASILY CREATE BARPLOTS WITH ERROR BARS IN R
ES集群中节点与分片的区别
(C语言)3个小代码:1+2+3+···+100=?和判断一个年份是闰年还是平年?和计算圆的周长和面积?
自然语言处理系列(一)——RNN基础
MSI announced that its motherboard products will cancel all paper accessories
Dynamic memory (advanced 4)
测试左移和右移
随机推荐
HOW TO CREATE AN INTERACTIVE CORRELATION MATRIX HEATMAP IN R
B high and beautiful code snippet sharing image generation
to_bytes与from_bytes简单示例
File operation (detailed!)
HOW TO ADD P-VALUES ONTO A GROUPED GGPLOT USING THE GGPUBR R PACKAGE
FLESH-DECT(MedIA 2021)——一个material decomposition的观点
【2022 ACTF-wp】
Natural language processing series (III) -- LSTM
深入理解PyTorch中的nn.Embedding
Dynamic debugging of multi file program x32dbg
How to Add P-Values onto Horizontal GGPLOTS
Repeat, tile and repeat in pytorch_ The difference between interleave
(C语言)八进制转换十进制
How to Create a Beautiful Plots in R with Summary Statistics Labels
BEAUTIFUL GGPLOT VENN DIAGRAM WITH R
全链路压测
The most understandable f-string tutorial in history, collecting this one is enough
Cmake cross compilation
The position of the first underline selected by the vant tabs component is abnormal
Deep understanding of NN in pytorch Embedding