C語言練習題:scanf 輸入(C language exercise: input with scanf )

Ping-Lun Liao
2 min readOct 4, 2020

練習一:大小寫轉換

讓使用者輸入小寫字母,程式轉成大寫字母。

讓使用者輸入大寫字母,程式轉成小寫字母。

Exercise 1: Case converter

Take a lowercase letter from the user and convert it to uppercase letter.

Take a uppercase letter from the user and convert it to lowercase letter.

練習一參考解法:
Exercise 1 solution:

#include <stdio.h>
#include <stdlib.h>
int main()
{
char letter, small, large;
printf("Please input a small letter:");
scanf(" %c", &small);
letter = small + ('A' - 'a');
printf("Large letter is %c\n", letter);
printf("Please input a large letter:");
scanf(" %c", &large);
letter = large + ('a' - 'A');
printf("Small letter is %c\n", letter);
return 0;
}

練習二:數學式
輸入4個整數a, b, c, d,求出(a + b) * (c + d)的結果。

Exercise 2: Equation

Get four integers a, b, c, d from the user. Calculate (a + b) * (c + d) and show the result.

練習二參考解法:
Exercise 2 solution:

#include <stdio.h>
#include <stdlib.h>
int main()
{
int a, b, c, d;
printf("Please input 4 integer: ");
scanf("%d%d%d%d", &a, &b, &c, &d);
printf("(a + b) * (c + d) = %d\n", (a + b) * (c + d) );
return 0;
}

練習三:華氏溫度
輸入攝氏溫度,程式計算出華氏溫度。公式為 F = C * (9/5) + 32。

Exercise 3: Fahrenheit
Enter a temperature in Celsius and convert it to Fahrenheit. Formula: F = C * (9/5) + 32.

練習三參考解法:
Exercise 3 solution:

#include <stdio.h>
#include <stdlib.h>
int main()
{
float celsius, fahrenheit;
printf("Please input a temperature in celsius:");
scanf("%f", &celsius);
fahrenheit = celsius * (9.0 / 5) + 32; printf("Celsius:%.2f\tFahrenheit:%.2f\n", celsius, fahrenheit);
return 0;
}

Originally published at https://yunlinsong.blogspot.com.

--

--