237
UNIVERSITY OF JAMMU C-Practical file Submitted to: Submitted by : Dr. Vinod Sharma Sonam Gandotra

Document_1

Embed Size (px)

Citation preview

Page 1: Document_1

UNIVERSITY OF JAMMU

C-Practical file

Submitted to: Submitted by : Dr. Vinod Sharma Sonam Gandotra

(04-MCA-2K11)

Certificate

Page 2: Document_1

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.

Dr. Vinod Sharma (Practical Incharge)

Page 3: Document_1

C Practical File

Index

S.No Name of the Program Page no.

Page 4: Document_1

1

Page 5: Document_1

Program1

Any year is input through the keyboard. Write a program to determine whether the year is a leap year or not.

Algorithm

1. Start2. Input year y3. if y is divisible by 100?

If yes goto 4 else goto 54. if y is divisible by 400?

Page 6: Document_1

If yes goto 6 else goto 75. if y is divisible by 4?

If yes goto 6 else goto 76. Print “year is a leap year” and goto 87. Print “year is not a leap year”8. Stop

Flowchart

Page 7: Document_1

/*Program to check whether the year is leap or not*/

#include<stdio.h>void main()

Page 8: Document_1

{int year;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{

if(year%4==0){

printf("%d is a leap year",year);

}else{

printf("%d is not a leap year",year);

}}

}

Output:

Page 9: Document_1

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.

Page 10: Document_1

Algorithm (Count(y)):1. Start2. Set c=03. If(y>2001)

a) for(i=2001;i<y;i++)i) If((i%4=0)&&(i%100!=0)||(i%400=0)),then c=c+1 and go to

step 6

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

5. Else c=c+0 and go to step 6 6. If(c=0),then return(c)7. Else return(c+1)8. Stop

Algorithm:1. Start2. Prompt and read year.3. Set z=y4. If(y>2001),then

a) d=y-2001b) CALL count(y)and set l=count(y)c) d=d+ld) If(d>8),then d=d%7e) If (d=0), then Display “Day is Sunday” and exit.f) If (d=1), then Display “Day is Monday” and exit.g) If (d=2), then Display “Day is Tuesday” and exit.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. Elsea) d=2001-yb) CALL count(y)and set l=count(y)c) d=d+ld) If(d>8),then d=d%7e) If (d=0), then Display “Day is Tuesday” and exit.f) If (d=1), then Display “Day is Monday” and exit.

Page 11: Document_1

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

Page 12: Document_1
Page 13: Document_1

/*ACCORDING TO GERGOGIAN CALENDER, IT WAS MONDAY ON 01/01/01.IF

Page 14: Document_1

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:

Page 15: Document_1

printf("\n 1st JANUARY OF %d is SUNDAY",z);break;

case 3:printf("1st JANUARY OF %d is SATURDAY",z);break;

case 4:printf("1st JANUARY OF %d is FRIDAY",z);break;

case 5:printf("1st JANUARY OF %d is THURSDAY",z);break;

case 6: printf("1st JANUARY OF %d Iis WEDNESDAY",z);

break;case 7:

printf("1st JANUARY OF %d is TUESDAY",z);break;

case 1:printf("1st JANUARY OF %d is MONDAY",z);break;

default: printf("1st JANUARY OF %d is TUESDAY",z);

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

}

Page 16: Document_1

Output2

Program no.3:

Page 17: Document_1

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 number’n’.3. Set m=n and rev=0.4. If ((n>9999)&&(n<100000))

a) While n!=>0i) rev=(rev*10)+m%10ii) n=n/10

b) Display “The reversed number”.c) If rev=m, then Display”Number is palindrome” and exit.d) Display”Number is not palindrome”

5. Display “Invalid Number”6. Stop

Page 18: Document_1

Flowchart:

Page 19: Document_1

/*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"); }

Page 20: Document_1

Output 3

Page 21: Document_1

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. Start2. Input the ages of Ram, Shyam and Ajay as a, b, c3. If a<b then

a) If a<c thenPrint “Ram is youngest”Go to 5b) Print ”Ajay is youngest”Go to 5

4. If b<c thena) Print ”Shyam is youngest”

Go to 5 b) Print ”Ajay is youngest”

5. Stop

Page 22: Document_1

Flowchart:

/*WAP TO DETERMINE YOUNGEST OF RAM,SHAM,AJAY*/

