Skip to content

Instantly share code, notes, and snippets.

@innovatorved
Created July 2, 2023 16:06
Show Gist options
  • Save innovatorved/37c4b076fe560499345fad6345ef495f to your computer and use it in GitHub Desktop.
Save innovatorved/37c4b076fe560499345fad6345ef495f to your computer and use it in GitHub Desktop.
Sanno Coding Question

Functions

1 Write a program in C program to check whether a number can be expressed as sum of two prime numbers.

#include <stdio.h>
#include <stdbool.h>

bool isPrime(int num)
{

    if (num <= 1)
    {
        return false;
    }
    for (int i = 2; i <= num - 1; i++)
    {
        if (num % i == 0)
        {
            return false;
        }
    }
    return true;
}

bool checkSumOfTwoPrimes(int num)
{
    for (int i = 2; i < num; i++)
    {
        int j = num - i;
        if (isPrime(i) && isPrime(j))
        {
            printf("%d + %d\n", i, j);
            return true;
        }
    }
    return false;
}

int main()
{
    int n;
    printf("Enter the Number: ");
    scanf("%d", &n);
    bool result = checkSumOfTwoPrimes(n);
    if (result)
    {
        printf("This is Sum of 2 Prime Number");
    }
    else
    {
        printf("Tihis is not");
    }

    printf("\n");
    return 0;
}

2 Write a program in C to print all perfect numbers in a given range using the function.

#include <stdio.h>
#include <stdbool.h>

bool CheckPerfectNumber(int num)
{
    int sum = 0;
    for (int i = 1; i < num; i++)
    {
        if (num % i == 0)
        {
            sum = sum + i;
        }
    }
    if (sum == num)
    {
        return true;
    }
    else
    {
        return false;
    }
}

int main()
{
    int start, end;
    printf("Enter a range: ");
    scanf("%d", &start);
    scanf("%d", &end);

    for (int i = start; i <= end; i++)
    {
        if (CheckPerfectNumber(i))
        {
            printf("%d ", i);
        }
    }
    printf("\n");
}

3 Write a program to sum the series 1/11 + 1/2! +1/3+...........+1/n!...

4 Write a program using function to calculate x to the power of y, where y can be either negative or positive.

#include <stdio.h>
#include <stdbool.h>

int CalculatePower(int n, int x)
{
    float n2 = n;

    if (x < 0)
    {
        for (int i = 1; i < -x; i++)
        {
            n2 = n2 / n;
        }
    }
    else
    {
        for (int i = 1; i < x; i++)
        {
            n2 = n2 * n;
        }
    }
    return n2;
}

int main()
{
    int n, x;
    printf("Enter a range: ");
    scanf("%d", &n);
    scanf("%d", &x);

    float result = CalculatePower(n, x);
    printf("%f", result);
}

Passing parameter to functions

5 Write a program in C to swap two integer numbers using call by reference and call by value method.

#include <stdio.h>
#include <stdbool.h>

void swapByReference(int *a, int *b)
{
    int temp = *a;
    *a = *b;
    *b = temp;
}

void swapByValue(int a, int b)
{
    int temp = a;
    a = b;
    b = temp;
    printf("After swapping by value: a = %d, b = %d\n", a, b);
}

int main()
{
    int a, b;
    printf("Enter a range: ");
    scanf("%d", &a);
    scanf("%d", &b);

    swapByValue(&a, &b);

    printf("Number Swapped %d , %d", a, b);
}

6 Write a program in C to calculate the sum of array elements by passing an array to a function.

Recursion

7 Write a recursive function in C to generate the Fibonacci series upto 'n' terms. 8 Write a program to calculate GCD using recursive function.

Sorting

9 Write a program in C to sort n numbers in ascending order using insertion sort.

Searching

10 Write a program in C to implement binary search in an array using function

Prime Number

bool isPrime(int num)
{

    if (num <= 1)
    {
        return false;
    }
    for (int i = 2; i <= num - 1; i++)
    {
        if (num % i == 0)
        {
            return false;
        }
    }
    return true;
}
@innovatorved
Copy link
Author

innovatorved commented Jul 2, 2023

6 Write a program in C to calculate the sum of array elements by passing an array to
a function.

int SumOfArray(int arr[], int n)
{
    int sum = 0;
    for (int i = 0; i < n; i++)
    {
        sum = sum + arr[i];
    }
    return sum;
}

@innovatorved
Copy link
Author

  1. Recurssive function fibonacchi series to nth term
#include <stdio.h>

int fibonacci(int n)
{
    // Base case
    if (n <= 1)
        return n;

    // Recursive case
    return fibonacci(n - 1) + fibonacci(n - 2);
}

int main()
{
    int n;
    printf("Enter the number of terms: ");
    scanf("%d", &n);

    printf("Fibonacci Series up to %d terms: ", n);
    for (int i = 0; i < n; i++)
    {
        printf("%d ", fibonacci(i));
    }

    return 0;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment