C-Program of Class 11

 1. Define string. Explain any four-string handling function used in C.

A String in C programming is a sequence of characters terminated with a null character ‘\0’. The C String is stored as an array of characters.

#include <stdio.h>
#include <string.h>
int main() {
    char str1[20] = "Hello";
    char str2[20] = "World";
    char str3[20];

    strcpy(str3, str1);
    printf("Copied string: %s\n", str3);

    strcat(str2, str1);
    printf("Concatenates string: %s\n", str1);
    printf("Length of str1: %lu\n", strlen(str1));

    if (strcmp(str1, str2) == 0)
        printf("str1 and str2 are equal\n");
    else if (strcmp(str1, str2) < 0)
        printf("str1 is less than str2\n");
    else
        printf("str1 is greater than str2\n");
    return 0;
}

strcat(): Concatenates (joins) two strings together. It takes two arguments: the destination string (where the result will be stored) and the source string (the string to be appended).

strcpy(): Copies the content of one string into another. It takes two arguments: the destination string (where the content will be copied) and the source string (the string to be copied from).

strlen(): Calculates the length of a string, excluding the null character ('\0'). It takes one argument: the string whose length is to be determined.

strcmp(): Compares two strings lexicographically. It returns an integer value less than, equal to, or greater than zero if the first string is found to be less than, equal to, or greater than the second string, respectively.

2. Write a C program to check whether given number is divisible by 5 and 7 or not. 

#include <stdio.h>

int main() {
    int num;
    printf("Enter a number: ");
    scanf("%d", &num);
    if (num % 5 == 0 && num % 7 == 0) {
        printf("%d is divisible by both 5 and 7.\n", num);
    } else {
        printf("%d is not divisible by both 5 and 7.\n", num);
    }

    return 0;
}

3. Write a C program to generate Fibonacci series i.e. 0 1 1 2 3 … 

#include <stdio.h>

int main() {
    int n, first = 0, second = 1, next, i;
    printf("Enter the number of terms: ");
    scanf("%d", &n);
    printf("Fibonacci Series: ");
    printf("%d %d ", first, second);
    for (i = 3; i <= n; i++) {
        next = first + second;
        printf("%d ", next);
        first = second;
        second = next;
    }

    printf("\n");

    return 0;
}

4. Write a C program to check whether given number is palindrome or not. 

#include <stdio.h>
int main() {
    int number, reversedNumber = 0, originalNumber;

    printf("Enter an integer: ");
    scanf("%d", &number);

    originalNumber = number;

    while (number > 0) {
        reversedNumber = reversedNumber * 10 + number % 10;
        number /= 10;
    }
    if (originalNumber == reversedNumber) {
        printf("Palindrome\n");
    } else {
        printf("Not a palindrome\n");
    }

    return 0;
}

5. Write a C program to enter salary of 500 employee and find the average salary. 

#include <stdio.h>

#define NUM_EMPLOYEES 500

int main() {
    float salaries[NUM_EMPLOYEES];
    float totalSalary = 0;
    float averageSalary;
    int i;

    printf("Enter the salaries of %d employees:\n", NUM_EMPLOYEES);
    for (i = 0; i < NUM_EMPLOYEES; i++) {
        printf("Enter salary for employee %d: ", i + 1);
        scanf("%f", &salaries[i]);
        totalSalary += salaries[i];
    }

    averageSalary = totalSalary / NUM_EMPLOYEES;

    printf("Average salary of %d employees: %.2f\n", NUM_EMPLOYEES, averageSalary);

    return 0;
}

6. Write a C program to enter 2X2 matrix and find sum of its elements. 
#include <stdio.h>

int main() {
    int matrix[2][2];
    int i, j, sum = 0;
    printf("Enter the elements of the 2x2 matrix:\n");
    for (i = 0; i < 2; i++) {
        for (j = 0; j < 2; j++) {
            printf("Enter element at position (%d, %d): ", i + 1, j + 1);
            scanf("%d", &matrix[i][j]);
        }
    }
    for (i = 0; i < 2; i++) {
        for (j = 0; j < 2; j++) {
            sum += matrix[i][j];
        }
    }
    printf("Sum of elements of the 2x2 matrix: %d\n", sum);

    return 0;
}

7. Write a C program to enter 10 number and find the greatest. 
#include <stdio.h>

int main() {
    int numbers[10];
    int i, greatest;
    printf("Enter 10 numbers:\n");
    for (i = 0; i < 10; i++) {
        printf("Enter number %d: ", i + 1);
        scanf("%d", &numbers[i]);
    }
    greatest = numbers[0];
    for (i = 1; i < 10; i++) {
        if (numbers[i] > greatest) {
            greatest = numbers[i];
        }
    }
    printf("The greatest number is: %d\n", greatest);

    return 0;
}

8. Write a C program that read two 2X2 matrices and perform matrix addition. 

#include <stdio.h>

