Grade-9-Introduction-to-C-Programming (1)
Grade-9-Introduction-to-C-Programming (1)
C
Programming
What is C?
· C is a general-purpose programming language that has been widely used since its
creation in the early 1970s.
· It is considered the foundation for many other programming languages such as C++,
Java, and Python.
· C is often used for system programming (like operating systems and hardware drivers),
as well as creating applications.
Why Learn C?
Learning C helps you understand how computers work at a deeper level.
C gives you more control over your computer’s hardware, memory, and processing power.
Many modern languages build upon the concepts and syntax of C, so
mastering it creates a strong foundation for learning other programming languages.
Basic Structure of a C Program
Every C program follows a specific structure, which includes:
#include <stdio.h> // This is a preprocessor directive.
int main() { // The main function, where the program execution begins.
printf("Hello, World!"); // Print statement to display output.
return 0; // Return 0 indicates that the program has successfully executed.
}
Explanation:
1. #include <stdio.h>: This line includes a standard library that allows us to use functions
like printf for output.
2. int main() {}: The main function is where the program starts executing. Every C
program must have a main function.
3. printf("Hello, World!\n");: This prints the text "Hello, World!" to the
screen. The \n represents a new line.
4. return 0;: This signals that the program ended successfully.
OUTPUT:
Hello, World!
Example 2. Simple Addition #include <stdio.h>
of Two Numbers int main() {
A program that takes two int num1, num2, sum;
printf("Enter two numbers: ");
numbers as input and outputs
scanf("%d %d", &num1, &num2);
their sum. sum = num1 + num2;
Explanation: printf("The sum is: %d\n", sum);
· The program asks for two return 0;
}
numbers using scanf, stores
them in num1 and num2,
and calculates their sum.
- printf displays the result.
Example 3. Check Even or Odd Number Example 4. Find the Largest of Three Numbers
A program to check whether a number entered A program to find the largest among three numbers
by the user is even or odd. entered by the user.
#include <stdio.h>
#include <stdio.h> int main() {
int main() { int numi, num2, num3;
int number; printf("Enter three numbers: ");
printf("Enter a number: "); scanf("%d %d %d", &num1, &num2, &num3);
scanf("%d", &number); if (num1 >= num2 && num1 >= num3) {
if (number % 2 == 0) { printf("%d is the largest number.\n", num1);
printf("%d is even.\n", number); } else if (num2 >= num1 && num2 >= num3)
} else { {
printf("%d is odd.\n", number); printf("%d is the largest number.\n", num2);
} } else {
return 0; printf("%d is the largest number.\n", num3);
} }
return 0;
}