C Programming: If Else, For Loop

更新於 發佈於 閱讀時間約 5 分鐘


Exercise 1: Write a program that prompts the user to enter two integers, and then outputs all even numbers between those two integers (inclusive). If the first integer is greater than the second integer, swap the two integers.

Here are two sample inputs and outputs:

Sample 1:

Enter two integers: 3 8
4 6 8

Sample 2:

Enter two integers: 10 5
6 8 10


Exercise 2: Write a program that prompts the user to enter an integer, and then outputs the sum of all the odd numbers between 1 and that integer (inclusive). If the input is not a positive integer, output an error message.

Here are two sample inputs and outputs:

Sample 1:

Enter an integer: 10
25

Sample 2:

Enter an integer: -5
Error: Input must be a positive integer.


Exercise 3: Write a program that prompts the user to enter an integer, and then outputs the sum of all the prime numbers between 1 and that integer (inclusive). If the input is not a positive integer, output an error message.

Here are two sample inputs and outputs:

Sample 1:

Enter an integer: 10
17

Sample 2:

Enter an integer: -5
Error: Input must be a positive integer.






Code Example of Exercise 1:


#include <stdio.h>

int main() {
int num1, num2,i;

printf("Enter two integers: ");
scanf("%d %d", &num1,&num2);

if(num1 > num2){
int temp = num1;
num1 = num2;
num2 = temp;
}

for(i = num1;i <= num2;i++){
if(i % 2==0){
printf("%d ", i);
}
}

return0;
}



Code Example of Exercise 2:

#include <stdio.h>

int main() {
int num, i,sum = 0;

printf("Enter an integer: ");
scanf("%d",&num);

if(num <= 0){
printf("Error: Input must be a positive integer.\n");
return0;
}

for(i = 1;i <= num;i++){
if(i % 2==1){
sum += i;
}
}

printf("%d\n",sum);

return0;
}


Code Example of Exercise 3:


#include <stdio.h>

int main() {
int num, i,j,is_prime,sum = 0;

printf("Enter an integer: ");
scanf("%d",&num);

if(num <= 0){
printf("Error: Input must be a positive integer.\n");
return0;
}

for(i = 2;i <= num;i++){
is_prime = 1;
for(j = 2;j <= i / 2;j++){
if(i % j == 0){
is_prime = 0;
break;
}
}
if(is_prime == 1){
sum += i;
}
}

printf("%d\n",sum);

return0;
}



留言
avatar-img
留言分享你的想法!
avatar-img
邵的沙龍
3會員
5內容數
邵的沙龍的其他內容
2023/08/26
C program: For Loop
2023/08/26
C program: For Loop
2023/08/26
C programming : Switch
2023/08/26
C programming : Switch
2023/08/25
C Programming: if else Examples
2023/08/25
C Programming: if else Examples
看更多
你可能也想看
Thumbnail
「欸!這是在哪裡買的?求連結 🥺」 誰叫你太有品味,一發就讓大家跟著剁手手? 讓你回購再回購的生活好物,是時候該介紹出場了吧! 「開箱你的美好生活」現正召喚各路好物的開箱使者 🤩
Thumbnail
「欸!這是在哪裡買的?求連結 🥺」 誰叫你太有品味,一發就讓大家跟著剁手手? 讓你回購再回購的生活好物,是時候該介紹出場了吧! 「開箱你的美好生活」現正召喚各路好物的開箱使者 🤩
Thumbnail
介紹朋友新開的蝦皮選物店『10樓2選物店』,並分享方格子與蝦皮合作的分潤計畫,註冊流程簡單,0成本、無綁約,推薦給想增加收入的讀者。
Thumbnail
介紹朋友新開的蝦皮選物店『10樓2選物店』,並分享方格子與蝦皮合作的分潤計畫,註冊流程簡單,0成本、無綁約,推薦給想增加收入的讀者。
Thumbnail
這題的題目會給我們一個輸入整數,要求我們判斷這個整數是否可以用2^k 的形式來表達 (二的冪)?
Thumbnail
這題的題目會給我們一個輸入整數,要求我們判斷這個整數是否可以用2^k 的形式來表達 (二的冪)?
Thumbnail
這題的題目會給我們一個輸入整數,要求我們判斷這個整數是否可以用4^k 的形式來表達?
Thumbnail
這題的題目會給我們一個輸入整數,要求我們判斷這個整數是否可以用4^k 的形式來表達?
Thumbnail
題目:如果提供的數字在0-9之間,請以文字形式返回。輸入1、輸出 “One”
Thumbnail
題目:如果提供的數字在0-9之間,請以文字形式返回。輸入1、輸出 “One”
追蹤感興趣的內容從 Google News 追蹤更多 vocus 的最新精選內容追蹤 Google News