You are on page 1of 5

PROGRAM 1: % program for calculating roots of the polynomial having order 2 clc; close all; clear all; disp

('program for calculation root of ploy.') a=input('enter 1st coefficient'); b=input('enter 2nd coefficient'); c=input('enter 3rd coefficient'); x=-b/(2*a); y= (sqrt(b*b-4*a*c))/(2*a); r1=x+y; r2= x-y; fprintf('first root = %f \t second root = %f \n', r1,r2) OUTPUT: program for calculation root of ploy. enter 1st coefficient2 enter 2nd coefficient3 enter 3rd coefficient6 first root = -0.750000 second root = -0.750000

Saving output to a file using fprintf command
PROGRAM 2: %program for calculating exponentiation of value of x from 1 to 11 % save the output to kalpesh.txt file using fprintf command x = 0:1:11; y = [x; exp(x)] fid = fopen('kalpes.txt', 'w'); fprintf(fid,'Prog. for e^x for several x\n') fprintf(fid,'val of x \tval of exp\n') fprintf(fid, '%6.2f \t\t%2.4f\n', y); fclose(fid)

output: is in the file named as kalpes.txt
Prog. for e^x for several x val of x val of exp 0.00 1.0000 1.00 2.7183 2.00 7.3891 3.00 20.0855 4.00 54.5982 5.00 148.4132 6.00 403.4288 7.00 1096.6332 8.00 2980.9580 9.00 8103.0839 10.00 22026.4658 11.00 59874.1417

750000 .'val of x \tval of x^2\n') fprintf(fid. end Utilising it in the script file x=input('enter 1st coefficient'). a.b.txt file x = 0:1:11. enter 1st coefficient2 enter 2nd coefficient3 enter 3rd coefficient6 first root = -0.750000 second root = -0. fclose(fid) output: is in the file named as bsps. y= (sqrt(b*b-4*a*c))/(2*a).*x] fid = fopen('bsps. fprintf(fid.y.txt'. x. 'w'). for x^2 for several x\n') fprintf(fid. fprintf('first root = %f \t second root = %f \n'. y=input('enter 2nd coefficient').z ).b) OUTPUT: program for calculation root of ploy. z=input('enter 3rd coefficient'). r2= x-y. y = [x.'Prog.c ) x=-b/(2*a). y). '%d \t \t%d\n'.txt Prog. b ] = root_jayesh( x. r1=x+y. [ a. r2 ] = root_jayesh( a.PROGRAM 3: %program for calculating square of value of x from 1 to 11 % save the output to bsps. for x^2 for several x val of x val of x^2 0 0 1 1 2 4 3 9 4 16 5 25 6 36 7 49 8 64 9 81 10 100 11 121 Creating function file: PROGRAM 4: Create a user defined function with name root_jayesh which determine roots of ploynomial of order 2 with two output and three input and use it in script file function [ r1.

age = input ('Enter hers/his age in years :'). elseif (age < 13) disp('Child'). else disp('Senior').'). elseif (age < 1) disp('Baby'). end OUTPUT: Enter an age : 20 Adult Switch case PROGRAM 7 %prog for mathematical operation to be calculating with switch loop x=input('enter 1st no.').name) else fprintf ('%s is not eligible to vote\n'.z) case 2 z=x-y. if ((age < 0)|(age >= 120)) disp('Error').'s'). two no. %using . switch op case 1 z=x+y. y=input('enter 2nd no. fprintf('addition = %f'. elseif (age < 18) disp('Teenager'). if age>=18 fprintf ('%s is eligible to vote\n'.if Statement PROGRAM 5: %prog for entering name and age of person and display whether to eligible to %vote or not. disp('enter 1 for addition') disp('enter 2 for substarction') disp('enter 3 for multiplition') disp('enter 4 for division') op=input('enter the operation= ').name) end OUTPUT: Enter the citizen full name :abcd Enter hers/his age in years :15 abcd is not eligible to vote PROGRAM 6: %program to asks the user for an age and then classifies it age=input('Enter an age : '). elseif (age < 60) disp('Adult'). name = input ('Enter the citizen full name :'.

fprintf('substraction = %f'.000000 Switch case with string PROGRAM 8 % program for traffic light controller using switch case with string disp(' **************** Traffic light condition *****************') light = input('Enter the following traffic light color: red. green. count=0. fprintf('multiplication = %f'.'s') switch light case'green' disp( 'It is safe to pass') case'red' disp('It is not safe to pass') case'yellow' disp('Proceed with caution') otherwise disp('Light is not functioning') end OUTPUT: **************** Traffic light condition ***************** Enter the following traffic light color: red. using while loop x= input('enter the no (end in 0 )'). .z) otherwise disp('wrong operation') end OUTPUT: enter 1st no.z) case 3 z=x*y. others:red light = red It is not safe to pass While Loop PROGRAM 9 % prog for calculating sum of numbers and average of the no.20 enter 2nd no.z) case 4 z=x/y.30 enter 1 for addition enter 2 for substarction enter 3 for multiplition enter 4 for division enter the operation= 3 multiplication = 600. green. fprintf('division = %f'. sum=0. while x~=0 sum=x+sum. others:'. yellow. yellow.

j) + B(i. 17 18 19] C=zeros(3).000000.C(i.sum. x= input('enter the no (end in 0 )').j) . for i=1:3 for j=1:3 C(i. 14 15 16.000000 Using FOR LOOP PROGRAM 10 % program for addition of 3x3 matrix using for loop A = [1 2 3. fprintf ('no entered = %d. count.j)) end fprintf('\n') end OUTPUT: A = 1 4 7 B = 11 12 13 14 15 16 17 18 19 addition 0f matrix is= 12 14 16 18 20 22 24 26 28 2 5 8 3 6 9 . sum=%f. Average= %f'. end ave= sum/count. 7 8 9] B = [11 12 13. sum=60. Average= 20.j) = A(i. 4 5 6. end end % to display output fprintf('addition 0f matrix is= \n') for i=1:3 for j=1:3 fprintf('%d\t'.ave) OUTUT: enter the no enter the no enter the no enter the no no entered = (end in 0 )10 (end in 0 )20 (end in 0 )30 (end in 0 )0 3.count= count+1.