Page 23: Document_1

#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");

elseprintf("Ajay is youngest\n");

}else{

if(b<c)printf("Shyam is youngest\n");

elseprintf("Ajay is youngest\n");

}}

Page 24: Document_1

Output:

Page 25: Document_1

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.

Page 26: Document_1

Flowchart:

Page 27: Document_1

/*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);

}

Page 28: Document_1

Output:

Page 29: Document_1

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 47. Display LCM =m*i8. Display HCF =(m*n)/(m*i)9. Stop.

Flowchart:

Page 30: Document_1

/*Write a program to find L.C.M and H.C.F of two

numbers.*/

#include<stdio.h>void main(){

int a,b,i,h;

Page 31: Document_1

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);

}

Page 32: Document_1

Output:

Page 33: Document_1

PROGRAM 7 :

Write a program to find the number of trailing zeroes in the factorial of a numberwithout finding the actual factorial.

Algorithm:-

1. Start.2. Prompt and read the number n.3. Set m=n, zeros=0.4. If (m>1&&m<=100), go to step 6.5. Display you have entered out of range 1to 100 and exit.6. While(m>=5)

i) zeros=zeros+m/5ii) m=m/5iii) go to step 6

7. Display number of trailing zeros8. Stop.

Flowchart:

Page 34: Document_1

/*Write a program to find the number of trailing zeroes in the factorial of a number without finding the actual factorial.*/

#include<stdio.h>

Page 35: Document_1

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");}

Page 36: Document_1

Output:

Page 37: Document_1

PROGRAM 8: Write a program to find the roots of the quadratic equation.

Algorithm:-

1. Start.2. Prompt and read the co efficient of x2, x and constant or a, b, c.3. d=b2-4ac4. if (d=0), then

a) r1=-b/2ab) r2=r1c) Display ”Roots are real and equal”and display r1 and r2d) Go to step 7

5. If(d>0),thena) r1=(-b+sqrt(d))/2ab) r2=(-b-sqrt(d))/2ac) Display ”Roots are real ”and display r1 and r2d) Go to step 7

6. If(d<0), thena) real=-b/2ab) imag=sqrt(-d)/2ac) Display ”Roots are imaginary ”and display r1=real+imag and

r2=real+imagd) Go to step 7

7. Stop.

Flowchart:

Page 38: Document_1

/*Write a program to find the roots of the quadratic equation.*/

#include<stdio.h>#include<math.h>

Page 39: Document_1

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); }}

Page 40: Document_1

Output

Page 41: Document_1

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. Start.2. Input (x1,y1),(x2,y2),(x3,y3).3. Set a=04. Calculate a=((x2*y3)-(x3*y2))+((x1*y3)-(x3*y1))+((x1*y2)-(x2*y1))5. If a=0,then display”Points lie on a straight line” and go to step 76. Display” points do not lie on straight line”.7. Stop

Page 42: Document_1

Flowchart:

Page 43: Document_1

//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");

}

Page 44: Document_1

Output

Page 45: Document_1

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*r4. 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.

Page 46: Document_1

Flowchart:

Page 47: Document_1

/*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);

}

Page 48: Document_1

Output

Page 49: Document_1

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.

Algorithm:

1. Start.2. Input character a.3. Is a>=65 and a<=90? If yes, go to step 7 else go to step 44. Is a>=97 and a<=122? If yes, go to step 8 else go to step 55. If a>=48 and a<=57? If yes, go to step 9 else go to step 66. Is ((a>=0 and a<=47) or(a>=58 and a<=64)or(a>=91 and

a<=96)or(a>=123 and 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.

Page 50: Document_1

Flowchart:

Page 51: Document_1

/*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.*/

Page 52: Document_1

#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); } }

Page 53: Document_1

Output

Page 54: Document_1

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. Start2. Input no. of late days d3. 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 76. Print “fine is 1 rupee” and stop.7. Is d>=11 and d<=30? If yes, go to step 8 else go to step 98. Print “fine is 5 rupees” and stop.9. Print “membership cancelled”.10. Stop.

Page 55: Document_1

Flowchart:

Page 56: Document_1

