Tuesday, March 1, 2011

CPDS WEEK Programs


Week 4
a) The total distance travelled by vehicle in ‘t’ seconds is given by distance      = ut+1/2at2 where ‘u’ and ‘a’ are the initial velocity (m/sec.) and acceleration (m/sec2). Write C program to find the distance travelled at regular intervals of time given the values of ‘u’ and ‘a’. The program should provide the flexibility to the user to select his own time intervals and repeat the calculations for different values of ‘u’ and ‘a’.
PROGRAM :
#include<stdio.h>
#include<conio.h>
void main()
{
        int u,t,a,n,x=0,i;
        float d;
        printf(" enter the values of u,a");
        scanf("%d%d",&u,&a);
        printf("\n enter the time interval and the limit ");
        scanf("%d%d",&n,&t);
        while(x<=t)
        {
        d=(u*x)+(a*x*x*0.5);
        printf("\nthe value of displacemnt for the  %d time is   %.2f\n",x,d);
        x+=n;
        }
getch( );
}


RESULT:
enter the values of u , a  :  4     5

 enter the time interval and the limit   2     16

the value of displacemnt for the  0 time is   0.00

the value of displacemnt for the  2 time is   18.00

the value of displacemnt for the  4 time is   56.00

the value of displacemnt for the  6 time is   114.00

the value of displacemnt for the  8 time is   192.00

the value of displacemnt for the  10 time is   290.00

the value of displacemnt for the  12 time is   408.00

the value of displacemnt for the  14 time is   546.00

the value of displacemnt for the  16 time is   704.00


a)     Write a C program, which takes two integer operands and one operator form the user, performs the operation and then prints the result. (Consider the operators +,-,*, /, % and use Switch Statement)
Program :

#include<conio.h>
#include<stdio.h>
void main( )
{
int a,b,result;
char c;
printf(" enter the two integers \n");
scanf("%d%d",&a,&b);
printf("1.for addition enter + \n");
printf("2.for subtraction enter - \n");
printf("3.for multiplication enter * \n");
printf("4.for division enter / \n");
printf("5.enter modulos to display the remainder \n");
printf(“Enter your CHOICE to perform the calculation \n”);
scanf(" %c",&c);
switch(c)
{
                        case ' + ' :
                        result=a+b;
                        printf(" the sum of  %d and %d is %d",a,b,result);
                        break;
                case ' - ' :
                        result=a-b;
                        printf(" the subraction of %d and %d  is%d",a,b,result);
                        break;
                case ' * ' :
                        result=a*b;
                        printf(" the multiplication of %d and %d is %d",a,b,result);
                        break;
                case ' / ' :
                        result=a/b;
                        printf(" the  divison of %d and %d is %d",a,b,result);
                        break;
                case ' % ' :
                                    result=a%b;
                                    printf(" the remainder of %d and %d id %d",a,b,result);
                                    break;
                        default:
                                    printf(" the enterd choice is not valid");
                                    break;
            }
            getch();
}
            RESULT:
Enter the two integers
29
12
1.For addition enter +
2.For subtraction enter -
3.For multiplication enter *
4.For division enter /
5.Enter modulos to display the remainder

Enter your CHOICE to perform the calculation :  -
The subraction of  29 and 12  is  : 17

Week 5
a) Write a C program to find both the larges and smallest number in a list of integers.
Program :
#include <stdio.h>
void main( )
{
        int a[30],max,min,i,n;
        printf(" Enter the Range of Numbers  : ");
        scanf("%d",&n);
        printf("\n Enter %d numbers \n",n);
        for(i=0;i<n;i++)
        scanf(" %d ", &a[ i ]);
        max=a[0];
        min =a[0];
        for(i=0;i<n;i++)
        {
                if(max<a[i])
                max=a[i];
                else
                if(min>a[i])
                min=a[i];
        }
        printf(" Maximum Value is %d  and  Minimum Value  is %d",max,min);
        }
RESULT :
Enter the Range of numbers  : 5
Enter 5 numbers
29  3  10  9  1
Maximum Value is 29 and Minimum Value  is 1


b) Write a C program that uses functions to perform the following:
            i) Addition of Two Matrices
           
