Feb 17, 2022

Comparing two files in C language.

Operations on More than one File. 

 #include<stdio.h>
#include<stdlib.h>
#include<string.h>

int main(int argc, char const* argv[])
{
    FILE* file_ptr1;
    FILE* file_ptr2;
    file_ptr1 = fopen("test1.txt", "r");
    file_ptr2 = fopen("test2.txt", "r");

    char word1 [15], word2 [15];
    int total_count = 0;
    while (fscanf(file_ptr1, "%s", word1) != EOF)
    {
        int count = 0;
        fseek(file_ptr2, 0, SEEK_SET);
        while (fscanf(file_ptr2, "%s", word2) != EOF)
        {
            if (strcmp(word1, word2) == 0)
            {
                count++;
            }
        }
        if (count == 0)
        {
            total_count++;
            printf("\"%s\" ---> not in file.\n", word1);
        }
    }
    printf("%d words are not in file.\n", total_count);


    fclose(file_ptr1);
    fclose(file_ptr2);
    return 0;
}

Reading and writing in files ( C language )

File operations in C.

 #include<stdio.h>
#include<stdlib.h>
#include<string.h>


int main(int argc, char const* argv[])
{
    FILE* file_ptr;
    file_ptr = fopen("test2.txt", "w");
    fprintf(file_ptr, "This is first file of c .\n");

    char* data = "happy to programming file operations .";
    fputs(data, file_ptr);
    fclose(file_ptr);

    file_ptr = fopen("test2.txt", "r");

    //feek(file_pointer,start away from,set the ptr) function  
    fseek(file_ptr, 1, SEEK_SET);
    char arr;
    for (int i = 0; i <= 5; i++)
    {
        fscanf(file_ptr, "%c", &arr);
        printf("%d--->%c\n", i, arr);
    }

    fclose(file_ptr);
    return 0;
}

File Handling in C language.

File operations-writing and reading in files.

 #include <stdio.h>
int main(int argc, char const* argv[])
{
// creating first file
// storing pointer of start of file
    FILE* first_file;
    first_file = fopen("test1.txt", "w");
    // checking if file is opened correctly
    if (first_file == 0)
    {
        printf("Not able to find file.");
    }
    else
    {
    // writes the content in file
        fprintf(first_file, "This is first file of c language .\n"); //--------------
        char* data = "I am happy to programming file operations .";
        fputs(data, first_file); //---------------
        // closing file
        fclose(first_file);
    }

// Reading the file content
    first_file = fopen("test1.txt", "r");

    char content [50];
    int i = 0;
// gets the line or(gets characters upto the index given(-1))
    while (fgets(content, 6, first_file) != NULL)
    {
        printf("\n%d\n", i);
        i++;
        // fgets(content,50,first_file);               //using fgets() function
        printf("%s", content);
    }


// //printing by using fscanf() function
// while (fscanf(first_file, "%s", content) != EOF)         //EOF ==> end of file
//     {
//     printf("%s\n", content);
//     }
// // fscanf(first_file, "%c", content);
// // printf("%c", *content);


    fclose(first_file);
    return 0;
}

Structures in C (2)

structures in C.

 #include<stdio.h>
struct student
{
    char* name;
    int roll_no;
    float perc_prev_yr;
    int admiss_yr;
};

int main(int argc, char const *argv[])
{
    struct student student_1;
    struct student student_2;
    struct student student_3;
    struct student student_4;

    student_2.name = "Madan";
    student_2.roll_no = 2002;
    student_2.perc_prev_yr = 78.31;
    student_2.admiss_yr = 2020;

    student_3.name = "Mohan";
    student_3.roll_no = 1950;
    student_3.perc_prev_yr = 85.0;
    student_3.admiss_yr = 2019;

    student_4.name = "Raman";
    student_4.roll_no = 1730;
    student_4.perc_prev_yr = 80.50;
    student_4.admiss_yr = 2017;

    printf("Name :%s\n", student_3.name);
    printf("Roll_number :%d\n", student_3.roll_no);
    printf("Previous Year Percentage :%.2f\n", student_3.perc_prev_yr);
    printf("Admission Year :%d\n", student_3.admiss_yr);

    return 0;
}

Structures in C language

struct keyword-use and operations

 #include <stdio.h>
