Friday, 28 October 2016

10b.
Aim:- Write a C Program to find the sum of numbers with arrays and pointers.
Program:-

#include<stdio.h>
void main()
{
intnumArray[10];
inti, sum = 0;
int *ptr;
printf("\nEnter 10 elements : ");
for (i = 0; i< 10; i++)
scanf("%d", &numArray[i]);
ptr = numArray; /* a=&a[0] */
for (i = 0; i< 10; i++)
  {
sum = sum + *ptr;
ptr++;
   }
printf("The sum of array elements : %d", sum);
}




10.a Write a C Program to Access Elements of an Array Using Pointer

Exercise - 10 Arrays and Pointers
a)Write a C Program to Access Elements of an Array Using Pointer

/* To Read integers into an array and Reversing them using Pointers*/
#include<stdio.h>
#define MAX 30
void main()
{
int size, i, arr[MAX];
int *ptr;
ptr = &arr[0];
printf("\nEnter the size of array : ");
scanf("%d", &size);
printf("\nEnter %d integers into array: ", size);
for (i = 0; i< size; i++)
  {
scanf("%d", ptr);
ptr++;
   }
ptr = &arr[size - 1];
printf("\nElements of array in reverse order are :");
for (i = size - 1; i>= 0; i--)
  {
printf("%d\t",*ptr);
ptr--;
   }

 }

Thursday, 20 October 2016

4c.c write a c program to display pascal triangle

#include<stdio.h>

int main()
{
int line,i,j;
printf("enter no of lines\n");
scanf("%d",&line);
for(i=0;i<line;i++)
{
for(j=0;j<line-i-1;j++)
printf(" ");
for(j=0;j<=i;j++)
printf(" %d",fact(i)/(fact(j)*fact(i-j)));
printf("\n");
}
return 0;
}
int fact(int num)
{
long f=1;
int i=1;
while(i<=num)
{
f=f*i;
i++;
}
return f;
}




8cb write a c program to perform operations on marix multiplication

#include<stdio.h>
void main()
{
int a[3][3],b[3][3],c[3][3],i,j,k;
printf("\n enter first matrix");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("\n enter second matrix");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
scanf("%d",&b[i][j]);
}
}
printf("\n first matrix is");
for(i=0;i<3;i++)
{
printf("\n");
for(j=0;j<3;j++)
{
printf("%d\t",a[i][j]);
}
}
printf("\n secon matrix is");
for(i=0;i<3;i++)
{
printf("\n");
for(j=0;j<3;j++)
{
printf("%d\t",b[i][j]);
}
}
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
c[i][j]=0;
for(k=0;k<3;k++)
c[i][j]=c[i][j]+a[i][k]*b[k][j];
}
}
printf("\n matrix multiplication is");
for(i=0;i<3;i++)
{
printf("\n");
for(j=0;j<3;j++)
{
printf("%d\t",c[i][j]);
}
}
}





8ca write a c program to perform operations on marix

#include<stdio.h>
void main()
{
int a[3][3],b[3][3],c[3][3],i,j;
printf("\n enter first matrix");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("\n enter second matrix");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
scanf("%d",&b[i][j]);
}
}
printf("\n first matrix is");
for(i=0;i<3;i++)
{
printf("\n");
for(j=0;j<3;j++)
{
printf("%d\t",a[i][j]);
}
}
printf("\n secon matrix is");
for(i=0;i<3;i++)
{
printf("\n");
for(j=0;j<3;j++)
{
printf("%d\t",b[i][j]);
}
}
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
c[i][j]=a[i][j]+b[i][j];
}
}
printf("\n matrix addition is");
for(i=0;i<3;i++)
{
printf("\n");
for(j=0;j<3;j++)
{
printf("%d\t",c[i][j]);
}
}
}





8.a write a c program to perform linear search.

#include<stdio.h>
void main()
{
int a[10],i,n,m,c=0;
printf("\n enter the size of the array");
scanf("%d",&n);
printf("\n enter the elements of the array");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
printf("\n enter the number to be searched");
scanf("%d",&m);
for(i=0;i<=n-1;i++)
{
if(a[i]==m)
{
printf("\n number %d found at position %dn",a[i],i);
c=1;
}
}
if(c==0)
{
printf("\n the number is not found");
}

}


7.c Write a C Program to compute the values of e^x value using Series expansion


#include<stdio.h>
#include<math.h>
#define PI 3.1415

double exp_x( int, int );

int fact( int );

int main()
{
    int choice, x, n;


                    printf( "e^x\nEnter x and n:\t" );
                    scanf( "%d %d", &x, &n );
                    printf( "e^%d(%d) = %f\n", x, n, exp_x( x, n ) );



}

double exp_x( int x, int n )
{
    int i = 1;
    float ex = 1;

    while ( i < n )
        {
            ex += ( double ) pow( x, i ) / fact( i );
            ++i;
        }

    return ex;
}



int fact( int num )
{
    int f = 0;

    if ( num == 1 )
        return 1;
    else
 f = num * fact( num - 1 );

    return f;
}