C programming for B.Sc

 

Matrix multiplication

 

#include<stdio.h>   

#include<conio.h> 

int main(){ 

int a[10][10],b[10][10],mul[10][10],r,c,i,j,k;   

 

printf("enter the number of row=");   

scanf("%d",&r);   

printf("enter the number of column=");   

scanf("%d",&c);   

printf("enter the first matrix element=\n");   

for(i=0;i<r;i++)   

{   

for(j=0;j<c;j++)   

{   

scanf("%d",&a[i][j]);   

}   

}   

printf("enter the second matrix element=\n");   

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 printing result   

for(i=0;i<r;i++)   

{   

for(j=0;j<c;j++)   

{   

printf("%d\t",mul[i][j]);   

}   

printf("\n");   

}   

return 0; 

}

 

 

 

 

 

Transpose of a matrix

 

 #include <stdio.h>

int main() {

  int a[10][10], transpose[10][10], r, c,i,j;

  printf("Enter rows and columns: ");

  scanf("%d %d", &r, &c);

 

  // asssigning elements to the matrix

  printf("\nEnter matrix elements:\n");

  for (i = 0; i < r; ++i)

  for (j = 0; j < c; ++j) {

    printf("Enter element a%d%d: ", i + 1, j + 1);

    scanf("%d", &a[i][j]);

  }

  // printing the matrix a[][]

  printf("\nEntered matrix: \n");

  for (i = 0; i < r; ++i)

  for (j = 0; j < c; ++j) {

    printf("%d  ", a[i][j]);

    if (j == c - 1)

    printf("\n");

  }

  // computing the transpose

  for (i = 0; i < r; ++i)

  for (j = 0; j < c; ++j) {

    transpose[j][i] = a[i][j];

  }

 

  // printing the transpose

  printf("\nTranspose of the matrix:\n");

  for (i = 0; i < c; ++i)

  for (j = 0; j < r; ++j) {

    printf("%d  ", transpose[i][j]);

    if (j == r - 1)

    printf("\n");

  }

  return 0;

}

Matrix substraction:

#include <stdio.h>

 

int main()

{

   int m, n, c, d, first[10][10], second[10][10], difference[10][10];

 

   printf("Enter the number of rows and columns of matrix\n");

   scanf("%d%d", &m, &n);

   printf("Enter the elements of first matrix\n");

 

   for (c = 0; c < m; c++)

     for (d = 0 ; d < n; d++)

       scanf("%d", &first[c][d]);

 

   printf("Enter the elements of second matrix\n");

 

   for (c = 0; c < m; c++)

     for (d = 0; d < n; d++)

         scanf("%d", &second[c][d]);

 

   printf("Difference of entered matrices:-\n");

 

   for (c = 0; c < m; c++) {

     for (d = 0; d < n; d++) {

       difference[c][d] = first[c][d] - second[c][d];

       printf("%d\t",difference[c][d]);

     }

     printf("\n");

   }

 

   return 0;

}

Leap year

#include <stdio.h>

int main() {

   int year;

   printf("Enter a year: ");

   scanf("%d", &year);

 

   // leap year if perfectly divisible by 400

   if (year % 400 == 0) {

      printf("%d is a leap year.", year);

   }

   // not a leap year if divisible by 100

   // but not divisible by 400

   else if (year % 100 == 0) {

      printf("%d is not a leap year.", year);

   }

   // leap year if not divisible by 100

   // but divisible by 4

   else if (year % 4 == 0) {

      printf("%d is a leap year.", year);

   }

   // all other years are not leap years

   else {

      printf("%d is not a leap year.", year);

   }

 

   return 0;

}

Sum of digits

#include<stdio.h> 

 int main()   

{   

int n,sum=0,m;   

printf("Enter a number:");   

scanf("%d",&n);   

while(n>0)   

{   

m=n%10;   

sum=sum+m;   

n=n/10;   

}   

printf("Sum is=%d",sum);   

return 0; 

}  

 

Binary to decimal

#include <stdio.h> 

#include <conio.h> 

void main() 

