当前位置:网站首页>Two hundred questions in C language (0 basic continuous update) (1~5)

Two hundred questions in C language (0 basic continuous update) (1~5)

2022-08-11 06:57:00 Lamb said test!

First, take the easiest one first!It is suitable for pure novice, and the boss can take a detour.

Output hello world!Let's say hello to the world!

   

#includeint main(){printf("Hello World!");return 0;}​​

Don't go when you see this!Let's gradually stack up the difficulty, okay?

Second, if m is a three-digit number, write the three-digit number formed by the reverse order of the ones, tens, and hundreds of m (for example: 123 is reversed to 321)

​​#includeint main(){int n = 123,t;do{t = n%10; //Remove the single digit of the right numberprintf("%d",t);n/=10; //Discard the right number and continue the loop}while(n);//n is equivalent to n!=0 Continue processing as long as the number of significant digits has not been processedreturn 0 ;}​​

3. Known int x=10, y=12; write an expression that exchanges the values ​​of x and y with each other!

(1) The easiest way!Use the third variable and then swap

​#includeint main(){int x=10,y=12,t;//Define the third variable t exchanget=x; x=y; y=t;//swapprintf("x=%d y=%d",x,y);return 0;}​​

(2) The second use of addition and subtraction exchange is also easy to understand!

​​​#includeint main(){int x=10,y=12;x=x+y;y=x-y;x=x-y;printf("x=%d y=%d",x,y);return 0;}​​​

(3) The third is to use bit operations, it doesn't matter if you don't understand!I will explain in detail later

​#includeint main(){int x=10,y=12;x=x^y;y=x^y;x=x^y;printf("x=%d y=%d",x,y);return 0;}​

4. Enter a character to determine whether it is an uppercase letter, if so, convert it to lowercase, otherwise do not convert it.Then output the resulting character.

​​#includeint main(){char ch; //Define a variable to accept the value read by the keyboardch=getchar();if(ch>='A'&&ch<='Z')// Determine whether it is uppercase{ch-=32;printf("%c",ch); //Output the converted a} //Remember that the ASCII code of uppercase A is 65, a is 97, and the difference between them is 32;elseprintf("%c",ch); // unchangedreturn 0;}​​

5. Judgment of odd and even numbers

​​#includeint main(){int num;scanf("%d",&num);if(num%2==0) //It must be even if divisible by 2printf("%d is even",num);else //then it's oddprintf("%d is odd",num);return 0;}​​

The five basic questions are updated here today. If you have any questions, please leave a message. The editor-in-chief has just entered the freshman year and is learning the advanced part of C language!Two hundred questions will be continuously updated later

If you find it useful, like, follow, and bookmark!

原网站

版权声明
本文为[Lamb said test!]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/223/202208110516455262.html