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
孩子寫功課時瞇眼?小心近視!這款喜光全光譜TIONE⁺光健康智慧檯燈,獲眼科院長推薦,網路好評不斷!全光譜LED、180cm大照明範圍、5段亮度及色溫調整、350度萬向旋轉,讓孩子學習更舒適、保護眼睛!
Thumbnail
孩子寫功課時瞇眼?小心近視!這款喜光全光譜TIONE⁺光健康智慧檯燈,獲眼科院長推薦,網路好評不斷!全光譜LED、180cm大照明範圍、5段亮度及色溫調整、350度萬向旋轉,讓孩子學習更舒適、保護眼睛!
Thumbnail
創作者營運專員/經理(Operations Specialist/Manager)將負責對平台成長及收入至關重要的 Partnership 夥伴創作者開發及營運。你將發揮對知識與內容變現、影響力變現的精準判斷力,找到你心中的潛力新星或有聲量的中大型創作者加入 vocus。
Thumbnail
創作者營運專員/經理(Operations Specialist/Manager)將負責對平台成長及收入至關重要的 Partnership 夥伴創作者開發及營運。你將發揮對知識與內容變現、影響力變現的精準判斷力,找到你心中的潛力新星或有聲量的中大型創作者加入 vocus。
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