{ 

    // declaration of variables 

    int num, binary_num, decimal_num = 0, base = 1, rem; 

    printf (" Enter a binary number with the combination of 0s and 1s \n"); 

    scanf (" %d", &num); // accept the binary number (0s and 1s) 

    binary_num = num; // assign the binary number to the binary_num variable   

    while ( num > 0) 

    { 

        rem = num % 10; /* divide the binary number by 10 and store the remainder in rem variable. */ 

        decimal_num = decimal_num + rem * base; 

        num = num / 10; // divide the number with quotient 

        base = base * 2; 

    } 

 

    printf ( " The binary number is %d \t", binary_num); // print the binary number 

    printf (" \n The decimal number is %d \t", decimal_num); // print the decimal  

    getch(); 

} 

 

Check palindrome or not

#include<stdio.h> 

int main()   

{   

int n,r,sum=0,temp;   

printf("enter the number=");   

scanf("%d",&n);   

temp=n;   

while(n>0)   

{   

r=n%10;   

sum=(sum*10)+r;   

n=n/10;   

}   

if(temp==sum)   

printf("palindrome number ");   

else   

printf("not palindrome");  

return 0; 

}  

Reverse of an integer

#include <stdio.h>

 

int main() {

    int num, reversedNum = 0, remainder;

 

    // Input the integer

    printf("Enter an integer: ");

    scanf("%d", &num);

 

    // Find the reverse of the integer

    while (num != 0) {

        remainder = num % 10;

        reversedNum = reversedNum * 10 + remainder;

        num /= 10;

    }

 

    // Display the reverse

    printf("Reverse of the entered integer: %d\n", reversedNum);

 

    return 0;

}

Ascending order

#include <stdio.h>

    void main()

    {

 

        int i, j, a, n, number[30];

        printf("Enter the value of N \n");

        scanf("%d", &n);

 

        printf("Enter the numbers \n");

        for (i = 0; i < n; ++i)

            scanf("%d", &number[i]);

 

        for (i = 0; i < n; ++i)

        {

 

            for (j = i + 1; j < n; ++j)

            {

 

                if (number[i] > number[j])

                {

 

                    a =  number[i];

                    number[i] = number[j];

                    number[j] = a;

 

                }

                }

 

        }

 

        printf("The numbers arranged in ascending order are given below \n");

        for (i = 0; i < n; ++i)

            printf("%d\n", number[i]);

 

    }

Largest  element

#include <stdio.h>

int main() {

  int n;

  int arr[100],i;

  printf("Enter the number of elements (1 to 100): ");

  scanf("%d", &n);

 

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

    printf("Enter number%d: ", i + 1);

    scanf("%d", &arr[i]);

  }

 

  // storing the largest number to arr[0]

  for ( i = 1; i < n; ++i) {

    if (arr[0] < arr[i]) {

      arr[0] = arr[i];

    }

  }

 

  printf("Largest element = %d", arr[0]);

 

  return 0;

}

Smallest element:

#include<stdio.h>

#include<conio.h>

int main()

{

    int arr[10], small, i,n;

    printf("enter an integer");

    scanf("%d",&n);

    printf("Enter elements: ");

    for(i=0; i<n; i++)

        scanf("%d", &arr[i]);

    i=0;

    small=arr[i];

    while(i<n)

    {

        if(small>arr[i])

            small = arr[i];

        i++;

    }

    printf("\nSmallest Number = %d", small);

 

    return 0;

}

Fibonacci series:

#include <stdio.h>

int main() {

 

  int i, n;

 

  // initialize first and second terms

  int t1 = 0, t2 = 1;

 

  // initialize the next term (3rd term)

  int nextTerm = t1 + t2;

 

  // get no. of terms from user

  printf("Enter the number of terms: ");

  scanf("%d", &n);

 

  // print the first two terms t1 and t2

  printf("Fibonacci Series: %d, %d, ", t1, t2);

 

  // print 3rd to nth terms

  for (i = 3; i <= n; ++i) {

    printf("%d, ", nextTerm);

    t1 = t2;

    t2 = nextTerm;

    nextTerm = t1 + t2;

  }

 

  return 0;

}

Sum of even numbers :

#include <stdio.h>

 

int main()

{

    int i, n, sum=0;

 

    /* Input upper limit from user */

    printf("Enter upper limit: ");

    scanf("%d", &n);

 

    for(i=2; i<=n; i+=2)

    {

        /* Add current even number to sum */

        sum += i;

    }

 

    printf("Sum of all even number between 1 to %d = %d", n, sum);

 

    return 0;

}

Sum of odd numbers :

#include <stdio.h>

 

