













Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!














Original practice notes for learning and revision.
1. How to Approach a C Program
Simple method
Example: Add two numbers
int a, b, sum; scanf("%d %d", &a;, &b;); sum = a + b; printf("%d", sum);
3. Program 2 — Add Two Numbers
Logic
Code
#include
int main() { int a, b, sum; printf("Enter two numbers: "); scanf("%d %d", &a;, &b;);
sum = a + b; printf("Sum = %d", sum);
return 0; }
Sample output
Enter two numbers: 10 20 Sum = 30
4. Program 3 — Basic Arithmetic
Code
#include
int main() { int a, b; scanf("%d %d", &a;, &b;);
printf("Addition = %d\n", a + b); printf("Subtraction = %d\n", a - b); printf("Multiplication = %d\n", a * b); printf("Remainder = %d\n", a % b);
return 0; }
6. Program 5 — Positive, Negative or Zero
Code
#include
int main() { int n; scanf("%d", &n;);
if (n > 0) printf("Positive"); else if (n < 0) printf("Negative"); else printf("Zero");
return 0; }
7. Program 6 — Pass or Fail
Code
#include
int main() { int marks; scanf("%d", &marks;);
if (marks >= 40) printf("Pass"); else printf("Fail");
return 0; }
Sample
Input: 67 Output: Pass
9. Program 8 — Largest of Three Numbers
Code
#include
int main() { int a, b, c, largest; scanf("%d %d %d", &a;, &b;, &c;);
largest = a;
if (b > largest) largest = b;
if (c > largest) largest = c;
printf("Largest = %d", largest);
return 0; }
10. Program 9 — Multiplication Table
Code
#include
int main() { int n; scanf("%d", &n;);
for (int i = 1; i <= 10; i++) { printf("%d x %d = %d\n", n, i, n * i); }
return 0; }
Sample output for 5
5 x 1 = 5 5 x 2 = 10 5 x 3 = 15 ... 5 x 10 = 50
12. Program 11 — Even Numbers from 1 to 20
Method 1 — Check each number
for (int i = 1; i <= 20; i++) { if (i % 2 == 0) printf("%d ", i); }
Method 2 — Start at 2
for (int i = 2; i <= 20; i += 2) { printf("%d ", i); }
13. Program 12 — Sum of First N Numbers
Code
#include
int main() { int n, sum = 0;
scanf("%d", &n;);
for (int i = 1; i <= n; i++) { sum += i; }
printf("Sum = %d", sum); return 0; }
Example
15. Program 14 — Factorial
Code
#include
int main() { int n; long long fact = 1;
scanf("%d", &n;);
for (int i = 1; i <= n; i++) { fact *= i; }
printf("Factorial = %lld", fact); return 0; }
16. Program 15 — break
#include
int main() { for (int i = 1; i <= 10; i++) { if (i == 6) break;
printf("%d ", i); } return 0; }
18. Program 17 — Nested Loops & Simple Pattern
Code
#include
int main() { for (int i = 1; i <= 5; i++) { for (int j = 1; j <= i; j++) { printf("* "); } printf("\n"); }
return 0; }
Output
19. Practice Set — Try Yourself
Challenge