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
邵的沙龍
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
這題的題目會給我們一個輸入整數,要求我們判斷這個整數是否可以用2^k 的形式來表達 (二的冪)?
Thumbnail
這題的題目會給我們一個輸入整數,要求我們判斷這個整數是否可以用2^k 的形式來表達 (二的冪)?
Thumbnail
本文分析導演巴里・柯斯基(Barrie Kosky)如何運用極簡的舞臺配置,將布萊希特(Bertolt Brecht)的「疏離效果」轉化為視覺奇觀與黑色幽默,探討《三便士歌劇》在當代劇場中的新詮釋,並藉由舞臺、燈光、服裝、音樂等多方面,分析該作如何在保留批判核心的同時,觸及觀眾的觀看位置與人性幽微。
Thumbnail
本文分析導演巴里・柯斯基(Barrie Kosky)如何運用極簡的舞臺配置,將布萊希特(Bertolt Brecht)的「疏離效果」轉化為視覺奇觀與黑色幽默,探討《三便士歌劇》在當代劇場中的新詮釋,並藉由舞臺、燈光、服裝、音樂等多方面,分析該作如何在保留批判核心的同時,觸及觀眾的觀看位置與人性幽微。
Thumbnail
題目:如果提供的數字在0-9之間,請以文字形式返回。輸入1、輸出 “One”
Thumbnail
題目:如果提供的數字在0-9之間,請以文字形式返回。輸入1、輸出 “One”
Thumbnail
這是一場修復文化與重建精神的儀式,觀眾不需要完全看懂《遊林驚夢:巧遇Hagay》,但你能感受心與土地團聚的渴望,也不急著在此處釐清或定義什麼,但你的在場感受,就是一條線索,關於如何找著自己的路徑、自己的聲音。
Thumbnail
這是一場修復文化與重建精神的儀式,觀眾不需要完全看懂《遊林驚夢:巧遇Hagay》,但你能感受心與土地團聚的渴望,也不急著在此處釐清或定義什麼,但你的在場感受,就是一條線索,關於如何找著自己的路徑、自己的聲音。
Thumbnail
《轉轉生》(Re:INCARNATION)為奈及利亞編舞家庫德斯.奧尼奎庫與 Q 舞團創作的當代舞蹈作品,結合拉各斯街頭節奏、Afrobeat/Afrobeats、以及約魯巴宇宙觀的非線性時間,建構出關於輪迴的「誕生—死亡—重生」儀式結構。本文將從約魯巴哲學概念出發,解析其去殖民的身體政治。
Thumbnail
《轉轉生》(Re:INCARNATION)為奈及利亞編舞家庫德斯.奧尼奎庫與 Q 舞團創作的當代舞蹈作品,結合拉各斯街頭節奏、Afrobeat/Afrobeats、以及約魯巴宇宙觀的非線性時間,建構出關於輪迴的「誕生—死亡—重生」儀式結構。本文將從約魯巴哲學概念出發,解析其去殖民的身體政治。
Thumbnail
這題的題目會給我們一個輸入整數,要求我們判斷這個整數是否可以用4^k 的形式來表達?
Thumbnail
這題的題目會給我們一個輸入整數,要求我們判斷這個整數是否可以用4^k 的形式來表達?
Thumbnail
5 月將於臺北表演藝術中心映演的「2026 北藝嚴選」《海妲・蓋柏樂》,由臺灣劇團「晃晃跨幅町」製作,本文將以從舞台符號、聲音與表演調度切入,討論海妲・蓋柏樂在父權社會結構下的困境,並結合榮格心理學與馮.法蘭茲對「阿尼姆斯」與「永恆少年」原型的分析,理解女人何以走向精神性的操控、毀滅與死亡。
Thumbnail
5 月將於臺北表演藝術中心映演的「2026 北藝嚴選」《海妲・蓋柏樂》,由臺灣劇團「晃晃跨幅町」製作,本文將以從舞台符號、聲音與表演調度切入,討論海妲・蓋柏樂在父權社會結構下的困境,並結合榮格心理學與馮.法蘭茲對「阿尼姆斯」與「永恆少年」原型的分析,理解女人何以走向精神性的操控、毀滅與死亡。
追蹤感興趣的內容從 Google News 追蹤更多 vocus 的最新精選內容追蹤 Google News