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;
}



3會員
5Content count
留言0
查看全部
發表第一個留言支持創作者!
你可能也想看
Google News 追蹤
Thumbnail
本專欄將提供給您最新的市場資訊、產業研究、交易心法、優質公司介紹,以上內容並非個股分析,還請各位依據自身狀況作出交易決策。歡迎訂閱支持我,獲得相關內容,也祝您的投資之路順遂! 每年 $990 訂閱方案👉 https://reurl.cc/VNYVxZ 每月 $99 訂閱方案👉https://re
Thumbnail
C# 迴圈(C# 教學) – for loop 可以說是程序編寫的基本功. 運用LOOPING可以減少程式碼, 只需透過for內的BLOCK, 就可把程式碼重用, 避免要不停把程序碼重覆. 以下我會介紹迴圈的結構, 運行方式, 如何離開迴圈, 以及迴圈的種類. 2) 迴圈的種類 3) 迴圈的用法
Thumbnail
條件句在編碼上隨處可見. 程式編碼就是一堆分別判斷情況, 再提出行動指令的邏輯. if為邏輯的根本. 就好像一個人去計劃做一件事或對一件事作預備, 不可能無假設, 沒有如果這字眼, 不用if這字眼, 就無法把情況判斷, 分類, 也無沒寫程式. 總而言之, 學條件句為最基本而必要. 其他相關:
Thumbnail
只要你肯聯繫我,我就願意再勇敢一次, 重道覆轍也沒關係, 就算你是陷阱、是無底深淵、是深不見底的大海, 只要是你,我願意跳下去, 願意沈溺於一片以你為名的深海。 我站在風口,整個世界都是你的氣息。 我將滿懷愛意的等待下個盛夏的風, 和遠方的你。 你知道 C-14 的半衰期有多久嗎? 它不及我在冥冥之
Thumbnail
Ironman伴讀小書僮又來了! 今天要念英文繪本給Piupiu和Piu 哥聽。 防疫期間在家育兒真的是很挑戰英雄,還是拯救地球簡單一點~ 跳一下來到繪本介紹, A Chair for My Mother -by Vera B. Williams 這本就是勸生女兒的繪本呀!故事中的小女孩孝順又貼心,
Thumbnail
int main()、註解//、include 、命名空間、using namespace
string SrcStr = "admin"; string[] UserNameCvt = SrcStr.Split(','); Roles.AddUsersToRole(UserNameCvt, "Administrator"); 註記: 網路上較少此種例子,多為string[] to S
Thumbnail
可能包含敏感內容
如果你有關注台灣媒體,就會知道安心亞是全台首位公開穿C字褲的女藝人,那時才2009年,跟現在相比之下算是民風更為保守的年代,一方面真的很佩服她的勇氣,另一方面又會覺得:穿C字褲有什麼好處呢?其實C字褲並不是大眾印象中跟情色畫上等號的東西,反而像是丁字褲的升級版而已,都能拿來做隱藏痕跡使用。希望這篇文
Thumbnail
C世代(Connected Generation, 連接世代)是美容保養品-網購的主力──了解他們的特質,才能讓保養品網路行銷大賣... 隨著智慧裝置普及,全球「網路購物」市場高速增長(根據eMarketer統計:全球零售電子商務銷售額,2017年為1.357萬億美元,預估至2021年將達3.554
Thumbnail
本專欄將提供給您最新的市場資訊、產業研究、交易心法、優質公司介紹,以上內容並非個股分析,還請各位依據自身狀況作出交易決策。歡迎訂閱支持我,獲得相關內容,也祝您的投資之路順遂! 每年 $990 訂閱方案👉 https://reurl.cc/VNYVxZ 每月 $99 訂閱方案👉https://re
Thumbnail
C# 迴圈(C# 教學) – for loop 可以說是程序編寫的基本功. 運用LOOPING可以減少程式碼, 只需透過for內的BLOCK, 就可把程式碼重用, 避免要不停把程序碼重覆. 以下我會介紹迴圈的結構, 運行方式, 如何離開迴圈, 以及迴圈的種類. 2) 迴圈的種類 3) 迴圈的用法
Thumbnail
條件句在編碼上隨處可見. 程式編碼就是一堆分別判斷情況, 再提出行動指令的邏輯. if為邏輯的根本. 就好像一個人去計劃做一件事或對一件事作預備, 不可能無假設, 沒有如果這字眼, 不用if這字眼, 就無法把情況判斷, 分類, 也無沒寫程式. 總而言之, 學條件句為最基本而必要. 其他相關:
Thumbnail
只要你肯聯繫我,我就願意再勇敢一次, 重道覆轍也沒關係, 就算你是陷阱、是無底深淵、是深不見底的大海, 只要是你,我願意跳下去, 願意沈溺於一片以你為名的深海。 我站在風口,整個世界都是你的氣息。 我將滿懷愛意的等待下個盛夏的風, 和遠方的你。 你知道 C-14 的半衰期有多久嗎? 它不及我在冥冥之
Thumbnail
Ironman伴讀小書僮又來了! 今天要念英文繪本給Piupiu和Piu 哥聽。 防疫期間在家育兒真的是很挑戰英雄,還是拯救地球簡單一點~ 跳一下來到繪本介紹, A Chair for My Mother -by Vera B. Williams 這本就是勸生女兒的繪本呀!故事中的小女孩孝順又貼心,
Thumbnail
int main()、註解//、include 、命名空間、using namespace
string SrcStr = "admin"; string[] UserNameCvt = SrcStr.Split(','); Roles.AddUsersToRole(UserNameCvt, "Administrator"); 註記: 網路上較少此種例子,多為string[] to S
Thumbnail
可能包含敏感內容
如果你有關注台灣媒體,就會知道安心亞是全台首位公開穿C字褲的女藝人,那時才2009年,跟現在相比之下算是民風更為保守的年代,一方面真的很佩服她的勇氣,另一方面又會覺得:穿C字褲有什麼好處呢?其實C字褲並不是大眾印象中跟情色畫上等號的東西,反而像是丁字褲的升級版而已,都能拿來做隱藏痕跡使用。希望這篇文
Thumbnail
C世代(Connected Generation, 連接世代)是美容保養品-網購的主力──了解他們的特質,才能讓保養品網路行銷大賣... 隨著智慧裝置普及,全球「網路購物」市場高速增長(根據eMarketer統計:全球零售電子商務銷售額,2017年為1.357萬億美元,預估至2021年將達3.554