int main() {
    int matrix1[2][2], matrix2[2][2], sum[2][2];
    int i, j;
    printf("Enter elements of the first 2x2 matrix:\n");
    for (i = 0; i < 2; i++) {
        for (j = 0; j < 2; j++) {
            printf("Enter element at position (%d, %d): ", i + 1, j + 1);
            scanf("%d", &matrix1[i][j]);
        }
    }
    printf("Enter elements of the second 2x2 matrix:\n");
    for (i = 0; i < 2; i++) {
        for (j = 0; j < 2; j++) {
            printf("Enter element at position (%d, %d): ", i + 1, j + 1);
            scanf("%d", &matrix2[i][j]);
        }
    }
    for (i = 0; i < 2; i++) {
        for (j = 0; j < 2; j++) {
            sum[i][j] = matrix1[i][j] + matrix2[i][j];
        }
    }
    printf("Resultant matrix after addition:\n");
    for (i = 0; i < 2; i++) {
        for (j = 0; j < 2; j++) {
            printf("%d ", sum[i][j]);
        }
        printf("\n");
    }

    return 0;
}

9. WAP to ask a number reverse it and print it.

#include <stdio.h>

int main() {
    int number, reversedNumber = 0, remainder;

    printf("Enter a number: ");
    scanf("%d", &number);

    while (number != 0) {
        remainder = number % 10;
        reversedNumber = reversedNumber * 10 + remainder;
        number /= 10;
    }
    printf("Reversed number: %d\n", reversedNumber);

    return 0;
}

10. Write a C program to enter the length, breadth, and height of a box and calculate the total surface area (TSA)   [Formula: TSA = 2 * (L * B + B * H + L * H)]

#include <stdio.h>

int main() {
    float length, breadth, height, TSA;

    printf("Enter the length of the box: ");
    scanf("%f", &length);
    printf("Enter the breadth of the box: ");
    scanf("%f", &breadth);
    printf("Enter the height of the box: ");
    scanf("%f", &height);
    TSA = 2 * (length * breadth + breadth * height + length * height);
    printf("Total Surface Area (TSA) of the box: %.2f\n", TSA);

    return 0;
}

11. Write a C program to enter the radius of a circle and print its circumference.

#include <stdio.h>

#define PI 3.14

int main() {
    float radius, circumference;
    printf("Enter the radius of the circle: ");
    scanf("%f", &radius);
    circumference = 2 * PI * radius;
    printf("The circumference of the circle with radius %.2f is %.2f\n", radius, circumference);

    return 0;
}

12. WAP to calculate product of n-natural number.(factorial number)

#include <stdio.h>

int main() {
    int n, factorial = 1;
    printf("Enter a positive integer: ");
    scanf("%d", &n);
    if (n < 0) {
        printf("Error: Factorial is not defined for negative numbers.\n");
    } else {
        for (int i = 1; i <= n; i++) {
            factorial *= i;
        }
        printf("Factorial of %d is: %d\n", n, factorial);
    }

    return 0;
}

13. WAP to ask 10 different number and print sum of them using array

#include <stdio.h>
#define NUMBERS_COUNT 10

int main() {
    int numbers[NUMBERS_COUNT];
    int sum = 0;
    printf("Enter 10 different numbers:\n");
    for (int i = 0; i < NUMBERS_COUNT; i++) {
        printf("Enter number %d: ", i + 1);
        scanf("%d", &numbers[i]);
    }
    for (int i = 0; i < NUMBERS_COUNT; i++) {
        sum += numbers[i];
    }
    printf("Sum of the numbers: %d\n", sum);

    return 0;
}

14. WAP to ask marks of 6 different subject and print total and percentage them using array.

#include <stdio.h>
#define SUBJECTS_COUNT 6

int main() {
    int marks[SUBJECTS_COUNT];
    int total = 0;
    float percentage;
    printf("Enter marks of 6 subjects:\n");
    for (int i = 0; i < SUBJECTS_COUNT; i++) {
        printf("Enter marks of subject %d: ", i + 1);
        scanf("%d", &marks[i]);
        total += marks[i];
    }
    percentage = (float)total / SUBJECTS_COUNT;
    printf("Total marks: %d\n", total);
    printf("Percentage: %.2f%%\n", percentage);

    return 0;
}

15. WAP to ask a number and check prime or composite.
#include <stdio.h>
int main() {
    int num, i, isPrime = 1;
    printf("Enter a positive integer: ");
    scanf("%d", &num);
    if (num <= 1) {
        isPrime = 0;
    } else {
        for (i = 2; i <= num / 2; i++) {
            if (num % i == 0) {
                isPrime = 0;
                break;
            }
        }
    }
    if (isPrime) {
        printf("%d is a prime number.\n", num);
    } else {
        printf("%d is a composite number.\n", num);
    }

    return 0;
}




Comments

Popular posts from this blog

C-Programming of Class 12