๐ 0. Programming from Zero
Start here even if you have never written a program. You will take a small problem, identify its inputs and expected output, write the solution steps, and turn those steps into a flowchart, pseudocode, and a C program.
0.1 What is a Problem?
A problem is a situation where we know some information, need a particular result, and must find a method to reach that result.
๐ Example: Student Marks
You have marks of five students and want to find the highest mark.
๐ Example: Shop Bill
You know the price and quantity of items and want to calculate the total bill.
๐ Example: Bus Capacity
You know the number of passengers and seats and want to know whether more passengers can enter.
0.2 How Do We Normally Solve a Problem?
For a small problem, you may find the answer in your head. To program the solution, write down the comparisons or calculations you used so that the same method works with other inputs.
๐ง Human Thinking
Problem: Find the highest mark among 72, 85, 61, 94 and 78.
- Look at the first mark.
- Compare it with the next mark.
- Keep the larger one.
- Repeat until all marks are checked.
- Answer: 94.
๐ป Computer Thinking
The computer needs the same idea written as exact instructions:
- Read all marks.
- Assume the first mark is the maximum.
- Compare each remaining mark with the maximum.
- If a larger mark is found, update the maximum.
- Print the final maximum.
0.3 What is Programming?
Programming means writing precise instructions that a computer can follow to perform a task. It also includes checking whether those instructions give the required result.
๐ Program
A program is the actual set of instructions written in a programming language.
๐ฃ๏ธ Programming Language
A programming language provides rules and vocabulary for writing instructions that can be translated into operations a computer can execute.
0.4 Why Do We Need a Programming Language?
You can describe an idea in English or Telugu, but a C program must follow Cโs rules. A compiler translates that source into a form that can be linked and run on the target system.
0.5 The Basic InputโProcessโOutput Idea
Many programming problems become easier when you first identify three things: Input, Process, and Output.
0.6 What is an Algorithm?
An algorithm is a clear, finite, step-by-step method for solving a problem. It describes what to do before we worry about the exact syntax of a programming language.
โ Everyday Algorithm โ Make Tea
๐งฎ Programming Algorithm โ Add Two Numbers
0.7 Characteristics of a Good Algorithm
โ Clear
A reader should know exactly what to do at each step. For example, say compare A with B rather than compare the numbers as needed.
โ Ordered
Steps should appear in the correct sequence.
โ Finite
For every permitted input, the method should reach its stopping step after a finite number of operations.
โ Correct
It should give the required answer for every permitted input, including boundary values, not just the first sample.
โ Practical
Each step should be possible to perform.
โ Testable
You should be able to check the steps using sample values.
0.8 What is a Flowchart?
A flowchart is a graphical representation of an algorithm. Different shapes represent different types of steps, and arrows show the direction of execution.
Beginning or end of the logic
Read input or display output
Calculation or processing step
A condition with branches such as Yes / No
Direction in which control moves
0.9 What is Pseudocode?
Pseudocode is an informal way to write program logic using simple, programming-like English. It is meant for people to understand and plan; it is not compiled or executed by the computer.
Common Pseudocode Words
BEGIN/ENDREADPRINTIF/ELSEFOR/WHILESETor assignment such asSUM โ A + B
Important
Pseudocode does not have one universal syntax. Keep your keywords and indentation consistent, and include enough detail for another student to follow the method.
0.10 From Problem to C Program โ Add Two Numbers
๐งฎ Add Two Numbers
1. Problem Statement
Read two integers and display their sum.
2. Identify Input, Process and Output
3. Algorithm
4. Flowchart
5. Pseudocode and C Program
BEGIN
READ A, B
SUM โ A + B
PRINT SUM
END
#include <stdio.h>
int main()
{
int a, b, sum;
scanf("%d %d", &a, &b);
sum = a + b;
printf("%d", sum);
return 0;
}
6. Dry Run
| Step | A | B | SUM | Output |
|---|---|---|---|---|
| Read input | 10 | 20 | โ | โ |
| SUM = A + B | 10 | 20 | 30 | โ |
| Print SUM | 10 | 20 | 30 | 30 |
0.11 Decision Making โ Certificate Eligibility
๐ Is the Student Eligible?
For this example, the rule is: a mark of 40 or more is Eligible; a lower mark is Not Eligible. This is a sample problem rule, so test 39, 40, and 41 to check the boundary.
Algorithm
Flowchart Idea
Pseudocode and C Program
BEGIN
READ MARKS
IF MARKS >= 40
PRINT "Eligible"
ELSE
PRINT "Not Eligible"
END IF
END
#include <stdio.h>
int main()
{
int marks;
scanf("%d", &marks);
if(marks >= 40)
printf("Eligible");
else
printf("Not Eligible");
return 0;
}
0.12 Repetition โ Print Numbers from 1 to 5
Suppose you need to print the numbers 1 to 5. You could write five separate print statements, but the action is repetitive. Programming gives us a better idea: repeat the same type of instruction using a loop.
Without Thinking About Repetition
print 1
print 2
print 3
print 4
print 5
Better Logical Idea
SET i โ 1
WHILE i โค 5
PRINT i
i โ i + 1
END WHILE
Algorithm
Pseudocode and C Program
BEGIN
FOR i โ 1 TO 5
PRINT i
END FOR
END
#include <stdio.h>
int main()
{
for(int i = 1; i <= 5; i++)
printf("%d ", i);
return 0;
}
0.13 Algorithm vs Flowchart vs Pseudocode vs Program
| Representation | Main Purpose | Can Computer Execute It Directly? |
|---|---|---|
| Problem | States what must be solved. | No |
| Algorithm | Describes the solution as ordered steps. | No |
| Flowchart | Shows the algorithm visually using symbols and arrows. | No |
| Pseudocode | Expresses the logic in simple programming-like language. | No |
| C Program | Implements the solution using valid C syntax. | After translation/compilation |
0.14 How to Think Before Writing Code
Before typing C statements, solve one small example on paper. Then answer these questions to check that the input, result, and steps are clear.
0.15 What is a Dry Run or Program Tracing?
A dry run means executing the logic manually using sample values. You track how variables change one step at a time before or while checking the program.
Example
A = 10
B = 20
SUM = A + B
SUM = 10 + 20
SUM = 30
Output = 30
For a loop, record the variable before the body and after the update. For a decision, record the condition result and the selected branch.
0.16 What if the Program Does Not Work?
๐ง Syntax Error
The code does not follow the rules of C, such as a missing semicolon or incorrect statement.
โ ๏ธ Runtime Problem
An operation fails while the program runs, such as opening a missing file. Some invalid C operations instead have undefined behavior, so they are not guaranteed to produce a visible error message.
๐ง Logic Error
The program runs, but the answer is wrong because the solution steps are wrong.
Finding and correcting such problems is called debugging. Errors are a normal part of programming practice.
0.17 A Simple Problem-Solving Strategy
0.18 Common Beginner Mistakes
First understand input, output and logic.
Remember the idea and steps, not only the final code.
If you use an example, execute it manually and understand every important step.
Examples often reveal what the problem really expects.
Break a large problem into smaller subproblems.
Compile, test, inspect the error, correct it, and try again.
Ask, โWhat steps are required to solve this problem?โ
0.19 Mini Problem-Solving Practice
For each problem, first identify the Input, Output, and the main logic. Try it yourself before opening the answer.
Output: Greater value
Logic: If A > B, output A; otherwise output B.
Output: Even or Odd
Logic: If the remainder when N is divided by 2 is 0, it is Even; otherwise it is Odd.
Output: Average
Logic: SUM = M1 + M2 + M3, then AVERAGE = SUM / 3.
Output: BILL
Logic: BILL = PRICE ร QUANTITY.
Output: 1 through 10
Logic: Start at 1, print the current number, increase it by 1, and repeat until 10.
Output: Largest value
Logic: Start the current largest value with A. Compare B with it and update if needed, then do the same for C. This handles equal and negative values without assuming zero is the largest.
0.20 Are You Ready to Start C?
You are ready for the next page when these ideas feel understandable. You do not need to be perfect yet.
0.21 How to Test a Program with Boundary Cases
Choose tests with expected answers before running the code. The array below tests 7, 0, and -3 in a positive-number function. Testing a non-numeric keyboard entry is a separate input-reading check; this function takes an int, not text.
Complete C program:
#include <stdio.h>
int isPositive(int n)
{
return n > 0;
}
int main(void)
{
int test[] = {7, 0, -3};
for (int i = 0; i < 3; ++i)
printf("%d: %s\n", test[i],
isPositive(test[i]) ? "positive" : "not positive");
return 0;
}
Expected output:
7: positive
0: not positive
-3: not positive
Step-by-step explanation:
- The test array contains one positive number, zero and a negative number.
- The loop checks all three against the same function.
- Comparing actual output with the expected output detects boundary mistakes.
0.22 Key Takeaway
Problem โ Understand โ Input/Output โ Manual Solution โ Algorithm โ Flowchart/Pseudocode โ C Program โ Test โ Debug โ Improve.
In the next topic, you will begin learning the C programming language itself.