/*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");}

Page 57: Document_1

Output:

Page 58: Document_1

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 Print“Equilateral triangle” and go to

step 7.4. If a=b or b=c or a=c, then Print”Isosceles triangle” and go to step 7.5. If a is not equal to both c and b and b is not equal to both a and c then,

Print “Scalene triangle” and go to step 7.6. If a*a==b*b + c*c or b*b==a*a + c*c or c*c==a*a + b*b then, Print

“Right angled triangle” and stop.7. Stop.

Page 59: Document_1

Flowchart:

Page 60: Document_1

/*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.*/

Page 61: Document_1

#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");

} }

Page 62: Document_1

Output:

Page 63: Document_1

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) Start.2) Prompt and read array of overtime of 10 employees a[10].3) Set i=14) If i<=10?, then go to step 5 otherwise go to step10 5) If a[i]>40, then go to step 6 otherwise go to step86) a[i]=a[i]-407) Print “Overtime of employee is a[i]*12 ”8) Print “Overtime of employee is Rs.0.00”9) i=i+1 and go to step 410) Stop.

Flowchart:

Page 64: Document_1

/*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>

Page 65: Document_1

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);}

}

Page 66: Document_1

Output

Page 67: Document_1

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 iii) Increment i with 1iii) Go to step3

4. Stop

Page 68: Document_1

Flowchart:

Page 69: Document_1

/*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 mainprintf("\nASCII value Equivalent Character");while(i<=255){printf("\n %d %c",i,i);i++;}

}

Page 70: Document_1

Output:

Page 71: Document_1

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. Start2. Input num, n, cube, d, sum.3. Print Armstrong numbers are:4. Initialize num=15. Is num<=500? If yes go to step 6 else go to step 136. n=num, initialize sum=07. Is n>0? If yes go to step 8 else go to step 9.8. Initialize d=n%10 and n=n/10

cube=d*d*dsum=sum + cube

9. Is num==sum? If yes go to step 10 else go to step 11.10. Print “num” and go to step 911. num++12. Go to step 513. Stop

Page 72: Document_1

Flowchart:

Page 73: Document_1

/*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.*/

Page 74: Document_1

#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);}

}

Page 75: Document_1

Output:

Page 76: Document_1

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. Set i=21

2. Print “welcome to game”

3. Print ”total number of match stick is 21”

4. Print ”pick 1,2,3 or 4 match stick”

5. Is i>1

6. Print ”enter your choice”

7. Input u

8. If u<1 or u>4 then go to step 9 otherwise go to step 10

9. Print ”invalid”and goto 4

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. Print”you loose”

18. End

Flowchart:

Page 77: Document_1

/*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:

Page 78: Document_1

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;

}}

}

Page 79: Document_1

Output:

Page 80: Document_1

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. Start2. Input d, b, p, z, n, i=1.3. p=0, z=0, n=04. Input d (no. of times user wants to enter)5. Is i<=d? if yes go to sep 6 else go to step 36. Input b7. Is b>0? If yes go to step 8 else go to step 98. Initialize p=p+19. Is b=0? If yes go to step 10 else go to step1110. Initialize z=z+111. Initialize n=n+1 and go to step 512. Print “Total numbers entered:”13. Print “number of positive numbers, negative numbers, and

zeros:”14. Stop

Page 81: Document_1

Flowchart:

Page 82: Document_1

/*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();}

Page 83: Document_1

Output:

Page 84: Document_1

Program 19:-

Write a program to receive an integer and find its octal equivalent.

Algorithm:-

1. Start2. Input number n.3. Save number n in nsave4. Initialize p=octal=05. Is n>0? If yes goto step 6 else goto step 106. rem=n%87. n=n/88. octal=octal + rem * pow(10, p)9. p = p+1 and goto step 510. Print octal11. Stop

Page 85: Document_1

Flowchart:

Page 86: Document_1

/*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();}

Page 87: Document_1

Output:

Page 88: Document_1

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. Start2. Read size of the array l.3. Prompt and read elements of array.4. Set max=a[0] , min=a[0] andi=0.5. Is i<l, then go to step 66. If max<=a[i],then go to step 7 7. Set max=a[i]8. i=i+1 and go to step 5.9. Is i<l, then go to step 1010. If min>=a[i],then go to step 11 11. Set min=a[i]12. i=i+1 and go to step 9.13. Print “Maximum”14. Print “Minimum”15. Print “Range=(max-min)”16. Stop.

Flowchart:

Page 89: Document_1

/*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.*/

Page 90: Document_1

