Assignment C Language 11-19 🎆

CODE_HUB



Yeh code VS code mein working hai

(error aaye toh batana)

💟 This is me <3

Output khudse kar lena (easy hai)



 Q.11 Write a recursive function that will generate and print the first n Fibonacci number. Test the function for n=10 and n=15.

Ans 

#include <stdio.h>
int fibonacci(int f);
int main()
{
    int n, f = 0, i;
    printf("Enter number");
    scanf("%d", &n);
    printf("Fibonacci series terms are:");
    for (i = 1; i <= n; i++)
    {
        printf("%d ", fibonacci(f));
        f++;
    }
    return 0;
}
int fibonacci(int n)
{
    if (n == 0 || n == 1)
        return n;
    else
        return (fibonacci(n - 1) + fibonacci(n - 2));
}



another way to direct print answer


#include <stdio.h>
int fib(int n)
{
    if (n <= 1)
        return n;
    return fib(n - 1) + fib(n - 2);
}

int main()
{
    int n = 15;
    printf("%d", fib(n));
    getchar();
    return 0;
}


Q.12    Write a function to calculate the factorial of a number using category return value & passing argument.[Hint: if n==0 or n==1 return 1 Else  return n!].

#include <stdio.h>
#include <math.h>
int main()
{
    int n;

    printf("Enter a Number to Find Factorial: ");
    scanf("%d", &n);
    printf("\nFactorial of a Given Number is: %d ", fact(n));
    return 0;
}
int fact(int x)
{
    int i, fact = 1, n;
    for (i = 1; i <= x; i++)
    {
        fact = fact * i;
    }
    return fact;
}


Q13.    Write a recursive program to find the Greatest Common Divisor of two numbers.

#include <stdio.h>
int hcf(int n1, int n2);
int main()
{
    int n1, n2;
    printf("Enter two positive integers: ");
    scanf("%d %d", &n1, &n2);
    printf("G.C.D of %d and %d is %d.", n1, n2, hcf(n1, n2));
    return 0;
}

int hcf(int n1, int n2)
{
    if (n2 != 0)
        return hcf(n2, n1 % n2);
    else
        return n1;
}


Q14.    Write a program to print 15 numbers in ascending order.

#include<stdio.h>
int main()
{
    int arr[5] ;
    int temp = 0;
    int length = sizeof(arr) / sizeof(arr[0]);
    printf("Enter the Elements of array: \n");
    for (int i = 0; i < length; i++)
    {
    scanf("%d",&arr[i]);
    }
    printf("Elements of original array: \n");
    for (int i = 0; i < length; i++)
    {
        printf("%d ", arr[i]);
    }

    for (int i = 0; i < length; i++)
    {
        for (int j = i + 1; j < length; j++)
        {
            if (arr[i] > arr[j])
            {
                temp = arr[i];
                arr[i] = arr[j];
                arr[j] = temp;
            }
        }
    }

    printf("\n");
    printf("Elements of array sorted in ascending order: \n");
    for (int i = 0; i < length; i++)
    {
        printf("%d ", arr[i]);
    }
    return 0;
}


Q15.    Write a program to search an element in an array.

1.    #include <stdio.h>

int main()
{
    int arr[20];//change value for big data
    int size, i, search, found;
    printf("Enter size of array: ");
    scanf("%d", &size);
    printf("Enter elements in array: ");
    for (i = 0; i < size; i++)
    {
        scanf("%d", &arr[i]);
    }

    printf("\nEnter element to search: ");
    scanf("%d", &search);

    found = 0;

    for (i = 0; i < size; i++)
    {

        if (arr[i] == search)
        {
            found = 1;
            break;
        }
    }
    if (found == 1)
    {
        printf("\n%d is found at position %d", search, i + 1);
    }
    else
    {
        printf("\n%d is not found in the array", search);
    }
    return 0;
}


Q16.Write a program to print diagonal elements and its sum of a matrix

#include <stdio.h>
void main()
{
    int mat[12][12];
    int i, j, row, col, sum = 0;
    printf("Enter the number of Row and Column\n");
    scanf("%d", &row);
    col = row;
    printf("Enter the elements \n");
    for (i = 0; i < row; i++)
    {
        for (j = 0; j < col; j++)
        {
            scanf("%d", &mat[i][j]);
        }
    }
    printf("The matrix\n");
    for (i = 0; i < row; i++)
    {
        for (j = 0; j < col; j++)
        {
            printf("%d ", mat[i][j]);
        }
        printf("\n");
    }
    for (i = 0; i < row; i++)
    {
        for (j = 0; j < col; j++)
        {
            if (i == j)
            {
                printf("The diagonal element are = %d\n", mat[i][j]);
                sum = sum + mat[i][j];
            }
        }
    }
    printf("The sum of diagonal elements of a square matrix = %d\n", sum);
}


