Tuesday, 1 March 2016

User Define Function- Reverse Number

Q. Write a C program to create a function that would reverse the any number.

For example:
Assume enter number = 57429
Result = 92475


Ans:

#include<stdio.h>
int rev(int );
int main()
{
 int num,res;  // res = result
 printf("Enter any number : ");
 scanf("%d", &num);
 res = rev(num);
 printf("Reverse order number = %d",res);
 return 0;
}

int rev(int n)
{
 int r=0;
 for(; n>=1; n=n/10)
   r = r*10 + n%10;
 return r;

}

Output


Difference of matrix

Q. Write a C program to accept value of matrix and print out the difference or subtract  of matrix.
OR
Q. Give a example of 2d array with suitable arithmetic operation.

Ans. 

#include<stdio.h>
#include<conio.h>
#define MAX 3
void input_mat(int [MAX][MAX], int, int);
void show_mat(int [MAX][MAX], int, int);
void diff_mat(int [MAX][MAX],int [MAX][MAX],int [MAX][MAX],int,int);
int main()
{
 int x[MAX][MAX],y[MAX][MAX],z[MAX][MAX];
 int row,col;
 printf("Enter no. of rows and columns : ");
 scanf("%d%d",&row, &col);
 printf("Enter values in first matrix :\n");
 input_mat(x,row,col);
 printf("Enter values in second matrix :\n");
 input_mat(y,row,col);
 diff_mat(x,y,z,row,col);
 printf("\nDisplay difference of two matrices :");
 show_mat(z,row,col);
 getch();
 return 0;
}


void input_mat(int matA[MAX][MAX], int r, int c)
{
  int i,j;
  for(i=0; i<r; i++)
  {
    for(j=0;j<c; j++)
       scanf("%d",&matA[i][j]);
  }
}


void diff_mat(int matA[MAX][MAX],int matB[MAX][MAX],int matC[MAX][MAX],int r,int c)
{
  int i,j;
  for(i=0; i<r; i++)
  {
    for(j=0;j<c; j++)
       matC[i][j]=matA[i][j]-matB[i][j];
  }
}


void show_mat(int mat[MAX][MAX], int r, int c)
{
  int i,j;
  for(i=0; i<r; i++)
  {
    for(j=0;j<c; j++)
       printf(" %d",mat[i][j]);
    printf("\n");
  }
}

Output:

Enter no. of rows and columns : 3 3
Enter values in First matrix :
4 5 3
7 6 1
6 9 5
Enter values in second matrix :
1 3 2
7 6 4
5 1 0
Display difference of two matrices :
3 2 1
0 0 -3
1 8 5

Sum of matrix

Q. Write a C program to accept value of matrix and print out the sum of matrix.
OR
Q. Give a example of 2d array with suitable arithmetic operation. 

Ans.

#include<stdio.h>
#include<conio.h>
#define MAX 3
void input_mat(int [MAX][MAX], int, int);
void show_mat(int [MAX][MAX], int, int);
void sum_mat(int [MAX][MAX],int [MAX][MAX],int [MAX][MAX],int,int);
int main()
{
 int x[MAX][MAX],y[MAX][MAX],z[MAX][MAX];
 int row,col;
 printf("Enter no. of rows and columns : ");
 scanf("%d%d",&row, &col);
 printf("Enter values in first matrix :\n");
 input_mat(x,row,col);
 printf("Enter values in second matrix :\n");
 input_mat(y,row,col);
 sum_mat(x,y,z,row,col);
 printf("\nDisplay sum of two matrices :");
 show_mat(z,row,col);
 getch();
 return 0;
}


void input_mat(int matA[MAX][MAX], int r, int c)
{
  int i,j;
  for(i=0; i<r; i++)
  {
    for(j=0;j<c; j++)
       scanf("%d",&matA[i][j]);
  }
}


void sum_mat(int matA[MAX][MAX],int matB[MAX][MAX],int matC[MAX][MAX],int r,int c)
{
  int i,j;
  for(i=0; i<r; i++)
  {
    for(j=0;j<c; j++)
       matC[i][j]=matA[i][j]+matB[i][j];
  }
}


void show_mat(int mat[MAX][MAX], int r, int c)
{
  int i,j;
  for(i=0; i<r; i++)
  {
    for(j=0;j<c; j++)
       printf(" %d",mat[i][j]);
    printf("\n");
  }
}

Output:

Enter no. of rows and columns : 3 3
Enter values in First matrix :
4 5 3
2 1 9
6 7 5
Enter values in second matrix :
1 3 2
7 6 4
2 1 0
Display sum of two matrices :
5 8 5
9 7 13
8 8 5


Transpose of matrix



Q. Write a C program to accept value of matrix and find the transpose matrix.

Ans.

#include<stdio.h>
#include<conio.h>
#define MAX 3
void input_mat(int [MAX][MAX], int, int);
void show_mat(int [MAX][MAX], int, int);
void trns_mat(int [MAX][MAX],int [MAX][MAX],int,int);
int main()
{
 int x[MAX][MAX],z[MAX][MAX];
 int row,col;
 printf("Enter no. of rows and columns : ");
 scanf("%d%d",&row, &col);
 printf("Enter values of %d X %d matrix :\n",row,col);
 input_mat(x,row,col);
 printf("\nYour entered matrix is : \n");
 show_mat(x,row,col);
 trns_mat(x,z,row,col);
 printf("\nTranspose of entered matrix is :\n");
 show_mat(z,row,col);
 return 0;
}

