Example: Input two numbers and output them from large to small (using Pointers)
The code is as follows:
#include"stdio.h"
void main(a)
{
int *p1,*p2,*p;
int a,b;
printf("Please enter the value of A \n");
scanf("%d",&a);
printf("Please enter the value of b \n");
scanf("%d",&b); p1=&a; p2=&b;if(a<b) { p=p1; p1=p2; p2=p; }printf("a=%d,b=%d\n",a,b);
printf("max=%d,min=%d\n",*p1,*p2);
}
Copy the code
Assuming input a=6,b=8, run the output window as follows:
Suppose the question was changed to: Take three numbers and output them from large to small. What should I do?
The implementation code of the inversion method is as follows:
#include<stdio.h>
void main(a) {
int a,b,c,t;
printf("Enter the value of A :");
scanf("%d",&a);
printf("Please enter the value of B :");
scanf("%d",&b);
printf("Please enter the value of C :");
scanf("%d",&c);
printf("a=%d,b=%d,c=%d\n",a,b,c);
if (a < b){// Change the value of a to b
t = a;
a = b;
b = t;
}
if (a < c) {// change the value of a to c
t = a;
a = c;
c = t;
}
if (b < c) {// select a from b and c
t = b;
b = c;
c = t;
}
printf("The three integers from largest to smallest are: %d %d %d\n",a,b,c);
}
Copy the code
Assuming input A =11,b=24,c=36, run the output window as follows:
The implementation code using the basic logical method is as follows:
#include"stdio.h"
void main(a)
{
int a,b,c,t;
printf("Please enter the value of A \n");
scanf("%d",&a);
printf("Please enter the value of b \n");
scanf("%d",&b);
printf("Please enter the value of c \n");
scanf("%d",&c);
if(a<b)
{
if(b<c)
{
printf("a=%d,b=%d,c=%d\n",a,b,c);
printf("C, B,a\n in descending order");
}
else if(b>c)
{
if(a<c)
{
printf("a=%d,b=%d,c=%d\n",a,b,c);
printf("B, C,a\n in descending order");
}
else if(a>c)
{
printf("a=%d,b=%d,c=%d\n",a,b,c);
printf("B, A,c\n in descending order"); }}}else if(a>b)
{
if(b>c)
{
printf("a=%d,b=%d,c=%d\n",a,b,c);
printf("A, B,c\n in descending order");
}
else if(b<c)
{
if(a>c)
{
printf("a=%d,b=%d,c=%d\n",a,b,c);
printf("A, C,b\n in descending order");
}
else if(a<c)
{
printf("a=%d,b=%d,c=%d\n",a,b,c);
printf("C, A,b\n in descending order"); }}}}Copy the code
Assuming the input values are still a=11,b=24,c=36, run the output window as follows:
Other exercises this week
C programming column
C programming > first week ① input two numbers, and make them from large to small output (using Pointers).
② Input the value of the radius r of the circle and output its area.
C language programming > the first week ③ enter a date, determine the date is the day of the year.
C programming > first week ④ output 9*9 multiplication table.
⑤ There are 1, 2, 3, 4 and 4 digits. How many different and no repeated digits can be made up of three digits? What are they?
First week ⑥ Print out all the “daffodil number”, the so-called “daffodil number” refers to a three-digit number, the cube sum of its digits is equal to the number itself.
Students with scores of > =90 are represented by A, those between 60 and 89 are represented by B, and those below 60 are represented by C.
Input two positive integers m and n, find their greatest common divisor and least common multiple.
The harder you work, the luckier you get! Come on, Aoli!!