#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);}

Page 91: Document_1

Output:

Page 92: Document_1

Program 21:-

Write a program to print all prime numbers between 1 to 300.

Algorithm:-

1. Start2. Set i=13. Is i<=300? If yes go to step 4 else go to step 134. Set isprime=1,j=25. Is j<=sqrt(i)? if yes go to step 6 else go to step 96. Is i%j==0,then7. isprime=08. break9. j++ and go to step 510. Is isprime= = 1? if yes go to step 1111. Print prime no i12. i++ and go to step 313. Stop

Page 93: Document_1

Flowchart:

Page 94: Document_1
Page 95: Document_1

/*Write a program to print all prime numbers between 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);}

}

}

Page 96: Document_1

Output:

Page 97: Document_1

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 (fact (i)):

1. Set f=1, j=12. Is (j<=i)?if yes go to step 33. f=f*j4. j=j+1 and go to step 25. Return f to the calling algorithm.

Algorithm:-

1. Start2. Set i=1, sum=0.03. Is i<=7? If yes go to step 4 else go to step 11.4. d=fact(i) //Function call5. term=i/d6. sum=sum+term7. i++ and go to step 38. display sum9. Stop

Page 98: Document_1

Flowchart:

Page 99: Document_1

/*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;

}

Page 100: Document_1

Output:

Page 101: Document_1

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. Start2. Set y=13. Is y<=6? If yes go to step 4 else go to step 104. Input x=5.55. Is x<=12.5? If yes go to step 6 else go to step 96. i = 2+(y+0.5*x)7. Print y, x and i8. x=x+0.5 and go to step 59. y=y+1 and go to step 310. Stop

Page 102: Document_1

Flowchart:

Page 103: Document_1

/*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);}

}}

Page 104: Document_1

Output:

Page 105: Document_1

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. Start2. Set i=65,j=713. Is i<=j if yes go to 4 otherwise go to 194. Is i<=j? if yes go to 5 otherwise go to 75. Print ascii value of i 6. i++ and go to 47. Set i=71 8. Is i>j? if yes go to step 9 otherwise go to step 11 9. Print space10. i—and go to step 811. Print backspace12. Set i=j13. Is i>=65? If yes go to step 14 otherwise go to step 1614. Print ascii value of i15. i-- and go to step 1316. Print new line17. Set i=6518. j-- and go to step 3 19. Stop

Flowchart:

Page 106: Document_1
Page 107: Document_1

/*Write a program to print the following output

Page 108: Document_1

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*/

#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--;

}}

Page 109: Document_1

Output:

Page 110: Document_1

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. Start2. Set i=1;3. Is i<=6000? If yes go to step 4 else go to 74. Print ASCII of 35. Print ASCII of 46. i++ and go to 37. Stop

Page 111: Document_1

Flowchart:

Page 112: Document_1

/*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.*/

#include<stdio.h>void main(){

int i;for(i=1;i<=6000;i++){

printf("%c%c",3,4);

}}

Page 113: Document_1

Output:

Page 114: Document_1

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=13. Is i<=3? If yes go to step 4 otherwise go to step 144. Print new line5. Is (j>i)? If yes go to step 6 otherwise go to step 86. Print tab 7. j-- and go to step 5 8. Is k<=i? If yes go to step 9 otherwise go to step 139. Print n10. Print tab11. n=n+112. k++ and go to step 813. i++ and go to step 314. Stop

Page 115: Document_1

Flowchart:

Page 116: Document_1

/*Write a program to produce the following output 1 2 3 4 5 6

Page 117: Document_1

7 8 9 10 */

#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");

}

Page 118: Document_1

Output:

Page 119: Document_1

Program 27:

Write a program to produce the following output

11 1

1 2 11 3 3 1

1 4 6 4 1

Algorithm:

1. Start2. Input n, sp=20,i=0,j=0,k=0;3. Is (i<=n)? if yes go to step 4 else go to step 214. Is (k<= (sp-2))? if yes go to 5 else go to 65. Print space and k=k+1 and go to step 46. (sp= (sp-2)) and go to step 77. Is j<=i? if yes go to step 88. Initialize f1=f2=f3=1 and t=i9. Is t!=0? if yes go to step 10 else go to 1210. f1 = f1*t11. decrement t and go to step 912. t = j Is t!=0? if yes go to step 13 else go to step 1513. f2 = f2*t14. decrement t and go to step 1215. t = i-j16. Is t!=0? if yes go to step17 else 1817. f3=f3*t decrement t and go to step 1618. Calculate z=f1/f2*f319. Print z20. Print new line21. StopFlowchart:

