C Language
- Installation of C – Compiler
- Familiarization with IDE of C – Compiler
- Write some programmes using printf ( ), Scanf ( ), Format, Specifier, Escape Sequences, getch()
- Write programmes like:
- Solving arithmetic problems to calculate average, percentage, and grades etc
- Calculating area, volumes, parameters of some geometric shapes
- Comparing numbers
- Solving quadratic Equation
- Finding factorial of given numbers
- Finding Table of a given number
- Generating / Summing of simple series (even/odd)
Experiment: Introduction to C Programming
In this experiment, we will familiarize ourselves with C programming language by installing a C compiler, exploring an Integrated Development Environment (IDE), and writing various programs covering basic concepts like input/output, arithmetic operations, geometric calculations, and more.
1. Installation of C Compiler
To begin with, you need to install a C compiler such as GCC (GNU Compiler Collection). Follow the installation instructions specific to your operating system.
2. Familiarization with IDE of C Compiler
After installing the compiler, familiarize yourself with the IDE (Integrated Development Environment) provided with the compiler. IDEs like Code::Blocks, Dev-C++, or Visual Studio Code are popular choices.
3. Writing Basic Programs
Start writing simple programs using printf(), scanf(), format specifiers, escape sequences, and getch() function for input/output operations.
4. Writing Programs
- Arithmetic Problems: Write programs to calculate average, percentage, and grades.
- Geometric Calculations: Calculate area, volume, and parameters of geometric shapes.
- Comparing Numbers: Compare two numbers and determine their relationship.
- Solving Quadratic Equation: Implement a program to solve quadratic equations.
- Factorial Calculation: Write a program to find the factorial of a given number.
- Table Generation: Generate the multiplication table of a given number.
- Simple Series: Generate or sum simple series like even or odd numbers.
Experiment Procedure
- Install the C compiler on your system.
- Explore the IDE provided with the compiler.
- Create a new project or file in the IDE.
- Write and save the C programs covering the mentioned topics.
- Compile the programs to check for any errors.
- Run the programs and verify the output.
- Make necessary corrections if required.
Arithmetic Problems
Calculate Average
#include <stdio.h>
int main() {
int n, sum = 0;
float average;
printf("Enter the number of elements: ");
scanf("%d", &n);
int arr[n];
printf("Enter %d numbers: ", n);
for(int i = 0; i < n; ++i) {
scanf("%d", &arr[i]);
sum += arr[i];
}
average = sum / (float)n;
printf("Average = %.2f\n", average);
return 0;
}
Geometric Calculations
// C program to calculate area of a circle
#include <stdio.h>
int main() {
float radius, area;
printf("Enter the radius of circle: ");
scanf("%f", &radius);
area = 3.14159 * radius * radius;
printf("Area of circle = %.2f\n", area);
return 0;
}
Comparing Numbers
// C program to compare two numbers
#include <stdio.h>
int main() {
int num1, num2;
printf("Enter two numbers: ");
scanf("%d %d", &num1, &num2);
if(num1 > num2)
printf("%d is greater than %d\n", num1, num2);
else if(num1 < num2)
printf("%d is less than %d\n", num1, num2);
else
printf("Both numbers are equal\n");
return 0;
}
Solving Quadratic Equation
// C program to solve quadratic equation
#include <stdio.h>
#include <math.h>
int main() {
float a, b, c, discriminant, root1, root2;
printf("Enter coefficients a, b and c: ");
scanf("%f %f %f", &a, &b, &c);
discriminant = b * b - 4 * a * c;
if(discriminant > 0) {
root1 = (-b + sqrt(discriminant)) / (2 * a);
root2 = (-b - sqrt(discriminant)) / (2 * a);
printf("Roots are real and different.\n");
printf("Root 1 = %.2f\n", root1);
printf("Root 2 = %.2f\n", root2);
}
else if(discriminant == 0) {
root1 = root2 = -b / (2 * a);
printf("Roots are real and same.\n");
printf("Root 1 = Root 2 = %.2f\n", root1);
}
else {
float realPart = -b / (2 * a);
float imaginaryPart = sqrt(-discriminant) / (2 * a);
printf("Roots are complex and different.\n");
printf("Root 1 = %.2f + %.2fi\n", realPart, imaginaryPart);
printf("Root 2 = %.2f - %.2fi\n", realPart, imaginaryPart);
}
return 0;
}
Factorial Calculation
// C program to calculate factorial of a number
#include <stdio.h>
int main() {
int n, factorial = 1;
printf("Enter a positive integer: ");
scanf("%d", &n);
for(int i = 1; i <= n; ++i) {
factorial *= i;
}
printf("Factorial of %d = %d\n", n, factorial);
return 0;
}
Table Generation
// C program to generate multiplication table of a number
#include <stdio.h>
int main() {
int num;
printf("Enter a number to generate its multiplication table: ");
scanf("%d", &num);
printf("Multiplication table of %d:\n", num);
for(int i = 1; i <= 10; ++i) {
printf("%d * %d = %d\n", num, i, num * i);
}
return 0;
}
Simple Series
Generate Even Numbers Series
// C program to generate a series of even numbers
#include <stdio.h>
int main() {
int n;
printf("Enter the number of terms: ");
scanf("%d", &n);
printf("Even Numbers Series:\n");
for(int i = 1; i <= n; ++i) {
printf("%d ", 2 * i);
}
printf("\n");
return 0;
}