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;
}

7.b Write a C Program to compute the values of cos x value using Series expansion


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


double cos_x( int, int );
int fact( int );

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


                    printf( "cos(x)\nEnter x and n:\t" );
                    scanf( "%d %d", &x, &n );
                    printf( "\ncos(%d)(%d) = %f\n", x, n, cos_x( x, n ) );

}


double cos_x( int ang_deg, int no_of_terms )
{
    int term, j;
    double value = 1.0, ang_rad = 0.0;
    ang_rad = ( double ) ang_deg * PI / 180;

    for ( term = 2, j = 1;term <= no_of_terms;term += 2, j++ )
        {
            value += ( double ) pow( -1, j ) * pow( ang_rad, term ) / fact( term );
        }

    return value;
}

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

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

    return f;
}

7 a .Write a C Program to compute the values ofsin x value using Series expansion


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

double sin_x( int, int );

int fact( int );

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


                    printf( "sin(x)\nEnter x and n:\t" );
                    scanf( "%d %d", &x, &n );
                    printf( "\nsin(%d)(%d) = %f\n", x, n, sin_x( x, n ) );



}


double sin_x( int ang_deg, int no_of_terms )
{
    int term, j;
    double value = 0.0, ang_rad = 0.0;
    ang_rad = ( double ) ang_deg * PI / 180;

    for ( term = 1, j = 2;term < no_of_terms*2;term += 2, j++ )
        {
            value += ( double ) pow( -1, j ) * pow( ang_rad, term ) / fact( term );
        }

    return value;
}


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

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

    return f;
}

Friday, 14 October 2016

8.b write a c program to perform bubble sort and selectionsort

8b.c
Aim:-write a c program to perform bubble sort.
Program:-
#include<stdio.h>
int main()
{
  int n,temp,i,j,a[20];
  printf("Enter total numbers of elements: ");
  scanf("%d",&n);
  printf("Enter %d elements: ",n);
  for(i=0;i<n;i++)
  scanf("%d",&a[i]);
  for(i=n-2;i>=0;i--)
  {
   for(j=0;j<=i;j++)
   {
    if(a[j]>a[j+1])
     {
       temp=a[j];
       a[j]=a[j+1];
       a[j+1]=temp;
     }
   }
 }
printf("After sorting: ");
for(i=0;i<n;i++)
printf(" %d",a[i]);
}
8bs.c
Aim:-write a c program to perform selection.
Program:-
#include<stdio.h>
int main()
{
 int s,i,j,temp,a[20];
 printf("Enter total elements: ");
 scanf("%d",&s);
 printf("Enter %d elements: ",s);
 for(i=0;i<s;i++)
 scanf("%d",&a[i]);
for(i=0;i<s;i++)
{
 for(j=i+1;j<s;j++)
  {
   if(a[i]>a[j])
   {
    temp=a[i];
    a[i]=a[j];
    a[j]=temp;
   }
 }
}
printf("After sorting is: ");
for(i=0;i<s;i++)
printf(" %d",a[i]);
}


Write a C Program to convert decimal to binary and hex (using switch call function the function)


6b
Aim:- Write a C Program to convert decimal to binary and hex (using switch call function the function)

Program:-

#include<stdio.h>
#include<math.h>
int D2B(int);
int D2H(int);
main()
{
  int ch,num;
  printf("\n1.Decimal 2 Binary.");
  printf("\n2.Decimal 2 Hexa.");
  printf("\nEnter your Choice:");
  scanf("%d",&ch);
  switch(ch)
   {
     case 1:
          printf("\nEnter any Decimal number:");
          scanf("%d",&num);
          printf("Binary number of %d is %d\n",num,D2B(num));
          break;
     case 2:
          printf("\nEnter any Decimal number:");
          scanf("%d",&num);
          printf("Hexa Decimal number of %d is",num);
          D2H(num);
          break;
     default:
         printf("\nEnter proper Choice:");
         break;
   }
}
int D2B(int n)
{
    int remainder;
    long binary = 0, i = 1;

    while(n != 0) {
        remainder = n%2;
        n = n/2;
        binary= binary + (remainder*i);
        i = i*10;
    }
   return binary;
}

int D2H(int n)
{
int rem[50],i=0,length=0;
while(n>0)
   {
      rem[i]=n%16;
      n=n/16;
      i++;
      length++;
   }
 for(i=length-1;i>=0;i--)
  {
    switch(rem[i])
    {
      case 10:
          printf("A");
          break;
      case 11:
          printf("B");
          break;
      case 12:
          printf("C");
          break;
      case 13:
          printf("D");
          break;
      case 14:
          printf("E");
          break;
      case 15:
          printf("F");
          break;
      default :
         printf("%d",rem[i]);
    }
  }
}


Write a C Program to make a simple Calculator to Add, Subtract, Multiply or Divide Using switch case.

Exercise – 6 Control Flow - III

Aim:Write a C Program to make a simple Calculator to Add, Subtract, Multiply or Divide Using switch case.

