Ge2115 - Computer Practice Lab - Practice Programs: Courtesy VRB Publishers
Ge2115 - Computer Practice Lab - Practice Programs: Courtesy VRB Publishers
1.AIM: To write a program to find the area and circumference of the circle. ALGORITHM: Step-1 Step-2 Step-3 Start the program. Input the radius of the circle. Find the area and circumference of the circle using the formula Area=3.14*r*r Circumference=2*3.14*r Print the area and the circumference of the circle Stop
/*TO FIND THE AREA AND CIRCUMFERENCE OF THE CIRCLE*/ #include<stdio.h> main() { float rad,area,circum; printf(\nEnter the radius of the circle); scanf(%f,&rad); area=3.14*rad*rad; circum=2*3.14*rad; printf(\nArea=%f,area); printf(\nCircumference=%f,circum); } SAMPLE INPUT AND OUTPUT: Enter the radius of the circle 5 Area=78.500000 Circumference=31.400000
2. AIM:
To write a program to insert an element in an array in the given position ALGORITHM: Step-1 Step-2 Step-3 Step-4 Step-5 Step-6 Step-7 Start the program Enter the size of the array Enter the elements of the array Print the elements of the array Enter the element to be inserted and its position in the array Set a loop up to the position you enter Push the order of the position by one, which are greater, then the position you entered
WWW.PROFMARIAMICHAEL.COM
Page 1
3.AIM:
To write a program to print the ascending and descending order of the given array. ALGORITHM: Step-1 Step-2 Step-3 Start the program Enter the size of the array Enter the elements of the array
WWW.PROFMARIAMICHAEL.COM
Page 2
WWW.PROFMARIAMICHAEL.COM
Page 3
//CONVERT BINARY NUMBER INTO DECIMAL NUMBER #include<stdio.h> main() { int bnum,digit,decimal=0,bin,base=0; printf(\nEnter the binary number...); scanf(%d,&bnum); printf(%d,bnum); bin=bnum; while(bnum!=0) { digit=bnum%10; decimal=decimal+(digit<<base); base=base+1; bnum=bnum/10; } printf(\nThe binary equivalent of %d in decimal=%d,bin,decimal); } SAMPLE OUTPUT: Enter the binary number...100 The binary equivalent of 100 in decimal=4
5.AIM:
To write a program to find the cosine value of the given x ALGORITHM: Step-1 Step-2 Step-3 Start the program Enter the value of X Convert X into radian
WWW.PROFMARIAMICHAEL.COM
Page 4
6.AIM:
To write a Program to find the exponent of the given ALGORITHM: Step-1 Step-2 Step-3 Step-4 Step-5 Step-6 PROGRAM: //EXPONENTIAL SERIES #include<stdio.h> Start the program Enter the x value and n value Set a loop up to n Find the exponent value of x temp=temp*x/i sum=sum+temp After the execution of the loop print the exponent value of x Stop number
WWW.PROFMARIAMICHAEL.COM
Page 5
7.AIM:
To write a program to find the factorial of the given number ALGORITHM: Step-1 Step-2 Step-3 Step-4 Step-5 PROGRAM: //TO FIND THE FACTORIAL OF THE GIVEN NUMBER #include<stdio.h> main() { int fact=1,i,num; printf(Enter the number); scanf(%d,&num); for(i=1;i<=num;i++) { fact=fact*i; } printf(The factorial of %d is %d,num,fact); } SAMPLE INPUT AND OUTPUT: Start the program Enter a number Set a loop to find the factorial of the given number using the formula Fact=Fact*I Print the factorial of the given number Stop
WWW.PROFMARIAMICHAEL.COM
Page 6
8.AIM:
To write a program to convert the Celsius into Fahrenheit ALGORITHM: Step-1 Step-2 Step-3 Step-4 Step-5 PROGRAM: //CONVERT THE CELCIUS INTO FAHRENTEIET #include<stdio.h> main() { float cel,faren; printf(Enter the Celsius value...); scanf(%f,&cel); faren=(1.8*cel)+32; printf(The fahrenteiet value of the given %f celsius value is %f,cel,faren); } SAMPLE INPUT AND OUTPUT Enter the Celsius value...45 The fahrenteiet value of the given 45.000000 celsius value is 113.000000 Start the program. Enter the Celsius value. Calculate the Fahrenheit value by using the formula given below. Fahreheit=(1.8*Celsius)+32 Print the Fahrenheit value Stop
9.AIM:
To write a program to generate the fibbonaci series ALGORITHM: Step-1 Step-2 Step-3 Step-4 Step-5 Step-6 Step-7 PROGRAM: Start the program Enter the number Check the number whether the number is zero or not. If zero print Zero value. If not zero go further. Set a loop up to the given number. fib=fib+a; a=b; b=c; Every increment in the loop prints the value of fib. After the execution of the loop stops the program
WWW.PROFMARIAMICHAEL.COM
Page 7
WWW.PROFMARIAMICHAEL.COM
Page 8
11.AIM:
Program to illustrate functions without arguments and no return values. ALGORITHM: Step-1 Step-2 Step-3 Step-4 Step-5 Step-6 PROGRAM: //FUNCTIONS WITH OUT ARGUMENTS AND NO RETURN VALUES #include<stdio.h> main() { void message(void); message(); } void message() { char str[100]; printf(Enter a string........); scanf(%s,str); printf(WELCOME TO...%s,str); } SAMPLE OUTPUT: Enter a string LAK WELCOME TO LAK Start the program Declare the function Call the function Enter the String Print the string End the program in the calling function
12.AIM:
To write a program to illustrate function with arguments and no return value.
WWW.PROFMARIAMICHAEL.COM
Page 9
13.AIM:
Program to illustrate parameter passed to the function. ALGORITHM: Step-1 Step-2 Step-3 Step-4 Step-5 Step-6 Step-7 Step-8 Step-9 Step-10 PROGRAM: Start the program Enter the size of the array Enter the elements of the array Print the array elements Call the function with base address of the array passed to it In the calling function gets the base address in the pointer variable Add the array elements Return the value from the calling function to the variable in the called function Print the sum in the called function End the program in the main function
WWW.PROFMARIAMICHAEL.COM
Page 10
14.AIM:
To write a program to illustrate a function with arguments values. ALGORITHM: Step-1 Step-2 Step-3 Step-4 Step-5 Step-6 Step-7 PROGRAM: Start the program Enter the two numbers Call the function with two arguments passed to it Add the two numbers in the calling function Return the addition value to the called function from the calling function Print the addition value in the main function End with return
WWW.PROFMARIAMICHAEL.COM
Page 11
15.AIM:
To write a program to find the largest and smallest of the given array. ALGORITHM: Step-1 Step-2 Step-3 Step-4 Step-5 Step-6 Step-7 Step-8 Step-9 Step-10 PROGRAM: //FIND THE LARGEST AND SMALLEST OF THE GIVEN ARRAY #include<stdio.h> main() { int a[100],i,small,large,no; printf(In how many numbers you want to find....); scanf(%d,&no); printf(Enter the elements of the array....); for(i=0;i<no;i++) Start the program Enter the size of array Enter the elements of the array Print the array elements Initialize the large and small is equal to the first element of the array Set a loop up to the array size Check the next element greater then the larger. If greater then as sign next element to the large Check the next element smaller then the larger. If smaller then assign next element to the small Print the value of large and small after the execution of the loop Stop
WWW.PROFMARIAMICHAEL.COM
Page 12
16.AIM:
To write a program to give the addition of two matrixes. ALGORITHM: Step-1 Step-2 Step-3 Step-4 Step-5 Step-6 Step-7 Step-8 Step-9 Step-10 Step-11 PROGRAM: // FIND THE ADDITION OF TWO MATRIXES #include<stdio.h> main() { int a[25][25],b[25][25],c[25][25],i,j,m,n; Start the program Enter the row and column of the matrix Enter the elements of the A matrix Enter the elements of the B matrix Print the A matrix in the matrix form Print the B matrix in the matrix form Set a loop up to the row Set a inner loop up to the column Add the elements of A and B in column wise and store the result in C matrix After the execution of the two loops. Print the value of C matrix Stop
WWW.PROFMARIAMICHAEL.COM
Page 13
WWW.PROFMARIAMICHAEL.COM
Page 14
17.AIM:
To write a program to multiply two matrixes. ALGORITHM: Step-1 Step-2 Step-3 Step-4 Step-5 Step-6 Step-7 Step-8 Step-9 Step-10 Step-11 Step-12 Step-13 PROGRAM: // MULTPLICATION OF TWO MATRIX #include<stdio.h> main() { int a[25][25],b[25][25],c[25][25],i,j,k,r,s; int m,n; printf(\nEnter the Rows and Columns of A matrix...); scanf(%d %d,&m,&n); printf(\nEnter the Rows and Columns of B matrix...); scanf(%d %d,&r,&s); if(m!=r) printf(\nThe matrix cannot multiplied); else { printf(\nEnter the elements of A matrix); for(i=0;i<m;i++) { for(j=0;j<n;j++) scanf(\t%d,&a[i][j]); } printf(\nEnter the elements of B matrix); for(i=0;i<m;i++) { for(j=0;j<n;j++) scanf(\t%d,&b[i][j]); } printf(\nThe elements of A matrix); Start the program Enter the row and column of the A matrix Enter the row and column of the B matrix Enter the elements of the A matrix Enter the elements of the B matrix Print the elements of the A matrix in matrix form Print the elements of the B matrix in matrix form Set a loop up to row Set a inner loop up to column Set another inner loop up to column Multiply the A and B matrix and store the element in the C matrix Print the resultant matrix Stop
WWW.PROFMARIAMICHAEL.COM
Page 15
18.AIM:
WWW.PROFMARIAMICHAEL.COM Courtesy VRB Publishers Page 16
of
19.AIM:
To write a program to find the roots of the quadratic equation.
WWW.PROFMARIAMICHAEL.COM
Page 17
// To find the roots of the quadratic equation #include<stdio.h> #include<math.h> main() { int a,b,c,d; float root1,root2; printf(Enter the values of a,b,c\n); scanf(%d %d %d,&a,&b,&c); d=b*b-4*a*c; if(d>=0) { root1=(-b+sqrt(d))/(2*a); root2=(+b+sqrt(d))/(2*a); printf(The roots of the values a=%d,b=%d,c=%d are\n %f %f,a,b,c,root1, root2); } else printf(The roots are imagenary); } SAMPLE OUTPUT: Enter the values of a,b,c 1 0 -9 The roots of the values a=1,b=0,c=-9 are 3.000000 3.000000
20.AIM:
To write a program to find the factorial of the given number using recursion ALGORITHM: Step-1 Step-2 Step-3 Step-4 Start the program Enter the number Call the recursive function passing the number to the recursive function as an argument. If the entered number is equal to one then return one to main function.
WWW.PROFMARIAMICHAEL.COM
Page 18
21.AIM:
To write a program to find whether the string is palindrome or not. ALGORITHM: Step-1 Step-2 Step-3 Step-4 Step-5 Step-6 Step-7 Step-8 Step-9 PROGRAM: //TO FIND WHETHER THE GIVEN STRING IS PALINDROME OR NOT #include<stdio.h> #include<stdlib.h> main() { Start the program Enter the string Find the string length using the strlen() function Print the string length Set a loop up to the half of the string length Compare every character above the middle character with the below character of the middle character If any character equal prints the given string is palindrome If the character is not equal then print the given string is not a palindrome Stop
WWW.PROFMARIAMICHAEL.COM
Page 19
22. AIM:
To find the number of vowels and number of consonants in a given string. PROGRAM: //TO FIND THE NUMBER VOWLES AND NUMBER OF CONSONANTS IN A GIVEN STRING #include<stdio.h> main() { int v=0,c=0,i=0; char str[25]; printf(Enter the string...); scanf(%s,str); while(str[i]!=\0') { switch(str[i]) { case a: case A: case E: case I: case O: case U: case e: case i: case o: case u: v++; break; default: c++; } i++; } printf(\nThe number of vowels is %d,v); printf(\nThe number of consonants is %d,c);
WWW.PROFMARIAMICHAEL.COM
Page 20
23.AIM:
To write the program to transpose the given matrix. ALGORITHM: Step-1 Step-2 Step-3 Step-4 Step-5 Step-6 Step-7 Step-8 PROGRAM: //TRANSPOSE OF GIVEN MATRIX #include<stdio.h> main() { int i,j,a[25][25],row,col; printf(\nEnter the number of rows and column of matrix); scanf(%d%d,&row,&col); printf(\nEnter the elements of the matrix); for(i=0;i<row;i++) for(j=0;j<col;j++) scanf(%d,&a[i][j]); printf(The given matrix); for(i=0;i<row;i++) { printf(\n); for(j=0;j<col;j++) printf(\t%d,a[i][j]); } printf(\nThe transpose of the given matrix); for(i=0;i<row;i++) { printf(\n); for(j=0;j<col;j++) printf(\t%d,a[j][i]); } } SAMPLE OUTPUT: Start the program Enter the row and column of the matrix Enter the elements of the matrix Print the elements of the matrix in the matrix format Set the loop up to row Set the inner loop up to column Print the matrix elements in the row wise Stop
WWW.PROFMARIAMICHAEL.COM
Page 21
24.AIM:
To write a program to find the sine value for the entered value. ALGORITHM Step-1 Step-2 Step-3 Step-4 Step-5 Step-6 Step-7 PROGRAM: // SINE SERIES #include<stdio.h> #include<math.h> main() { int no,i; float x,a,sum,b; printf(Enter the numbers); scanf(%f %d,&x,&no); b=x; x=x*3.141/180; a=x; sum=x; for(i=1;i<no+1;i++) { a=(a*pow((double)(-1),(double)(2*i-1))*x*x)/(2*i*(2*i+1)); sum=sum+a; } printf(Sin(%f) value is %f,b,sum); } SAMPLE OUTPUT: Enter the numbers 30 100 Start the program Enter the values x and n Convert the value x into radians Set a loop up to n Find the value of the sine by using the formula Print the value of sine x after the execution of the loop stop
WWW.PROFMARIAMICHAEL.COM
Page 22
25.AIM:
To write a program to print the student name, roll no, average mark and their grades. ALGORITHM: Step-1 Step-2 Step-3 Step-4 Step-5 Step-6 Step-7 Step-9 PROGRAM: //STUDENT RECORD USING POINTER AND STRUCT #include<stdio.h> main() { struct student { char name[25]; char regno[25]; int avg; char grade; } stud[50],*pt; int i,no; printf(Enter the number of the students...); scanf(%d,&no); for(i=0;i<no;i++) { printf(\n student[%d] information:\n,i+1); printf(Enter the name); scanf(%s,stud[i].name); printf(\nEnter the roll no of the student); scanf(%s,stud[i].regno); printf(\nEnter the average value of the student); scanf(%d,&stud[i].avg); } pt=stud; for(pt=stud;pt<stud+no;pt++) { if(pt->avg<30) pt->grade=D; else if(pt->avg<50) pt->grade=C; else if(pt->avg<70) pt->grade=B; else Start the program Initialize the structure variable Enter the number of student Set a loop up to the number of student Enter the student name, roll no, average marks Find their grades Print the student name, roll no, average and their grade Stop
WWW.PROFMARIAMICHAEL.COM
Page 23
26.AIM:
To write a program to swap two number without using pointer ALGORITHM: Step-1 Step-2 Step-3 Step-4 Step-5 Step-6 Step-7 Step-8 PROGRAM: // SWAP THE NUMBER USING THE POINTER #include<stdio.h> main() { int x,y; Start the program Enter the two numbers Call the display function Pass the address of the two numbers to the calling function Get the address in the calling function in the pointer Swap the number using temporary variable Print the swamped values in the main function Stop
WWW.PROFMARIAMICHAEL.COM
Page 24
27.AIM:
To write a program to find the string length and concatenation of string. ALGORITHM: Step-1 Step-2 Step-3 Step-4 Step-5 Step-6 Step-7 PROGRAM: // TO FIND THE STRING LENGTH OF THE STRING #include<stdio.h> #include<string.h> main() { char str1[50],str2[]= WELCOME; int len; printf(Enter the string...); scanf(%s,str1); printf(\nThe string length of %s is %d,str1,strlen(str1)); printf(\nTheconcatenation string length is %d and its string is %s,strlen(str1),strcat(str1,str2)); } SAMPLE OUTPUT: Enter the string... LAK The string length of lak is 3 Start the program Enter the string Find the string length using the function strlen() Print the string length of the entered string concatenation the two string using the function strcat() Print the concatenated string Stop
WWW.PROFMARIAMICHAEL.COM
Page 25
28.AIM:
To write a program to print the pascal triangle ALGORITHM: Step-1 Step-2 Step-3 Step-4 Step-5 Step-6 Step-7 PROGRAM: //CONSTRUCT PASCAL TRIANGLE #include<stdio.h> main() { int noline,i,j,temp; printf(Enter the number of lines to print); scanf(%d,&noline); for(i=1;i<=noline;i++) { for(j=1;j<=noline-i;j++) printf( ); temp=i; for(j=1;j<=i;j++) printf(%4d,temp++); temp=temp-2; for(j=1;j<i;j++) printf(%4d,temp-); printf(\n\n); } printf(\n); } SAMPLE OUTPUT: Enter the number of lines to print 5 1 2 3 2 3 4 5 4 3 4 5 6 7 6 5 4 5 6 7 8 9 8 7 6 5 Start the Enter the Construct Construct Pirnt the After the Stop program number of lines a loop up to the given number another inner loop number execution of the loop
29.AIM:
To write a program to find whether the number is prime Or not. ALGORITHM:
WWW.PROFMARIAMICHAEL.COM
Page 26
30.AIM:
To write a program to find the reverse of the string. ALGORITHM: Step-1 Step-2 Step-3 Step-4 Step-5 Step-6 Step-7 Step-8 PROGRAM: Start the program Enter the string length Print the string length Enter the string character by character Pass the length of the string to the recursive function If the sting length is 1 then print the character If the string length is greater than one then reduces the string length by one and call the recursive function. Stop
WWW.PROFMARIAMICHAEL.COM
Page 27
31.AIM:
To write a program to find the size of the data types. ALGORITHM:
WWW.PROFMARIAMICHAEL.COM
Page 28
SAMPLE OUTPUT: The size of integer is...2 The size of float is... 4 The size of character is...8
32.AIM:
To find the sum and average of the given array ALGORITHM:
PROGRAM: //FIND THE SUM AND AVERAGE OF THE GIVEN NUMBERS #include<stdio.h> main() { int a[100],i,no,sum=0; float avg=0; printf(\nEnter the number of elements); scanf(%d,&no); printf(Enter the numbers); for(i=0;i<no;i++) { scanf(%d,&a[i]); sum=sum+a[i]; } avg=(float)sum/no; printf(sum=%d\naverage=%f,sum,avg); } Sample Output: Enter the number of elements5 Enter the numbers 1 2 3 4 5 sum=15 average=3.000000
33.AIM:
To write a program to convert the string into upper case
WWW.PROFMARIAMICHAEL.COM
Page 29
PROGRAM: //CONVERT THE LOWER CASE TO UPPER CASE LETTER #include<stdio.h> main() { int i=0; char str[100]; printf(Enter the string); scanf(%s,str); while(str[i]!=\0') { printf(%c,toupper(str[i])); i++; } } Sample Ouput Enter the string GuRu GURU
34.AIM:
To write a program to find the largest of the three numbers. ALGORITHM: Step-1 Step-2 Step-3 step-4 Step-5 Step-6 Step-7 Step-8 PROGRAM: //TO FIND THE LARGEST OF THE THREE NUMBERS #include<stdio.h> main() { int a,b,c,big; printf(Enter the three numbers); Start the program Enter the three numbers Assign large to first number Check the next number is greater then the large. If greater then assign large to next number Compare the next number with large Do the step-4 Print the larger value of the three number Stop
WWW.PROFMARIAMICHAEL.COM
Page 30
35.AIM:
To write a program to find the standard deviation of the given array. ALGORITHM: Step-1 Step-2 Step-3 Step-4 Step-5 Step-6 PROGRAM: //COMPUTE STANDARD DEVIATION USING FUNCTION #include<stdio.h> #include<math.h> main() { int i,num; float dev,list[100]; float standev(); printf(\nEnter the size of the list); scanf(%d,&num); printf(\nEnter the elements of the list); for(i=0;i<num;i++) scanf(%f,&list[i]); printf(\nEntered elements are); for(i=0;i<num;i++) printf(\n%f,list[i]); dev=standev(list,num); printf(\n Standard Deviatiion of the list is %10.5f\n,dev); } float standev(float lis[100],int no) { int i; float mean,dev,sum=0.0; float avg(); Start the program Enter the array size Enter the elements of the array Print the Entered elements Call the function to calculate standard deviation Call the average function from the standard deviation function
WWW.PROFMARIAMICHAEL.COM
Page 31
36.AIM:
To write a menu driven program to find 1. Factorial of the given number 2. Prime number or not 3. Even number or Odd number 4. Exit ALGORITHM: Step-1 Step-2 Step-3 Step-4 Step-5 Step-6 Step-7 Step-8 Step-9 Step-10 Step-11 Step-12 Start the program Enter the number Enter your choice using switch statement If you choose the choice 1. Find the factorial set a loop up to the entered number Calculate factorial fact=fact*I Print the factorial value If you choose the choice 2.Find whether the number is prime or not set a loop up to number-1 Check the number is divide by any number other than one and the number itself If divide absolutely then print the number is not a prime If not divide other than one and itself then print the number is prime
WWW.PROFMARIAMICHAEL.COM
Page 32
WWW.PROFMARIAMICHAEL.COM
Page 33
37. AIM :
To print Magic Suare numbers. Algorithm Step-1 Step-2 Step-3 Step-3.1 Step-3.1.1 Start the program Declare x, y and z variables as int data type. for i=1 to less than 10, x = x+ 1 for y:=z to less than or equal to 10, y =y+z for z = 10 to less than or equal to 100, z=z+10 print the Magic Square print x-z, x+z-y, x+y print x+y+z, x, x-y-z print x-y, x+y-z, x+z Stop the program
Step-4 PROGRAM
#include <stdio.h> #include<string.h> main ( ) { int x, y, z; for (x=0; x<=1; x+=2) for (y=0; y<=1; y+=2)
WWW.PROFMARIAMICHAEL.COM
Page 34
38.AIM:
Program to print Triangle of Numbers. ALGORITHM Step-1 Step-2 Step-3 Step-4 Step-5 Step-5.1 Step-5.1.1 Step-5.2 l Step-6 PROGRAM #include<stdio.h> main( ) { int i, j, k, l, n; printf("Type N-no of lines in triangle \n") ; scanf("%d",&n); l=1; for (i=1, j=n-i; i<=n; i++, j--) { for (k=1; k<=j; k++) printf(" "); for (k=1; k<=l; k++) printf(" %d", i); printf("\n") ; l+=2; } } Start the program Declare the variables i, j, k, l and n as int data type Read N numbers of lines in a triangle Initialize l=1 for i=1, j=n-i to less than or equal to n for k=1 to less than or equal to j print " " for k=1 to less than or equal to 1 print "i" is incremented by 2 Stop the program.
WWW.PROFMARIAMICHAEL.COM
Page 35
39.AIM:
Program to find whether a year is Leap year. ALGORITHM
PROGRAM #include<stdio.h> main() { int ye; printf ("Enter the year \n"); scanf("%d", &ye); if (ye%4==0) printf("It is a Leap Year \n"); else printf("It is Not a Leap Year\n"); } SAMPLE OUTPUT Enter the year 2000 It is a Leap Year
40. AIM:
Program to print the following Triangle. 5 4 5 3 4 5 2 3 4 5 1 2 3 4 5 ALGORITHM Step-1 Step-2 Step-3 Step-4 Step-4.1 Step-5 PROGRAM #include<stdio.h> Start the program Declare i, j and n as int data type Read the number of lines for i=n to greater than or equal to 0 for j=i to less than n print y Stop the program
WWW.PROFMARIAMICHAEL.COM
Page 36
41.AIM:
Write a program to read n numbers and find the maximum and the minimum. ALGORITHM Step-1 Step-2 Step-3 Step-4 Step-5 Step-5.1 Step-6 Step-7 Step-7.1 Step-7.1.1 Step-7.2.1 Step-8 Step-9 PROGRAM #include<stdio.h> main () { int table[50], i, cnt, max, min; clrscr(); printf ("Enter number of elements in the array\n"); scanf("%d", &cnt); printf("Enter the elements\n"); Start the program Declare array table[50], i, cnt, max and min as int data type Enter the number of elements in the array Enter the elements into the array for i=1 to less than or equal to cnt Read the values from the console. Initialize max = min = table [1] for i=2 to less than or equal to cnt check if (max < table [i]) max = table [i] min = table[i] print maximum value and minimum value Stop the program
WWW.PROFMARIAMICHAEL.COM
Page 37
42.AIM:
Program to find the determine of 2 2 matrix. ALGORITHM Step-1 Step-2 Step-3 Step-4 Step-4.1 Step-4.1.1 Step-5 Step-6 Step-7 Step-7.1.1 Step-8 Step-8.1 Step-9 PROGRAM #include<stdio.h> main() { int a[2][2], d, i, j; printf ("Enter any 2x2 matrix\n"); for (i=0;i<2;i++) for (j=0;j<2;j++) scanf ("%d", &a[i][j]); d=a[0][0] * a[1][1] - a[0][1] * a[1][0]; printf ("--------------------------\n"); Start the program Declare a two-dimensional array a[2][2],d, i and j as int data type. Enter any 22 matrix for i=0 to less than 2 for j=0 to less than 2 Read a[i][j] Initialize d = a[0][0] * a[1][1] - a[0][1] * a[1][0]: print Determinant of matrix for i=0 to less than 2 print a[i][j] print The Determinant value is print d. Stop the program
WWW.PROFMARIAMICHAEL.COM
Page 38
SAMPLE OUTPUT Enter any 2x2 matrix 1 2 3 4 ---------------Matrix ---------------1 2 3 4 ---------------The Determinant value is -2
43. AIM:
Program to check whether a matrix is an upper triangular. ALGORITHM Step-1 Step-2 Step-3 Step-4 Step-5 Step-5.1 Step-5.1.1 Step-6 Step-6.1 Step-6.1.1 Step-6.1.2 Step-7 Step-7.1 Step-8 PROGRAM #include<stdio.h> Start the program Declare a two dimensional array a[10][10], i, j, m, n and flag=1 as int data type. Enter the order of matrix Enter the matrix rowwise for i=0 to less than or equal to m for j=0 to less than or equal to n Read a [i][j] for i=0 to less than or equal to m for j=0 to less than or equal to n check if (a[i][j]!=0) Print The given matrix is not an upper triangular Initialize flag=0 Check if (flag) print The given matrix is an upper triangular Stop the program.
WWW.PROFMARIAMICHAEL.COM
Page 39
44.
AIM:
Program to demonistrate Macros with arguments.
ALGORITHM Step-1 Step-2 Step-3 Step-4 Step-5 Step-6 Step-7 Step-8 Step-9 Step-9.1 Step-10 Step-10.1 Step-11 Step-12 Start the program Define square(x) as ((x)*(x)) Define for-loop loop (index, max) as for (index=0; index<max; index+i) Define maximum of two max(x,y) as ((x) (y)) ? (x):(y)) Declare two arrays vec[10], sq[10], a, b, i, n and large as int data type print Program to compute print 1. Largest element in the Array print 2. Square of each array element Read Size of the Array Enter elements of the Array Macro calling loop (i, n) Read the elements and stored in the array vec(i) To display array elements by calling macro loop (i, n) print the vec(i) Initialize large=0 Call macro loop (i, n)
WWW.PROFMARIAMICHAEL.COM
Page 40
WWW.PROFMARIAMICHAEL.COM
Page 41
45. AIM:
Program to delete duplicates in a Vector ALGORITHM Step-1 Step-2 Start the program Declare the variables i, j, k, n, num and flag=0 and declare array a(50) as Float data type. Step-3 Enter the size of vector Step-4 Initialize num=n Step-5 Enter vector elements Step-5.1 for i=0 to less than n Read a[i] Step-6 Print vector elements Step-6.1 for i=0 to less than n print a[i] Step-7 Removing duplicates Step-7.1 for i=0 to less than n-1 Step-7.1.1 for j = i+1 to less than n Step-7.1.1 Check if (a[i]=a[j]) n=n-1 Step-7.1.1 flag = 1 Step-7.1.1 j = j-1 Step-8 Check if (flag==0) Print No duplicates found in vector Step-9 Else Print Vector has no. of duplicates and print it Step-10 Print Vector after deleting duplicates Step-10.1 for i=0 to less than n printa[i] Step-11 Stop the program. PROGRAM #include<stdio.h> main() { int i,j,k,n, num, flag=0; float a[50]; printf("Size of vector?"); scanf("%d", &n); num = n; printf("\nEnter Vector elements ?\n"); for (i=0; i<n; i++) scanf("%f", &a[i]); for (i=0; i<n-1; i++) for (j=i+1; j<n; j++) { if (a[i]==a[j]) { n=n-1; for (k = j; k < n; k++) a[k] = a[k+1];
WWW.PROFMARIAMICHAEL.COM
Page 42
46. AIM:
Program to insert an element into the Vector ALGORITHM Step-1 Step-2 Step-3 Step-4 Step-5 Step-6 Step-7 Step-8 Step-9 Step-10 Step-11 Start the program Declare the variables i, j, k, n and pos as int and a[50], item as float data type. Enter the size of vector Enter the elements into the vector Step-4.1 for i=0 to less than n Read and stored in a[i] for i=0 to less than n Step-5.1 Print the vector elements a[i] Read the element to be inserted and print Enter the position of insertion Increment n by 1 for k=n to greater than or equal to pos Step-9.1 a[k] = a[k-1] a[--pos] = item print Vector after insertion
WWW.PROFMARIAMICHAEL.COM
Page 43
5.00
6.00
47. AIM:
Program to print the upper and lower Triangle of the matrix ALGORITHM Step-1 Step-2 Step-3 Step-4 Step-4.1 Step-4.1.1 Step-5 Step-5.1 Start the program Declare a two dimensional array a[10][10], i, j, m and n as int data type. Enter the size of matrix and print that size Enter the elements into the matrix for i=0 to less than n for j = 0%y to less than m Read a[i][j] for i=0 to less than n for j=0 to less than m print a[i][j]
WWW.PROFMARIAMICHAEL.COM
Page 44
48. AIM:
WWW.PROFMARIAMICHAEL.COM Courtesy VRB Publishers Page 45
Recursive Function Step-1 Step-2 Step-3 Step-3.1 Step-3.2 Step-3.2.1 Step-3.3 Step-3.3.1 Step-3.3.2 Step-3.4 Step-3.4.1 Step-4 Step-4.1 Step-5 PROGRAM #include<stdio.h> int key; main() { int a[50], i, n, loc; int bin(int *,int,int); printf("Enter the size of the array : "); scanf("%d", &n); printf("\nEnter Array elements (Ascending Order) :\n\n"); for (i=0; i<n; i++) scanf("%d", &a[i]); printf("\nEnter Element to be Searched : "); scanf("%d", &key); loc=bin(a,0,n); Global declaration of an array b(50) low and high Declare mid as static int of local declaration and i as int data type. mid=(low+high) check if (key<b(mid)) high = mid-1 bin(b, low, high) else if (key==b(mid)) low = mid+1 bin (b, low, high) else if (key==b(mid))) return(mid+1) else return(0) Stop the program.
WWW.PROFMARIAMICHAEL.COM
Page 46
49.
AIM:
Program to Find substring of a given string
ALGORITHM Step-1 Step-2 Step-3 Step-4 Step-4.1 Step-5 Step-6 Step-7 Start the program Declare mainstr[50], substr[50] as char data type and count, pos, i, j, len and num as int data type Enter the main string for len=0 to mainstr(len)!=\0 print Its length Enter the starting position of substring Enter number of characters in substring check if (pos<=0 or Count<=0 or pos>len)
WWW.PROFMARIAMICHAEL.COM
Page 47
WWW.PROFMARIAMICHAEL.COM
Page 48
AIM:
Program to Replace A portion of string ALGORITHM Step-1 Step-2 Step-3 Step-4 Step-4.1 Step-5 Step-6 Step-7 Step-8 Step-8.1 Step-9 Step-9.1 Step-9.1.1 Step-9.2 Step-10 Step-10.1 Step-10.2 Step-10.3 Step-10.3.1 Step-10.3.2 Step-10.4 Step-10.5 Step-10.6 Step-10.6.1 Step-10.6.2 Step-10.7 Step-10.8 Step-11 Step-12 Start the program Declare mainstr[50], repstr[50], save[50] as Char data type and i, j, k, pos, num, last, last, len1, len2, len as int data type. Enter the main string. for len1=0 to mainstr[len1]! = \0 Print The length of the string Enter position from where it is to be replaced Enter number of characters to be replaced Enter replacing string for (len2=0 to repstr(len2)! = \0 print the length; len2 Check if (pos>len1) for (i=0) to less than len2 mainstr[len1+i-1] = repstr[i] len = len1+len2 else last = pos + len2-1 j = 0 for i = pos+num-1 to less than len1 save[j] = mainstr[i] j++ len = j j = 0 for i = --pos to less than last mainstr[i] = repstr[j] j++ for i=0 to less than len Step-10.7.1 mainstr[last] = save[i] Step-10.7.2 Last++ for i = last to less than len1 Step-10.8.1 mainstr[i] = Print REPLACED String Stop the program
WWW.PROFMARIAMICHAEL.COM
Page 49
WWW.PROFMARIAMICHAEL.COM
Page 50
WWW.PROFMARIAMICHAEL.COM
Page 51