1
1
1
-Practical file
Submitted
(04-MCA-2K11)
Certificate
This is to certify that this practical file belongs to Sonam Gandotra of MCA-1 semester of University Of Jammu bearing roll number 04-MCA-2K11. This student has done the entire practical in the lab under my supervision.
Practical File
Index
S.No 1 Name of the Program Page no.
Program1 Any year is input through the keyboard. Write a program to determine whether the year is a leap year or not.
Algorithm
1. Start 2. Input year y 3. if y is divisible by 100? If yes goto 4 else goto 5 4. if y is divisible by 400? If yes goto 6 else goto 7 5. if y is divisible by 4? If yes goto 6 else goto 7 6. Print year is a leap year and goto 8 7. Print year is not a leap year 8. Stop
Flowchart
printf("Enter the year : "); scanf("%d",&year); if(year%100==0) //To check whether the year is centennial year { if(year%400==0) { printf("%d is a leap year",year); } else { printf("%d is not a leap year",year); } else { }
Output:
Program 2: According to the Gregorian calendar, it was Monday on date 01/01/01. If any year is input through the keyboard, write a program to find out what is the day on 1st January of this year.
4. Else If(y<2001)
b) for(i=y;i<2001;i++) ii) If((i%4=0)&&(i%100!=0)||(i%400=0)),then c=c+1 and go to step 6
h) If (d=3), then Display Day is Wednesday and exit. i) If (d=4), then Display Day is Thursday and exit. j) If (d=5), then Display Day is Friday and exit. k) If (d=6), then Display Day is Saturday and exit. l) If (d=7), then Display Day is Sunday and exit.
5. Else
a) d=2001-y b) CALL count(y)and set l=count(y) c) d=d+l d) If(d>8),then d=d%7 e) If (d=0), then Display Day is Tuesday and exit. f) If (d=1), then Display Day is Monday and exit. g) If (d=2), then Display Day is Sunday and exit. h) If (d=3), then Display Day is Saturday and exit. i) If (d=4), then Display Day is Friday and exit. j) If (d=5), then Display Day is Thursday and exit. k) If (d=6), then Display Day is Wednesday and exit. l) If (d=7), then Display Day is Tuesday and exit.
6. Stop.
Flowchart
ANY YEAR IS INPUT THROUGH KEYBOARD,WAP TO FIND OUT WHAT IS THE DAY ON 1ST JANUARY OF THIS YEAR.*/
#include<stdio.h> int count(int y); void main() { int y,d,l,z; printf("Enter the year : "); scanf("%d",&y); z=y; if(y>=2001) //For year greater thanand equal to 2001 { d=y-2001; l=count(y); d=d+l; while(d>8) { d=d%7; } switch(d) { case 0: printf("1st JANUARY OF %d is SUNDAY\n",z); break; case 1: printf("\n 1st JANUARY OF %d is MONDAY\n",z); break; case 2: printf("1st JANUARY OF %d is TUESDAY\n",z); break; case 3: printf("1st JANUARY OF %d is WEDNESDAY\n",z); break; case 4: printf("1st JANUARY OF %d is THURSDAY\n",z); break; case 5: printf("1st JANUARY OF %d is FRIDAY\n",z); break; case 6: printf("1st JANUARY OF %d is SATURDAY\n",z); break; case 7: printf("1st JANUARY OF %d is SUNDAY\n",z); break; default: break; } } else //For year less than 2001 { d=2001-y; l=count(y); d=d+l; while(d>8) { d=d%7; } switch(d) { case 2: printf("\n 1st JANUARY OF %d is SUNDAY",z);
break; case 3: printf("1st JANUARY break; case 4: printf("1st JANUARY break; case 5: printf("1st JANUARY break; case 6: printf("1st JANUARY OF break; case 7: printf("1st JANUARY break; case 1: printf("1st JANUARY break; default: printf("1st JANUARY OF break; } } } int count(int y) { int i,c=0; if(y>2001) {
for(i=2001;i<y;i++) { if((i%4==0)&&(i%100!=0)||(i%400==0)) c=c+1; //No. of leap year } } else if(y<2001) { for(i=y;i<2001;i++) { if((i%4==0)&&(i%100!=0)||(i%400==0)) c=c+1; //No. of leap year } } else { c=c+0; } if(c==0) return(c); else return(c+1); //Leap year found than +1 for the extra day }
Output2
Program no.3:
A five digit number is entered through the keyboard. Write a program to obtain the reverse of the number and to determine whether the original and reversed numbers are equal or not. (Palindrome)
Algorithm: 1. Start. 2. Prompt and read the five digit numbern. 3. Set m=n and rev=0. 4. If ((n>9999)&&(n<100000))
a) While n!=>0 i) rev=(rev*10)+m%10 ii) n=n/10 b) Display The reversed number. c) If rev=m, then DisplayNumber is palindrome and exit. d) DisplayNumber is not palindrome
Flowchart:
/*A five digit number is entered through the keyboard. Write a program to obtain the reverse of the number and to determine whether the original and reversed numbers are equal or not.(Palindrome)*/ #include<stdio.h> void main() { int n,m=0,d; printf("Enter five digit number : "); scanf("%d",&n); d=n; if((n>9999)&&(n<100000)) //To check whether the no is in range or not. { while(n!=0) { m=m*10+(n%10); //To create the reverse of the number. n=n/10; } printf("\nThe reverse of %d is %d",d,m); if(d==m) printf("\n%d is a palindrome\n",d); else printf("\n%d is not a palindrome\n",d); } else printf("\nInvalid number!!!!!!!!!!!!!!!\nPlease enter a valid five digit number.\n"); }
Output 3
Program 4:
If the ages of Ram, Shyam and Ajay are input through the keyboard, write a program to determine the youngest of the three.
Algorithm: 1. Start
2. Input the ages of Ram, Shyam and Ajay as a, b, c
3. If a<b then
a) If a<c then Print Ram is youngest Go to 5 b) Print Ajay is youngest Go to 5 4. If b<c then a) Print Shyam is youngest Go to 5 b) Print Ajay is youngest
5. Stop
Flowchart:
/*WAP TO DETERMINE YOUNGEST OF RAM,SHAM,AJAY*/ #include<stdio.h> void main() { int a,b,c; printf("Enter the age of Ram:"); scanf("%d",&a); printf("\nEnter the age of Shyam:"); scanf("%d",&b); printf("\nEnter the age of Ajay:"); scanf("%d",&c); if(a<b) { if(a<c) printf("Ram is youngest\n"); else printf("Ajay is youngest\n"); } else { if(b<c) printf("Shyam is youngest\n"); else printf("Ajay is youngest\n"); } }
Output:
Program 5 :
Write a program to calculate the simple interest and compound interest and the difference between the two.
Algorithm:-
1. Start. 2. Input p,r,t,si,ci,I,diff. 3. Si=(p*r*t)/100. 4. Print si. 5. i=(1+(r/100))^t. 6. ci=(p-i)-p. 7. Printf ci. 8. Diff=ci-si. 9. Print diff. 10. Stop.
Flowchart:
/*WAP to calculate simple interest and compound interest and the difference between the two.*/ #include<stdio.h> #include<math.h> void main() { float p,r,t,si,ci,a,d,e; printf("Enter Principal:"); scanf("%f",&p); printf("\nEnter Rate:"); scanf("%f",&r); printf("\nEnter Time:"); scanf("%f",&t); si=((p*r*t)/100); printf("\nPrincipal : %f \nRate : %f \nTime : %f ",p,r,t); printf("\nSimple Interest = %f",si); d=(1+(r/100)); e=pow(d,t); a=(p*e); ci=a-p; printf("\nCompound Interest = %f",ci); printf("\nDifference between Compound Interest & Simple Interest = %f\n",ci-si); }
Output:
PROGRAM 6:
Write a program to find L.C.M and H.C.F of two numbers.
Algorithm:1. Start. 2. Input two numbers a and b. 3. Set i=1. 4. While i<n goto step5. 5. If(m*i)%n=0, then go to step 8, otherwise go to step 7. 6. Increment I by 1, go to step 4 7. Display LCM =m*i 8. Display HCF =(m*n)/(m*i) 9. Stop.
Flowchart:
#include<stdio.h> void main() { int a,b,i,h; printf("Enter 2 numbers:"); scanf("%d%d",&a,&b); for(i=1;i<b;i++) { if((a*i)%b==0) break; } printf("LCM of %d and %d is = %d\n",a,b,a*i); h=(a*b)/(a*i); printf("HCF of %d and %d is = %d\n",a,b,h); }
Output:
PROGRAM 7 :
Write a program to find the number of trailing zeroes in the factorial of a numberwithout finding the actual factorial. Algorithm:1. 2. 3. 4. 5. 6. Start. Prompt and read the number n. Set m=n, zeros=0. If (m>1&&m<=100), go to step 6. Display you have entered out of range 1to 100 and exit. While(m>=5)
i) zeros=zeros+m/5 ii) m=m/5 iii) go to step 6
Flowchart:
/*Write a program to find the number of trailing zeroes in the factorial of a number without finding the actual factorial.*/
#include<stdio.h>
void main() { int n,m,zeros=0; printf("Enter number : "); scanf("%d",&n); m=n; if((m>1)&&(m<=100)) { while(m>=5) { zeros=zeros+m/5; m=m/5; } printf("\nThe trailing zeros of %d are %d \n",n,zeros); } else printf("\nYou have entered the number out of given range(1 to 100)\n"); }
Output:
Go to step 7
5. If(d>0),then
a) b) c) d) r1=(-b+sqrt(d))/2a r2=(-b-sqrt(d))/2a Display Roots are real and display r1 and r2 Go to step 7
6. If(d<0), then
a) b) c) d) real=-b/2a imag=sqrt(-d)/2a Display Roots are imaginary and display r1=real+imag and r2=real+imag Go to step 7
7. Stop.
Flowchart:
/*Write a program to find the roots of the quadratic equation.*/ #include<stdio.h> #include<math.h>
void main() { float a,b,c,d,r1,r2,real,imag; printf("Enter the coefficient of x*x:"); scanf("%f",&a); printf("Enter the coefficient of x:"); scanf("\n%f",&b); printf("Enter the constant:"); scanf("\n%f",&c); printf("The entered quadratic equation is %.2fx*x+%.2fx+%.2f",a,b,c); d=(b*b)-(4.0*a*c); printf("\nThe discriminant of entered quadratic equation %.2fx*x+%.2fx+%.2f is %.2f ",a,b,c,d); if(d==0) { printf("\nRoots are real and equal"); r1=(-b)/(2.0*a); r2=r1; printf("\nThe roots of the equation are %.2f and %.2f .\n",r1,r2); } if(d>0) { printf("\nRoots are real"); r1=((-b)+sqrt(d))/((2.0)*a); r2=((-b)-sqrt(d))/((2.0)*a); printf("\nTrhe roots of the equation are %.2f and %.2f .\n\n",r1,r2); } if(d<0) { printf("\nRoots are imaginary"); real=(-b)/(2.0*a); imag=((sqrt(-d))/(2.0*a)); printf("\nThe roots of the equation are %.2f-%.2f(i) and %.2f+ %.2f(i)",real,imag,real,imag); } }
Output
PROGRAM 9:
Given three points (x1,y1),(x2,y2),(x3,y3), write a program to check if all the three points fall on one straight line.
Algorithm:1. 2. 3. 4. 5. 6. 7. Start. Input (x1,y1),(x2,y2),(x3,y3). Set a=0 Calculate a=((x2*y3)-(x3*y2))+((x1*y3)-(x3*y1))+((x1*y2)-(x2*y1)) If a=0,then displayPoints lie on a straight line and go to step 7 Display points do not lie on straight line. Stop
Flowchart:
//Write a program to check if all the three points(x1,y1),(x2,y2),(x3,y3) fall on one straight line. #include<stdio.h> void main() { int x1,x2,x3,y1,y2,y3,a,b,s=15,i; printf("OUTPUT:\n"); for(i=1;i<=70;i++) { printf("%c",s); } printf("\n\n\nEnter the coordinates of first point : "); scanf("%d %d",&x1,&y1); printf("\nEnter the coordinates of second point : "); scanf("%d %d",&x2,&y2); printf("\nEnter the coordinates of third point : "); scanf("%d %d",&x3,&y3); a=((x2*y3)-(x3*y2))+((x1*y3)-(x3*y1))+((x1*y2)-(x2*y1)); if(a==0) printf("\nThe three points fall on one straight line\n\n\n"); else printf("\nThe three points do not fall on one straight line\n\n\n"); for(i=1;i<=70;i++) { printf("%c",s); } printf("\n"); }
Output
PROGRAM 10:
Given coordinates (x,y) of the centre of circle andits radius. Write a program that determines wheher the point lies inside the circle, outside the circle or on the circle.
Algorithm:1. Start. 2. Read the co ordinates of centre (x,y), point (a,b) and radius r. 3. Calculate l=(x-a)(x-a)+(y-b)(y-b)-r*r 4. If l ==0? If yes goto step 7 else goto step5. 5. Is l<0? If yes goto step 8 else goto step 6. 6. Is l>0? If yes goto step 9 else goto step 10. 7. Print Point lies on the circle and stop. 8. Print Point lies inside the circle and stop. 9. Print Point lies outside the circle and stop. 10. Stop.
Flowchart:
/*Given coordinates (x,y) of the centre of circle and its radius. Write a program that determines wheher the point lies inside the circle, outside the circle or on the circle.*/
#include<stdio.h> #include<math.h> void main() { float x,y,a,b,r,l; printf("Enter the co-ordinates of (x,y)\n"); scanf("%f%f",&x,&y); printf("Centre=(%.1f,%.1f)\n",x,y); printf("\nEnter the co-ordinates of (a,b)\n"); scanf("%f%f",&a,&b); printf("Co-ordinates of point=(%.1f,%.1f)\n",a,b); printf("\nEnter the radius"); scanf("%f",&r); printf("Radius of the given circle is=%.1f\n",r); l=(pow((x-a),2)+pow((y-b),2)-(r*r)); if(l>0) printf("Point(%.1f,%.1f) lies outside the circle\n",a,b); if(l<0) printf("Point(%.1f,%.1f) lies inside the circle\n",a,b); if(l==0) printf("Point(%.1f,%.1f) lies on the circle\n",a,b); }
Output
Program 11:
Any character is entered through keyboard,write a program to find whether the character entered is a capital letter,a small case letter, a digit or a special symbol.
a<=127)) if yes, go to step 10,else go to step 11. 7. Print This is capital case letter and stop. 8. Print This is small case letter and stop. 9. Print This is a digit and stop. 10. Print This is special case letter and stop.
11. Print This is invalid character
12. Stop.
Flowchart:
/*Any character is entered through keyboard,write a program to find whether the character entered is a capital letter,a small case letter, a digit or a special symbol.*/
#include<stdio.h> void main() { char n; printf("\n PRESS F11 TO EXIT\t"); while(5) { printf("\n Enter character\t"); n=getch(); if((n>=65)&&(n<=90)) printf("%c is a capital letter",n); else if((n>=97)&&(n<=122)) printf("%c is a small case letter",n); else if((n>=48)&&(n<=57)) printf("%c is a digit",n); else if(((n>=0)&&(n<=47))||((n>=58)&&(n<=64))||((n>=91)&&(n<=96))|| ((n>=123)&&(n<=127))) printf("%c is a special symbol",n); else exit(0); } }
Output
Program 12: A library charged fine for returning book late. For 0-5 days charge is 50 paisa. For 6-10 days charge is 1 rupee. For 11-30 days charge is 5 rupees and after 30 days the membership is cancelled. Write a program to accept the number of days the member is late to return the book and display the fine.
Algorithm: 1. Start
2. Input no. of late days d 3. Is d<=5? If yes, go to step 4 else go to step 5. 4. Print fine is 50 paise and stop. 5. Is d>=6 and d<=10? If yes, go to step 6 else go to step 7 6. Print fine is 1 rupee and stop. 7. Is d>=11 and d<=30? If yes, go to step 8 else go to step 9 8. Print fine is 5 rupees and stop.
Flowchart:
/*A library charged fine for returning book late. For 0-5 days charge is 50 paise. For 6-10 days charge is 1 rupee. For 11-30 days charge is 5 rupees and after 30 days the membership is cancelled. Write a program to accept the number of days the member is late to return the book and display the fine.*/
#include<stdio.h> void main() { int n; printf("Enter the number of days the member is late: "); scanf("%d",&n); if(n<=5) printf("\nThe fine is 50 paise\n"); else if((n>=6)&&(n<=10)) printf("\nThe fine is 1 rupee\n"); else if((n>10)&&(n<=30)) printf("\nThe fine is 5 rupees\n"); else printf("\nThe membership is cancelled\n"); }
Output:
Program 13:
If three sides of a triangle are entered through the keyboard, write a program to check whether the triangle is isosceles, equilateral, scalene or right angled triangle.
Algorithm: 1. Start.
2. Read the three sides of triangle as a, b, c. 3. If a is equal to both c and b then PrintEquilateral triangle and go to step 7. 4.
5. If a is not equal to both c and b and b is not equal to both a and c then, Print
Flowchart:
/*If three sides of a triangle are entered through the keyboard, write a program to check whether the triangle is isosceles, equilateral, scalene or right angled triangle.*/
#include<stdio.h> #include<conio.h> void main() { int a, b,c; printf("\nEnter the first side of triangle : "); scanf("%d",&a); printf("\nEnter the second side of triangle : "); scanf("%d",&b); printf("\nEnter the third side of triangle : "); scanf("%d",&c); if(a==b&&b==c) printf("\n Triangle is equilateral"); if(a==b||b==c||c==a) printf("\n Triangle is isosceles"); if(a!=b&&b!=c&&c!=a) { printf("\n Triangle is scalene"); if(a*a==b*b+c*c||b*b==a*a+c*c||c*c==a*a+b*b) printf("\n and is a right triangle"); } }
Output:
Program 14:
Write a program to calculate overtime pay of 10 employee, overtime is paid at the rate of Rs 12.00 per hour for every hour worked above 40 hours. Assume that the employees do not work for fractional part of an hour.
Algorithm: 1) 2) 3) 4) 5) 6)
7) 8)
Start. Prompt and read array of overtime of 10 employees a[10]. Set i=1 If i<=10?, then go to step 5 otherwise go to step10 If a[i]>40, then go to step 6 otherwise go to step8 a[i]=a[i]-40 Print Overtime of employee is a[i]*12 Print Overtime of employee is Rs.0.00 i=i+1 and go to step 4 Stop.
9) 10)
Flowchart:
/*Write a program to calculate overtime pay of 10 employee, overtime is paid at the rate of Rs 12.00 per hour for every hour worked above 40 hours. Assume that the employees do not work for fractional part of an hour.*/ #include<stdio.h> void main()
int a[10],i; for(i=1;i<=10;i++) { printf("enter the time of %d employee :",i); scanf("%d",&a[i]); } for(i=1;i<=10;i++) { if(a[i]>40) { a[i]-=40; printf("overtime pay of %d employee is Rs. %d\n",i,a[i]*12); } else printf("overtime pay of %d employee is Rs. 0.00\n",i); }
Output
Program 15:
Write a program to print all the ASCII values and their equivalent characters using a while loop. The ASCII value vary from 0 to 255.
Algorithm 1. Start 2. Initialized i=0 3. While i<=255
i) Print the ASCII value of i ii) Increment i with 1 iii) Go to step3
4. Stop
Flowchart:
/*Write a program to print all the ASCII values and their equivalent characters using a while loop. The ASCII value vary from 0 to 255.*/
#include<stdio.h> void main() { int i=0;//start of main printf("\nASCII value while(i<=255) { printf("\n %d i++; } }
Output:
Program 16:Write a program to print out all Armstrong numbers beween 1 to 500.If the sum of cubes of each digit of the number is equal to the number itself, then the number is called an Armstrong number.
Algorithm:1. 2. 3. 4. 5. 6. 7. 8. Start Input num, n, cube, d, sum. Print Armstrong numbers are: Initialize num=1 Is num<=500? If yes go to step 6 else go to step 13 n=num, initialize sum=0 Is n>0? If yes go to step 8 else go to step 9. Initialize d=n%10 and n=n/10 cube=d*d*d sum=sum + cube 9. Is num==sum? If yes go to step 10 else go to step 11. 10. Print num and go to step 9 11. num++ 12. Go to step 5 13. Stop
Flowchart:
/*Write a program to print out all Armstrong numbers beween 1 to 500.If the sum of cubes of each digit of the number is equal to the number itself, then the number is called an Armstrong number.*/
#include<stdio.h> void main() { int n,m,c,d; printf("Armstrong numbers between 1 and 500 are:\n"); for(n=1;n<=500;n++) { d=n; c=0; while(d!=0) { m=d%10; c=c+(m*m*m); d=d/10; } if(c==n) printf("%d\n",n); } }
Output:
Program 17:
Write a program for a matchstick game being played between the computer and a user. Your program should ensure that the computer always wins. Rules for the game are as follows: There are 21 matchsticks, the computer asks the player to pick 1, 2, 3, or 4 matchsticks, after the person picks, the computer does its picking and whoever is forced to pick the last matchstick, loses the game.
Algorithm
1. 2. 3. 4. 5. 6. 7.
Set i=21 Print welcome to game Print total number of match stick is 21 Print pick 1,2,3 or 4 match stick Is i>1 Print enter your choice Input u
10. i=i-u 11. Print number of match stick left = i; 12. c=5-u
13. Print computer picks match sticks ,c 14. i=i-c 15. Print number of match stick left = i 16. Goto 5 17. Printyou loose 18. End Flowchart:
/*Write a program for a matchstick game being played between the computer and a user. Your program should ensure that the computer always wins. Rules for the game are as follows:
There are 21 matchsticks, thecomputer asks the player to pick 1, 2, 3, or 4 matchsticks, after the person picks, the computer does its picking and whoever is forced to pick the last matchstick, loses the game. */ #include<stdio.h> void main() { int ms=21,p,c; while(1) { printf("\n No. of match sticks left=%d",ms); printf("\n Pick up 1,2,3 or 4 sticks"); scanf("%d",&p); if((p>4)||(p<1)) continue; ms=ms-p; printf("\n No. of sticks left=%d",ms); c=5-p; printf("\n Out of which computer picks up %d",c); ms=ms-c; if(ms==1) { printf("\n No. of sticks left=%d",ms); printf("\nYou lost the game!!!!!!!!\n\n"); break; } } }
Output:
Program 18:Write a program to enter the numbers till the user wants and at the end it should display the count of positive, negative and zeros entered.
Algorithm:1. Start 2. Input d, b, p, z, n, i=1. 3. p=0, z=0, n=0 4. Input d (no. of times user wants to enter) 5. Is i<=d? if yes go to sep 6 else go to step 3 6. Input b 7. Is b>0? If yes go to step 8 else go to step 9 8. Initialize p=p+1 9. Is b=0? If yes go to step 10 else go to step11 10. Initialize z=z+1 11. Initialize n=n+1 and go to step 5 12. Print Total numbers entered: 13. Print number of positive numbers, negative numbers, and zeros: 14. Stop
Flowchart:
/*Write a program to enter the numbers till the user wants and at the end it should display the count of positive, negative and zeros entered.*/
#include<stdio.h> #include<conio.h> void main() { int d,b,p=0,z=0,n=0,i; printf("Enter the size of the list : "); scanf("%d",&d); for(i=1;i<=d;i++) { printf("Enter the number:"); scanf("%d",&b); if(b>0) p=p+1; else if(b==0) z=z+1; else n=n+1; } printf("No of positive numbers is %d\n",p); printf("No of zeros is %d\n",z); printf("No of negative numbers is %d\n",n); getch(); }
Output:
Program 19:Write a program to receive an integer and find its octal equivalent.
Algorithm:1. Start 2. Input number n. 3. Save number n in nsave 4. Initialize p=octal=0 5. Is n>0? If yes goto step 6 else goto step 10 6. rem=n%8 7. n=n/8 8. octal=octal + rem * pow(10, p) 9. p = p+1 and goto step 5 10. Print octal 11. Stop
Flowchart:
/*Write a program to receive an integer and find its octal equivalent.*/ #include<stdio.h> #include<conio.h> #include<math.h> void main() { long int n,rem,octal,i,p,nsave; printf("Enter the decimal number:"); scanf("%ld",&n); nsave=n; p=octal=0; while(n>0) { rem=n%8; n=n/8; octal= octal + rem * pow(10,p); p++; } printf("The octal equivalent of is %ld",octal); getch(); }
Output:
Program 20:
Write a program to find the range of a set of numbers. Range is the difference between the smallest and the biggest number in the list.
Algorithm: 1. 2. 3. 4. 5.
6.
Start Read size of the array l. Prompt and read elements of array. Set max=a[0] , min=a[0] andi=0. Is i<l, then go to step 6 If max<=a[i],then go to step 7 Set max=a[i] i=i+1 and go to step 5. Is i<l, then go to step 10 If min>=a[i],then go to step 11 Set min=a[i] i=i+1 and go to step 9. Print Maximum Print Minimum Print Range=(max-min)
7. 8.
9. 10.
16.
Stop.
Flowchart:
/*Write a program to find the range of a set of numbers. Range is the difference between the smallest and the biggest number in the list.*/
#include<stdio.h> void main() { int i,a[15],l,max,min; printf("Enter size of array : "); scanf("%d",&l); printf("\nEnter the elements of the array: "); for(i=0;i<l;i++) { scanf("%d",&a[i]); } printf("List of entered numbers is "); for(i=0;i<l;i++) { printf("%d\t",a[i]); } max=a[0]; min=a[0]; for(i=0;i<l;i++) { if(max<=a[i]) max=a[i]; } for(i=0;i<l;i++) { if(min>=a[i]) min=a[i]; } printf("\nGreatest number is %d ",max); printf("\nSmallest number is %d ",min); printf("\nRange is %d\n",max-min); }
Output:
Algorithm:1. Start 2. Set i=1 3. Is i<=300? If yes go to step 4 else go to step 13 4. Set isprime=1,j=2 5. Is j<=sqrt(i)? if yes go to step 6 else go to step 9 6. Is i%j==0,then 7. isprime=0 8. break 9. j++ and go to step 5 10. Is isprime= = 1? if yes go to step 11 11. Print prime no i 12. i++ and go to step 3 13. Stop
Flowchart:
1 to 300*/
#include<stdio.h> #include<math.h> void main() { int i,j,isprime; for(i=1;i<=300;i++) { isprime=1; for(j=2;j<=sqrt(i);j++) { if(i%j==0) { isprime=0; break; } } if (isprime==1) { printf("\t%d",i); } } }
Output:
Program 22:Write a program to add first seven terms of the following series using a for loop: 1/1! + 2/2! + 3/3! + - - - - - - - -
Algorithm:1. 2. 3. 4. 5. 6. 7. 8. 9. Start Set i=1, sum=0.0 Is i<=7? If yes go to step 4 else go to step 11. d=fact(i) //Function call term=i/d sum=sum+term i++ and go to step 3 display sum Stop
Flowchart:
/*Write a program to add first seven terms of the following series using a for loop: 1/1! + 2/2! + 3/3! + - - - - - - - - */
#include<stdio.h> void main() { float s=0.0,d,term; int i; int func(int); for(i=1;i<=7;i++) { d=func(i); term=i/d; s=s+term; } printf("1/1! + 2/2! + 3 /3! +...............upto 7 terms = %f\n",s); } int func(int x) { int f=1,j; for(j=1;j<=x;j++) { f=f*j; } return f; }
Output:
Program-23
According to a study, the approximate level of intelligence of a person can be calculated using the formula: i = 2+(y+0.5x) Write a program that will produce a table of values of i,y and x, where y varies from 1 to 6, and, for each value of y,x varies from 5.5 to 12.5 in steps of 0.5.
Algorithm:1. 2. 3. 4. 5. 6. 7. 8. 9. 10. Start Set y=1 Is y<=6? If yes go to step 4 else go to step 10 Input x=5.5 Is x<=12.5? If yes go to step 6 else go to step 9 i = 2+(y+0.5*x) Print y, x and i x=x+0.5 and go to step 5 y=y+1 and go to step 3 Stop
Flowchart:
/*According to a study, the approximate level of intelligence of a person can be calculated using the formula: i = 2+(y+0.5x) Write a program that will produce a table of values of i,y and x, where y varies from 1 to 6, and, for each value of y,x varies from 5.5 to 12.5 in steps of 0.5.*/
#include<stdio.h> void main() { float x,i; int y; for(y=1;y<=6;y++) { for(x=5.5;x<=12.5;x+=0.5) { printf("i= %.2f for y= %d and x= %.2f \n",(2+(y+(0.5*x))),y,x); } } }
Output:
Program-24
Write a program to print the following output A B C D E F G F E D C B A A B C D E F F E D C B A A B C D E E D C B A A B C D D C B A A B C C B A A B B A A A
Algorithm:1. Start 2. Set i=65,j=71 3. Is i<=j if yes go to 4 otherwise go to 19 4. Is i<=j? if yes go to 5 otherwise go to 7 5. Print ascii value of i 6. i++ and go to 4 7. Set i=71 8. Is i>j? if yes go to step 9 otherwise go to step 11 9. Print space 10. iand go to step 8 11. Print backspace 12. Set i=j 13. Is i>=65? If yes go to step 14 otherwise go to step 16 14. Print ascii value of i 15. i-- and go to step 13 16. Print new line 17. Set i=65 18. j-- and go to step 3 19. Stop
Flowchart:
*/
A A A A A A A
B B B B B B
C C C C C
D E F G F E D D E F F E D D E E D D D
C C C C C
B B B B B B
A A A A A A A
#include<stdio.h> void main() { int i=65,j=71; while(i<=j) { for(i=65;i<=j;i++) printf("%c",i); for(i=71;i>j;i--) printf(" "); printf("\b"); for(i=j;i>=65;i--) printf("%c",i); printf("\n"); i=65; j--; } }
Output:
Program-25
Write a program to fill the entire screen with diamond and heart alternatively. The ASCII value of heart is 3 and that of diamond is 4.
Algorithm:1. Start 2. Set i=1; 3. Is i<=6000? If yes go to step 4 else go to 7 4. Print ASCII of 3 5. Print ASCII of 4 6. i++ and go to 3 7. Stop
Flowchart:
/*Write a program to fill the entire screen with diamond and heart alternatively. The ASCII value of heart is 3 and that of diamond is 4.*/
Output:
Program-26
Write a program to produce the following output 1 2 3 4 5 6 7 8 9 10
Algorithm:1. Start 2. Set i=0, j=3, k=0 and n=1 3. Is i<=3? If yes go to step 4 otherwise go to step 14 4. Print new line 5. Is (j>i)? If yes go to step 6 otherwise go to step 8 6. Print tab 7. j-- and go to step 5 8. Is k<=i? If yes go to step 9 otherwise go to step 13 9. Print n 10. Print tab 11. n=n+1 12. k++ and go to step 8 13. i++ and go to step 3 14. Stop
Flowchart:
*/
#include<stdio.h> void main() { int i,j,n=1,k,l; for(i=0;i<=3;i++) { printf("\n\n"); for(j=3;j>i;j--) printf("\t"); for(k=0;k<=i;k++) { printf("%d\t",n); printf("\t"); n++; } } printf("\n"); }
Output:
Program 27:
1 4
1 3
1 3
1 4
Algorithm: 1. Start 2. Input n, sp=20,i=0,j=0,k=0; 3. Is (i<=n)? if yes go to step 4 else go to step 21 4. Is (k<= (sp-2))? if yes go to 5 else go to 6 5. Print space and k=k+1 and go to step 4 6. (sp= (sp-2)) and go to step 7 7. Is j<=i? if yes go to step 8 8. Initialize f1=f2=f3=1 and t=i 9. Is t!=0? if yes go to step 10 else go to 12 10. f1 = f1*t 11. decrement t and go to step 9 12. t = j Is t!=0? if yes go to step 13 else go to step 15 13. f2 = f2*t 14. decrement t and go to step 12 15. t = i-j 16. Is t!=0? if yes go to step17 else 18 17. f3=f3*t decrement t and go to step 16 18. Calculate z=f1/f2*f3 19. Print z 20. Print new line 21. Stop
Flowchart:
1 */
#include<stdio.h> #include<conio.h> void main() { int i, j,n,f1,f2,f3,t,z,k,sp=20; clrscr(); printf("enter n:"); scanf("%d",&n); for(i=0;i<=n;i++) { for(k=0;k<sp-2;k++) printf(" "); sp=sp-2; for(j=0;j<=i;j++) { f1=f2=f3=1; t=i; while(t!=0) { f1=f1*t; t--; } t=j; while(t!=0) { f2=f2*t; t--; } t=i-j; while(t!=0) { f3=f3*t; t--; } z=f1/(f2*f3); printf("%4d",z); } printf("\n"); } getch(); }
Output:
Program 28:
Natural logarithm can be approximated by the following series: x-1/x+1/2(x-1/x)^2+1/2(x-1/x)^3+---------------If x is input through keyboard,write a program to calculate sum of first term of this series.
Algorithm:1.
2.
Start Input the value of x Set i=2 y=1/x s=x-y z=s Is i<=7? if yes go to 8 else go to 10 s=(s+(pow(z,i)/2)) i++ and go to 7
3.
4. 5. 6. 7. 8. 9.
10. Print s
11.
Stop
Flowchart:
/* Natural logarithm can be approximated by the following series: x-1/x+1/2(x-1/x)^2+1/2(x-1/x)^3+---------------If x is input through keyboard,write a program to calculate sum of first term of this series.*/
#include<stdio.h> #include<math.h> void main() { float x,y,z,s=0.0; int i; printf("Enter the value of x "); scanf("%f",&x); y=1/x; s=(x-y); z=s; for(i=2;i<=7;i++) { s=(s+(pow(z,i)/2)); } printf("sum of series (x - 1/x) + 1/2(x - 1/x)^2 + 1/2(x - 1/x)^3 + 1/2(x - 1/x)^4 + .....upto 7 terms is %f",s); }
Output:
Program 29:
Write a function pow(a,b) to calculate the value of a raised to b.
Algorithm:1. Start 2. Input a and b 3. Call power function 4. Print the value of ab 5. Stop
ALGORITHM OF FUNCTION:1. Start 2. Initialize x=1, i=1 3. Is i<=b? If yes go to step 4 else go to step6 4. x=x*a 5. i=i+1 and go to step3 6. Return the value x
Flowchart:
/* Write
b*/
#include<stdio.h> void pow(int,int); void main() { int a,b; printf("Enter Number : "); scanf("%d",&a); printf("Enter Power : "); scanf("%d",&b); pow(a,b); } void pow(int x,int y) { long int s=1,i; for(i=1;i<=y;i++) { s=s*x; } printf("\n %d^%d = %ld\n",x,y,s); }
Output:
Program 30:
Write a recursive function to obtain the Fibonacci series.
Algorithm:1 .Start 2. Set a=1, b=1 3. Input limit n 4. Print 1 5. Call function rec (a,b,1) 6. Stop
Algorithm of function:1. Start 2. Is i==n? If yes go to step 3 else go to step 4 3. Return 1 4. Print y 5. Return rec (y,x+y,i+1); 6. Stop
Flowchart:
#include<stdio.h>
int n; int rec(int,int,int); void main() { int a=1,b=1; printf("Enter the limit upto which fibonacci series is to generated:"); scanf("%d",&n); printf("\t 1"); rec(a,b,1); } int rec(int x, int y, int z) { int i=1; if (i==n) return 1; else printf("/t %d",y); return rec(y,x+y,i+1); }
Output:
Program 31:
Implement the selection sort algorithm.
Algorithm:1. Start 2. Input n and set i=0 3. Is i<n? If yes go to step4 else go to step 6 4. Input a[i] 5. i++ and go to step 3 6. i=0, i=i+1 7. Is i<n-1 ? If yes go to step 8 else go to step 15 8. Is (j<n)? If yes go to step9 else go to step 14 9. Is (a[i]>a[j])? If yes go to step10 else go to 13 10. temp= a[i] 11. a[i]=a[j] 12. a[j]=temp 13. j++ and go to step 8 14. i++ and go to step 7 15. Initialize i=0 16. Is i<n? If yes go to step 17 else go to step 19 17. Print sorted list a[i] 18. i++ and go to step 16 19. Stop
Flowchart:
#include<stdio.h> void main() { int a[25],i,j,temp,n; printf("Enter the number of elements of array"); scanf("%d", &n); printf(":Enter the elements:"); for (i=0;i<n;i++) { scanf("%d", &a[i]); } for (i=0;i<n-1;i++) { for (j=i++,j<n;j++) { if(a[i]>a[j]) { temp=a[i]; a[i]=a[j]; a[j]=temp; } } } printf("The sorted elements of array are:") for (i=0;i<n;i++) printf("\n %d",a[i]); }
Output:
Program 32:
Write a program which performs the following tasks 1. Initialize an integer array of 10 elements in main () 2. Pass the entire array to a function modify () 3. In modify () multiply each element of array by 3 4. Return the control to main () and print the new array element in main ()
Algorithm:1. Start 2. Initialize a[10] 3. Call function modify (a) 4. Initialize i=0 5. Is i<10? If yes go to step 6 else go to step8 6. Print a[i] 7. i++ and go to step5 8. Stop
Algorithm of function:1. 2. 3. 4. 5. 6. Start Initialize j=0 Is j<10?if yes go to step4 else go to step7 b[j]=b[j]*3 j++ and go to step3 Stop
Flowchart:
/* Write a program which performs the followng tasks 1.Initialize an integer array of 10 elements in main() 2.Pass the entire array to a function modify() 3.In modify() multiply each element of array by 3 4.Return the control to main() and print the new array element in main()*/
#include<stdio.h> #define size 10 void modify(int); void main() { int a[size],i; printf("enter the 10 elements in the array : "); for(i=0;i<size;i++) { scanf("%d",&a[i]); } modify(a); printf("the modified elements of the array are : "); for(i=0;i<size;i++) { printf("%d\n",a[i]); } } void modify(int b[]) { int j; for(j=0;j<size;j++) { b[j]=b[j]*3; } }
Output:
Program 33:
Write a program which when executed would keep converting every capital letter on the screen to small case letter and every small case letter to capital letter. The procedures should stop the moment the user hits a key from the keyboard.
Algorithm: 1. 2.
3.
Start Read the string str. While (!kbhit())?if yes go to step 4 otherwise go to step 14. Set i=0 Is str[i]=\0? if yes go to step 6 otherwise go to step 13 Set chk=str[i] Is ((chk>96)&&(chk<123))? If yes go to step 8 otherwise go to step 10 str[i]=toupper(str[i]) Print str[i] and go to step 12
4.
5.
6. 7. 8. 9.
12. i++ and go to step 5 13. Print newline and go to step 3 14. Stop.
Flowchart:
/* Write a program which when executed would keep converting every capital letter on the screen to small case letter and every small case letter to capital letter. The procedures should stop the moment the user hits a key from the keyboard.*/ #include<stdio.h> #include<conio.h> #include<ctype.h> void main() { char str[20]; int i,chk; printf("\nEnter the string :\t"); gets(str); while(!kbhit()) { for(i=0;str[i]!='\0';i++) { chk=str[i]; if(chk>96&&chk<123) { str[i]=toupper(str[i]); printf("%c",str[i]); } else { str[i]=tolower(str[i]); printf("%c",str[i]); } } printf("\n"); } }
Output:
Program 34
Write a program to find if a square matrix is symmetric or not.
Algorithm: 1. Start. 2. Accept the rows and columns of the matrix. 3. Accept the elements of the matrix. 4. Display the matrix 5. If rows = column then go to 6 else go to 9 6. Interchange the rows and columns of the matrix. 7. Check the transposed matrix with the original matrix. 8. If both are same, then display the entered matrix is symmetric. 9. Else display the entered matrix is not symmetric. 10. Stop.
Flowchart:
#include<stdio.h> void main() { int a[10][10],i,j,m,n,flag=0; printf("Enter number of rows : "); scanf("%d",&m); printf("Enter number of columns : "); scanf("%d",&n); for(i=1;i<=m;i++) { printf("Enter the elements of %d row ",i); for(j=1;j<=n;j++) { scanf("%d",&a[i][j]); } } printf("The entered %d*%d matrix is \n",m,n); for(i=1;i<=m;i++) { for(j=1;j<=n;j++) { printf("%d\t",a[i][j]); } printf("\n"); } if(m==n) {
for(i=1;i<=m;i++) { for(j=1;j<=n;j++) { if(a[i][j]!=a[j][i]) flag=1; } } if (flag==0) printf("\nThe entered matrix is symmetric"); else printf("\nThe entered matrix is not symmetric");
else
Output:
Program 35:
Write a program to find the norm of a matrix.
Algorithm: 1. 2. 3. 4. 5. 6. 7. 8. Start. Input the rows and col of a matrix a. Set i=1,j=1,s=0 Input the elements of the matrix. Is i<=rows ?if yes go to 6 else go to 13 Is j<=col ?if yes go to 7 else goto11 Input a[i][j] s=s+pow((a[i][j]),2)
14. norm=sqrt(s)
15. Display norm 16. Stop.
Flowchart:
#include<math.h> void main() { int a[10][10],s=0,i,j,m,n; float no; printf("Enter the order of the matrix:"); printf("\nRow = "); scanf("%d",&m); printf("\nColumn = "); scanf("%d",&n); printf("\nEnter the matrix:"); for(i=1;i<=m;i++) { printf("\nEnter the elements of %d row:",i); for(j=1;j<=n;j++) { scanf("%d",&a[i][j]); s=s+pow((a[i][j]),2); } } printf("\nThe matrix is:\n"); for(i=1;i<=m;i++) { for(j=1;j<=n;j++) { printf("%d\t",a[i][j]); } printf("\n"); } no=sqrt(s); printf("The norm of matrix is %0.3f",no); }
Output:
Program 36:
For the following set of sample data, compute the standard deviation and the mean. -6,-12,8,13,11,6,7,2,-6,-9,-10,11,10,9,2. The formula for standard deviation is sqrt of (xi-x)^2. where xi is data item and x is mean.
Algorithm:1. Start 2. Initialize mean=0,n=15,i=0. 3. Is i<=14? If yes go to step 4 else go to step 6 4. Mean=mean + data[i] 5. I++ and go to step3 6. Mean=mean/n 7. Print mean 8. Initialize i=0 9. Is i<=14? 10. Std[i]=sqrt(pow((data[i]-mean),2)/n) 11. Print std[i] 12. i++ and go to step9 13. Stop
Flowchart:
/* For the following set of sample data,compute the standard deviation and the mean. -6,-12,8,13,11,6,7,2,-6,-9,-10,11,10,9,2.
xi is data item
#include<stdio.h> #include<math.h> void main() { int a[15]={-6,-12,8,13,11,6,7,2,-6,-9,-10,11,10,9,2}; float x=0.0,m,sd,p; int i; for(i=0;i<15;i++) { x=x+a[i]; } m=x/15; printf("Mean is %0.2f\n",m); for(i=0;i<15;i++) { p=pow((a[i]-m),2); sd=sqrt(p/15); printf("Standard deviation of %d is %0.3f\n",a[i],sd); } }
Output:
Program 37: Write a program that converts a string like 124 to an integer 124
Algorithm: 1. Start
2. Input string number in an array str[20]
3. n=0,i=0
4. If str [i] !=\0 if yes then go to 5 else go to 8 5. If str [i]>=48 and str[i]<=57 if yes then go to 6 else go to 7
9. end
Flowchart:
124*/
#include<stdio.h>
void main() { char str[20]; int i,n=0; printf("Enter the number as string"); gets(str); for(i=0;str[i]!=\0;i++) { if(str[i]>=48 && str[i]<=57) n=n*10+str[i]-48; } printf("The entered number is %d\n",n); }
Output:
Practical 38:
Write a program to find sum of two matrices.
Algorithm: 1. 2. 3. 4.
5. 6.
Start. Set i=1,j=1 Input the rows and col of a matrix Input the elements of the matrix a Is i<=rows ?if yes go to 6 else go to 12 Is j<=col ?if yes go to 7 else goto11 Input a[i][j] j++ goto6
7. 8. 9.
10. i++ 11. goto 5 12. i=j=1 13. Input the elements of the matrix b
14. Is i<=rows ?if yes go to 15 else go to 21 15. Is j<=col ?if yes go to 1 else goto11
17. j++ 18. goto15 19. i++ 20. goto 14 21. i=j=1
22. Is i<=row? If yes go to step23 else go to step20 23. Is j<=col? If yes go to step 24 else go to step 26 24. c[i][j[]=a[i][j + b[i][j] 25. j++ and go to step 23 26. i++ and go to step 22
27. i=j=1.
28. Is i<r=ow? If yes go to step29 else go to 33 29. Is j<=col?if yes go to step30 else go to step32 30. Print the matrix c[i][j] 31. j++ and go to step29 32. i++ and go to step 28 33. Stop
Flowchart:
Stop
#include<stdio.h> void main() { int a[10][10],b[10][10],c[10][10],i,j,m,n,k; int ch; printf("Enter the order of the matrix:"); printf("\nRow = "); scanf("%d",&m); printf("\nColumn = "); scanf("%d",&n); printf("\nEnter the first matrix:"); for(i=1;i<=m;i++) { printf("\nEnter the elements of %d row:",i); for(j=1;j<=n;j++) { scanf("%d",&a[i][j]); } } printf("\nEnter the second matrix:"); for(i=1;i<=m;i++) { printf("\nEnter the elements of %d row:",i); for(j=1;j<=n;j++) { scanf("%d",&b[i][j]); } } printf("\nThe first matrix is:\n"); for(i=1;i<=m;i++) { for(j=1;j<=n;j++) { printf("%d\t",a[i][j]); } printf("\n"); } printf("\nThe second matrix is:\n"); for(i=1;i<=m;i++) { for(j=1;j<=n;j++) { printf("%d\t",b[i][j]); } printf("\n"); } printf("\nThe addition of above two matrices is:\n"); for(i=1;i<=m;i++) {
} }
Output:
Practical 39:
Write a program to find difference of two matrices
Algorithm
1. 2. 3. 4.
5. 6.
Start. Set i=1,j=1 Input the rows and col of a matrix Input the elements of the matrix a Is i<=rows ?if yes go to 6 else go to 12 Is j<=col ?if yes go to 7 else goto11 Input a[i][j] j++ goto6
7. 8. 9.
10. i++ 11. goto 5 12. i=j=1 13. Input the elements of the matrix b
14. Is i<=rows ?if yes go to 15 else go to 21 15. Is j<=col ?if yes go to 1 else goto11
17. j++ 18. goto15 19. i++ 20. goto 14 21. i=j=1
22. Is i<=row? If yes go to step23 else go to step20 23. Is j<=col? If yes go to step 24 else go to step 26
24. c[i][j[]=a[i][j+b[i][j]
25. j++ and go to step 23 26. i++ and go to step 22
27. i=j=1.
28. Is i<=row? If yes go to step29 else goto33 29. Is j<=col? if yes go to step30 else go to step32 30. Print the matrix c[i][j] 31. j++ and go to step29 32. i++ and go to step 28 33. Stop
Flowchart:
Stop
/* Write a program to find difference of two matrices.*/ #include<stdio.h> void main() { int a[10][10],b[10][10],c[10][10],i,j,m,n,k; int ch; printf("Enter the order of the matrix:"); printf("\nRow = "); scanf("%d",&m); printf("\nColumn = "); scanf("%d",&n); printf("\nEnter the first matrix:"); for(i=1;i<=m;i++) { printf("\nEnter the elements of %d row:",i); for(j=1;j<=n;j++) { scanf("%d",&a[i][j]); } } printf("\nEnter the second matrix:"); for(i=1;i<=m;i++) { printf("\nEnter the elements of %d row:",i); for(j=1;j<=n;j++) { scanf("%d",&b[i][j]); } } printf("\nThe first matrix is:\n"); for(i=1;i<=m;i++) { for(j=1;j<=n;j++) { printf("%d\t",a[i][j]); } printf("\n"); } printf("\nThe second matrix is:\n"); for(i=1;i<=m;i++) { for(j=1;j<=n;j++) { printf("%d\t",b[i][j]); } printf("\n"); } printf("\nThe subtraction of above two matrices is:\n"); for(i=1;i<=m;i++) { for(j=1;j<=n;j++) {
} }
printf("%d\t",a[i][j]+b[i][j]); } printf("\n");
Output:
Program 40:
Write a program to find the trace of a given matrix.
Algorithm: 1. Start. 2. Read row and col 3. If row != col then print not a square matrix. So trace cant be found and go to 11 4. Read the matrix a[row][col]. 5. Display matrix a[row][col] 6. Set sum=0. 7. Set i=1. 8. Check if i <= row, if not then go to step 10. 9. sum=sum+a[i][i] 10. Increment i by 1 and go to step 6. 11. Display count. 12. Stop.
Flowchart:
#include<stdio.h> void main() { int a[10][10],b[10][10],c[10][10],i,j,m,n,sum=0; int ch; printf("Enter the order of the matrix:"); printf("\nRow = "); scanf("%d",&m); printf("\nColumn = "); scanf("%d",&n); if(m!=n) printf("%d * %d matrix is not a square matrix.\nSo trace can't be found.",m,n); else { printf("\nEnter the matrix:"); for(i=1;i<=m;i++) { printf("\nEnter the elements of %d row:",i); for(j=1;j<=n;j++) { scanf("%d",&a[i][j]); } } printf("\nThe matrix is:\n"); for(i=1;i<=m;i++) { for(j=1;j<=n;j++) { printf("%d\t",a[i][j]); } printf("\n"); } for(i=1;i<=m;i++) { sum=sum+a[i][i]; } printf("Trace of matrix is %d\n",sum); } }
Output:
Program 41:
Write a program to find multiplication of two matrices
Algorithm: 1. Start. 2. Set i=1,j=1 3. Input the rows and col of a matrix 4. Input the elements of the matrix a
5. Is i<=rows ?if yes go to 6 else go to 12 6. Is j<=col ?if yes go to 7 else goto11
7. Input a[i][j] 8. j++ 9. goto6 10. i++ 11. goto 5 12. i=j=1 13. Input the elements of the matrix b
14. Is i<=rows ?if yes go to 15 else go to 21 15. Is j<=col ?if yes go to 1 else goto11
25.c[i][j]=0;
26. is k<=col? if yes go to step27 else go to step29
27.c[i][j]=c[i][j]+a[i][k]*b[k][j]
28. k++ and go to 26 29. Print(c[i][j]) 30. j++ and go to 24 31. Print new line 32. i++ and go to 23 33. Stop
Flowchart:
of two matrices.*/
#include<stdio.h> void main() { int a[10][10],b[10][10],c[10][10],i,j,m,n,k; int ch; printf("Enter the order of the matrix:"); printf("\nRow = "); scanf("%d",&m); printf("\nColumn = "); scanf("%d",&n); printf("\nEnter the first matrix:"); for(i=1;i<=m;i++) { printf("\nEnter the elements of %d row:",i); for(j=1;j<=n;j++) { scanf("%d",&a[i][j]); } } printf("\nEnter the second matrix:"); for(i=1;i<=m;i++) { printf("\nEnter the elements of %d row:",i); for(j=1;j<=n;j++) { scanf("%d",&b[i][j]); } } printf("\nThe first matrix is:\n"); for(i=1;i<=m;i++) { for(j=1;j<=n;j++) { printf("%d\t",a[i][j]); } printf("\n"); } printf("\nThe second matrix is:\n"); for(i=1;i<=m;i++) { for(j=1;j<=n;j++) { printf("%d\t",b[i][j]); } printf("\n"); } printf("\nThe multiplication of above two matrices is:\n"); for(i=1;i<=m;i++) { for(j=1;j<=n;j++) { c[i][j]=0; for(k=1;k<=n;k++)
Output:
Program 42:
Write a program to swap two numbers using call by value.
Algorithm: 1. Start 2. Input a and b 3. Call swap(a,b) 4. Stop
Algorithm Of function swap: 1. Initialize c=a 2. Initialize a=b 3. Initialize b=c 4. Print a and b 5. Stop
Flowchart:
/*Write a program to swap two numbers using call by value.*/ #include<stdio.h> void swap(int,int); void main() { int a,b,c; printf("Enter the first number:"); scanf("%d",&a); printf("Enter the second number:"); scanf("%d %d",&b); swap(a,b); } void swap(int a,int b) { int c; c=a; a=b; b=c; printf("The swapped value of a and b is %d and %d ",a,b); }
Output:
Program 43:
Write a program to swap two numbers using call by reference.
Algorithm: 1. Start 2. Input a and b
3. Call swap(a, b)
4. Stop
Algorithm Of function swap: 1. Initialize c=a 2. Initialize a=b 3. Initialize b=c 4. Print a and b 5. Stop
Flowchart:
#include<stdio.h> void swap(int*,int*); void main() { int a,b,c; printf("Enter two numbers:"); scanf("%d %d",&a,&b); swap(&a,&b); printf("After swap a=%d,b=%d",a,b); } void swap(int*a,int*b) { int c; c=*a; *a=*b; *b=c; }
Output:
Program 44:
Write a program to verify if the sum of five elements of a 5x5 matrix with elements 1 25 belonging to different rows and columns is 65 or not
Algorithm:
1. Set n=5; 2. Set i, j=n/2 3. Set k=1 4. Is k< square(n) , goto step 5 else goto step 15
5. matrix[i][j]=k
6. Is (k mod n)=0 goto step 7 else goto step 9
12. i=i+n
13. If j>(n-1) goto step 14 else goto step 4
14. j=j-n and goto step 4 15. Display matrix[5x5] 16. Stop
Flowchart:
/*Write a program to verify if the sum of five elements of a 5x5 matrix with elements 1 25 belonging to different rows and columns is 65 or not.*/ #include<stdio.h> void main() { int n=5, matrix[5][5], k=1; int nsqr = n * n; int i=0, j=n/2; for (k=1; k<=nsqr; ++k) { matrix[i][j] = k; i--; j++; if (k%n == 0) { i += 2; --j; } else { if (j==n) j -= n; else if (i<0) i += n; } } for(i=0;i<5;i++) { for(j=0;j<5;j++) printf("%d\t",matrix[i][j]); printf("\n\n"); } }
// start position
Output:
Program 45:
Write a program that replaces two or more consecutive blanks in a string by a single blank . For example , if the input is Grim return to the planet of apes!! The output should be Grim return to the planet of apes!!
9. Stop
Flowchart:
/*Write a program that replaces two or more consecutive blanks in a string by a single blank . */ #include<stdio.h> #include<string.h> void main() { int i; char str[100]; printf("Enter any string\n\n"); gets(str); printf("\n\nModified String : \n\n"); for(i=0;str[i]!='\0';i++) { if(str[i]==' ') continue; if(str[i-1]==' ') printf(" "); printf("%c",str[i]); } printf("\n\n\n"); }
Output:
Program 45:
Write a program to sort a set of names sorted in an array in alphabetical order
Algorithm:
1. Start
2. Initialize 3. Is i<10 ? if yes go to step 4 else go to 6 4. Input names[i] 5. i++ and go to 3 6. Initialize i=0, j=i+1 7. Is i<9 ? if yes go to step 8 else go to step 15 8. Is j<10? If yes go to step 9 else go to step 14 9. If (strcmp(names[i],names [j])>0)?if yes go to step10 else goto step 13 10. strcpy (temp, names[i]) 11. strcpy(names[i],names[j]) 12. strcpy(names[j],temp) 13. j++ and go to step 8 14. i++ and go to step 7 15. Print sorted list 16. Initialize i=0 17. Is i<10? If yes go to step 18 else go to step 20 18. Print names[i] 19. i++ and go to step 17 20. Stop
Flowchart:
/*Write a program to sort a set of names sorted in an array in alphabetical order.*/ #include<stdio.h> #include<string.h> #define size 5 #define size1 20 void main() { char a[size][size1],i,j,b[size1]; printf("Enter the list of names"); for(j=0;j<size;j++) { printf("\nEnter the %d name:\n",j+1); gets(a[j]); } printf("list of names is\n"); for(j=0;j<size;j++) { printf("%s\n",a[j]); } for(i=0;i<size-1;i++) { for(j=i+1;j<size;j++) { if(strcmp(a[i],a[j])>0) { strcpy(b,a[i]); strcpy(a[i],a[j]); strcpy(a[j],b); } } } printf("list of names in ascending order is \n"); for(j=0;j<size;j++) { printf("%s\n",a[j]); } }
Output:
Program 46:
Write a program for finding factorial of number using recursion.
Algorithm: 1. 2. 3. 4. 5. 6. 7. 8. 9. Start Input number n Initialize fact=1 is n>1? If yes go to step 5 else go to step 8 Calculate fact=fact*n Initialize n=n-1 go to step 4 Print fact Stop
Flowchart:
/*Program
for
finding
factorial
of number
using
recursion.*/
#include<stdio.h> int fact (int); void main() { int n, fvalue; printf("Enter the number"); scanf("%d",&n); fvalue = fact(n); printf("The factorial of %d is %d", n fvalue ); } int fact (int n) { if (n==0) return 1; else return n*fact(n-1); }
Output:
Program 47: