Home > Back-end >  Beginner C code
Beginner C code

Time:04-11

Determine whether each year in 2000-2500 is a leap year, and the results will be output:
 # include 
Int main () {

Int year;
For (year=2000; Year & lt;=2500; Year++) {
If (year % 4==0 & amp; & Year % 100!=0 | | year %==0 400) {
Printf (" % d is a leap year \ n ", year);
}
The else {
Printf (" % d is not a leap year \ n ", year);
}
}

return 0;
}

For 1 * 2 * 3 * 4 * 5:
 
#include
Int main () {
Int t=1;//multiplier
Int I=2;//the multiplicand
While (I & lt; {
=5)T=t * I;
i=i + 1;
}
Printf (" % d ", t);
return 0;
}

There are 50 students to output results in more than 80 points students student number and grade:
 # include 
Int main () {
Int I, n [50], g [50].//I represents how many students are, n [I] on behalf of the student id, g [I] for achievement
for(i=0; I & lt; 50; + + I) {
Printf (" please enter the first % d students student number and grade: ", I + 1);
The scanf (" % d, % d ", & amp; N [I], & amp; G [I]);
}
for(i=0; I & lt; 50; + + I) {
[I] if (g & gt; {
=80)Student id: printf (" % d, achievement: % d \ n ", n [I], g [I]);
}
}
return 0;
}

Given a positive integer greater than or equal to 3, whether it is a prime number:
 # include 
Int main () {
int n,i;
Printf (" please enter a positive integer greater than or equal to 3: ", n);
The scanf (" % d ", & amp; N);
Int prime (int x);
I=prime (n);
If (I==1) {
Printf (" % d is not a prime \ n ", n);
} else {
Printf (" % d is a prime \ n ", n);
}
return 0;
}
Int prime (int x) {
int y;
If (x & gt; {
=3)For (y=2; Y & lt; X - 1; + + y) {
If (x % y==0)
return 1;
}
return -1;
} else {
Printf (" please enter a positive integer greater than or equal to 3! \n");
}
}

O 1-1/2 + a third - quarter +... + 1/99-1/100:
 # include 
Int main () {
Float term;//the value of the current item
Float sum=1;//the current accumulation and
Float deno=2;//the current item of the denominator
Int sign=1; The current item number//symbol
While (deno & lt; {
=100)Sign=(1) * sign;
The term=sign * (1/deno);
Sum=sum + term;
Deno=deno + 1;
}
Printf (" % f ", sum);
return 0;
}

CodePudding user response:

good

CodePudding user response:

Reference:
//given a positive integer greater than or equal to 3, whether it is a prime number: 

#include
Int main () {

Int prime (int x);
int n,i;
Printf (" please enter a positive integer greater than or equal to 3: ", n);
While (the scanf (" % d ", & amp; N) & amp; & N & lt; 3) {
Printf (" please enter a positive integer greater than or equal to 3! \n");
}
I=prime (n);
If (I==1) {
Printf (" % d is a prime \ n ", n);
} else {
Printf (" % d is not a prime \ n ", n);
}

return 0;
}

Int prime (int x) {
int y;
If (x & lt;=3) return x & gt; 1;
For (y=2; Y & lt; X - 1; + + y)
If (x % y==0)
return 0;
return 1;
}

CodePudding user response:

Fixed line 8 upstairs, much n
 printf (" please enter a positive integer greater than or equal to 3: "); 

For 1 * 2 * 3 * 4 * 5, you can also write so:
//o 1 * 2 * 3 * 4 * 5: 

#include
Int main () {
Int t=1;//multiplier
Int I=5;//the multiplicand
While (t *=I, I -, i> 0);
Printf (" % d ", t);
return 0;
}
  • Related