Feb 17, 2022

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

Feb 7, 2022

HackerRank -Pointers in C (C Language)

Operations on pointers-finds the sum and difference between numbers given.

 #include <stdio.h>
void update(int* a , int* b) {

    int temp = *a;
    *a = temp + *b;
    *b = temp - *b;
    if (*b < 0)
    {
        *b *= (-1);
    }

}

int main() {
    int a , b;
    int* pa = &a , * pb = &b;
    printf("Enter two Numbers : ");
    scanf("%d %d" , &a , &b);
    update(pa , pb);
    printf(" sum : %d\n Difference : %d" , a , b);

    return 0;
}

Euler Problem-Printing the pattern (C language)

Printing the patterns of number.

 /*
printing the pattern as given below for given number--
3 3 3 3 3
3 2 2 2 3
3 2 1 2 3
3 2 2 2 3
3 3 3 3 3
*/
#include <stdio.h>

int main()
{
    int n;
    printf("Enter : ");
    scanf("%d" , &n);
    // Complete the code to print the pattern.
    for (int i = 0; i < (2 * n) - 1; i++)
    {
        for (int j = 0; j < (2 * n) - 1; j++)
        {
            int min = i < j ? i : j;
            min = min < (2 * n - j - 2) ? min : (2 * n - j - 2);
            min = min < (2 * n - i - 2) ? min : (2 * n - i - 2);

            int index = n - (min);

            printf("%d " , index);
        }
        printf("\n");
    }

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