PROGRAM:
#include<stdio.h>
#include <stdio.h>
void scan(int a[][5],int m1,int n1)
{
        int i,j;
        for(i=0;i<m1;i++)
                for(j=0;j<n1;j++)
        {
      printf("enter the %d,%d element\t",i,j);
     scanf("%d",&a[i][j]);
        }
}
void print(int a[][5],int m1,int n1)
{

        int i,j;
        for(i=0;i<m1;i++)
        {
                for(j=0;j<n1;j++)
                        printf("%d \t",a[i][j]);
                printf("\n");
        }
}
void add(int a[][5],int b[][5],int c[][5],int m1,int n1)
{        int i,j;
        for(i=0;i<m1;i++)
                for(j=0;j<n1;j++)
                        c[i][j]=a[i][j]+b[i][j];


}
int main()
{        int m1,n1,m2,n2,a[5][5],b[5][5],c[5][5];
        printf("\nEnter order of first matrix ");
        scanf("%d%d",&m1,&n1);

        printf("\nEnter order of second matrix ");
        scanf("%d%d",&m2,&n2);
        if (m1!=n1 || m2!=n2)
        {
          printf("\nMatrix Addition is not possible ");
          exit(0);
        }
        printf("\nEnter elements of first matrix : \n");
        scan(a,m1,n1);

        printf("\nEnter elements of second matrix : \n");
        scan(b,m1,n1);

        add(a,b,c,m1,n1);
        printf("\n The first matrix is\n");
        print(a,m1,n1);

        printf("\n The second matrix is\n");
        print(b,m2,n2);
        printf("\n The sum matrix is\n");
        print(c,m2,n2);
}

RESULT:
Enter order of first matrix 2   2

Enter order of second matrix 2   2

Enter elements of first matrix :
enter the 0,0 element   2
enter the 0,1 element   2
enter the 1,0 element   2
enter the 1,1 element   2

Enter elements of second matrix :
enter the 0,0 element   2
enter the 0,1 element   2
enter the 1,0 element   2
enter the 1,1 element   2

 The first matrix is
2       2
2       2

 The second matrix is
2       2
2       2

 The sum of matrix is
4       4
4       4

ii) Multiplication of Two Matrices
PROGRAM :
#include<stdio.h>
#include <stdio.h>
void scan(int a[ ][5],int m1,int n1)
{
        int i,j;
        for(i=0;i<m1;i++)
                for(j=0;j<n1;j++)
        {
      printf("enter the %d , %d element \t",i,j);
     scanf("%d",&a[ i ] [ j ]);
        }
}
void print(int a[ ][ 5 ],int m1,int n1)
{
        int i,j;
        for(i=0;i<m1;i++)
        {
                for(j=0;j<n1;j++)
                        printf("%d \t",a[ i ] [ j ]);
                printf("\n");
        }
}
void mul(int a[ ][ 5 ],int b[ ][ 5 ],int c[ ][ 5 ],int m1,int n1,int n2)
{        int i,j,k;
        for(i=0;i<m1;i++)
                for(j=0;j<n2;j++)
                 {
                    c[ i ] [ j ]=0;
                    for(k=0;k<n1;k++)
                        c[ i ] [ j ]+=a[ i ] [ k ]*b[ k ] [ j ];

                 }
}
int main( )
{
        int m1,n1,m2,n2,a[5] [5],b[5] [5],c[5] [5];
        printf("\nEnter order of first matrix ");
        scanf("%d%d",&m1,&n1);

        printf("\nEnter order of second matrix ");
        scanf("%d%d",&m2,&n2);
        if (n1!=m2)
        {
          printf("\nMatrix multiplication is not possible ");
          exit(0);
        }
        printf("\nEnter elements of first matrix : \n");
        scan(a,m1,n1);

        printf("\nEnter elements of second matrix : \n");
        scan(b,m2,n2);

        mul(a,b,c,m1,n1,n2);
        printf("\n the first matrix is\n");
        print(a,m1,n1);

        printf("\n the second matrix is\n");
        print(b,m2,n2);
        printf("\n the product matrix is\n");
        print(c,m1,n2);
}


RESULT:
Enter order of first matrix 2
2

Enter order of second matrix 2
2

Enter elements of first matrix :
enter the 0,0 element   2
enter the 0,1 element   2
enter the 1,0 element   2
enter the 1,1 element   2

Enter elements of second matrix :
enter the 0,0 element   2
enter the 0,1 element   2
enter the 1,0 element   2
enter the 1,1 element   2

 The first matrix is
2       2
2       2

 The second matrix is
2       2
2       2

 The product of matrix is
8       8
8       8

Week 6
a) Write a C program that uses functions to perform the following operations:
            i) To insert a sub-string in to given main string from a given position.






ii) To delete n Characters from a given position in a given string.
PROGRAM :
#include<stdio.h>
#include <stdio.h>
void  main(  )
{
  char s1[80],s2[40];
  int len,pos,i,j,n;
clrscr(  ):
  printf("enter s1 : ");
  gets(s1);
  printf("Enter position  of deletion and number  of characters to delete : ");
  scanf("%d%d",&pos,&n);
  for(i=0;s1[ i ];i++);
  len=i;
  for(j=0,i=pos-1;j<len;j++,i++)
       s1[i]=s1[i+n];
  s1[len-n]='\0';
  printf("\nResultant string after deletion is \t :\t%s\n",s1);
getch(  );
 }



