C programming (Tan Haoqiang version) Chapter II after class exercises

(that is, bottle A was originally filled with vinegar, but now it is changed into soy sauce, and bottle B is the opposite)

Obviously, if only two bottles are not enough, a third empty bottle C needs to be added

The steps are as follows:

First pour the vinegar in bottle A into empty bottle C;

Then wash bottle A and pour the soy sauce in bottle B into bottle A;

Finally, wash bottle B and pour the soy sauce in empty bottle C into bottle B;

This realizes the interchange between the two

2. Input ten numbers in turn and output the largest number

I use the array + loop method for this problem

First, define an array to hold ten input numbers;

Then the values are exchanged by comparing the sizes one by one through a cycle;

Finally, the first element of the array is the maximum value (adjustable), and finally the maximum value is output

Code implementation:

int a [ 10 ];
printf( "Please enter ten numbers to compare in turn:\n" );
for ( int i = 0; i < 10;i++ ) {
scanf_s( "%d", &a[i] );
}
for ( int i = 0; i < 10;i++ ) {
for ( int j = i + 1; j < 11;j++) {
if (a[i]<a[j] ) {
int t;
t = a [ i ];
a [ i ] = a [ j ];
a [ j ] = t;
}
}
}
printf( "Sorted sequence:\n" );
for ( int i = 0; i < 10;i++ ) {
printf( "%2d", a[i] );
}
printf( "\n" );
printf( "The largest number is:\n%d", a [ 0 ] );

The operation results are shown in the figure below: 3. There are three numbers a, B and C. It is required to output them in size order

Judge first

int a, b, c;
printf( "Please enter three numbers:\n" );
scanf_s( "%d %d %d", &a, &b, &c );
if (a>b ) {
int t;
t = b;
b = a;
a = t;
}
if (a>c ) {
int t;
t = c;
c = a;
a = t;
}
if (b>c ) {
int t;
t = c;
c = b;
b = t;
}

printf( "%d %d %d", a, b, c );

The operation results are as follows: 4. Find 1 + 2 + 3 ++ one hundred

First define a variable sum;

We initialize it to 0;

Finally, it is used to hold the results calculated by the cycle

The code is as follows:

int sum = 0;
for ( int i = 1; i <= 100;i++ ) {
sum += i;
}
printf( "%d", sum );

The operation results are as follows: 5. Judge whether a number n can be divided by 3 and 5 at the same time

Conditional juxtaposition uses & & this logical operator

int n;
printf( "Please enter the number to be judged:\n" );
scanf_s( "%d", &n );
if (n%3==0&&n%5==0 ) {
printf( "%d It can be divided by 3 and 5 at the same time", n );
}
else {
printf( "%d Cannot be divided by 3 and 5 at the same time", n );
}

Operation results:  6. Output prime numbers between 100-200

Prime: a natural number that has no other factors except 1 and itself.

For example, 3 is prime

To judge a prime number is to judge its square root and see whether the square root is divisible (2 - the integer of its square root),

If divisible, it is prime, otherwise it is not

for example

If 24 and 24 are between 4 and 5, we only need to judge whether 24 can be divided into integers in the range of 2-his square root,

Maybe I'm a little confused

You can look at this brother and write more clearly

http://c.biancheng.net/view/498.html

The code is directly attached here:

#include<stdio.h>
#include<math.h>
int main(){

int prime( int i );//Function declaration
printf( "100-200 The prime number of is:\n" );
for ( int i = 100; i <= 200;i++ ) {
prime( i );
}
return 0;
}
int prime( int i) {

int j = ( int ) sqrt( i );
int k;
for (  k = 2; k <= j;k++ ) {
if (i%k==0 ) {
break;
}
}
if ( k > j )
printf( "%d It's a prime\n",i );

}

Here I use a function to wrap, which looks more refreshing

Operation results: 7. Find the greatest common divisor of two numbers m and n

I use the transformation phase division method to obtain the maximum common divisor and the minimum common multiple

Those who don't understand the idea can see my article https://blog.csdn.net/weixin_44143600/article/details/118979768?spm=1001.2014.3001.5501

I'll attach the code directly here

int sum=0;

int n, m,r;
printf( "Please enter two numbers to be calculated:\n" );
scanf_s( "%d %d", &n, &m );
if (n<m ) {
int t;
t = n;
n = m;
m = t;
}
sum = n * m;
while ( m!=0 )
{
r = n % m;
n = m;
m = r;
}
printf( "The greatest common divisor is:%d\n", n );
printf( "The least common multiple is:%d\n", sum/n );

Operation results: 8. Find the root of univariate quadratic equation Consider separately:

1. There are two unequal real roots -- > >0
2. There are two equal real roots -- > =0
3. This equation has no solution -- > <0

Square root function sqrt

The code is as follows:

double a, b, c,x1,x2;

printf( "Please enter the three coefficients of the equation:\n" );
scanf_s( "%lf %lf %lf", &a, &b, &c );
double del = pow( b, 2 ) - 4 * (a * c);
x1 = ( ( -b ) + sqrt( del ) ) / 2;
x2 = ( ( -b ) - sqrt( del ) ) / 2;
if (del>0 ) {
printf( "The equation has two real roots\n" );
printf( "namely\n" );
printf( "x1=%lf\n", x1 );
printf( "x2=%lf\n", x2 );
}
else if (del==0 ) {
printf( "The equation has a real root\n" );
printf( "x1=x2=%lf\n", x1 );
}
else {
printf( "This equation has no solution!" );
}

Operation results:

Two real roots: Two equal real roots: No real root: If you have done something wrong, please correct it!

Send a private letter or comment!

Keywords: C

Added by lorddraco98 on Tue, 04 Jan 2022 11:50:24 +0200