Page 120: Document_1

/*Write a program to produce the following output

11 1

1 2 1 1 3 3 1 1 4 6 4 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();

}

Page 121: Document_1

Output:

Page 122: Document_1

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. Start2. Input the value of x3. Set i=24. y=1/x5. s=x-y6. z=s7. Is i<=7? if yes go to 8 else go to 108. s=(s+(pow(z,i)/2))9. i++ and go to 710. Print s11. Stop

Page 123: Document_1

Flowchart:

Page 124: Document_1

/* 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);}

Page 125: Document_1

Output:

Page 126: Document_1

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

Page 127: Document_1

Flowchart:

Page 128: Document_1

/* Write a function pow(a,b) to calculate the value of a raised to b*/

Page 129: Document_1

#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);

}

Page 130: Document_1

Output:

Page 131: Document_1

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

Page 132: Document_1

Flowchart:

Page 133: Document_1

/*Write a recursive function to obtain the Fibonacci sequence*/

Page 134: Document_1

#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);}

Page 135: Document_1

Output:

Page 136: Document_1

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 1310. temp= a[i]11. a[i]=a[j]12. a[j]=temp13. j++ and go to step 814. i++ and go to step 715. Initialize i=016. Is i<n? If yes go to step 17 else go to step 1917. Print sorted list a[i]18. i++ and go to step 1619. Stop

Page 137: Document_1

Flowchart:

Page 138: Document_1

/*Implement the selection sort algorithm*/

#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]);

}

Page 139: Document_1

Output:

Page 140: Document_1

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. Start 2. Initialize j=03. Is j<10?if yes go to step4 else go to step74. b[j]=b[j]*35. j++ and go to step36. Stop

Page 141: Document_1

Flowchart:

Page 142: Document_1

/* 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 10void 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;

}}

Page 143: Document_1

Output:

Page 144: Document_1

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. Start2. Read the string str.3. While (!kbhit())?if yes go to step 4 otherwise go to step 14.4. Set i=05. Is str[i]=’\0’? if yes go to step 6 otherwise go to step 136. Set chk=str[i]7. Is ((chk>96)&&(chk<123))? If yes go to step 8 otherwise go to step 108. str[i]=toupper(str[i])9. Print str[i] and go to step 1210. str[i]=tolower(str[i])11. Print str[i] and go to step 1212. i++ and go to step 513. Print newline and go to step 314. Stop.

Page 145: Document_1

Flowchart:

Page 146: Document_1

/* 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");

}}

Page 147: Document_1

Output:

Page 148: Document_1

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 matrix5. If rows = column then go to 6 else go to 96. 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.

Page 149: Document_1

Flowchart:

Page 150: Document_1

/* Write a program to find if a square matrix is symmetric or not.*/

Page 151: Document_1

#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");}

elseprintf("\nThe entered matrix is not a square matrix.\nHence it is

not symmetric");

}

Page 152: Document_1

Output:

Page 153: Document_1

Program 35:

Write a program to find the norm of a matrix.

Algorithm:

1. Start.2. Input the rows and col of a matrix a.3. Set i=1,j=1,s=04. Input the elements of the matrix.5. Is i<=rows ?if yes go to 6 else go to 136. Is j<=col ?if yes go to 7 else goto117. Input a[i][j]8. s=s+pow((a[i][j]),2)9. j++10. goto611. i++12. goto 513. Display matrix14. norm=sqrt(s)15. Display norm16. Stop.

Page 154: Document_1

Flowchart:

Page 155: Document_1

/* Write a program to find the norm of a matrix.*/

#include<stdio.h>

Page 156: Document_1

#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);}

Page 157: Document_1

Output:

Page 158: Document_1

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. Start2. Initialize mean=0,n=15,i=0.3. Is i<=14? If yes go to step 4 else go to step 64. Mean=mean + data[i]5. I++ and go to step36. Mean=mean/n7. Print mean8. Initialize i=09. Is i<=14? 10. Std[i]=sqrt(pow((data[i]-mean),2)/n)11. Print std[i]12. i++ and go to step913. Stop

