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
覺得黏在額頭上的"條碼瀏海"很阿雜嗎?日本熱銷的「KOIZUMI迷你瀏海梳」,不僅小巧便攜,更能快速加熱造型,無論是齊瀏海、空氣瀏海還是韓系碎蓋髮,都能輕鬆打理!瀏海順了,一整天心情就好了!
Thumbnail
覺得黏在額頭上的"條碼瀏海"很阿雜嗎?日本熱銷的「KOIZUMI迷你瀏海梳」,不僅小巧便攜,更能快速加熱造型,無論是齊瀏海、空氣瀏海還是韓系碎蓋髮,都能輕鬆打理!瀏海順了,一整天心情就好了!
Thumbnail
走完朝聖之路和TMB後,我發現真正能撐住長時間健行的,不只是腳力,而是那些讓生活更舒服的小物。這篇整理了我在TMB實測後覺得超好用的三樣登山神器——防水襪、肥皂袋、速乾毛巾,每一樣都讓旅程更輕鬆!
Thumbnail
走完朝聖之路和TMB後,我發現真正能撐住長時間健行的,不只是腳力,而是那些讓生活更舒服的小物。這篇整理了我在TMB實測後覺得超好用的三樣登山神器——防水襪、肥皂袋、速乾毛巾,每一樣都讓旅程更輕鬆!
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