Introduction to C Language: Installation, Familiarization, Programming

Introduction to C Language

C Language

  1. Installation of C – Compiler
  2. Familiarization with IDE of C – Compiler
  3. Write some programmes using printf ( ), Scanf ( ), Format, Specifier, Escape Sequences, getch()
  4. 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

  1. Install the C compiler on your system.
  2. Explore the IDE provided with the compiler.
  3. Create a new project or file in the IDE.
  4. Write and save the C programs covering the mentioned topics.
  5. Compile the programs to check for any errors.
  6. Run the programs and verify the output.
  7. Make necessary corrections if required.

 

Arithmetic Problems

Calculate Average

#include <iostream>
using namespace std;

int main() {
    int n, sum = 0;
    float average;

    cout << "Enter the number of elements: ";
    cin >> n;

    int arr[n];
    cout << "Enter " << n << " numbers: ";
    for(int i = 0; i < n; ++i) {
        cin >> arr[i];
        sum += arr[i];
    }

    average = sum / static_cast(n);
    cout << "Average = " << average << endl;

    return 0;
}

Geometric Calculations

// C++ program to calculate area of a circle

#include <iostream>
using namespace std;

int main() {
    float radius, area;

    cout << "Enter the radius of circle: ";
    cin >> radius;

    area = 3.14159 * radius * radius;
    cout << "Area of circle = " << area << endl;

    return 0;
}

Comparing Numbers

// C++ program to compare two numbers

#include <iostream>
using namespace std;

int main() {
    int num1, num2;

    cout << "Enter two numbers: ";
    cin >> num1 >> num2;

    if(num1 > num2)
        cout << num1 << " is greater than " << num2 << endl;
    else if(num1 < num2)
        cout << num1 << " is less than " << num2 << endl;
    else
        cout << "Both numbers are equal" << endl;

    return 0;
}

Solving Quadratic Equation

// C++ program to solve quadratic equation

#include <iostream>
#include <cmath>
using namespace std;

int main() {
    float a, b, c, discriminant, root1, root2;

    cout << "Enter coefficients a, b and c: ";
    cin >> a >> b >> c;

    discriminant = b * b - 4 * a * c;

    if(discriminant > 0) {
        root1 = (-b + sqrt(discriminant)) / (2 * a);
        root2 = (-b - sqrt(discriminant)) / (2 * a);
        cout << "Roots are real and different." << endl;
        cout << "Root 1 = " << root1 << endl;
        cout << "Root 2 = " << root2 << endl;
    }
    else if(discriminant == 0) {
        root1 = root2 = -b / (2 * a);
        cout << "Roots are real and same." << endl;
        cout << "Root 1 = Root 2 = " << root1 << endl;
    }
    else {
        float realPart = -b / (2 * a);
        float imaginaryPart = sqrt(-discriminant) / (2 * a);
        cout << "Roots are complex and different." << endl;
        cout << "Root 1 = " << realPart << " + " << imaginaryPart << "i" << endl;
        cout << "Root 2 = " << realPart << " - " << imaginaryPart << "i" << endl;
    }

    return 0;
}

Factorial Calculation

// C++ program to calculate factorial of a number

#include <iostream>
using namespace std;

int main() {
    int n, factorial = 1;

    cout << "Enter a positive integer: ";
    cin >> n;

    for(int i = 1; i <= n; ++i) {
        factorial *= i;
    }

    cout << "Factorial of " << n << " = " << factorial << endl;

    return 0;
}

Table Generation

// C++ program to generate multiplication table of a number

#include <iostream>
using namespace std;

int main() {
    int num;

    cout << "Enter a number to generate its multiplication table: ";
    cin >> num;

    cout << "Multiplication table of " << num << ":" << endl;
    for(int i = 1; i <= 10; ++i) {
        cout << num << " * " << i << " = " << num * i << endl;
    }

    return 0;
}

Simple Series

Generate Even Numbers Series

// C++ program to generate a series of even numbers

#include <iostream>
using namespace std;

int main() {
    int n;

    cout << "Enter the number of terms: ";
    cin >> n;

    cout << "Even Numbers Series:" << endl;
    for(int i = 1; i <= n; ++i) {
        cout << 2 * i << " ";
    }
    cout << endl;

    return 0;
}

Post a Comment

Previous Post Next Post