void input_mat(int matA[MAX][MAX], int r, int c)
{
  int i,j;
  for(i=0; i<r; i++)
  {
    for(j=0;j<c; j++)
       scanf("%d",&matA[i][j]);
  }
}


void trns_mat(int matA[MAX][MAX],int matT[MAX][MAX],int r,int c)
{
  int i,j;
  for(i=0; i<r; i++)
  {
    for(j=0;j<c; j++)
       matT[j][i] = matA[i][j];
  }
}


void show_mat(int mat[MAX][MAX], int r, int c)
{
  int i,j;
  for(i=0; i<r; i++)
  {
    for(j=0;j<c; j++)
       printf(" %d",mat[i][j]);
    printf("\n");
  }
}

Output:


Product of matrix

Q. Write a C program to accept value of matrix and find out the product of matrix.
OR
Q. Give a example of 2d array with suitable arithmetic operation.

Ans. 


#include<stdio.h>
#include<conio.h>
#define MAX 3
void input_mat(int [MAX][MAX], int, int);
void show_mat(int [MAX][MAX], int, int);
void prod_mat(int [MAX][MAX],int [MAX][MAX],int [MAX][MAX],int,int);
int main()
{
 int x[MAX][MAX],y[MAX][MAX],z[MAX][MAX];
 int row,col;
 printf("Enter no. of rows and columns : ");
 scanf("%d%d",&row, &col);
 printf("Enter values in first matrix :\n");
 input_mat(x,row,col);
 printf("Enter values in second matrix :\n");
 input_mat(y,row,col);
 prod_mat(x,y,z,row,col);
 printf("\nDisplay product of two matrices :");
 show_mat(z,row,col);
 getch();
 return 0;
}

void input_mat(int matA[MAX][MAX], int r, int c)
{
  int i,j;
  for(i=0; i<r; i++)
  {
    for(j=0;j<c; j++)
       scanf("%d",&matA[i][j]);
  }
}


void prod_mat(int matA[MAX][MAX],int matB[MAX][MAX],int matC[MAX][MAX],int r,int c)
{
 int i,j,k;
 for(i=0; i<r; i++)
 {
  for(j=0;j<c; j++)
  {
    matC[i][j]=0; 
    for(k=0;k<c; k++)
    {
     matC[i][j]=matC[i][j]+matA[i][j]*matB[i][j];
    }
  }
 }

void show_mat(int mat[MAX][MAX], int r, int c)
{
  int i,j;
  for(i=0; i<r; i++)
  {
    for(j=0;j<c; j++)
       printf(" %d",mat[i][j]);
    printf("\n");
  }
}

Output:-

Enter no. of rows and columns : 3 3
Enter values in First matrix :
4 5 3
2 1 9
6 7 5
Enter values in second matrix :
1 3 2
7 6 4
2 1 0
Display product of two matrices :
12 45 18
42 18 108
36 21 0

Diagonal sum of matrix

Q. Write a C program to find the sum of diagonal elements in matrix.

Hint: If you have following typical square 3X3 matrix, then diagonal elements will be:

1  2  3
4  5  6
7  8  9

In above 3X3 matrix there are two types diagonal element as:
first diagonal elements are  1, 5, 9 and

second diagonal elements are 3, 5,7

Ans.

#include<stdio.h>
#include<conio.h>
#define MAX 5
int main()
{
  int mat[MAX][MAX],row,col;
  int i,j,d1=0,d2=0;
  printf("Enter no. of rows and columns : ");
  scanf("%d%d",&row,&col);
  printf("Enter the elements of matrix:\n");
  if(row==col)
  {
    for(i=0; i<row; i++)
    {
      for(j=0; j<col; j++)
         scanf("%d",&mat[i][j]);
    }
    for(i=0,j=col-1; i<row || j>=0; i++,j--)
    {
       d1=d1+mat[i][i];
       d2=d2+mat[i][j];
    }
    printf("\nThe sum of first diagonal elements : %d",d1);
    printf("\nThe sum of second diagonal elements : %d",d2);
  }
  else
  {
     printf("Rows and columns are not equal!");
     printf("\nTry again!");
  }
  getch();
  return 0;

}

Output:

Enter no. of rows and columns : 3  2
Enter the elements of matrix:
Rows and columns are not equal!

Try again!

Enter no. of rows and columns : 3 3
Enter the elements of matrix:
1  2  3
4  5  6
7  8  9
The sum of first diagonal elements : 15

The sum of second diagonal elements : 15 

Palindrome using pointer

Q. write a program to accept a string and find out whether it is palindrome or not  using pointer.

Ans.

#include<stdio.h>
#include<conio.h>
int main()
{
 char str[30];
 char *p,*t;
 printf("Enter any string : ");
 gets(str);
 for(p=str ; *p!=NULL ; p++);
  for(t=str, p-- ; p>=t; )
  {
    if(*p==*t)
    {
        p--;
        t++;
    }
    else
        break;
  }
  if(t>p)
       printf("\nString is palindrome");
  else
       printf("\nString is Not palindrome");
  getch();
  return 0;

}

Output:

Enter any string : SHAREMARKET

String is not palindrome

Ads Inside Post