Friday, March 4, 2011

Computer Programming Week 4 Lab 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’.


ALGORITHM
STEP1:  START
STEP2:  INITIALIZE x=0
STEP3:  READ u,a,n,t
STEP:4   REPEAT UNTIL x<=t
STEP5:    d=(u*x)+(a*x*x*0.5);
STEP:6  DISPLAY x,d
STEP7: STOP

    FLOW CHART



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)

ALGORITHM
STEP1: Start
STEP2:  Declare a,b,c
STEP3: Read a,b,c
STEP4: c=’+’ ,display sum of a&b is a+b
               c=’-’ , substraction of a&b is a-b
               c=’*’, multiplication of a&b is a*b
               c=’/’, division of a&b is a/b
               c=’%’,r emainder of a&b is a%b
               else display invalid choice
STEP5: stop
FLOWCHART


 
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



No comments:

Post a Comment