int main()

{

    int i, n, sum=0;

 

    /* Input upper limit from user */

    printf("Enter upper limit: ");

    scanf("%d", &n);

 

    for(i=1; i<=n; i+=2)

    {

        /* Add current even number to sum */

        sum += i;

    }

 

    printf("Sum of all even number between 1 to %d = %d", n, sum);

 

    return 0;

}

Factorial in c

#include <stdio.h>

int main() {

    int n, i;

    int fact = 1;

    printf("Enter an integer: ");

    scanf("%d", &n);

 

    // shows error if the user enters a negative integer

    if (n < 0)

        printf("Error! Factorial of a negative number doesn't exist.");

    else {

        for (i = 1; i <= n; ++i) {

            fact *= i;

        }

        printf("Factorial of %d = %d", n, fact);

    }

 

    return 0;

}

Find the roots of a quadratic equation

#include <math.h>

#include <stdio.h>

int main() {

    float a, b, c, discriminant, root1, root2, realPart, imagPart;

    printf("Enter coefficients a, b and c: ");

    scanf("%f %f %f", &a, &b, &c);

 

    discriminant = b * b - 4 * a * c;

 

    // condition for real and different roots

    if (discriminant > 0) {

        root1 = (-b + sqrt(discriminant)) / (2 * a);

        root2 = (-b - sqrt(discriminant)) / (2 * a);

        printf("root1 = %.2lf and root2 = %.2lf", root1, root2);

    }

 

    // condition for real and equal roots

    else if (discriminant == 0) {

        root1 = root2 = -b / (2 * a);

        printf("root1 = root2 = %f;", root1);

    }

 

    // if roots are not real

    else {

        realPart = -b / (2 * a);

        imagPart = sqrt(-discriminant) / (2 * a);

        printf("root1 = %f+%fi and root2 = %f-%fi", realPart, imagPart, realPart, imagPart);

    }

 

    return 0;

}

Series of prime numbers upto nth terms

#include<stdio.h>

void main(){

   int i, num, n, count;

   printf("Enter the range: ");

   scanf("%d", &n);

   printf("The prime numbers in between the range 1 to %d:",n);

   for(num = 1;num<=n;num++){

      count = 0;

      for(i=2;i<=num/2;i++){

         if(num%i==0){

            count++;

       

      }

   }

   if(count==0 && num!= 1)

      printf("%d ",num);

   }

}

Check whether a number is prime or not

#include<stdio.h> 

int main(){   

int n,i,m=0,flag=0;   

printf("Enter the number to check prime:");   

scanf("%d",&n);   

m=n/2;   

for(i=2;i<=m;i++)   

{   

if(n%i==0)   

{   

printf("Number is not prime");   

flag=1;   

 

}   

}   

if(flag==0)   

printf("Number is prime");    

return 0; 

 }   

Implement newton forward and backward interpolition method

#include <stdio.h>

 

int main() {

    int n,i,j;

 

    printf("Enter the number of data points: ");

    scanf("%d", &n);

 

    float x[n], y[n][n];

 

    printf("Enter the data points:\n");

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

        printf("x[%d] = ", i);

        scanf("%f", &x[i]);

        printf("y[%d] = ", i);

        scanf("%f", &y[i][0]);

    }

 

    // Calculate the divided differences for Newton's forward interpolation

    for (j = 1; j < n; j++) {

        for ( i = 0; i < n - j; i++) {

            y[i][j] = y[i + 1][j - 1] - y[i][j - 1];

        }

    }

 

    // Perform Newton's forward interpolation

    float value, result_forward = y[0][0], term_forward = 1;

    printf("Enter the value for interpolation: ");

    scanf("%f", &value);

 

    for ( i = 1; i < n; i++) {

        term_forward *= (value - x[i - 1]) / (i);

        result_forward += term_forward * y[0][i];

    }

 

    // Calculate the divided differences for Newton's backward interpolation

    for ( j = 1; j < n; j++) {

        for ( i = n - 1; i >= j; i--) {

            y[i][j] = y[i][j - 1] - y[i - 1][j - 1];

        }

    }

 

    // Perform Newton's backward interpolation

    float result_backward = y[n - 1][0], term_backward = 1;

 

    for ( i = 1; i < n; i++) {

        term_backward *= (value - x[n - i]) / (i);

        result_backward += term_backward * y[n - 1][i];

    }

 

    // Print the results

    printf("Newton's Forward Interpolation at x = %.2f is %.6f\n", value, result_forward);

    printf("Newton's Backward Interpolation at x = %.2f is %.6f\n", value, result_backward);

 

    return 0;

}

