Feb 17, 2022

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;
}

Example through learning datatypes and mathematical operations ( C language)

Mathematical operations using float and integers.

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

int main()
{
    int a, b;
    float c, d;
    scanf("%d",&a);
    scanf("%d",&b);
    printf("\n");
    scanf("%f",&c);
    scanf("%f",&d);

    printf("%d %d\n", a + b, a - b);
    printf("%.1f %.1f", c + d, c - d);


    return 0;
}

Characters and Strings in C language

More string operations in C language

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

int main()
{
    char ch;
    char str[10];
    char sen[25];

    scanf("%c", &ch);
    scanf("%s", str);
    scanf("\n");
    scanf("%[^\n]%*c", sen);

    printf("%c\n", ch);
    printf("%s\n", str);
    printf("%s", sen);

    return 0;
}

Character arrays in C (Strings)

Operations on Strings (input and output)

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

int main()
{
    char string1[] = "stringA";
    char string2[] = "stringa";
    int arr_size = sizeof(string1) / sizeof(char);

    // printing string
    printf("%s \n" , string1);
    printf("%c \n" , *string1);
    printf("%s \n" , string1);

    //comparing strings
    int diff = strcmp(string1 , string2);
    printf("%d \n" , diff);

//function to take input of the string type by user
    fgets(string1 , arr_size , stdin);

    //printing string using loop
    for ( int i = 0; i < arr_size; i++ )
    {
        printf("%c" , *(string1 + i));
    }

    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 ...