Program:-

#include<stdio.h>
void main()
{
int a,b,c,ch;
printf("enter a, b values");
scanf("%d%d",&a,&b);
printf(" 1.ADD 2.SUB 3.MUL 4.DIV ");
printf("\nenter your choice");
scanf("%d",&ch);
switch(ch)
                {
                case 1:c=a+b;
                                printf("\nsum is %d",c);
                                break;
                case 2:c=a-b;
                                printf("\nsub is %d",c);
                                break;
                case 3:c=a*b;
                                printf("\nmul is %d",c);
                                break;
                case 4: c=a/b;
                                printf("\ndiv is %d",c);
                                break;
                default:printf("enter correct choice");
                                break;
                }

}

Write a C Program illustrating Factorial and print fibnocci series without Recursion.

5bb
Aim:-Write a C Program illustrating Factorial without Recursion.

Program:-

#include <stdio.h>
int main()
{
  int c, n, fact = 1;
  printf("Enter a number to calculate it's factorial\n");
  scanf("%d", &n);
  for (c = 1; c <= n; c++)
    fact = fact * c;
  printf("Factorial of %d = %d\n", n, fact);
}
5bc
Aim:-Write a c program to print fibnocci series with out recursion.
Program:-
#include<stdio.h>
int main()
{
  int k,r;
  int i=0,j=1,f;
  printf("Enter the number range:");
  scanf("%d",&r);
  printf("FIBONACCI SERIES: ");
  printf("%d %d",i,j);
  for(k=2;k<r;k++)
  {
    f=i+j;
    i=j;
    j=f;
    printf(" %d",j);
  }
   return 0;

}

Write a C Program demonstrating of parameter passing in Functions and returning values

Exercise – 5 Functions

5a1.c
Aim:-a) Write a C Program demonstrating of parameter passing in Functions and returning values.
#include<stdio.h>
void add();
 main()
{
                printf("without parameters, without return values\n");
                add();
}

void add()
{
int a,b,temp;
printf("enter two no's for addition");
scanf("%d%d",&a,&b);
                printf("the  two no's are\n%d\t%d\n",a,b);

temp=a+b;
printf("after addition \n%d",temp);

}



5a2.c
#include<stdio.h>
void add(int,int);
 main()
{
            int x,y;
            printf("with parameters, with out return values\n");
            printf("enter two no's for addition");
            scanf("%d%d",&x,&y);
            printf("the  two no's are\n%d\t%d\n",x,y);
            add(x,y);
}

void add(int a, int b)
{
int temp;
temp=a+b;
printf("after addition \n%d",temp);

}



5A3.C
#include<stdio.h>
int add();
 main()
{
            int x;
            printf("without parameters, with return values\n");
           
             x=add();
            printf("after addition \n%d",x);
           
}

int add()
{
            int temp,a,b;
            printf("enter two no's for addition");
            scanf("%d%d",&a,&b);
            printf("the  two no's are\n%d\t%d\n",a,b);
            temp=a+b;
            return temp;


}




5A4.C
#include<stdio.h>
int add(int,int);
 main()
{
                int x,y,z;
                printf("with  parameters, with return values\n");
                printf("enter two no's for addition");
                scanf("%d%d",&x,&y);
                printf("the  two no's are\n%d\t%d\n",x,y);
                 z=add(x,y);
                printf("after addition \n%d",z);
               
}

int add(int a,int b)
{
                int temp;
                temp=a+b;
                return temp;
}


Write a C program to print Floyd Triangle

4b.c

Aim:-Write a C program to print Floyd Triangle
 
Program:-

#include<stdio.h>
   int main()
{
   int i, j, k = 1;
   int range;
   printf("Enter the range: ");
   scanf("%d", &range);
   printf("\nFLOYD'S TRIANGLE : \n");
   for (i = 1; i <= range; i++)
     {
         for (j = 1; j <= i; j++, k++)
         {
             printf("%d", k);
         }
      printf("\n");
   }

}

Write a C Program to Find Whether the Given Number is Armstrong number

4a2.c
Aim:- b)Write a C Program to Find Whether the Given Number is Armstrong number

Program:-

#include<stdio.h>
 main()
{
    int num,r,sum=0,temp;
    printf("Enter a number: ");
    scanf("%d",&num);
    temp=num;
    while(num!=0)
    {
         r=num%10;
         num=num/10;
         sum=sum+(r*r*r);
    }
    if(sum==temp)
    printf("%d is an Armstrong number",temp);
    else
    printf("%d is not an Armstrong number",temp);

}

Write a C Program to Find Whether the Given Number is Prime Number

Exercise – 4 Control Flow - II

a)Write a C Program to Find Whether the Given Number is Prime Number

program:-


#include<stdio.h>
void main()
{
                int n,i,c=0;
printf("enter a number to check if it is a prime");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
                if(n%i==0)          
                {
                                c=c+1;
                }
}
if(c==2)
printf("%d is a prime\n",n);
else
printf("%d is not a prime\n",n);

}