struct busi_biscuit
{
    char* type;
    int min_price_rs;
    int gm_wgt;
};

int main()
{
    // creating struct of given type
    struct busi_biscuit parle_G;
    // creating struct of given type
    struct busi_biscuit Jim_jam;

    //assigning values to it
    parle_G.type = "Non-creamy";
    parle_G.min_price_rs = 5;
    parle_G.gm_wgt = 50;

    Jim_jam.type = "creamy";
    Jim_jam.min_price_rs = 25;
    Jim_jam.gm_wgt = 150;

    printf("Jim_jam(type): %s\n" , Jim_jam.type);
    printf("Jim_jam.minimum price(Rs) : %d Rs\n" , Jim_jam.min_price_rs);
    printf("Jim_jam Weight(gm) : %d gm \n" , Jim_jam.gm_wgt);

    return 0;
}

Use of conditional statements-Using switch in C language.

Use of switch in C language.

 #include <stdio.h>
int main(int argc, char const *argv[])
{
    int a=11;

    while (a != 10)
    {
        // switch case
        printf("Enter Number :");
        scanf("%d", &a);
        switch (a)
        {
        case 0:
        {
            printf("%d :zero is the number.\n", a);
            break;
        }
        case 1:
        {
            printf("%d :one is the number.\n", a);
            break;
        }
        case 2:
        {
            printf("%d :two is the number.\n", a);
            break;
        }
        case 3:
        {
            printf("%d :three is the number.\n", a);
            break;
        }
        default:
        {
            printf("Not valid input.\n");
            break;
        }
        }
    }

    return 0;
}

Hacker-rank problem -marks sum problem of students. ( C language )

//Hackerrank problem -marks sum problem of students.

#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>


int marks_summation(int* marks , int number_of_students , char gender)
{

    int sum = 0 , i;
    if ( gender == 'b' )
    {
        for ( int i = 0; i < number_of_students; i += 2 )
        {
            sum += marks[i];
        }
    }
    else
    {
        for ( int i = 1; i < number_of_students; i += 2 )
        {
            sum += marks[i];
        }
    }
    return sum;
}

int main()
{
    int number_of_students;
    char gender;
    int sum;

    scanf("%d" , &number_of_students);
    int* marks = (int*)malloc(number_of_students * sizeof(int));

    for ( int student = 0; student < number_of_students; student++ )
    {
        scanf("%d" , (marks + student));
    }

    scanf(" %c" , &gender);
    sum = marks_summation(marks , number_of_students , gender);
    printf("%d" , sum);

    free(marks);

    return 0;
}

Euler Problem-finding difference between sum of square and square of sum problem. ( C language )

solving maths problems using C language.

 /*
The sum of the squares of the first ten natural numbers is,
 1^2 + 2^2 + 3^2 + ... 10^2 = 385
The square of the sum of the first ten natural numbers is,
(1+2+3+4...........+10)^2 = 3025

Hence the difference between the sum of the squares of the first ten natural numbers
 and the square of the sum is .
 3025-385=2640

Find the difference between the sum of the squares of the first one hundred
natural numbers and the square of the sum.
 */

#include <stdio.h>


int main()
{
    int sum_upto;
    sum_upto = 100;
    int sum_of_squares = 0;
    int square_of_sum = 0;
    for (int i = 1; i <= sum_upto; i++)
    {
        sum_of_squares += (i * i);
        square_of_sum += ( i );
    }
    square_of_sum *= square_of_sum;
    int diff = square_of_sum - sum_of_squares;
    printf("Difference is : %d", diff);

    return 0;
}

Use of mathematical operations and implementation (C language )

Problem-Finding the sum of digits of five digit number.

 #include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>

int main()
{

    int n;
    scanf("%d", &n);
    // Complete the code to calculate the sum of the five digits on n.
    int num;
    int base = 10000;
    int sum = 0;
    // printf("%d\n",(num-(num%base))/base );
    // printf("%d\n",(num%base));

    for (int i = 1; i <= 5; i++)
    {
        num = (n - (n % base)) / base;
        n = (n % base);
        base /= 10;
        sum += num;
    }
    printf("%d\n", sum);

    return 0;
}

Comparing two files in C language.

Operations on More than one File.    # include < stdio.h > # include < stdlib.h > # include < string.h > int main ( int ...