20_10

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 C program which sort strings in lexicographical order.

 Source code:

//Write a C program which sort strings in lexicographical order.


#include<stdio.h>

#include <string.h>

int main()

{

    int i, j;

    char str[10][50], temp[50];

printf("Enter a string: ");

    /* Read 10 elements */

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

        scanf("%s[^\n]",str[i]);

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

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

        {

            if(strcmp(str[i], str[j])>0)

            {

                strcpy(temp, str[i]);

                strcpy(str[i], str[j]);

                strcpy(str[j], temp);

            }

        }

printf("In Lexicographical Order\n");

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

    {

        puts(str[i]);

    }

    return 0;

}

Output:
C program which sort strings in lexicographical order


Write a C program to pass multidimensional array to a function as it's parameter.

 Source code:

/*Consider a program, of passing multidimensional array to a function. Consider an two dimensional array of size 3x3 and write a function. Write a function displayarray() by passing two dimensional array as it's parameter.*/


#include<stdio.h>

void displayarray(int arr[3][3]);

void displayarray(int arr[3][3])

{

    int i, j;

    printf("The complete array is");

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

    {

        printf("\n");

                 

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

        {

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

        }

    }

}

int main()

{

    int arr[3][3], i, j;

    /* Read 9 elements */    

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

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

    {

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

        {

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

        }

    }

   /* passing the array as argument */

    displayarray(arr);

    return 0;

}

Output:
C program to pass multidimensional array to a function as it's parameter


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...