Bisection method:

#include <stdio.h>

#include <math.h>

 

int main() {

    double a, b, c, tolerance;

 

    // Get user input for the interval [a, b] and tolerance

    printf("Enter the interval [a, b]: ");

    scanf("%lf %lf", &a, &b);

 

    printf("Enter the tolerance: ");

    scanf("%lf", &tolerance);

 

    // Check if the initial values have opposite signs

    if ((a * a - 4) * (b * b - 4) >= 0) {

        printf("Bisection method cannot be applied as f(a) and f(b) have the same sign.\n");

        return 1; // You can handle this case differently based on your requirements.

    }

 

    // Bisection method loop

    do {

        // Find the midpoint

        c = (a + b) / 2;

 

        // Check if the root is found

        if ((c * c - 4) == 0.0) {

            break;

        }

 

        // Update the interval based on the sign of the function at the midpoint

        if ((c * c - 4) * (a * a - 4) < 0) {

            b = c;

        } else {

            a = c;

        }

 

    } while ((b - a) >= tolerance);

 

    // Print the result

    printf("The root is approximately %.6f\n", c);

 

    return 0;

}

Implement  trapezoidal rule

#include <stdio.h>

 

int main() {

    int n;

 

    printf("Enter the number of intervals: ");

    scanf("%d", &n);

 

    double a, b;

 

    printf("Enter the lower limit (a): ");

    scanf("%lf", &a);

 

    printf("Enter the upper limit (b): ");

    scanf("%lf", &b);

 

    double h = (b - a) / n;

    double result = 0.5 * (a*a + b*b); // Initialize with the values at the boundaries

 

    for (int i = 1; i < n; i++) {

        double x = a + i * h;

        result += x * x;

    }

 

    result *= h;

 

    printf("Result using the trapezoidal rule: %.6f\n", result);

 

    return 0;

}

Implement simpson 1/3 rule

#include <stdio.h>

 

int main() {

    int n;

 

    printf("Enter the number of intervals (must be even): ");

    scanf("%d", &n);

 

    if (n % 2 != 0) {

        printf("Number of intervals must be even for Simpson's 1/3 rule.\n");

        return 1;

    }

 

    double a, b;

 

    printf("Enter the lower limit (a): ");

    scanf("%lf", &a);

 

    printf("Enter the upper limit (b): ");

    scanf("%lf", &b);

 

    double h = (b - a) / n;

    double result = a * a + b * b; // Initialize with the values at the boundaries

 

    for (int i = 1; i < n; i++) {

        double x = a + i * h;

        result += 2 * (i % 2 == 0 ? 2 * x * x : 4 * x * x);

    }

 

    result *= h / 3;

 

    printf("Result using Simpson's 1/3 rule: %.6f\n", result);

 

    return 0;

}

Runge kutta method

#include <stdio.h>

 

int main() {

    double h, x0, y0, xn;

 

    // Get user input for step size (h), initial values (x0 and y0), and the final x-value (xn)

    printf("Enter the step size (h): ");

    scanf("%lf", &h);

 

    printf("Enter the initial x-value (x0): ");

    scanf("%lf", &x0);

 

    printf("Enter the initial y-value (y0): ");

    scanf("%lf", &y0);

 

    printf("Enter the final x-value (xn): ");

    scanf("%lf", &xn);

 

    // Perform the fourth-order Runge-Kutta method without a separate function

    while (x0 < xn) {

        double k1 = h * (x0 * x0 - y0);

        double k2 = h * ((x0 + h / 2) * (x0 + h / 2) - (y0 + k1 / 2));

        double k3 = h * ((x0 + h / 2) * (x0 + h / 2) - (y0 + k2 / 2));

        double k4 = h * ((x0 + h) * (x0 + h) - (y0 + k3));

 

        y0 = y0 + (k1 + 2 * k2 + 2 * k3 + k4) / 6;

        x0 = x0 + h;

    }

 

    // Print the result

    printf("The result at x = %.2f is y = %.6f\n", xn, y0);

 

    return 0;

}

Comments

Popular Posts