Page 159: Document_1

Flowchart:

Page 160: Document_1

/* 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.

Page 161: Document_1

The formula for standard deviation is sqrt of (xi-x)^2.where xi is data item and x is mean.*/

#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);}

}

Page 162: Document_1

Output:

Page 163: Document_1

Program 37:

Write a program that converts a string like “124” to an integer 124

Algorithm:

1. Start2. Input string number in an array ”str[20]”3. n=0,i=04. If str [i] !=’\0’ if yes then go to 5 else go to 85. If str [i]>=48 and str[i]<=57 if yes then go to 6 else go to 76. n=n*10+ str [i]-487. i=i+1 and go to 48. Print n9. end

Page 164: Document_1

Flowchart:

Page 165: Document_1

/*WRITE A PROGRAM THAT CONVERTS A STRING LIKE “124” TO AN INTEGER 124*/

Page 166: Document_1

#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);}

Page 167: Document_1

Output:

Page 168: Document_1

Practical 38:

Write a program to find sum of two matrices.

Algorithm:

1. Start.2. Set i=1,j=13. Input the rows and col of a matrix 4. Input the elements of the matrix a5. Is i<=rows ?if yes go to 6 else go to 126. Is j<=col ?if yes go to 7 else goto117. Input a[i][j]8. j++9. goto610. i++11. goto 512. i=j=113. Input the elements of the matrix b14. Is i<=rows ?if yes go to 15 else go to 2115. Is j<=col ?if yes go to 1 else goto1116. Input b[i][j]17. j++18. goto1519. i++20. goto 1421. i=j=122. Is i<=row? If yes go to step23 else go to step2023. Is j<=col? If yes go to step 24 else go to step 2624. c[i][j[]=a[i][j + b[i][j]25. j++ and go to step 2326. i++ and go to step 2227. i=j=1.28. Is i<r=ow? If yes go to step29 else go to 3329. Is j<=col?if yes go to step30 else go to step3230. Print the matrix c[i][j]

Page 169: Document_1

31. j++ and go to step2932. i++ and go to step 2833. Stop

Page 170: Document_1

Flowchart:

Page 171: Document_1
Page 172: Document_1

Stop

Page 173: Document_1

/*Write a program to find sum 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 addition of above two matrices is:\n");for(i=1;i<=m;i++){

Page 174: Document_1

for(j=1;j<=n;j++){

printf("%d\t",a[i][j]+b[i][j]);}printf("\n");

}}

Page 175: Document_1

Output:

Page 176: Document_1

Practical 39:

Write a program to find difference of two matrices

Algorithm

1. Start.2. Set i=1,j=13. Input the rows and col of a matrix 4. Input the elements of the matrix a5. Is i<=rows ?if yes go to 6 else go to 126. Is j<=col ?if yes go to 7 else goto117. Input a[i][j]8. j++9. goto610. i++11. goto 512. i=j=113. Input the elements of the matrix b14. Is i<=rows ?if yes go to 15 else go to 2115. Is j<=col ?if yes go to 1 else goto1116. Input b[i][j]17. j++18. goto1519. i++20. goto 1421. i=j=122. Is i<=row? If yes go to step23 else go to step2023. Is j<=col? If yes go to step 24 else go to step 2624. c[i][j[]=a[i][j+b[i][j]25. j++ and go to step 2326. i++ and go to step 2227. i=j=1.28. Is i<=row? If yes go to step29 else goto3329. Is j<=col? if yes go to step30 else go to step3230. Print the matrix c[i][j]31. j++ and go to step29

Page 177: Document_1

32. i++ and go to step 2833. Stop

Page 178: Document_1

Flowchart:

Page 179: Document_1
Page 180: Document_1

Stop

Page 181: Document_1

/* 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++){

Page 182: Document_1

printf("%d\t",a[i][j]+b[i][j]);}printf("\n");

}}

Page 183: Document_1

Output:

Page 184: Document_1

Program 40:

Write a program to find the trace of a given matrix.

Algorithm:

1. Start.2. Read row and col3. If row != col then print ”not a square matrix. So trace can’t be found”

and go to 114. 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.

Page 185: Document_1

Flowchart:

Page 186: Document_1

/* Write a program to find the trace of a given matrix.*/

#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);

}}

Page 187: Document_1

Output:

Page 188: Document_1

