20_10

Write a C program to find a root of the equation a*x*x + b*x +c using Bi Section method.

 Source code:

/*Numerical Method: Bi Section method to find a root of the equation a*x*x + b*x +c.  The program read should the values of a,b and c and values for starting left and right points. */


#include <stdio.h>

#include <math.h>

double myFunction(double x, double a, double b,double c)

{

  return a*x*x + b*x +c;

}

int main(void){

double a,b,c;

double leftpt, rightpt, midpt, epsilon = .0000001;

double midvalue, rtvalue, root;

printf("Enter the value of 'a','b' and 'c': ");

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

printf("Enter the value of left and right point: ");

scanf("%lf %lf", &leftpt, &rightpt);

do {

        midpt = (leftpt + rightpt)/2;

        rtvalue = myFunction(rightpt,a,b,c);

        midvalue = myFunction(midpt,a,b,c);

        if (rtvalue * midvalue >= 0)

            rightpt = midpt;

        else 

leftpt  = midpt;

} while ((rightpt - leftpt) > epsilon);


root = (rightpt+leftpt)/2;

printf("Root for equation%5.2lf*x**2+%5.2lf*x+%5.2lf is",a,b,c);

printf("%5.2lf\n",root);

return 0;

}

Output:

C program to find a root of the equation using Bi Section method


Write a C program which will generate Fibonacci sequence using recursive function printFibonacci(int n).

 Source code:

/*A recursive procedure or routine is one that has the ability to call itself. Consider a C program which will generate Fibonacci sequence using function printFibonacci(int n). The sequence is characterized by the fact that every number after the first two is the sum of the two preceding ones. By definition, the first two numbers in the Fibonacci sequence are either 1 and 1, or 0 and 1, depending on the chosen starting point of the sequence, and each subsequent number is the sum of the previous two. Here the default is 0,1. So in case of input range 10 it will generate 12 numbers. */


#include<stdio.h>

void printFibonacci(int n){


    static int first=0,second=1,sum;


    if(n>0){

        sum = first + second;

        first = second;

        second = sum;

        printf("%d ",sum);

        printFibonacci(n-1);

    }

}

void printFibonacci(int);


int main(){

    int k,n;

    long int i=0,j=1,f;

    printf("Enter the nth term: ");

    scanf("%d",&n);


printf("%d %d ",0,1);

printFibonacci(n);

    return 0;

}

Output:
C program which will generate Fibonacci sequence using recursive function


Write a program in C to add two numbers using pointers.

 Source code:

/*Write a program in C to add two numbers using pointers. You have to write the fsum() function which accepts the address of two variables and returns the sum of their values to the main function. */


#include <stdio.h>

int fsum(int *n1, int *n2)

{

int sum = 0;

sum = *n1 + *n2;

return(sum);

}

int main()

{

   int no1, no2, sum;

 

   printf(" Input the first number : ");

   scanf("%d", &no1);

   printf(" Input the second  number : ");

   scanf("%d", &no2);   

 

   printf("The sum of the entered numbers is : %d\n",fsum(&no1,&no2));

   return 0;

}

Output:

add two numbers using pointers


Write a C program to swap two integers using pointers.

 Source code:

/*Write a C program to swap two integers using pointers. You have to write a swap function that will accept the address of two integer and swap their values.  */


#include <stdio.h>

void swap(int *a,int *b)

{

    int t;

     t   = *a;

    *a   = *b;

    *b   =  t;

}

int main()

{

    int num1,num2;

    printf("Enter 1st number: ");

    scanf("%d",&num1); /*Enter value of num1 */

    printf("Enter 2nd number: ");

    scanf("%d",&num2); /*Enter value of num2 */

     

    swap(&num1,&num2);

     

    /*print values after swapping */

    printf("num1=%d, num2=%d\n",num1,num2);    

    return 0;

}

Output:

C program to swap two integers using pointers


Write a C Program to find if the given square matrix is skew symmetric or not.

 Source code:

/*A Matrix whose Transpose is negative to that of the original Matrix, it is known as a Skewed Symmetric Matrix. Write a C Program to find if the given square matrix is skew symmetric or not. 

Ex. A^T = -A*/


#include<stdio.h>

void tranpose();

void getmatrix();

int check();

 

int m, n, rows, columns;

int matrix[3][3], transpose[3][3];

void tranpose()

{

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

    {

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

        {

            transpose[columns][rows] = matrix[rows][columns];

        }

    }

}

 

void getmatrix()

{

printf("Enter a matrix:\n");

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

    {

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

        {

            scanf("%d", &matrix[rows][columns]);

        }

    }

}

 

int check()

{

    if(m == n)

    {

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

        {

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

            {

                if(matrix[rows][columns] != -transpose[rows][columns])

                {

                    break;

                }

            }

            if(columns != m)

            {

                break;

            }

        }

    if(rows == m)

        {

            return 0;

        }

    }

    else

    {

        return 1;

    }

}

int main()

{

    int x;

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

    scanf("%d", &m); /*Enter the Number of Rows: */

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

    scanf("%d", &n); /*Enter the Number of Columns: */

    getmatrix();  /*Get the Elements of the Square Matrix from input case */

    tranpose(); /*Function to perform transpose of the matrix */

    x = check(); /*To check if the matrix is a skewed symmetric matrix */

    if(x == 0)

    {

        printf("The Entered Matrix is A Skewed Symmetric Matrix\n");

    }

    else

    {

        printf("The Entered Matrix is Not A Skewed Symmetric Matrix\n");

    }

    return 0;

}

Output:
C Program to find if the given square matrix is skew symmetric or not




Write a C program which will sort number of elements in an ascending by using bubble sort using pointer.

 Source code:

/*Write a C program which will sort number of elements in an ascending by using bubble sort. We will rewrite the algorithm by using pointer. Read 5 elements and sort in ascending order. */


#include<stdio.h>

void bubble(int *ptr,int s)

{

int i,j;

int temp;

for(i=1;i<s;i++)

  {

  for(j=0;j<s-i;j++)

    {

    if(*(ptr+j)>*(ptr+j+1))

    {

      temp=*(ptr+j);

      *(ptr+j)=*(ptr+j+1);

      *(ptr+j+1)=temp;

    }

    }

}

}

int main()

{

int arr[10];

int i;

int size=5;

/* Read Array Elements */

printf("Enter 10 elements: ");

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

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

 

bubble(arr,size);

printf("The sorted elements:\n");

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

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

return 0;

}

Output:
C program which will sort number of elements in an ascending by using bubble sort using pointer


Write a program in C to convert a decimal number to binary using recursion.

 Source code: //Write a program in C to convert a decimal number to binary using recursion. #include<stdio.h> long convertB_to_D(int d...