C Programming: For Loop

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

Exercise 1: Print Numbers from 1 to N

Description: Write a program that takes a positive integer N as input and prints all the numbers from 1 to N.

Input Example:

Enter a positive integer N: 5

Output Example:

Numbers from 1 to 5:
1
2
3
4
5


Exercise 2: Calculate Factorial

Description: Write a program that takes a positive integer N as input and calculates the factorial of N.

Input Example:

Enter a positive integer N: 4

Output Example:

Factorial of 4 is 24

Exercise 3: Print Multiplication Table

Description: Write a program that takes a positive integer N as input and prints the multiplication table for N.

Input Example:

Enter a positive integer N: 7

Output Example:

Multiplication table for 7:
7 x 1 = 7
7 x 2 = 14
7 x 3 = 21
...
7 x 10 = 70






Exercise 1 Complete Code:

#include <stdio.h>

int main() {
int N;

printf("Enter a positive integer N: ");
scanf("%d", &N);

printf("Numbers from 1 to %d:\n", N);
for (int i = 1; i <= N; ++i) {
printf("%d\n", i);
}

return 0;
}


Exercise 2 Complete Code:

#include <stdio.h>

int main() {
int N;
unsigned long long factorial = 1;

printf("Enter a positive integer N: ");
scanf("%d", &N);

for (int i = 1; i <= N; ++i) {
factorial *= i;
}

printf("Factorial of %d is %llu\n", N, factorial);

return 0;
}


Exercise 3 Complete Code:

#include <stdio.h>

int main() {
int N;

printf("Enter a positive integer N: ");
scanf("%d", &N);

printf("Multiplication table for %d:\n", N);
for (int i = 1; i <= 10; ++i) {
printf("%d x %d = %d\n", N, i, N * i);
}

return 0;
}


留言
avatar-img
留言分享你的想法!
avatar-img
邵的沙龍
3會員
5內容數
邵的沙龍的其他內容
2023/08/27
C Programming: If-Else, For Loop Exercises
2023/08/27
C Programming: If-Else, For Loop Exercises
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
TOMICA第一波推出吉伊卡哇聯名小車車的時候馬上就被搶購一空,一直很扼腕當時沒有趕緊入手。前陣子閒來無事逛蝦皮,突然發現幾家商場都又開始重新上架,價格也都回到正常水準,估計是官方又再補了一批貨,想都沒想就立刻下單! 同文也跟大家分享近期蝦皮購物紀錄、好用推薦、蝦皮分潤計畫的聯盟行銷!
Thumbnail
TOMICA第一波推出吉伊卡哇聯名小車車的時候馬上就被搶購一空,一直很扼腕當時沒有趕緊入手。前陣子閒來無事逛蝦皮,突然發現幾家商場都又開始重新上架,價格也都回到正常水準,估計是官方又再補了一批貨,想都沒想就立刻下單! 同文也跟大家分享近期蝦皮購物紀錄、好用推薦、蝦皮分潤計畫的聯盟行銷!
Thumbnail
每年4月、5月都是最多稅要繳的月份,當然大部份的人都是有機會繳到「綜合所得稅」,只是相當相當多人還不知道,原來繳給政府的稅!可以透過一些有活動的銀行信用卡或電子支付來繳,從繳費中賺一點點小確幸!就是賺個1%~2%大家也是很開心的,因為你們把沒回饋變成有回饋,就是用卡的最高境界 所得稅線上申報
Thumbnail
每年4月、5月都是最多稅要繳的月份,當然大部份的人都是有機會繳到「綜合所得稅」,只是相當相當多人還不知道,原來繳給政府的稅!可以透過一些有活動的銀行信用卡或電子支付來繳,從繳費中賺一點點小確幸!就是賺個1%~2%大家也是很開心的,因為你們把沒回饋變成有回饋,就是用卡的最高境界 所得稅線上申報
Thumbnail
這題的題目會給我們一個輸入整數,要求我們判斷這個整數是否可以用4^k 的形式來表達?
Thumbnail
這題的題目會給我們一個輸入整數,要求我們判斷這個整數是否可以用4^k 的形式來表達?
Thumbnail
題目會給定兩個參數,一個是底數x,一個是次方n。要求我們計算出x^n。 要求實作myPow(self, x: float, n: int) -> float 函數的內部邏輯。 也就是說,不可以呼叫程式語言內建計算指數的library
Thumbnail
題目會給定兩個參數,一個是底數x,一個是次方n。要求我們計算出x^n。 要求實作myPow(self, x: float, n: int) -> float 函數的內部邏輯。 也就是說,不可以呼叫程式語言內建計算指數的library
追蹤感興趣的內容從 Google News 追蹤更多 vocus 的最新精選內容追蹤 Google News