Home > Back-end >  Introduction to C code
Introduction to C code

Time:05-14

O equation ax + bx + c ^ 2=0 roots, there are two different real root, there are two equal real root
 # include 
#include
Int main () {
Int a, b, c;
Printf (" please input a, b, c: ");
The scanf (" % d, % d, % d ", & amp; A, & amp; B, & amp; C);
Float m, n;
Int d=b * b - (4 * a * c);
If (d & gt; 0 {
B + m=((1) * SQRT (d))/(2 * a);
N=((1) * b - SQRT (d))/(2 * a);
Printf (" there are two is not equal to the real root of m=% f, n=% f ", m, n);
{} else if (d==0)
M=(1)/(2 * a) * b;
N=(1)/(2 * a) * b;
Printf (" have two equal real root of m=% f, n=% f ", m, n);
} else {
Printf (" there is no real root!" );
}
return 0;
}

The output is a leap year year 1900-2000 years, but it can't be divided exactly by 100 can be divided exactly by 4, divisible by 400
 # include 
Int main () {
Int year;
For (year=1900; Year & lt;=2000; 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;
}

Find the greatest common divisor of m and n two
 # include 
Int main () {
Int m, n, r;
Printf (" please enter the number two: ");
The scanf (" % d, % d ", & amp; M, & amp; n);

While (n!=0) {
R=m % n;
M=n;
N=r;
}
Printf (" the greatest common divisor of the number two is: % d \ n ", m);
return 0;
}

Input the number 10, output is one of the biggest number
 # include 
Int main () {
Int n [10].
int i;
Int Max=n [0];
Printf (" please enter the number 10: ");
for(int i=0; i <=10; + + I)
The scanf (" % d ", & amp; N [I]);
for(int i=0; i <=10; + + I)
Max=n [I] & gt; Max? N [I] : Max;
Printf (" Max=% d \ n ", Max).
return 0;
}

CodePudding user response:

 if (year % 4==0 & amp; & Year % 100!=0 | | year %==0 400) 

This change:
 if ((year % 4==0 & amp; & Year % 100!=0) | | year %==0 400) 

Some logic clearer ~

 # include 
Int main () {
Int n [10].
//int I;
//int Max=n [0];//random values, which may be Max also can not let go of random values
Int Max;
Printf (" please enter the number 10: ");
for(int i=0; i <=10; + + I)
The scanf (" % d ", & amp; N [I]);
Max=n [0];//in this initialization
for(int i=0; i <=10; + + I)
Max=n [I] & gt; Max? N [I] : Max;
Printf (" Max=% d \ n ", Max).
return 0;
}

For your reference

Max initialization has a problem, may lead to abnormal results program;

CodePudding user response:

The
reference
# include
Int main () {
Int n [10].
int i;
Int Max=n [0];
Printf (" please enter the number 10: ");
for(int i=0; i <=10; + + I)
The scanf (" % d ", & amp; N [I]);
for(int i=0; i <=10; + + I)
Max=n [I] & gt; Max? N [I] : Max;
Printf (" Max=% d \ n ", Max).
return 0;
}

For () in an array subscript cross-border operation, should be changed to: the for (int I=0; i<10; i++)

CodePudding user response:

Ok, thank you
  • Related