Program 41:

Write a program to find multiplication of two matrices

Algorithm:

1. Start.2. Set i=1,j=13. Input the rows and col of a matrix 4. Input the elements of the matrix a5. Is i<=rows ?if yes go to 6 else go to 126. Is j<=col ?if yes go to 7 else goto117. Input a[i][j]8. j++9. goto610. i++11. goto 512. i=j=113. Input the elements of the matrix b14. Is i<=rows ?if yes go to 15 else go to 2115. Is j<=col ?if yes go to 1 else goto1116. Input b[i][j]17. j++18. goto1519. i++20. goto 1421. display a[ ][ ] and b[ ] [ ]22. i=j=1.23. Is i<=row? If yes go to step24 else goto3324. is j<=col? if yes go to step25 else go to step3125. c[i][j]=0;26. is k<=col? if yes go to step27 else go to step2927. c[i][j]=c[i][j]+a[i][k]*b[k][j]28. k++ and go to 2629. Print(“c[i][j]”)30. j++ and go to 2431. Print new line

Page 189: Document_1

32. i++ and go to 2333. Stop

Page 190: Document_1

Flowchart:

Page 191: Document_1
Page 192: Document_1
Page 193: Document_1
Page 194: Document_1

/*Write a program to find multiplication 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++)

Page 195: Document_1

c[i][j]=c[i][j]+a[i][k]*b[k][j];printf("%d\t",c[i][j]);

}printf("\n");

}}

Page 196: Document_1

Output:

Page 197: Document_1

Program 42:

Write a program to swap two numbers using call by value.

Algorithm:

1. Start2. Input a and b3. Call swap(a,b)4. Stop

Algorithm Of function swap:

1. Initialize c=a2. Initialize a=b3. Initialize b=c4. Print a and b5. Stop

Page 198: Document_1

Flowchart:

Page 199: Document_1

/*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);}

Page 200: Document_1

Output:

Page 201: Document_1

Program 43:

Write a program to swap two numbers using call by reference.

Algorithm:

1. Start2. Input a and b3. Call swap(a, b)4. Stop

Algorithm Of function swap:

1. Initialize c=a2. Initialize a=b3. Initialize b=c4. Print a and b5. Stop

Page 202: Document_1

Flowchart:

Page 203: Document_1

/*Write a program to swap two numbers using call by reference.*/

#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;

}

Page 204: Document_1

Output:

Page 205: Document_1

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/23. Set k=14. Is k< square(n) , goto step 5 else goto step 155. matrix[i][j]=k6. Is (k mod n)=0 goto step 7 else goto step 97. i=i+28. j=j-19. i=i-110. j=j+111. Is i<0 goto 12 else goto step 1312. i=i+n13. If j>(n-1) goto step 14 else goto step 414. j=j-n and goto step 415. Display matrix[5x5]16. Stop

Page 206: Document_1

Flowchart:

Page 207: Document_1

/*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; // start positionfor (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");}

}

Page 208: Document_1

Output:

Page 209: Document_1

Program 45:

Write a program that replaces two or more consecutive blanks in a string by a single blank . For example , if the input isGrim return to the planet of apes!!The output should beGrim return to the planet of apes!!

Algorithm :

1. Input string in str[100]2. Set i=0;3. Is str[i]!=Null, then go to step 4 else, go to step 94. Is str[i]= blank space, then i=i+1 and go to step 35. Is str[i-1]= blank space, then go to step 66. Print single blank space7. Print ‘str[i]’8. i=i+1 , and go to step 39. Stop

Page 210: Document_1

Flowchart:

Page 211: Document_1

/*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");}

Page 212: Document_1

Output:

Page 213: Document_1

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

Page 214: Document_1

Flowchart:

Page 215: Document_1

/*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 20void 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]);

}}

Page 216: Document_1

Output:

Page 217: Document_1

Program 46:

Write a program for finding factorial of number using recursion.

Algorithm:

1. Start 2. Input number n 3. Initialize fact=1 4. is n>1? If yes go to step 5 else go to step 8 5. Calculate fact=fact*n 6. Initialize n=n-1 7. go to step 4 8. Print fact 9. Stop

Page 218: Document_1

Flowchart:

Page 219: Document_1

/*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;elsereturn n*fact(n-1);}

Page 220: Document_1

Output:

Page 221: Document_1

Program 47: