Lecture 4 June
Lecture 28 May
Notes
Introduction to C Programming – Class Notes
Lesson Overview:
In this session, we covered the fundamentals of C programming, focusing on writing basic programs using:
- Variables
- Input/output functions (
printf,scanf) - Format specifiers like
%d - Conditional statements (
if,else,else if)
1. Variables in C
- A variable is a container for storing data values.
- In C, you must declare the variable type before using it.
int age; // Declaration
age = 25; // Assignment
int score = 90; // Declaration + Assignment
2. Output with printf()
- Used to display text or values on the screen.
Syntax:
printf("Text to display");
int age = 18;
printf("Age: %d", age); // %d is replaced by the value of age
3. Input with scanf()
- Used to take user input from the console.
scanf("%d", &age); // Reads an integer into variable 'age'
4. Conditional Statements (if, else if, else)
Used to perform different actions based on different conditions.
if (condition) {
// code if true
} else if (another condition) {
// code if first is false but this is true
} else {
// code if all conditions are false
}
Assignment
Instructions:
You are required to write a C program that performs the following tasks:
- Prompt the user to enter the number of students.
- For each student, ask for their scores (you may assume one score per student or modify to include multiple subjects as per your preference).
- Calculate the total and average score.
- Assign a grade based on the average using
if-elsestatements. - Display the results for each student:
- Student number or name (if applicable)
- Score
- Average
- Grade
Submit your code in the ‘submit code’ block below.