Showing posts with label cp. Show all posts
Showing posts with label cp. Show all posts

Sunday 8 October 2017

Convert a square matrix to Lower and Upper Triangular Matrix


Upper and Lower triangle Matrix Using C program

In this program we will be discussing on how to convert a square matrix into corresponding upper and lower triangle matrix.

LOGIC:

If for a(ij) :

  1. i=j, represents main diagonal elements
  2. i>j, represents elements to the left of main diagonal elements
  3. i<j, represents elements to the right of main diagonal elements


   #include<stdio.h>
   #include<conio.h>
   void main()
   {
   clrscr();
   int m,n,i,j;
   float a[100][100],cpy[100][100];
   printf("Enter number of rows ");
   scanf("%d",&m);
   printf("\n Enter elements for matrix \n");
   for(i=0;i<m;i++)
   for(j=0;j<m;j++)
   {
   scanf("%f",&a[i][j]);
   cpy[i][j]=a[i][j];
   }
   for(i=0;i<m;i++)
   for(j=0;j<m;j++)
   {
   if(i>j)
   a[i][j]=0;
   if(i<j)
   cpy[i][j]=0;
   }
   printf("Upper triangle matrix is \n");
   for(i=0;i<m;i++)
   {
   for(j=0;j<m;j++)
   printf("%f\t",a[i][j]);
   printf("\n");
   }
   printf("Lower triangle matrix is \n");
   for(i=0;i<m;i++)
   {
   for(j=0;j<m;j++)
   printf("%f\t",cpy[i][j]);
   printf("\n");
   }
   getch();
   }
Read more ...

Saturday 30 September 2017

Sum of numbers using c program

In this tutorial we will discuss on how to find the sum of two numbers using c program.
For any c program the header file that must be included is stdio.h which stands for standard input output.

PROGRAM:

#include<stdio.h>
void main()
{
int a,b,sum;
printf(“Enter two numbers \n”);
scanf(“%d%d”,&a,&b);
sum=a+b;
printf(“Sum of two numbers is %d”,sum);
}



OUTPUT:

Enter two numbers
2 3

Sum of two numbers is 5
Read more ...

Upper case String to Lower case string

/* C Program - Convert Uppercase String to Lowercase */
  
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
  clrscr();
  char str[20];
  int i;
  printf("Enter the String (Enter First Name) in uppercase : ");
  scanf("%s",str);
  for(i=0;i<=strlen(str);i++)
  {
     if(str[i]>=65 && str[i]<=92)
     {
  str[i]=str[i]+32;
     }
  }
  printf("\nThe String in Lowercase = %s",str);
  getch();
}

Concept 

ASCII code of A is 65 and ASCII code of Z is 92.
ASCII code of a is 97 and Ascii code of z is 122.
Read more ...

Search an element in an array

#include<stdio.h>
void main() {
   int a[30], ch, num, i;
   printf("\nEnter no of elements :");
   scanf("%d", &num);
   printf("\nEnter the values :");
   for (i = 0; i < num; i++) {
      scanf("%d", &a[i]);
   }
   //Read the element to be searched
   printf("\nEnter the elements to be searched :");
   scanf("%d", &ch);
   //Search starts from the zeroth location
   i = 0;
   while (i < num && ch != a[i]) {
      i++;
   }
   //If i < num then Match found
   if (i < num) {
      printf("Number found at the location = %d", i + 1);
   } else {
      printf("Number not found");
   }
Read more ...

Matrix addition,multiplication and substraction using c program

    /*  MATRIX ADDITION, SUBTRACTION AND MULTIPLICATION  */

#include<stdio.h>
#include<conio.h>
void main()
 {
   int i,j,c,r,k;
   int a[20][20],b[20][20],ma[20][20],ms[20][20];
   int mm[20][20];
   clrscr();
   printf("\n\t\tINPUT:");
   printf("\n\t\t------");
   printf("\n\t\tEnter the value for row and column:  ");
   scanf("%d%d",&c,&r);
   printf("\n\t\tEnter the value for matrix A\n");
   for(i=0;i<c;i++)
     {
       for(j=0;j<r;j++)
        {
         scanf("%d",&a[i][j]);
        }
       printf("\n");
     }
   printf("\n\t\tEnter the value for matrix B\n");
   for(i=0;i<c;i++)
     {
       for(j=0;j<r;j++)
        {
          scanf("%d",&b[i][j]);
        }
       printf("\n");
     }
   for(i=0;i<c;i++)
     {
       for(j=0;j<r;j++)
         {
          ma[i][j]=a[i][j]+b[i][j];
          ms[i][j]=a[i][j]-b[i][j];
         }
     }
   for(i=0;i<c;i++)
     {
       for(j=0;j<r;j++)
         {
           mm[i][j]=0;
           for(k=0;k<c;k++)
             {
               mm[i][j] +=a[i][k]*b[k][j];
             }
         }
     }
   printf("\n\t\tOUTPUT:");
   printf("\n\t\t-------");
   printf("\n\t\tThe addition matrix is:\n");
   for(i=0;i<c;i++)
     {
       for(j=0;j<r;j++)
         {
          printf("\t\t%d",ma[i][j]);
         } 
       printf("\n");
     }
   printf("\n\t\tThe subtraction matrix is:\n");
   for(i=0;i<c;i++)
     {
       for(j=0;j<r;j++)
         {
           printf("\t\t%d",ms[i][j]);
         }
       printf("\n");
     }
   printf("\n\t\tThe multiplication matrix is:\n");
   for(i=0;i<c;i++)
     {
       for(j=0;j<r;j++)
         {
           printf("\t\t%d",mm[i][j]);
         }
       printf("\n");
     }
   getch();
 }


INPUT:
------
Enter the value for row and column:  2   2
Enter the value for matrix A
4   3
6   2
Enter the value for matrix B
8   1
0   5

OUTPUT:
-------
The addition matrix is:
          12   4
          6    7
The subtraction matrix is:
          -4   2
           6  -3
The multiplication matrix is:
           32  19


           48  16
Read more ...

Find mean,median and mode using c program


#include<stdio.h>
main()
{
int i,j,a[20]={0},sum=0,n,t,b[20]={0},k=0,c=1,max=0,mode;
float x=0.0,y=0.0;
printf("\nEnter the limit\n");
scanf("%d",&n);
printf("Enter the set of numbers\n");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
sum=sum+a[i];
}
x=(float)sum/(float)n;
printf("Mean\t= %f",x);

for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if(a[i]>a[j])
{
t=a[i];
a[i]=a[j];
a[j]=t;
}
}
}
if(n%2==0)
y=(float)(a[n/2]+a[(n-1)/2])/2;
else
y=a[(n-1)/2];
printf("\nMedian\t= %f",y);

for(i=0;i<n-1;i++)
{
mode=0;
for(j=i+1;j<n;j++)
{
if(a[i]==a[j])
{
mode++;
}
}
if((mode>max)&&(mode!=0))
{
k=0;
max=mode;
b[k]=a[i];
k++;
}
else if(mode==max)
{
b[k]=a[i];
k++;
}
}
for(i=0;i<n;i++)
{
if(a[i]==b[i])
c++;
}
if(c==n)
printf("\nThere is no mode");
else
{
printf("\nMode\t= ");
for(i=0;i<k;i++)
printf("%d ",b[i]);
}
}

Read more ...

Convert a square matrix to Lower and Upper Triangular Matrix

Upper and Lower triangle Matrix Using C program In this program we will be discussing on how to convert a square matrix into correspon...