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

foobar problem-Make some Fun ! (C language)

Read the problem below and create your own problem like this.

 /*FooBar Problem: print numbers from 1 to n (user inp), but
print 'foo' if the number is divisible by 3,
 'bar' if the number is divisible by 5, and
 if divisible by both print 'foobar', instead of number
Eg:
    1
    n=5
    2
    foo
    4
    bar */
#include <stdio.h>

void fooBar(int *num)
{
    for (int i = 1; i <= *num; i++)
    {
        int div_3 = i % 3;
        int div_5 = i % 5;
        if (!div_3 && !div_5)
        {
            printf("foobar\n");
        }
        else if (!div_3)
        {
            printf("foo\n");
        }
        else if (!div_5)
        {
            printf("bar\n");
        }
        else
        {

            printf("%d\n", i);
        }
    }
}
int main(int argc, char const *argv[])
{
    int number;
    scanf("%d", &number);
    fooBar(&number);

    return 0;
}

HackerRank-Printing Frequency of digits in Number (C language)

Printing the frequency of digits (0 to 9). 

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

int main() {
    char str[1000];
    scanf("%s" , str);
    int arr[10] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
    int i = 0;
    while (str[i] != '\0')
    {
        if (str[i] == '0' || str[i] == '1' || str[i] == '2' || str[i] == '3' ||
            str[i] == '4' || str[i] == '5' ||
            str[i] == '6' || str[i] == '7' || str[i] == '8' || str[i] == '9')
        {
            arr[str[i] - '0']++;
        }

        i++;
    }
    for (int i = 0; i < 10; i++)
    {
        printf("%d " , arr[i]);
    }



    return 0;
}

HackerRank-Array operations (C language)

Printing the words of given sentence on new line.

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

int main() {

    char *s;
    s = malloc(1024 * sizeof(char));
    scanf("%[^\n]", s);
    s = realloc(s, strlen(s) + 1);
    //Write your logic to print the tokens of the sentence here.
    //writes words of sentence on new line.
    int i = 0;
    while (s[i] != '\0')
    {
        printf("%c", s[i]);
        i++;
        if (s[i]==' ')
        {
            i++;
            printf("\n");
        }
       
    }
   

    return 0;
}

Euler Problem - Iterative Sequence (C language)

Finding number of steps to reach 1 by given conditions.

 /* The following iterative sequence is defined for the set of positive integers:
n → n/2 (n is even)
n → 3n + 1 (n is odd)

Using the rule above and starting with 13, we generate the following sequence:

13 → 40 → 20 → 10 → 5 → 16 → 8 → 4 → 2 → 1
It can be seen that this sequence (starting at 13 and finishing at 1)
contains 10 terms
Although it has not been proved yet (Collatz Problem), it is thought that all starting
numbers finish at 1.

Which starting number, under one million, produces the longest chain?

NOTE: Once the chain starts the terms are allowed to go above one million.*/

#include <stdio.h>
#include <inttypes.h>
#include <stdlib.h>
#include <time.h>
#include <Windows.h>

// void max_count(int *arr, int range)
// {
//     int max = range;
//     for (range; range > 0; range--)
//     {
//         // printf("counts : %d\n", *(arr + range));
//         if (*(arr + range) > *(arr+max))
//         {
//             max = range;
//         }
//     }
//     free(arr);
//     printf("Maximum chain number:%d\n", max);
// }

void collatz(uint32_t range)
{
    int max=0;
    uint32_t *num_count = (uint32_t *)calloc(range, sizeof(int));
    *(num_count) = 0;  
    for (uint32_t i = 1; i <= range; i++)
    {
        int count = 1;
        uint32_t num = i;
        while (num != 1)
        {
            // printf("%d ->", num);
            if (num < i)
            {
                count += *(num_count + num)-1;
                break;
            }
            if (num%2 == 0)
            {
                num = num / 2;
            }else{
                num = 3 * num + 1;
            }
            count++;
        }
        // printf("1\n");
        *(num_count + i) = count;
        if (*(num_count + i) > *(num_count+max))
        {
            max = i;
        }
    }
    free(num_count);
    printf("Maximum chain number:%d\n", max);  
}

int main(int argc, char const *argv[])
{
    //clock
     // to store the execution time of code
    double time_spent = 0.0;
    clock_t begin = clock();

    //function
    collatz(1000000);



    //sleep clock
    Sleep(3);
    clock_t end = clock();
 
    // calculate elapsed time by finding difference (end - begin) and
    // dividing the difference by CLOCKS_PER_SEC to convert to seconds
    time_spent += (double)(end - begin) / CLOCKS_PER_SEC;
 
    printf("The elapsed time is %f seconds", time_spent);
    return 0;
}

Finding Prime numbers (C languaage)

Finding prime numbers upto given number.

 #include<stdio.h>
int main()
{
    //prime numbers
    int prime;
    for (int prime = 2; prime < 100; prime++)
    {
        int count = 0;
        for (int i = 2; i <= prime / 2; i++)
        {
            if (prime % i == 0)
            {
                count++;
            }

        }
        if (count == 0)
        {
            printf("%d\n" , prime);
        }


    }

    return 0;
}

Euler Problem - Finding prime factors of numbers (C language)

Finding prime factors of numbers .

 /*The prime factors of 13195 are 5, 7, 13 and 29.
What is the largest prime factor of the number 600851475143 ?*/
#include <stdio.h>
int main()
{
    int prime, num;
    num = 13195;
    for (prime = 2; prime <= num / 2; prime++)
    {
        int count = 0;
        for (int i = 2; i <= prime / 2; i++)
        {
            if (prime % i == 0)
            {
                count++;
            }
        }
        if (count == 0 && num % prime == 0)
        {
            // num = num / prime;
            printf("%d\n", prime);
        }
    }

    return 0;
}

Find if the number is positive or Negative (C Language)

finding the sign (+/-) of number using condition statements

 #include <stdio.h>
int main()
{
    // Program to check if number is positive or negative
    int a;
    printf("Enter the Number (to check +ve/-ve ) : ");
    scanf("%d" , &a);
    if (a > 0)
    {
        printf("Result : %d Number is positve." , a);
    }
    else if (a == 0)
    {
        printf("Result : %d Number is zero(neither +ve or -ve)." , a);
    }
    else
    {
        printf("Result : %d Number is Negative." , a);
    }

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