Q17.Write a program to find the multiplication of two matrices

#include <stdio.h>
int main()
{
    int a[10][10], b[10][10], mul[10][10], r, c, i, j, k;
    printf("enter the number of row & col =");
    scanf("%d", &r);
    c = r;
    printf("enter the first matrix element ");
    for (i = 0; i < r; i++)
    {
        for (j = 0; j < c; j++)
        {
            scanf("%d", &a[i][j]);
        }
    }
    printf("enter the second matrix element ");
    for (i = 0; i < r; i++)
    {
        for (j = 0; j < c; j++)
        {
            scanf("%d", &b[i][j]);
        }
    }

    printf("multiply of the matrix\n");
    for (i = 0; i < r; i++)
    {
        for (j = 0; j < c; j++)
        {
            mul[i][j] = 0;
            for (k = 0; k < c; k++)
            {
                mul[i][j] += a[i][k] * b[k][j];
            }
        }
    }

    for (i = 0; i < r; i++)
    {
        for (j = 0; j < c; j++)
        {
            printf("%d ", mul[i][j]);
        }
        printf("\n");
    }
    return 0;
}




Q18.1.   Write a menudriven program which performs the following operations using string functions.

a)   Length of a string

b)   Compare two Strings

c)   Copy one string to another

d)   Concatenate two Strings

// 18.  Write a menudriven program which perform the following operations using string functions.
// a)   Length of a string
// b)   Compare two Strings
// c)   Copy one string to another
// d)   Concatenate two Strings
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main()
{
    char str1[20], str2[20];
    int ch, i, j;
    do
    {
        printf("\tMENU");
        printf("\n------------------------------\n");
        printf("1:Find Length of String");
        printf("\n2:Concatenate Strings");
        printf("\n3:Copy String ");
        printf("\n4:Compare Strings");
        printf("\n5:Exit");
        printf("\n------------------------------\n");
        printf("\nEnter your choice: ");
        scanf("%d", &ch);
        switch (ch)
        {
        case 1:
            printf("Enter String: ");
            scanf("%s", str1);
            i = strlen(str1);
            printf("Length of String : %d\n\n", i);
            break;
        case 2:
            printf("\nEnter First String: ");
            scanf("%s", str1);
            printf("Enter Second string: ");
            scanf("%s", str2);
            strcat(str1, str2);
            printf("String After Concatenation : %s\n\n", str1);
            break;
        case 3:
            printf("Enter a String1: ");
            scanf("%s", str1);
            printf("Enter a String2: ");
            scanf("%s", str2);
            printf("\nString Before Copied:\nString1=\"%s\",String2=\"%s\"\n", str1, str2);
            strcpy(str2, str1);
            printf("-----------------------------------------------\n");
            printf("\"We are copying string String1 to String2\" \n");
            printf("-----------------------------------------------\n");
            printf("String After Copied:\nString1=\"%s\", String2=\"%s\"\n\n", str1, str2);
            break;
        case 4:
            printf("Enter First String: ");
            scanf("%s", str1);
            printf("Enter Second String: ");
            scanf("%s", str2);
            j = strcmp(str1, str2);
            if (j == 0)
            {
                printf("Strings are Same\n\n");
            }
            else
            {
                printf("Strings are Not Same\n\n");
            }
            break;
        case 5:
            exit(0);
            break;
        default:
            printf("Invalid Input. Please Enter valid Input.\n\n ");
        }
    } while (ch != 6);
    return 0;
}




1.   Q19.Write a program to check and print whether a string is palindrome or not.


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

int main()
{
    char string1[20];
    int i, length;
    int flag = 0;

    printf("Enter a string:");
    scanf("%s", string1);

    length = strlen(string1);

    for (i = 0; i < length; i++)
    {
        if (string1[i] != string1[length - i - 1])
        {
            flag = 1;
            break;
        }
    }

    if (flag)
    {
        printf("%s is not a palindrome", string1);
    }
    else
    {
        printf("%s is a palindrome", string1);
    }
    return 0;
}

A

a

1


Comments

Popular posts from this blog

O.S. MIDSEM by god

O.S. UNIT 5 (QUES/ANS)

MINI project format >> 1st sem