C Programming
Student Notes — CSE 1282
Loops · Decisions · Arrays · Patterns · Recursion
■ Basics & I/O Variables, printf, scanf
■ Decisions if / else / nested if
■ Loops for / while / do-while
■ Arrays 1D arrays & examples
■ Recurrence Factorial, Fibonacci
■ Patterns Nested loops & shapes
Dept. of EEE · Varendra University
C Programming Notes · Page 1
Chapter 1 — Hello World & Basic Structure
Every C program starts with a main() function. Think of it as the 'entry door' of your program. When
you run the program, the computer walks through this door first.
■ Always include <b>#include<stdio.h></b> at the top — it gives you printf and scanf.
Anatomy of a C Program
1 #include<stdio.h> // 1. Import the 'stdio' toolbox
2
3 main() // 2. Entry point of the program
4 { // 3. Opening brace — block starts
5 printf("Hello!"); // 4. Print text to screen
6 return 0; // 5. Tell OS: program finished OK
7 } // 6. Closing brace — block ends
Variables & Data Types
A variable is a named box in memory that holds a value. You must declare its type before using it.
Type Keyword Example Memory
Whole numbers int int age = 20; 4 bytes
Decimal numbers float float gpa = 3.8; 4 bytes
Single character char char grade = 'A'; 1 byte
■■ Variable names are case-sensitive! 'Score' and 'score' are different.
Reading Input from User
C Programming Notes · Page 2
1 #include<stdio.h>
2 main()
3 {
4 int a, b, c;
5 printf("Enter two numbers: ");
6 scanf("%d %d", &a, &b); // & means 'address of'
7 c = a + b;
8 printf("Sum = %d", c);
9 return 0;
10 }
%d = integer | %f = float | %c = character | %s = string
C Programming Notes · Page 3
Chapter 2 — Making Decisions (if / else)
Decision statements let your program choose different paths based on conditions — just like
choosing which road to take at a junction.
The if / else Chain
Condition TRUE? → Run this block if (num > 0) { ... }
No — try next → else if block else if (num < 0) { ... }
Still no → default else else { ... }
Example — Grade Checker
1 #include<stdio.h>
2 int main(void)
3 {
4 int num;
5 printf("Enter your mark: ");
6 scanf("%d", &num);
7
8 if (num >= 80) // 80 or above
9 printf("A+ Grade");
10 else if (num >= 60) // 60 to 79
11 printf("B Grade");
12 else if (num > 40) // 41 to 59
13 printf("D Grade");
14 else // 40 or below
15 printf("Failed");
16 }
■■ Use == for comparison, = for assignment. if (a == b) not if (a = b)!
Comparison Operators Quick Reference
Operator Meaning Example
== Equal to if (a == b)
!= Not equal to if (a != b)
C Programming Notes · Page 4
> Greater than if (a > b)
< Less than if (a < b)
>= Greater or equal if (a >= b)
<= Less or equal if (a <= b)
C Programming Notes · Page 5
Chapter 3 — Loops (The Repeat Machine ■)
A loop tells the computer to keep repeating a block of code until a condition is met. Without loops,
you would have to write the same line 100 times for 100 iterations!
The Three Types of Loop
Loop When to Use Checks condition
for You know exact number of repeats Before each repeat
while Repeat while something is true Before each repeat
do-while Run at least once, then check AFTER each repeat
■ The for Loop — The Counter Loop
The for loop packs three things into one line: start, condition, and step.
for ( initialise ; condition ; update ) { body }
Part What it does Example
initialise Set counter before loop starts i = 1
condition Keep looping while this is TRUE i <= 5
update Change counter after each pass i++ (i = i + 1)
Example — Sum of Natural Numbers
1 #include<stdio.h>
2 int main()
3 {
4 int n, i, sum = 0;
5 printf("Enter n: ");
6 scanf("%d", &n);
7
8 for (i = 1; i <= n; i++) // i goes 1,2,3,...,n
9 {
10 sum = sum + i; // add i to running total
11 }
12 printf("Sum = %d", sum);
13 return 0;
14 }
C Programming Notes · Page 6
■ Trace through with n=3: i=1→sum=1, i=2→sum=3, i=3→sum=6. Done!
Step-by-Step Trace (n = 5)
Step (i) i <= 5? sum = sum + i New sum
1 ■ Yes 0+1 1
2 ■ Yes 1+2 3
3 ■ Yes 3+3 6
4 ■ Yes 6+4 10
5 ■ Yes 10 + 5 15
6 ■ No — STOP — 15 (final)
C Programming Notes · Page 7
■ The while Loop
The while loop checks the condition first. If it is false from the start, the body never runs.
while ( condition ) { body }
1 int i = 1;
2 while (i <= 5) // check before entering
3 {
4 printf("%d ", i);
5 i++; // must update manually!
6 }
7 // Prints: 1 2 3 4 5
■■ Always update the counter inside a while loop — forgetting causes an INFINITE LOOP!
■ The do-while Loop
The do-while loop runs the body first, then checks. This guarantees at least one execution — useful
for menus!
do { body } while ( condition );
1 int i = 1;
2 do
3 {
4 printf("%d ", i);
5 i++;
6 } while (i <= 5); // check AFTER each run
7 // Prints: 1 2 3 4 5
Side-by-Side Comparison
Feature for while do-while
Condition check Before Before After
Min. executions 0 0 1 (always)
Counter in header ■ Yes ■ No ■ No
Best for... Known count Unknown count Menus/login
C Programming Notes · Page 8
Chapter 4 — Recurrence & Famous Sequences ■
A recurrence formula defines each term using the previous terms. Loops are the perfect tool for
computing them step by step.
■ Factorial — n!
The factorial of a number n is the product of all positive integers up to n.
n! = n × (n-1) × (n-2) × ... × 2 × 1
Special case: 0! = 1
Factorial Recurrence Formula
fact(n) = n × fact(n-1)
n Calculation Result
0! 1 (by definition) 1
1! 1×1 1
2! 2×1 2
3! 3×2×1 6
4! 4×3×2×1 24
5! 5×4×3×2×1 120
6! 6×5×4×3×2×1 720
1 #include<stdio.h>
2 int main()
3 {
4 int c, n, f = 1; // f stores the factorial
5 printf("Enter number: ");
6 scanf("%d", &n);
7
8 for (c = 1; c <= n; c++)
9 f = f * c; // multiply f by each c
10
11 printf("Factorial = %d", f);
12 return 0;
13 }
Trace for n = 4
c f=f×c f value
C Programming Notes · Page 9
1 1×1 1
2 1×2 2
3 2×3 6
4 6×4 24 ■
C Programming Notes · Page 10
■ Fibonacci Series
In the Fibonacci sequence, each number is the sum of the two numbers before it. It appears
everywhere in nature — flower petals, spirals, even galaxies!
F(0) = 0, F(1) = 1
F(n) = F(n-1) + F(n-2) for n ≥ 2
F(0) F(1) F(2) F(3) F(4) F(5) F(6) F(7)
0 1 1 2 3 5 8 13
How it grows: 0, 1, 0+1=1, 1+1=2, 1+2=3, 2+3=5, 3+5=8...
Step-by-Step Trace (First 6 Terms)
Step first second next = first+second
Start 0 1 —
c=2 0 1 0+1 = 1
c=3 1 1 1+1 = 2
c=4 1 2 1+2 = 3
c=5 2 3 2+3 = 5
c=6 3 5 3+5 = 8
C Programming Notes · Page 11
1 #include<stdio.h>
2 int main()
3 {
4 int n, first=0, second=1, next, c;
5 printf("How many terms? ");
6 scanf("%d", &n);
7
8 for (c = 0; c < n; c++)
9 {
10 if (c <= 1) // first two terms are special
11 next = c;
12 else // recurrence formula here
13 {
14 next = first + second;
15 first = second; // shift window forward
16 second = next;
17 }
18 printf("%d\n", next);
19 }
20 return 0;
21 }
■ The key idea: always keep track of the LAST TWO numbers to compute the next one.
C Programming Notes · Page 12
Chapter 5 — Arrays ■
An array is a collection of variables of the same type, stored in consecutive memory locations.
Think of it as a row of labelled boxes.
int age[5]; // declares 5 integer boxes: age[0] ... age[4]
age[0] age[1] age[2] age[3] age[4]
? ? ? ? ?
Index starts at 0! First element = age[0], last = age[4]
Finding the Largest Element
1 #include<stdio.h>
2 int main()
3 {
4 int i, n;
5 float arr[100];
6 printf("How many elements? ");
7 scanf("%d", &n);
8
9 for (i = 0; i < n; i++) // read each element
10 scanf("%f", &arr[i]);
11
12 for (i = 1; i < n; i++) // find largest
13 if (arr[0] < arr[i])
14 arr[0] = arr[i]; // promote new largest
15
16 printf("Largest = %.2f", arr[0]);
17 return 0;
18 }
■ We treat arr[0] as 'current champion'. Any element bigger becomes the new champion!
C Programming Notes · Page 13
Chapter 6 — Patterns & Nested Loops ■
Patterns use nested loops — a loop inside another loop. The outer loop controls rows, the inner
loop controls columns.
How Nested Loops Work
Outer loop runs R times → Inner loop runs C times each → Total = R
× C
Example — Pyramid of Stars
1 #include<stdio.h>
2 int main()
3 {
4 int row, c, n;
5 printf("Rows: ");
6 scanf("%d", &n);
7
8 for (row=1; row<=n; row++) // outer: each row
9 {
10 for (c=1; c<=n-row; c++) // inner: spaces
11 printf(" ");
12 for (c=1; c<=2*row-1; c++) // inner: stars
13 printf("*");
14 printf("\n"); // new line after each row
15 }
16 return 0;
17 }
Pattern Output (n = 5)
Row Output Stars = 2×row - 1
Row 1 * 1 star
Row 2 *** 3 stars
Row 3 ***** 5 stars
Row 4 ******* 7 stars
Row 5 ********* 9 stars
■ Formula: Stars in row r = 2r - 1 and Spaces before = n - r
C Programming Notes · Page 14
Even / Odd Checker
1 #include<stdio.h>
2 int main()
3 {
4 int num;
5 printf("Enter number: ");
6 scanf("%d", &num);
7 if (num % 2 == 0) // % gives remainder
8 printf("%d is EVEN", num);
9 else
10 printf("%d is ODD", num);
11 return 0;
12 }
% is the modulo operator — it returns the remainder after division.
C Programming Notes · Page 15
Chapter 7 — Leap Year & Decimal to Binary ■■
Leap Year Logic
A leap year has 366 days. The rule uses three conditions checked in order:
Rule Condition Is Leap?
1 Divisible by 400 ■ YES
2 Divisible by 100 (not 400) ■ NO
3 Divisible by 4 (not 100) ■ YES
4 None of the above ■ NO
1 if (year%400 == 0)
2 printf("Leap year");
3 else if (year%100 == 0)
4 printf("NOT a leap year");
5 else if (year%4 == 0)
6 printf("Leap year");
7 else
8 printf("NOT a leap year");
Decimal → Binary Conversion
Every decimal (base-10) number can be represented in binary (base-2) using only 0s and 1s. The
program uses bit-shifting to extract each bit from position 31 down to 0.
100 in binary = 00000000000000000000000001100100
C Programming Notes · Page 16
1 #include<stdio.h>
2 int main()
3 {
4 int n, c, k;
5 scanf("%d", &n);
6 for (c = 31; c >= 0; c--) // examine each bit
7 {
8 k = n >> c; // right-shift by c positions
9 if (k & 1) // check last bit
10 printf("1");
11 else
12 printf("0");
13 }
14 return 0;
15 }
■■ n >> c shifts the bits of n right by c places. & 1 masks all but the last bit.
C Programming Notes · Page 17
Quick Reference Cheat Sheet ■
All Loop Syntax at a Glance
1 // FOR LOOP
2 for (i = 0; i < n; i++) { ... }
3
4 // WHILE LOOP
5 while (condition) { ... }
6
7 // DO-WHILE LOOP
8 do { ... } while (condition);
9
10 // SUM of 1..n: sum = n*(n+1)/2
11 // FACTORIAL n!: for(c=1;c<=n;c++) f=f*c;
12 // FIBONACCI: next = first + second;
Key Formulas
Concept Formula
Sum 1 to n n × (n+1) / 2
n! Recurrence fact(n) = n × fact(n-1), fact(0)=1
Fibonacci F(n) = F(n-1) + F(n-2), F(0)=0, F(1)=1
Pyramid stars Stars in row r = 2r - 1
Spaces in row Spaces = n - r
Even check n % 2 == 0
Leap year year%400==0 OR (year%4==0 AND year%100!=0)
Common Mistakes to Avoid
■ Using = instead of == in conditions
■ Forgetting & in scanf → scanf("%d", &n;)
■ Array index out of bounds (accessing arr[n] when size is n)
■ Infinite loop: forgetting to update counter in while loop
■ 0! is 1, not 0 — always initialise factorial variable as 1
■ Fibonacci: the first two terms (F(0)=0, F(1)=1) are special cases
C Programming Notes · Page 18
Happy Coding! ■ — CSE 1282, Dept. of EEE, Varendra University
C Programming Notes · Page 19