1.10.2012

Transpose of matrix


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

Ans.

/*program of transpose of matrix*/
#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");
  }
}


The output of above program would be:

Output of Transpose of matrix C program
Figure: Screen shot for transpose of matrix C program


You might also like:
  1. Difference of two matrix C program
  2. Product of matrix C program
  3. Sum of diagonal elements C program
  4. Sum of two matrix C program

1.09.2012

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.

/*program of sum of two matrix*/
#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

You might also like:
  1. Difference of two matrix C program
  2. Transpose of matrix C program
  3. Product of matrix C program
  4. Sum of diagonal elements C program
  5. Sum of all corner elements C program

1.08.2012

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.

/*program of difference of two matrix*/
#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

You might also like:
  1. Sum of two matrix C program
  2. Transpose of matrix C program
  3. Product of matrix C program
  4. Sum of diagonal elements C program
  5. Sum of all corner elements C program

1.07.2012

Quadratic eq. roots

Q. Write a program to find the roots of a quadratic equation.

Ans.


Process of finding root of quadratic equation in C program:
The roots of any quadratic equation of the the form,
 
ax2+bx+c=0
has minimum two roots.
The formula of calculating these roots are as:



Where:r1,r2 are roots. First of all user entered values of a,b and c. Then it finds out numerator- which is the discriminate. Then checks if the numerator is greater, equals or less than zero. Prints out the results according to the situation.


/*program to find the root of a quadratic equation*/
#include<stdio.h>

#include<conio.h>
#include<math.h>
int main()
{
  int a,b,c;
  float r1,r2,up;
  printf("Enter value of a : ");
  scanf("%d", &a);
  printf("Enter value of b : ");
  scanf("%d", &b);
  printf("Enter value of c : ");
  scanf("%d", &c);
  up=(b*b)-(4*a*c);
  if(up>0)

 {
    printf("\n ROOTS ARE REAL ROOTS\n");
    r1 = ((-b) + sqrt(up)) /(2*a);
    r2 = ((-b) - sqrt(up)) /(2*a);
    printf("\n ROOTS : %f, %f\n", r1, r2);
  }
  else if(up==0)
  {
    printf("\n ROOTS ARE EQUAL\n");
    r1 = (-b/(2*a));
    printf("\n ROOT IS...: %f\n", r1);
  }
  else
     printf("\n ROOTS ARE IMAGINARY ROOTS\n");
  getch();

 return 0;
}

Output:-
 

Enter value of a : 6
Enter value of b : -13
Enter value of c : 6
ROOTS ARE REAL ROOTS
ROOTS : 1.500000, 0.666667



Enter value of a : 1
Enter value of b : 1
Enter value of c : -2
ROOTS ARE REAL ROOTS
ROOTS : 1.000000, -2.000000

1.05.2012

Armstrong number

Q. Write a program to check whether a number is a Armstrong number or not.

Ans.

Definition of Armstrong number: An Armstrong number of three digits is an integer such that the sum of the cubes of its digits is equal to the number itself.
In other word “A number is Armstrong if it is equal the sum of cube of its digits.”
Example of Armstrong number is 371 because according to definition cube of its digits  sum will be equal to number so
Armstrong number 371=(3)3+(7)3+(1)3
                 371=27+343+1
                 371=371

/*program to check whether a number is Armstrong or not*/
#include<stdio.h>
#include<conio.h>
int main()
{
 int n,num,rem,sum=0;
 printf("Enter any number : ");
 scanf("%d", &num);
 for(n=num; n>=1; n=n/10)
 {
   rem=n%10;
   sum=sum+(rem*rem*rem);
 }
 if(num==sum)
    printf("Number is Armstrong number");
 else
    printf("Number is not Armstrong number");
 getch();
 return 0;
}

Output:-

Enter any number :254
Entered number is not Armstrong number
                   
Enter any number : 371
Entered number is Armstrong number


Related Program:
  1. Amicable number C program
  2. Flowchart for checking number is Armstrong or not
  3. Perfect number C program
  4. Print Armstrong number range C program

1.04.2012

Amicable numbers


Q. writes a C program to check whether given two numbers are amicable numbers or not.

Ans.

Definition of Amicable numbers: Amicable numbers are two numbers so related that the sum of the proper divisors of the one is equal to the other, unity being considered as a proper divisor but not the number itself.


For example, lets a pair or two number pair is (220,284),
For the proper divisor of 220 are 1, 2, 4, 5, 10, 11, 20, 22, 44, 55, 110, of which the sum is 284.
And the proper divisor of 284 are 1, 2, 4, 71, 142 of which the sum is 220.

/*program to check whether given numbers are amicable or not*/
#include<stdio.h>
#include<conio.h>
int main()
{
 int n1,n2,s1=0,s2=0,i;
 printf("Enter first number : ");
 scanf("%d",&n1);
 printf("Enter second number : ");
 scanf("%d",&n2);
 for(i=1; i<n1 ; i++)
 {
   if(n1%i==0)
      s1=s1+i;
 }
 for(i=1; i<n2 ; i++)
 {
   if(n2%i==0)
      s2=s2+i;
 }
 if(n1==s2 && n2==s1)
  printf("Given numbers are Amicable Numbers");
 else
  printf("Given numbers are Not Amicable Numbers");
 getch();
 return 0;
}

Output:-

Enter first number : 452
Enter second number : 264
Given numbers are Not Amicable Numbers

Enter first number : 220
Enter second number : 284
Given numbers are Amicable Numbers


Related Programs:
  1. Search Perfect number C program
  2. Search Armstrong number C program
  3. Print Armstrong number range C program
  4. Flowchart for check a number is Armstrong or not

Perfect number

Q. Write a C program to check whether a given number is a perfect number or not.

Ans.

Definition of Perfect number: A positive integer n is called a perfect number if it is equal to the sum of all of its positive divisors, excluding n itself.


For example, 6 is perfect integer number, because 1, 2 and 3 are its proper positive divisors and 1+2+3=6.
The next perfect number is 28 because 1+2+4+7+14=28.
The next perfect number is 496 because
1+2+4+8+16+31+62+124+248=496.

/*program to check whether a number is perfect or not*/
#include<stdio.h>
#include<conio.h>
int main()
{
 int i,n,num,sum=0;
 printf("Enter a number : ");
 scanf("%d",&num);
 n=num;
 for(i=1; i<n; i++)
 {
  if(num%i==0)
     sum=sum+i;
 }
 if(num==sum)
  printf("Given number is Perfect Number ");
 else
  printf("Given number is Not Perfect Number ");
 getch();
 return 0;
}

Output:-

Enter a number : 7543
Given number is Not Perfect Number

Enter a number : 8128
Given number is Perfect Number


Related Programs:
  1. Search Armstrong number C program
  2. Search Amicable number C program
  3. Print Armstrong number range C program
  4. Flowchart for check a number is Armstrong or not