RESULT:
Enter string : panchamukesh

enter position of deletion and number of characters to delete : 1      6

Resultant string after deletion is       :      mukesh



b) Write a C program to determine if the given string is a palindrome or not
PROGRAM:

#include<stdio.h>
#include<conio.h>
#include<string.h>
void  main( )
                        {                                                    
int len,i,n,flag=0;
        char s[20];
clrscr( );
        printf(" Enter the string that has to be checked\n");
        gets(s);
        len=strlen(s);
        n=len/2;
        for(i=0;i<n;i++)
        {
                if(s[i]!=s[len-i-1])
                {
                        flag=1;                        break;
                }
        }
        if(flag==0)
                        printf(" the given string is a palindrome");
        else
                printf(" the given string is not a palindrome");
getch( );
        }

RESULTS:
Enter the string that has to be checked   :    MADAM
The given string is a palindrome

Week 7
a)     Write a C program that displays the position or index in the string S where the string T begins, or – 1 if S doesn’t contain T.

PROGRAM :

#include <string.h>
#include<stdio.h>
#include<conio.h>
void main()
{ 
char s[40],sub[20];
  int i,j,m,n,t,flag,k;
clrscr( );
  printf("\nEnter main string : ");
  gets(s);
  printf("\nEnter sub string to search : ");
  gets(sub);
  m=strlen(s);  n=strlen(sub);
  for(i=0;i<m;i++)
  {
    if (s[i]==sub[0])
    {
      t=i;
      for (j=0;j<n;j++)
      {  if (sub[j]==s[t])
            flag=1;
        else
        {
           flag=0;  break;
        }
        t++;
      }
    }  // if end
    if (flag==1)  break;
  }  // for end
 if (flag==1)
   printf("Substring  %s  found at %d  ",sub,i+1);
else
    printf("\nNot found \n\n");
getch( );
}
RESULT:

Enter main string : This Is Mechanical C Program's

Enter sub string to search : Mech

Substring  Mech  found at 9


b)     Write a C program to count the lines, words and characters in a given text.

PROGRAM:
#include <string.h>
#include<stdio.h>
#include<conio.h>
void main( )
{ 
char s[40];
  int i=0,nw=0,nl=0,nc=0;
clrscr( );
  printf("\nEnter text and type ^Z at end :    ");
  while((s[i++]=getchar())!=EOF) ; // read chars and store in s until we type ^Z  
    for(i=0;s[i]!=EOF;i++)
  {    if (s[i]=='\n')
    {
     nl++;    nw++;
    }
    else if ((s[i]==' ' || s[i]=='\t') && (s[i+1]!=' '||s[i+1]!='\t'))
       nw++;
    else
      nc++;
  }
  printf("\nNo of lines = %d\nNo of words = %d\nNo of chars = %d\n",nl,nw,nc);
getch( );
}    

RESULT:

Enter text and type ^Z at end :    Hai
I
Am
Writing
C Program
To count Number Of Lines
Number Of Words
Number Of Characters^Z

No of lines = 7
No of words = 16
No of chars = 72


Week 8
a)     Write a C program to generate Pascal’s triangle.
PROGRAM:
#include<stdio.h>
#include<conio.h>
void main(  )
{
  int i,j,k,n,sp;
clrscr( );
  printf("Enter n (no of rows) of triangle : ");
  scanf("%d",&n);
    for(i=0;i<n;i++)
  {
      for(sp=0;sp<40-2*i;sp++) // loop to print spaces
         printf(" ");
      for(j=0;j<=i;j++) // loop to print a row
      {
        if(j==0|| i==0)
            k=1;
        else
            k=k*(i-j+1)/j;
        printf(" %2d ",k);
      }
     printf("\n");    // to go to starting pos of next line
  }
getch( );
 }



RESULT:
Enter n (no of rows) of triangle : 5
                                          1
                                        1   1
                                      1   2   1
                                    1   3   3   1
                                  1   4   6   4   1



b)     Write a C program to construct a pyramid of numbers.
PROGRAM:


#include<stdio.h>
#include<conio.h>
void main( )
{
  int i,j,k=1,n,sp;
clrscr( );
  printf("Enter n (no of rows) of pyramid : ");
  scanf("%d",&n);
    for(i=1;i<=n;i++)
  {
      for(sp=0;sp<40-2*i;sp++) // loop to print spaces
         printf(" ");
      for(j=1;j<=i;j++) // loop to print a row
        printf(" %2d ",k++);
   printf("\n");    // to go to starting position of next line
  }
getch( ) ;
}
RESULT:
Enter n (no of rows) of pyramid : 5
                                        1
                                      2   3
                                    4   5   6
                                  7   8   9  10
                               11  12  13  14  15

No comments:

Post a Comment