Você está na página 1de 85

UVPCE Practical-1

Explain Directory and Rules for filename. Directory is a logical storage unit that enable computer users to group files in a systematic way in named file. It also contains sub-directories. Rules for Filename:The filename is divided into two parts Filename and Extension which is separated by dot(.) The filename should be maximum thirty two characters long, but first eights are recognized by DOS system. Extension must not exceed three characters. It is optional part in filename. Certain characters like *, . , + , - , \ , / should not be included in filename. Space should not be allowed in filename. Filename contain alphabet like [A-Z] , [a-z] etc. Certain characters like $ , # , ^ , , are allowed as filename. Explain Following DOS commands 1. Md :- It is used to create new directory. Ex. md < Directory Name > 2. Dir :- It displays list of all files and sub directories. Ex. Dir 3. Dir \ p :- It displays list of files and sub directories in page wise. Ex. Dir \ p 4. Dir \ w :- It displays list of files and sub directories in width wise. Ex. Dir \w 5. Dir *.c \ p :- It displays all files having .c extension in page wise. Ex. Dir *.c \ p 6. Dir ?p*.* :- It displays all files whose second character is p. Ex. Dir ?p*.* 7. Cd :- It is used to change directory. Ex. cd <filename> 8. Cd \ :- It is used to enter into root directory Ex. Cd \

9. Cd.. :- It is used to mone directory up in the hierarchy of directory. Ex. Cd.. Explain the purpose of Editor and Compiler. Editor :- It is the operating Environment in which we write all our C program ( Source Code ) and can modify them. Compiler :- It is the program which checks our written C program ( Source Code ) for any Syntax error and incorrect statement.It converts Source code into object code. Explain the purpose of following short cut keys 1. Alt + f :- It opens file menu. 2. Alt + e :- It opens edit menu. 3. Alt + o :- It opens Option menu. 4. f2 :- It saves the program. 5. f3 :- It opens the previously saved file. 6. Alt + f3 :- It closes the current file. 7. Alt + x :- It exits from Turboc. 8. Alt + f9 :- It compiles .C program. 9. Ctrl +f9 :- It run .C program. 10.Alt + f5 :- It shows the output. 11.Ctrl + f1 :- It is used to find help for particular topic. 12.Shift + f1 :- It is used to see the help index. 13.f7 :- It is used for debugging the program. 14.f9 :- It is used to make .exe 15.f6 : - It is used to move between open window. Explain the difference between save and save as. Save :- It is used to store the changes / modification in already stored / saved file. Save as :- It is used to store the already stored file with a different filename.

Practical-2
1)/*Write a C program to display Hello on the screen.*/ #include<stdio.h> void main() { printf(Hello); } 2)/* Write a C program which display Hello World a) Using one printf b) Using two printf c) Using three printf */ a) #include<stdio.h> void main() { printf(Hello\nWorld); } b) #include<stdio.h> void main() { printf(Hello\n); printf(World); } c) #include<stdio.h> void main() { printf(Hello); printf(\n); printf(World); }

3) /* Write a C program which displays a) Hello World b) Hello World */ a) #include<stdio.h> void main() { printf(\Hello World\); }

b) #include<stdio.h> void main() { printf(\Hello World\); } 4) /* Write a C program which displays ************** * Hello World* ************** */ #include<stdio.h> void main() { printf(************** ); printf(* Hello\tWorld *); printf(************** ); } 5) */ Write a C program to print a) 15/8/1947 b) 15/8/1947 c) 15/8/1947 d) 15\8\1947 e) 15\\8\\1947 /* a) #include<stdio.h> void main() {

printf(15/8/1947); } b) void main() { printf(\15/8/1947\ ); } c) void main() { printf(\15/8/1947\ ); } d) void main() { printf(15\\8\\1947); } e) void main() { printf(15\\\\8\\\\1947); }

Practical-3
1) /*Write a program which prints integer constant(15) and floating constant(20.153000). (a) 15 20.153000(tab in between) (b) 15 20.153000(space in between) (c) 15 is an integer constant & 20.153000 is a floating constant. (d) 15 20.153000 */ #include<stdio.h> #include<conio.h> void main() { const int a=15; const float b=20.153000; clrscr(); printf("\t\n\n%d\t%f",a,b); printf("\t\n\n%d %f",a,b); printf("\t\n\n%d is an integer constant & %f is a floating constant.",a,b); printf("\t\n\n%d\n %f",a,b); getch(); }

2) /*Write the above program for integer variable a=15 and floating variable b=20.153000.*/ #include<stdio.h> #include<conio.h> void main() { int a=15; float b=20.153000; clrscr();

printf("%d\t%f",a,b); getch(); } 3) /*Specify format specifier for :

char - %c unsigned char - %c int - %d unsigned int - %u signed int - %d long int - %ld unsigned long int - %ul float - %f double - %lf long double - %lf */ 4a) /* See the output of following program and give the conclusion about the output.*/ a) #include<stdio.h> #include<conio.h> void main() { int a=5,b=32767; int c=32768; int d=32769; int e=-32768,f=-32769,g=-32770; clrscr(); printf("%d %u\n",a,a); printf("%d %u\n",b,b); printf("%d %u\n",c,c); printf("%d %u\n",d,d); printf("%d %u\n",e,e); printf("%d %u\n",f,f); printf("%d %u\n",g,g); getch(); } /* Conclusion: This program follows the cycle rule for various datatypes. when variable value exceeds the range of integer at minus side it gets the value anticlockwise from the greatest

positive value and if the variable value exceeds the range of integer at positive side it gets the value clockwise from the lowest nagative value.*/ 4b) /* See the output of following program and give the conclusion about the output*/. (b) #include<stdio.h> #include<conio.h> void main() { unsigned int a=32767,b=32768; unsigned int c=65535; unsigned int d=65536; unsigned int e=65537,f=-1,g=-2; clrscr(); printf("%d %u\n",a,a); printf("%d %u\n",b,b); printf("%d %u\n",c,c); printf("%d %u\n",d,d); printf("%d %u\n",e,e); printf("%d %u\n",f,f); printf("%d %u\n",g,g); getch(); } /* Conclusion: This program follows the cycle rule for various datatypes. when variable value exceeds the range of integer at minus side it gets the value anticlockwise from the greatest positive value and if the variable value exceeds the range of nteger at positive side it gets the value clockwise from the lowest nagative value. */ 4c) /* See the output of following program and give the conclusion about the output.*/ (c) #include<stdio.h> #include<conio.h> void { char char char main() j='xyz'; //you will get an error. a='p',b='pr',c=97,d=48,e=305,f=97; g='a';

char h='125'; //you will get an error. char i; clrscr(); printf("%c %d\n",a,a); printf("%c %d\n",b,b); printf("%c %d\n",c,c); printf("%c %d\n",d,d); printf("%c %d\n",e,e); printf("%c %d\n",f,f); printf("%c %d\n",g,g); printf("%c %d\n",h,h); i=getch(); } /* Conclusion: Character type variable can take only 1 character.If we try to store more than character ,it displays an error. And for correctly stored single character in char type variable %c specifies that character and %d will give the ascii value of that character. */ 5a)/*Consider the following data : Your program has two integer variables a and b. Their values are 15 and 20 respectively.This prog.add two integer data and put the sum in third variable called sum and then prints it on the screen. */ #include<stdio.h> #include<conio.h> void main() { int a=15,b=20; int sum=0; clrscr(); sum=a+b; printf("\n Sum = %d",sum); getch(); } 5b) /*Write program p3-5a.c for floating type variables.*/

#include<stdio.h>

#include<conio.h> void main() { float a=15,b=20; float sum=0; clrscr(); sum=a+b; printf("\n Sum = %f",sum); getch(); } 5c ) /*Write program p3-5a.c for char type variables.*/

#include<stdio.h> #include<conio.h> void main() { char a='15',b='20'; char sum=0; clrscr(); sum=a+b; printf("\n Sum = %c",sum); getch(); }

Practical 4
1a) /* Write a program which scans two int variable a & b. Then it adds these two variables and puts the sum in third variable called sum and then prints it on the screen.Write the program using two different ways (a) using one scanf() (b) using two scanf() */ #include<stdio.h> #include<conio.h> void main() { int a,b,sum=0; clrscr(); printf("\n Enter the value scanf("%d %d",&a,&b); sum=a+b; printf("\n Sum = %d",sum); getch(); }

of a and b :\n ");

1b) /*Write a program p4-1a.c (b) using two scanf() */ #include<stdio.h> #include<conio.h> void main() { int a,b,sum=0; clrscr(); printf("\n Enter the value of a :\n "); scanf("%d",&a);

printf("\n Enter the value of b :\n "); scanf("%d",&b); sum=a+b; printf("\n Sum = %d",sum); getch(); } 2) /* Write the program p4-1a.c for floating type variable. */ #include<stdio.h> #include<conio.h> void main() { float a,b,sum=0; clrscr(); printf("\n Enter the value of a and b :\n "); scanf("%f %f",&a,&b); sum=a+b; printf("\n Sum = %f",sum); getch(); } 3) /* Write the program p4-1a.c for character type variable. */ #include<stdio.h> #include<conio.h> void main() { char a,b,sum; clrscr(); printf("\n Enter the value of a and b:\n "); scanf("%c %c",&a,&b); sum=a+b; printf("\n Sum = %c",sum); getch(); } 4) /* Write a program to calculate simple interest. */

#include<stdio.h>

#include<conio.h> void main() { float p,r,y,i=0; clrscr(); printf("\n Enter principle amount :"); scanf("%f",&p); printf("\n Enter the interest rate :"); scanf("%f",&r); printf("\n Enter the no. of years :"); scanf("%f",&y); i=(p*r*y)/100; printf("\n Simple Interest : %f",i); getch(); } 5) /* See the output of following program and give reason for output. */ #include<stdio.h> #include<conio.h> void main() { int a=15,b=20; int c,d,e; clrscr(); c = a > b; d = a + b; e = a && b; printf("%d %d %d",c,d,e); getch(); } /* Reason : (1) value of c will become 0 because a>b is wrong so it will return 0, that will be stored in c. (2) value of d will become 35 .Because it will store the sum of 15 and 20. (3) value of e will become 1, because a&&b is true as a and b both are Nonzero. so it will return 1, that will be stored in e. */

6) /*Demonstrate difference between the postfix and prefix increment operator using a program.*/ #include<stdio.h> #include<conio.h> void main() { int a,x,y; clrscr(); printf("\n Enter the value of a: "); scanf("%d",&a); y=a++; x=++a; printf("\nY=%d",y); printf("\nX=%d",x); getch(); } 7) /*Demonstrate the use of a conditional operator using a program.*/ #include<stdio.h> #include<conio.h> void main() { int a,b,x; clrscr(); printf("\n Enter the value of a:"); scanf("%d",&a); printf("\n Enter the value of b:"); scanf("%d",&b); x=a>b?a:b; printf("%d is the greater value",x); getch(); }

8) /*Write a program which finds the root of a quadratic equation. Equation: If delta > = 0 (Where delta= b*b - 4*a*c ) then solutions are x1 = (-b-sqrt(delta)) / 2*a; x2 = (b+sqrt(delta)) / 2*a; Else sulutions are not possible. */ #include<stdio.h> #include<conio.h> #include<math.h> void main() { float delta,a,b,c,x1,x2; clrscr(); printf("\n Enter the value of A ,B and C :"); scanf("%f %f %f",&a,&b,&c); delta=b*b - 4*a*c; printf("\n Value of Delta = %f\n",delta); if(delta>=0) { x1=(-b+sqrt(delta))/2*a; x2=(-b-sqrt(delta))/2*a; printf("\n Roots are X1 = %f and X2= %f",x1,x2); } else printf("\n Roots are not possible."); getch(); } 9) /*Write a program to find the volume of a cylinder. vol= pi*(r*r)*h */ #include<stdio.h> #include<conio.h> void main() { float r,h,vol; const float pi=3.14; clrscr();

printf("\n Enter the radius and height of a cylinder in CM:"); scanf("%f %f",&r,&h); vol=pi*(r*r)*h; printf("\n Volume of a Cylinder is = %f CM^3",vol); getch(); } 10) /*Write a program which scans the distance in feets and prints the same in inches.*/ #include<stdio.h> #include<conio.h> void main() { float feet,inches; clrscr(); printf("\n Enter the distance in feet :"); scanf("%f",&feet); inches=feet*12; printf("\n The distance in inches = %f",inches); getch(); }

Practical-5

1) /*Input distance between two cities in KM through keyboard. Write a program to convert and print this distance in meter , feet ,inches and centimeter . ( 1 meter = 3.33 feets , 1 feet = 12 inches )*/ #include<stdio.h> #include<conio.h> void main() { float inches,feet,meter,km; clrscr(); printf("\n Enter the distance between two cities in km :"); scanf("%f",&km); meter =km*1000; feet =meter*3.33; inches=feet*12; printf("\n The same distance in Meter = %f\n\n\t\t\t Feet = %f\n\n\t\t\t and Inches = %f",meter,feet,inches); getch(); } 2) /* Write a Program to convert an angle from degree to radian radian = degree * PI / 180 */ #include<stdio.h> #include<conio.h> void main() { const float pi=3.14; float degree,radian; clrscr(); printf("\n Enter the value of angle in degree : "); scanf("%f",&degree); radian = degree * pi / 180;

printf("\n Radian = %f ",radian); getch(); } 3) /* Write a Program to convert an angle from radian to degree. degree = radian * 180 / pi */ #include<stdio.h> #include<conio.h> void main() { const float pi=3.14; float degree,radian; clrscr(); printf("\n Enter the value of angle in radian : "); scanf("%f",&radian); degree = radian * 180 / pi; printf("\n Degree = %f ",degree); getch(); } 4) /*Write a program which find out cos , sin , tan of given angle. (Note : After entering value of angle in degree convert it into radian.) */ #include<stdio.h> #include<conio.h> #include<math.h> void main() { float radian,degree,b,c,d; const float pi = 3.14; clrscr(); printf("\n Enter the value of angle in degree : ");

scanf("%f",&degree); radian=(degree*pi)/180; b=cos(radian); c=sin(radian); d=tan(radian); printf("\n Cos of %f Degree = %f \n\n Sin of %f degree = %f \n\n Tan of %f Degree = %f",degree,b,degree,c,degree,d); getch(); }

5) /*Write a program to find out ( a^x log a base e ) hint: ( use pow(a,x) to find out a^x and use log(a) to find out log a base e) */ #include<stdio.h> #include<conio.h> #include<math.h> void main() { float a,x,ans; clrscr(); printf("\n Enter the value of a and x: "); scanf("%f %f",&a,&x); ans=pow(a,x)*log(a); printf("\n Answer = %f",ans); getch(); }

6a) /*Write a program to use mathmatics functions given below [1] x = -23.6 ceil(x);

floor(x); abs(x); exp(x); */ #include<stdio.h> #include<conio.h> #include<math.h> void main() { const float x=-23.6; float a,b,c,d; clrscr(); a=ceil(x); b=floor(x); c=abs(x); d=exp(x); printf("\n Celing value of -23.6 = %f",a); printf("\n\n Floor value of -23.6 = %f",b); printf("\n\n Absolute value of -23.6 = %f",c); printf("\n\n Exponetion value of -23.6 = %f",d); getch(); } 6b) /*Write a program to use mathmatics functions given below [2] x = 19.6, y = 8.2 fmod(x,y); */ #include<stdio.h> #include<conio.h> #include<math.h> void main() { const float x=19.6,y=8.2; float ans; clrscr(); ans=fmod(x,y);

printf("\n fmod(19.6,8.2) = %f",ans); getch(); }

Practical-6 1) /*Write a program to swap two integers a and b. You are allowed to use any additional variable.*/ #include<stdio.h> #include<conio.h> #include<math.h> void main() { int a=2,b=4,temp; clrscr(); printf("\n Before swaping value of A = %d and B = %d",a,b); temp=a; a=b; b=temp; printf("\n\n After swaping value of A = %d and B = %d",a,b); getch(); }

2) /*Write the p6-1.c program without using any additional variable.*/ #include<stdio.h> #include<conio.h> #include<math.h> void main() { int a=2,b=4; clrscr(); printf("\n Before swaping A = %d and B = %d",a,b); b=a+b; a=b-a; b=b-a; printf("\n\n After swaping A = %d and B = %d",a,b); getch(); } 3) /*Write a program, which scans number of days and then prints it in months.*/ #include<stdio.h> #include<conio.h> #include<math.h> void main() { int days; float months; clrscr(); printf("\n Enter no of days : "); scanf("%d",&days); months=days/30; printf("\n Months = %f",months);

getch(); } 4) /*Explain the difference between following expressions. a+b / c+d (a+b)*(a-b) a+b/2 (a+b )/ (c+d) a+b*a-b (a+b)/2 where a,b,c,d are int variables.*/ #include<stdio.h> #include<conio.h> void main() { int a,b,c,d; float x1,x2,y1,y2,z1,z2; clrscr(); printf("\n Enter the value of A,B,C and D :"); scanf("%d %d %d %d",&a,&b,&c,&d); x1=(float)(a+b / c+d); x2=(float)((a+b )/ (c+d)); y1=(float)(a+b*a-b); y2=(float)((a+b)*(a-b)); z1=(float)(a+b/2); z2=(float)((a+b)/2); printf("\n Value of X1 = %f and X2 = %f",x1,x2); printf("\n Value of Y1 = %f and Y2 = %f",y1,y2); printf("\n Value of Z1 = %f and Z2 = %f",z1,z2); getch(); } /* Conclusion: For a=b=c=d=2, Value of X1 = 5 and X2 = 1 Value of Y1 = 4 and Y2 = 0 Value of Z1 = 3 and Z2 = 2

Difference between X1 and X2, Y1 and Y2, Z1 and Z2 is due to the Operators Precedence and Associativity. */ 6a) /* See the output of the following program and give the conclusion about the output. (a) */ #include<stdio.h> #include<conio.h> void main() { int x; x=3*4%5; printf("x=%d",x); getch(); } 6b) /* See the output of the following program and give the conclusion about the output. (b) */ #include<stdio.h> #include<conio.h> void main() { int x; clrscr(); x=3+4-7*8/5%10; printf("x=%d",x); getch(); } 6c) /* See the output of the following program and give the conclusion about the output. (c) */

#include<stdio.h> #include<conio.h> void main() { int x; clrscr(); x=4%5+6%5; printf("%d",x); getch(); } 6d) /* See the output of the following program and give the conclusion about the output. (d) */ #include<stdio.h> #include<conio.h> void main() { int x; clrscr(); s= -3*-4%-6/-5; printf("%d",x); getch(); } 6e) /* See the output of the following program and give the conclusion about the output. (e) */ #include<stdio.h> #include<conio.h> void main() { float a=1.5; int b=3; clrscr(); a=b/2+b*8/b-b+a/3;

printf("%f",a); getch(); } 6f) /* See the output of the following program and give the conclusion about the output. (f) */ #include<stdio.h> #include<conio.h> void { main() int a,b; clrscr(); a=5.999999; b=5.000001; printf("a=%d getch(); } 6g) /* See the output of the following program and give the conclusion about the output. (g) */ #include<stdio.h> #include<conio.h> void main() { int x=10,y=5,p,q; clrscr(); p=x>9; q=x>3 && y!=3; printf("p=%d q=%d",p,q); getch(); } 6h) /* See the output of the following program and give the conclusion about the output. (h) */

b=%d",a,b);

#include<stdio.h> #include<conio.h> void main() { int a=30,b=40,x; clrscr(); x=(a!=10) && (b =50); printf("x=%d",x); getch(); } 6i) /* See the output of the following program and give the conclusion about the output. (i) */ #include<stdio.h> #include<conio.h> void main() { int x=11,y=6,z; clrscr(); z=x==5 || y != 4; printf("z=%d",z); getch(); } 6j ) /* See the output of the following program and give the conclusion about the output. (j) */ #include<stdio.h> #include<conio.h> void main() { int i=-4,j,num=10; clrscr(); j=i%-3; j=(j?0:num*num); printf("j=%d",j);

getch(); } 6k) /* See the output of the following program and give the conclusion about the output. (k) */ #include<stdio.h> #include<conio.h> void main() { int x=3; clrscr(); x*=x+4; printf("x=%d",x); getch(); } 6l) /* See the output of the following program and give the conclusion about the output. (l) */ #include<stdio.h> #include<conio.h> void main() { int x=3,y,z; clrscr(); z=y=x; z*=y=x*x; printf("x=%d y=%d z= %d",x,y,z); getch(); } 6m) /* See the output of the following program and give the conclusion about the output. (m) */

#include<stdio.h> #include<conio.h> void main() { int x=3,z; clrscr(); z=x++ + 10; printf("x=%d z=%d",x,z); getch(); } 6n) /* See the output of the following program and give the conclusion about the output. (n) */ #include<stdio.h> #include<conio.h> void main() { int x=3,z; clrscr(); z=x---111; printf("x=%d z=%d",x,z); getch(); } 6o) /* See the output of the following program and give the conclusion about the output. (o) */ #include<stdio.h> #include<conio.h> void main() { int x=3,z; clrscr(); z=x++ + ++x; printf("x=%d z=%d",x,z); getch();

6p) /* See the output of the following program and give the conclusion about the output. (p) */ #include<stdio.h> #include<conio.h> void main() { int i=3,j; clrscr(); j=++i*++i*++i; printf("%d %d",i,j); getch(); } 6q) /* See the output of the following program and give the conclusion about the output. (q) */ #include<stdio.h> #include<conio.h> void main() { int x=10,y,z; clrscr(); z=y=x; y-=x--; z-=--x; x-=--x - x--; printf("x=%d y=%d z= %d",x,y,z); getch(); }

6r) /* See the output of the following program and give the conclusion about the output. (r) */ #include<stdio.h> #include<conio.h> void main() { int x,y,z; clrscr(); x=y=z=1; z=++x && ++y && ++z; printf("x=%d y=%d z= %d",x,y,z); getch(); }

6s) /* See the output of the following program and give the conclusion about the output. (s) */ #include<stdio.h> #include<conio.h> void main() { int x,y,z; clrscr(); x=y=z=-1; z=++x && ++y || ++z; printf("x=%d y=%d z= %d",x,y,z); getch(); }

6t) /* See the output of the following program and give the conclusion about the output. (t) */ #include<stdio.h> #include<conio.h> void main() { int a=b=c=d=30; clrscr(); printf("%d %d %d %d",a,b,c,d); getch(); } 6u) /* See the output of the following program and give the conclusion about the output. (u) */ #include<stdio.h> #include<conio.h> void main() { float a,b; int i,j; clrscr(); a=(i=sizeof(i),j=sizeof(b),i+j); printf("%f",a); getch(); }

Practical 7 1) // Write a Program which scans two integer numbers and then find which one is // greater. If both the number are equal it displays appropriate message. #include<stdio.h> #include<conio.h> void main() { int a,b; printf("\n Enter two different numbers A and B:"); scanf("%d %d",&a,&b); if(a>b) printf("A is greater than B"); else if(b>a) printf("B is greater than A"); else printf("A and B are equal"); getch(); } 2) // Write a Program p7-1.c using switch(). #include<stdio.h> #include<conio.h> void main() { int a,b; clrscr(); printf("\n Enter two diffrent numbers A and B."); scanf("%d %d",&a,&b); switch(a>b) { case 0:switch(a==b) { case 0: printf("\nB is greater than A."); break;

case 1: printf("\nA and B are equal."); break; } break; case 1: printf("\nA is greater than B."); break; } getch(); } 3) // Write a Program which scans three integer numbers and then find which one is // greater. #include<stdio.h> #include<conio.h> void main() { int a,b,c; clrscr(); again: printf("\n Enter three different numbers A, B and C :"); scanf("%d %d %d",&a,&b,&c); if(a>b && a>c) printf("A is greater than B and C."); else if(b>a && b>c) printf("B is greater than A and C."); else if(c>a && c>b) printf("C is greater than A and B."); else { printf("Numbers are not different. Again enter three nos."); goto again; } getch(); }

4) // Write a Program which scans two integer numbers . // If both the number are equal then reject them and scan again. // and then find which one is greater. #include<stdio.h> #include<conio.h> void main() { int a,b; clrscr(); start: printf("\n Enter two different numbers A and B:"); scanf("%d %d",&a,&b); if(a>b) printf("A is greater than B"); else if(b>a) printf("B is greater than A"); else { printf("A and B are equal"); goto start; } getch(); } 5) // Write a Program p7-4.c using switch(). #include<stdio.h> #include<conio.h> void main() { int a,b; clrscr(); start: printf("\n Enter two diffrent numbers A and B."); scanf("%d %d",&a,&b); switch(a>b) {

case 0:switch(a==b) { case 0: printf("\nB is greater than A."); break; case 1: printf("\nA and B are equal."); goto start; } break; case 1: printf("\nA is greater than B."); break; } getch(); } 6) // Write a Program p7-5.c using switch() for three numbers. #include<stdio.h> #include<conio.h> void main() { int a,b,c; clrscr(); start: printf("\n Enter three diffrent numbers A,B and C."); scanf("%d %d %d",&a,&b,&c); switch(a==b==c) { case 1:goto start; break; case 0:switch(a>b) { case 1: if(a>c) printf("\n A is the greatest Number."); else

printf("\n C is the greatest Number."); break; case 0: if(b>c) printf("\n B is the greatest Number."); else printf("\n C is the greatest Number."); break; } break; } getch(); } 7) // write a program which scan any integer and then finds the sum of all the digits of the number. #include<stdio.h> #include<conio.h> void main() { int no,sum=0; clrscr(); printf("\n Enter the Number:"); scanf("%d",&no); while(no>0) { sum+=no%10; no=no/10; } printf("Sum of all digits : %d",sum); getch(); } 8) //write a program which finds whether the entered number is odd or even. //Display an appropriate message if entered number is zero. #include<stdio.h>

#include<conio.h> void main() { int no; clrscr(); printf("\n Enter the number:"); scanf("%d",&no); if(no==0) printf("\n Entered no is zero."); else if(no%2==0) printf("\n Entered no is even"); else printf("\n Entered no is odd"); getch(); } 9) //write a program which scans any three digit integer and then displays //it in reverse order(e.g. 123-->321) #include<stdio.h> #include<conio.h> #include<math.h> void main() { int no,rev=0; clrscr(); printf("\n Enter the Number:"); scanf("%d",&no); printf("\n Reverse Number :"); while(no>0) { rev=no%10; printf("%d",rev); no=no/10; } getch(); }

10) //write the p7-9.c program in which to display the number in reverse order //without using printf() in loop. #include<stdio.h> #include<conio.h> #include<math.h> void main() { int no,rev=0; clrscr(); printf("\n Enter the Number:"); scanf("%d",&no); while(no>0) { rev=rev*10; rev+=no%10; no=no/10; } printf("Reverse Number : %d",rev); getch(); } 11) //write a program which scans a character and then finds whether it is //uppercase,lowercase or a digit. Do not use any character test function. #include<stdio.h> #include<conio.h> void main() { char ch; clrscr(); printf("\n Enter the character."); scanf("%c",&ch); if(ch>=65 && ch<=90)

printf("\n Entered character is in Uppercase."); else if(ch>=48 && ch<=57) printf("\n Entered character is digit"); else if(ch>=97 && ch<=122) printf("\n Entered character is in lowercase."); else printf("\n Character is not Alphanumerical Character."); getch(); } 12) //write a program which scans a character and then finds whether it is //uppercase,lowercase or a digit. Do not use any character test function. #include<stdio.h> #include<conio.h> void main() { char ch; clrscr(); printf("\n Enter the character."); scanf("%c",&ch); if(ch>=65 && ch<=90) printf("\n Entered character is in Uppercase."); else if(ch>=48 && ch<=57) printf("\n Entered character is digit"); else if(ch>=97 && ch<=122) printf("\n Entered character is in lowercase."); else printf("\n Character is not Alphanumerical Character."); getch(); }

13) //write a program to find whether the entered number is prime or not. #include<stdio.h> #include<conio.h> void main() { int no,i,flag=0; clrscr(); printf("\n Enter the number :"); scanf("%d",&no); for(i=2;i<no;i++) { if(no%i==0) { flag=1; break; } } if(flag==1) printf("\n Entered number is not prime."); else printf("\n Entered number is prime."); getch(); } 14a) //write a program to find factorial of any integer using (a) for loop #include<stdio.h> #include<conio.h> void main() { int no,i,fact=1; clrscr(); printf("\n Enter the Number :"); scanf("%d",&no); for(i=2;i<=no;i++) { fact=fact*i;

} printf("\n Factorial = %d",fact); getch(); } 14b) //write a program to find factorial of any integer using (a) while loop #include<stdio.h> #include<conio.h> void main() { int no,i=2,fact=1; clrscr(); printf("\n Enter the Number :"); scanf("%d",&no); while(i<=no) { fact=fact*i; i++; } printf("\n Factorial = %d",fact); getch(); } 14c) //write a program to find factorial of any integer using (a)do...while loop #include<stdio.h> #include<conio.h> void main() { int no,i=2,fact=1; clrscr(); printf("\n Enter the Number :"); scanf("%d",&no); do { fact=fact*i; i++;

}while(i<=no); printf("\n Factorial = %d",fact); getch(); } 15) //write a program to generate fibonacci series. #include<stdio.h> #include<conio.h> void main() { int x=0,y=1,z=0,limit=5,i; clrscr(); printf("\n Fibonacci Series :\n\n"); for(i=1;i<=limit;i++) { printf("\t %d",z); z=x+y; x=y; y=z; } getch(); }

16) //write a program which checks whether the entered number is palindrome or not. #include<stdio.h> #include<conio.h> #include<math.h> void main() { int no,rev=0,temp; clrscr(); printf("\n Enter the Number:"); scanf("%d",&no); temp=no; while(no>0) {

rev=rev*10; rev+=no%10; no=no/10; } if(temp==rev) printf("\nNumber is Palindrome."); else printf("\nNumber is not Palindrome."); getch(); } 17) //write a program which scans two integer multiplies these integers and //puts the result in third variable. Do not use multiplication operator. #include<stdio.h> #include<conio.h> void main() { int no1,no2,result=0,i; clrscr(); printf("\n Enter two integer numbers ."); scanf("%d %d",&no1,&no2); for(i=1;i<=no1;i++) { result+=no2; } printf("\n Result= %d",result); getch(); } 18) //write a program to find the number of and sum of all integers greater //than 100 and less than 200 that are divisible by 7. #include<stdio.h> #include<conio.h> void main() {

int sum=0,i,count=0; clrscr(); for(i=101;i<200;i++) { if(i%7==0) { sum+=i; count++; } } printf("\n Sum= %d",sum); printf("\n Number = %d",count); getch(); } 19a) /* write a program to make floyd's triangle. 1 2 3 4 5 6 7 8 9 10 79----------------91 */ #include<stdio.h> #include<conio.h> void main() { int i,j,limit=13,count=1; clrscr(); printf("\n Floyd's Triangle.\n\n"); for(i=1;i<=limit;i++) { for(j=1;j<=i;j++) { printf(" %d",count); count++; } printf("\n\n"); } getch();

} 19b) /* write a program to print the following triangle. 1 2 2 3 3 3 4 4 4 4 5 5 5 5 5 */ #include<stdio.h> #include<conio.h> void main() { int i,j,limit=5; clrscr(); printf("\n Floyd's Triangle.\n\n"); for(i=1;i<=limit;i++) { for(j=1;j<=i;j++) { printf(" %d",i); } printf("\n\n"); } getch(); }

Practical - 8 1)/* Write a program which scans five values into an array and displays them in following form. a[0]=value1 a[1]=value2

a[2]=value3 a[3]=value4 a[4]=value5 */ #include<stdio.h> #include<conio.h> void main() { int a[5],i; clrscr(); printf("\n Enter five values :"); for(i=0;i<5;i++) { scanf("%d",&a[i]); } for(i=0;i<5;i++) { printf("\n a[%d]= %d",i,a[i]); } getch(); } 2) /* Write a program which adds all the elements of a floating array and display result */ #include<stdio.h> #include<conio.h> void main() { float a[5],sum=0; int i; clrscr(); printf("\n Enter five floating point values :"); for(i=0;i<5;i++) { scanf("%f",&a[i]); sum+=a[i]; }

printf(" Sum of all floating numbers = %f",sum); getch(); } 3a) /* Write a program which finds highest element with the position from (a) int array */ #include<stdio.h> #include<conio.h> void main() { int i,a[5],max=0; clrscr(); printf("\n Enter five integer values :"); for(i=0;i<5;i++) { scanf("%d",&a[i]); } for(i=0;i<5;i++) { if(a[i]>max) max=a[i]; } printf("Maximum = %d",max); getch(); } 3b) /* Write a program which finds highest element with the position from (b) char array */ #include<stdio.h> #include<conio.h> void main() { int i; char a[5],max=' '; clrscr(); printf("\n Enter five characters(without blank spaces or enter key:");

for(i=0;i<5;i++) { scanf("%c",&a[i]); } for(i=0;i<5;i++) { if(a[i]>max) max=a[i]; } printf("Maximum = %c",max); getch(); } 4) /* Write a program to arrange the elements in ascending order of (a) int array */ #include<stdio.h> #include<conio.h> void main() { int i,j,a[5],temp=0; clrscr(); printf("\n Enter five integer values :\n "); for(i=0;i<5;i++) { scanf("%d",&a[i]); } for(i=0;i<5;i++) { for(j=i+1;j<5;j++) { if(a[i]>a[j]) { temp=a[i]; a[i]=a[j]; a[j]=temp; } } } printf("\n Numbers in Ascending Order : "); for(i=0;i<5;i++)

printf("\n%d",a[i]); getch(); } 5) /*Cosider the following data - Three int array A,B and C of five elements - Scan the two array A and B - Array C is the summation of array A and B. - Don't use any other array. */ #include<stdio.h> #include<conio.h> void main() { int a[5],b[5],c[5],i; clrscr(); printf("\n Enter elements for array A :\n"); for(i=0;i<5;i++) scanf("%d",&a[i]); printf("\n Enter elements for array B :\n"); for(i=0;i<5;i++) scanf("%d",&b[i]); printf("\n Resultant array :\n "); for(i=0;i<5;i++) { c[i]=a[i]+b[4-i]; printf("\n %d",c[i]); } getch(); } 6) /* Write a program which counts and prints all odd elements of an int array. */ #include<stdio.h> #include<conio.h> void main() {

int a[10],i,count=0; clrscr(); printf("\n Enter 10 elements :\n "); for(i=0;i<10;i++) { scanf("%d",&a[i]); } for(i=0;i<10;i++) { if(a[i]%2!=0) { count++; printf("\n %d",a[i]); } } printf("\n Total odd numbers = %d",count); getch(); } 7) /* Write a program to delete an element of an array.*/ #include<stdio.h> #include<conio.h> void main() { int a[5],i,pos; clrscr(); printf("\n Enter 5 elements of an array :\n"); for(i=0;i<5;i++) { scanf("%d",&a[i]); } printf("\n Enter position(0-4) which is to be deleted :"); scanf("%d",&pos); if(pos>0 && pos<4) { for(i=pos;i<5;i++) { a[i]=a[i+1]; } for(i=0;i<4;i++)

{ printf("\n%d ",a[i]); } } else { printf("\n Entered position is not valid.Retry..."); } getch(); } 8) /* Write a program to remove duplicate values from an array and display the array. */ #include<stdio.h> #include<conio.h> void main() { int a[10],i,j,k,count=10; clrscr(); printf("\n Enter 10 elements of an array :\n"); for(i=0;i<10;i++) { scanf("%d",&a[i]); } for(i=0;i<10;i++) { for(j=i+1;j<10;j++) { if(a[i]==a[j]) { for(k=j;k<10;k++) a[k]=a[k+1]; j--; count--; } } } printf("\n Array after removing duplicate elements : \n ");

for(i=0;i<count;i++) { printf("\n%d ",a[i]); } getch(); } 9) /*Read two matrices of n*m multiply them and display the resultant matrix.*/ #include<stdio.h> #include<conio.h> void main() { int a[3][3],b[3][3],c[3] [3],i,j,k; clrscr(); printf("\n Enter elements of 3x3 matrix A :\n"); for(i=0;i<3;i++) { for(j=0;j<3;j++) { scanf("%d",&a[i] [j]); } } printf("\n Enter elements of 3x3 matrix B :\n"); for(i=0;i<3;i++) { for(j=0;j<3;j++) { scanf("%d",&b[i] [j]); } } printf("\n Multiplication of Matrix :\n"); for(i=0;i<3;i++) { for(j=0;j<3;j++) {

c[i][j]=0; for(k=0;k<3;k++) { c[i][j]+=a[i] [k]*b[k][j]; } printf("\t%d",c[i][j]); } printf("\n"); } getch(); } 10) /*Read two matrices of 3x3 and add them, then display the resultant matrix.*/ #include<stdio.h> #include<conio.h> void main() { int a[3][3],b[3][3],c[3] [3],i,j,k; clrscr(); printf("\n Enter elements of 3x3 matrix A :\n"); for(i=0;i<3;i++) { for(j=0;j<3;j++) { scanf("%d",&a[i] [j]); } } printf("\n Enter elements of 3x3 matrix B :\n"); for(i=0;i<3;i++) { for(j=0;j<3;j++) { scanf("%d",&b[i] [j]); } }

printf("\n Addition of two Matrices :\n\n "); for(i=0;i<3;i++) { for(j=0;j<3;j++) { c[i][j]=a[i][j] +b[i][j]; printf("\t%d",c[i] [j]); } printf("\n"); } getch(); }

Practical 9 1a) // write a prog to find length of string (a) using strlen() #include<stdio.h> #include<conio.h> #include<string.h> void main() { char str[20];

int len; clrscr(); printf("\nEnter the string : "); gets(str); len = strlen(str); printf("\nThe length of string is : %d",len); getch(); } 1b) // write a prog to find length of string (b) without using strlen() #include<stdio.h> #include<conio.h> void main() { char str[20]; int i,len=0; clrscr(); printf("Enter the string : "); gets(str); for(i=0;str[i]!='\0';i++) len++; printf("\nThe length of string is : %d",len); getch(); } 2a) //Write a prog to reverse a string (a) using strrev() #include<stdio.h> #include<conio.h> #include<string.h> void main() { char str[20]; clrscr(); printf("\nEnter the string : "); gets(str); strrev(str); printf("\n\nThe reverse string is : %s",str); getch(); }

2b) // write a prog to reverse the string (B) without using strrev() #include<stdio.h> #include<conio.h> #include<string.h> void main() { char str1[20],str2[20]; int len,i,j; clrscr(); printf("Enter the string : "); gets(str1); len=strlen(str1); for(i=len-1,j=0;i>=0;i--,j++) str2[j] = str1[i]; str2[j] = '\0'; printf("\nThe reverse string is : %s",str2); getch(); } 3a) // write a prog to copy one string to another #include<stdio.h> #include<conio.h> #include<string.h> void main() { char str1[20],str2[20]; clrscr(); printf("Enter the string : "); gets(str1); strcpy(str2,str1); printf("\nThe copied string is : %s",str2); getch(); } 3b) // write a prog to copy one string to another (B) without using strcpy() #include<stdio.h> #include<conio.h> void main()

{ char str1[20],str2[20]; int i; clrscr(); printf("Enter the string : "); gets(str1); for(i=0;str1[i]!='\0';i++) str2[i] = str1[i]; str2[i] = '\0'; printf("\nThe copied string is : %s",str2); getch(); } 4a) // write a prog to concatanate two strings (A) using strcat() #include<stdio.h> #include<conio.h> void main() { char str1[40],str2[20]; clrscr(); printf("Enter the first string : "); gets(str1); printf("Enter the second string : "); gets(str2); strcat(str1,str2); printf("\n\nThe concatanated string is : %s",str1); getch(); } 4b) // write a prog to concatanate two strings (B) without using strcat() #include<stdio.h> #include<conio.h> void main() { char str1[40],str2[20]; int i,j; clrscr(); printf("Enter the first string : "); gets(str1);

printf("Enter the second string : "); gets(str2); for(i=0;str1[i]!='\0';i++); for(j=0;str2[j]!='\0';j++,i++) str1[i] = str2[j]; str1[i] = '\0'; printf("\n\nThe resultant string is : %s",str1); getch(); } 5a) // write a prog to convert string into lowercase (A) using strlwr() #include<stdio.h> #include<conio.h> #include<string.h> void main() { char str[20]; clrscr(); printf("Enter the string : "); gets(str); strlwr(str); printf("\nThe converted string is : %s",str); getch(); } 5b) //write a prog to convert string into lowercase (B) without using strlwr() #include<stdio.h> #include<conio.h> void main() { char str[20]; int i; clrscr(); printf("Enter the string : "); gets(str); for(i=0;str[i]!='\0';i++) if(str[i]>=65 && str[i]<=91)

str[i]+=32; printf("the resultant string is : %s",str); getch(); } 6a) // write a prog to convert string into uppercase (A) using strupr() #include<stdio.h> #include<conio.h> #include<string.h> void main() { char str[20]; clrscr(); printf("Enter the string : "); gets(str); strupr(str); printf("\nThe converted string is : %s",str); getch(); } 6b) //write a prog to convert string into uppercase (B) without using strupr() #include<stdio.h> #include<conio.h> void main() { char str[20]; int i; clrscr(); printf("Enter the string : "); gets(str); for(i=0;str[i]!='\0';i++) if(str[i]>=97 && str[i]<=122) str[i]-=32; printf("the resultant string is : %s",str); getch(); }

7) //write a prog to swap even positioned charactar with odd positioned char #include<stdio.h> #include<conio.h> #include<string.h> void main() { char str[20],temp; int i,len; clrscr(); printf("Enter the string : "); gets(str); len = strlen(str); for(i=0;i<len-1;i+=2) { temp = str[i]; str[i] = str[i+1]; str[i+1] = temp; } printf("\nThe resultant string is : %s",str); getch(); } 8) // write a prog to check whether the entered string is palindrom or not. #include<stdio.h> #include<conio.h> #include<string.h> void main() { char str[20],dummy[20]; int i,j=1; clrscr(); printf("Enter the string : "); gets(str); strcpy(dummy,str); strrev(dummy); for(i=0;str[i]!='\0';i++) if(str[i]!=dummy[i]) { j=0;

break; } if(j==1) printf("\n\nString is palindrome. "); else printf("\n\nString is NOT palindrome. "); getch(); } 10) // write a program to sort the characters in a given string. // (i.e. 'program' -> 'agmoprr') #include<stdio.h> #include<conio.h> #include<string.h> void main() { char str[20],ch; int len,i,j; clrscr(); printf("Enter the string : "); gets(str); len = strlen(str); for(i=0;i<=len-1;i++) for(j=i+1;j<=len-1;j++) { if(str[i]>str[j]) { ch = str[i]; str[i] = str[j]; str[j] = ch; } } printf("\nThe resultant string is : %s",str); getch(); } 11) /* Write a program to cut a portion of a string. Take input for string and both positions for cutting. (suppose string is "palindrome", cut this string from 4-7, then resultant string will be "palome") */ #include<stdio.h>

#include<conio.h> #include<string.h> void main() { char str[20],res[20]; int start,end,i,j,len; clrscr(); printf("Enter the string : "); gets(str); len = strlen(str); read: printf("Enter the starting index : "); scanf("%d",&start); printf("Enter the ending index : "); scanf("%d",&end); if(start>len || end<0 || start>end || end>len-1) { printf("\nInvalid index. Enter again.\n"); goto read; } for(i=0,j=0;i<start-1;i++,j++) res[j] = str[i]; for(i=end;i<=len;i++,j++) res[j] = str[i]; res[j] = '\0'; printf("\n\nThe resultant string is : %s",res); getch();

} 12) /* Write a program to replace a pattern in a string. Take input for string, pattern and replacement. (suppose string is "palindrome", pattern is "indro" and replacement is "abc" then resultant string will be "palabcme"). */ #include<stdio.h> #include<conio.h> #include<string.h>

void main() { char str[15],s1[15],s2[15],*ptr,*dum; int pos,i,j; clrscr(); printf("Enter the string : "); gets(str); strcpy(dum,str); again: printf("Enter the pattern : "); gets(s1); printf("Enter the replacement : "); gets(s2); ptr = strstr(str,s1); pos = strlen(str)-strlen(ptr); if(ptr) { for(i=pos,j=0;j<=strlen(s2);i++,j++) str[i] = s2[j]; i--; for(j=pos+strlen(s1);dum[j]!='\0';j++,i++) str[i] = dum[j]; str[i] = '\0'; } else { printf("\n\t\t\t\tNOT FOUND !!"); printf("\n\tAgain "); goto again; } printf("\n\nThe result is : %s",str); getch(); } 13) // Take a array of n strings. Scan n strings. Sort this array. #include<stdio.h> #include<conio.h> #include<string.h> void main() { char str[5][20],dummy[20]; int i,j; clrscr();

printf("Enter 5 string : "); for(i=0;i<5;i++) gets(str[i]); for(i=0;i<5;i++) for(j=i+1;j<5;j++) { if(strcmp(str[i],str[j]) > 0 ) { strcpy(dummy,str[i]); strcpy(str[i],str[j]); strcpy(str[j],dummy); } } printf("\n\nAfter sorting : \n"); for(i=0;i<5;i++) printf("\n%s",str[i]); } Practical 10 1)// Write a function line() to print a line (80 hyphens (-)). #include<stdio.h> #include<conio.h> void line() { int i; for(i=0;i<80;i++) printf("-"); } void main() { clrscr(); line(); printf("\nhello world\n"); line(); getch(); } 2) /* Modify the above function as lineprint() that will take two argument,

a character and an integer. The character should be printed the no of times specified by the integer value. */ #include<stdio.h> #include<conio.h> void lineprint(char ch, int i) { int j; for(j=0;j<i;j++) printf("%c",ch); } void main() { char ch; int i; clrscr(); printf("Enter the char : "); scanf("%c",&ch); printf("Enter the occurance of char : "); scanf("%d",&i); lineprint(ch,i); printf("\nhello world\n"); lineprint(ch,i); getch(); } 3) /* Write a fuction multiply() which recieves a float and an integer from main(), finds the product of these two and returns the product. Print this product through main(). */ #include<stdio.h> #include<conio.h> float multiply(float f, int i) { return(f*i); } void main() { int i; float f,answer;

clrscr(); printf("Enter the value of integer : "); scanf("%d",&i); printf("Enter the value of float : "); scanf("%f",&f); answer = multiply(f,i); printf("\n\nThe multiplication = %f",answer); getch(); } 4) /* Write a function isprime() which should return 1 if the number passed as a parameter is prime otherwise 0. */ #include<stdio.h> #include<conio.h> int isprime(int no) { int i,flag=1; for(i=2;i<no;i++) { if(no%i==0) { flag=0; break; } } return(flag); } void main() { int no,res; clrscr(); printf("Enter the no : "); scanf("%d",&no); res = isprime(no); if(res) printf("\n\nThe no is prime"); else printf("\n\nNot prime"); getch(); } 5) /* Write a function oddeven() to check that the number passed as a

parameter is odd or even. */ #include<stdio.h> #include<conio.h> int oddeven(int j) { if(j%2==0) return(0); else return(1); } void main() { int no,result; clrscr(); printf("Enter the no : "); scanf("%d",&no); result = oddeven(no); if(result) printf("\nEntered no is ODD"); else printf("\nEntered no is EVEN"); getch(); } 6) // Write a function sortarray() to sort an array passed as an argument. #include<stdio.h> #include<conio.h> void sortarray(int a[]) { int i,j,temp; for(i=0;i<5;i++) for(j=i+1;j<5;j++) if(a[i]>a[j]) { temp = a[i]; a[i] = a[j]; a[j] = temp; } printf("\n\nAfter Sorting : \n"); for(i=0;i<5;i++) printf("%d\n",a[i]); } void main() {

int ar[5],i; clrscr(); printf("Enter 5 values : "); for(i=0;i<5;i++) scanf("%d",&ar[i]); sortarray(ar); getch(); } 7) /* Write a function xstrlen() which will return the length of a string (passed as an argument). */ #include<stdio.h> #include<conio.h> int xstrlen(char str[]) { int i; for(i=0;str[i]!='\0';i++); return(i); } void main() { char st[20]; int len; clrscr(); printf("\nEnter the string : "); gets(st); len = xstrlen(st); printf("\n\nThe length of string is getch(); } 8) /* Write a function xstrlwr() which will convert uppercase string into lowercase string (passed as an argument) and will print it. */ #include<stdio.h> #include<conio.h> void xstrlwr(char str[]) { int i; : %d",len);

for(i=0;str[i]!='\0';i++) str[i] = str[i] + 32; printf("\n\nThe converted string is : %s",str); } void main() { char st[30]; clrscr(); printf("\nEnter the string : "); gets(st); xstrlwr(st); getch(); } 9) // Write a function swap() to swap values of two variables. #include<stdio.h> #include<conio.h> void swap(int a, int b) { int temp; temp = a; a = b; b = temp; printf("\n\nAfter swapping : \n"); printf("\na = %d",a); printf("\n\nb = %d",b); } void main() { int a,b; clrscr(); printf("\nEnter a :"); scanf("%d",&a); printf("\nEnter b :"); scanf("%d",&b); swap(a,b); getch(); } 10) /* Write a function sumofn() to calculate the sum of 1 to n numbers using recursion. */

#include<stdio.h> #include<conio.h> int sumofn(int n) { int temp; temp = n; if(temp == 0) return(0); else temp = temp + sumofn(n-1); return(temp); } void main() { int no,ans; clrscr(); printf("Enter the no : "); scanf("%d",&no); ans = sumofn(no); printf("\n\nAnswer = %d",ans); getch(); } 11a) /* Write a function fact() to obtain the factorial of given number: (a) without recursion */ #include<stdio.h> #include<conio.h> long int fact(int no) { long int temp=1; while(no>0) { temp = temp * no; no--; } return(temp); } void main() { int no;

long int fac; clrscr(); printf("Enter the no : "); scanf("%d",&no); fac = fact(no); printf("\n\nThe ans is : %ld",fac); getch(); } 11b) /* Write a function fact() to obtain the factorial of given number: (a) with recursion */ #include<stdio.h> #include<conio.h> long int fact(int no) { long int temp=no; if(no==0) return(1); else { temp=temp*fact(no-1); } return(temp); } void main() { int no; long int fac; clrscr(); printf("Enter the no : "); scanf("%d",&no); fac = fact(no); printf("\n\nThe ans is : %ld",fac); getch(); } 12a) /* Take any +ve integer from keyboard. Write a function sumdigit() to calculate sum of digits: (a) without recursion #include<stdio.h> #include<conio.h>

*/

int sumdigit(int n) { int i,result=0; while(n>0) { i=n%10; result += i; n = n/10; } return(result); } void main() { int no,ans; clrscr(); printf("Enter the no : "); scanf("%d",&no); ans = sumdigit(no); printf("\nThe ans is : %d",ans); getch(); } 12b) /* Take any +ve integer from keyboard. Write a function sumdigit() to calculate sum of digits: (b) with recursion */ #include<stdio.h> #include<conio.h> int sumdigit(int n) { int i,result; if(n<0) return(0); else result = (n%10) + sumdigit(n/10); return(result); } void main() { int no,ans; clrscr(); printf("Enter the no : "); scanf("%d",&no);

ans = sumdigit(no); printf("\nThe ans is : %d",ans); getch(); } 13) /* Write a function sum() to calculate the sum of the series given below using recursion. e.g. for n = 4 => s = (1) + (1 + 2) + (1 + 2 + 3) + (1 + 2 + 3 + 4). */ #include<stdio.h> #include<conio.h> //int total=0,total1=0; int sum(int no) { int res=no,i; if(no==0) return(0); else { int m=0; for(i=1;i<no;i++) m+=i; res = res + m+sum(no-1); } return(res); } void print(int n) { int i; printf("("); for(i=1;i<n;i++) printf("%d + ",i); printf("%d)",i); } void main() { int i,no,res; clrscr(); printf("Enter the no :"); scanf("%d",&no); res = sum(no);

// printf("\n\nThe result is : %d",res); printf("\n\n"); for(i=1;i<no;i++) { print(i); printf(" + "); } print(i); printf(" = %d",res); getch(); }

Practical-11 2) /* Write a program using structure to read following data of 3 students: [a] Roll no. [b] Student name [c] Marks in subject-1 [d] Marks in subject-2 Compute and print total marks and percentage of all students. */ #include<stdio.h> #include<conio.h> struct student { int rn,sub1,sub2,total; float per; char name[20]; }; void main() { struct student st[3]; int i; clrscr(); for(i=0;i<3;i++) { printf("\nEnter the info of student %d",i+1); printf("\nEnter name : "); scanf("%s",st[i].name); printf("\nEnter Roll no : "); scanf("%d",&st[i].rn); printf("\nEnter marks of sub1 & sub2 : "); scanf("%d%d",&st[i].sub1,&st[i].sub2); st[i].total = st[i].sub1 + st[i].sub2; st[i].per = st[i].total / 2; } for(i=0;i<3;i++) { printf("\n\n\tThe info of student %d",i+1); printf("\nName : %s",st[i].name); printf("\nRoll no :%d",st[i].rn); printf("\nsub1 = %d\nsub2 = %d",st[i].sub1,st[i].sub2);

printf("\nTotal marks : %d",st[i].total); printf("\nPercentage : %f",st[i].per); } getch(); } 3) /* Write a structure called Game that will describe the following information: [a] player name [b] team name [c] maximum score Using Game, declare an array Player with 3 elements and write a program to read the information about all the 3 players and print a team-wise containing names of players with their maximum scores.*/ #include<stdio.h> #include<conio.h> struct game { char pl_name[3][20]; int max_score[3]; char team[20]; }info[3]; void main() { clrscr(); int i,j; for(i=1;i<=3;i++) { printf("\n Enter the name of team %d:",i); scanf("%s",info[i].team); for(j=1;j<=3;j++) { printf("\n Enter the name of player %d : ",j);

scanf("%s",info[i].pl_name[j]); printf("\n Enter maximum score of this player : "); scanf("%d",info[i].max_score[j]); } } for(i=1;i<=3;i++) { printf("\nTeam %d : %s",i,info[i].team); for(j=1;j<=3;j++) { printf("\n\tPlayer Name : %s",info[i].pl_name[j]); printf("\n\tMax Score : %d",info[i].max_score[j]); } } getch(); } 4) /* There are 3 students in the class. The student's records as shown below: Roll no Marks of sub-1 Marks of sub-2 Marks of sub-3 [integer] [integer] [integer] [integer]

Student fails if he gets less than 50 marks in any subject. [maximum marks are 100] Passing class is given below: First class :marks >=70% Second class :marks < 70% and >=55% Pass class : marks < 55% and >=50% Print the result of each student with heading

Roll No. Marks2 Marks3 Also print....

Marks1

* Total number of students in each class * Total number of student failing * Number of student failing in more than one subject * Print the report on sorted order of student name #include<stdio.h> #include<conio.h> struct student { int rn,sub1,sub2,sub3; }; void main() { student stud[3]; int i,m=0,failed=0,f=0; clrscr(); for(i=0;i<3;i++) { printf("\nEnter the info of student %d",i+1); printf("\nEnter Roll-no : "); scanf("%d",&stud[i].rn); printf("\nEnter the marks of sub1, sub2 & sub3 : "); scanf("%d%d %d",&stud[i].sub1,&stud[i].sub2,&stud[i].sub3); if(stud[i].sub1 < 50 || stud[i].sub2 < 50 || stud[i].sub3 < 50) failed++; if((stud[i].sub1 < 50 && stud[i].sub2 < 50) || (stud[i].sub2 < 50 && stud[i].sub3 <50) || (stud[i].sub3 <50 && stud[i].sub1 <50)) f++; m++; } */

printf("\nRoll No. Marks1 Marks2 Marks3\n"); for(i=0;i<3;i++) printf("\n%d\t\t %d %d %d",stud[i].rn,stud[i].sub1,stud[i].sub2,stud[i].sub3); printf("\n\nTotal no of students : %d",m); printf("\nTotal no of failed students : %d",failed); printf("\nNo of students failed in more than one sub : %d",f); getch(); } 5) /* Write a program to enter and display the employee data using nested structure. Take three structures department , address, employee . Take department and address as a member of employee . */ #include<stdio.h> #include<conio.h> struct employee { char name[20]; struct dept { char dpt[20]; }dept; struct address { char street[30]; char city[20]; char state[15]; }addr; }; void main() { struct employee emp[3]; int i; clrscr(); for(i=0;i<3;i++) { printf("\nEnter the info of emp-%d",i+1); printf("\n\nEnter the name : "); scanf("%s",emp[i].name); printf("\nEnter the dept : "); scanf("%s",emp[i].dept.dpt); printf("\nEnter the address : ");

printf("\nStreet : "); scanf("%s",emp[i].addr.street); printf("\nCity : "); scanf("%s",emp[i].addr.city); printf("\nState : "); scanf("%s",emp[i].addr.state); } printf("\n\n\t\tINFORMATION OF EMPLOYEE\n"); for(i=0;i<3;i++) { printf("\nInfo of emp-%d",i+1); printf("\nName : %s",emp[i].name); printf("\nDept : %s",emp[i].dept.dpt); printf("\nAddress : %s",emp[i].addr.street); printf("\n %s",emp[i].addr.city); printf("\n %s",emp[i].addr.state); } getch(); } 6) /* Write a program to display the contents of a structure passing the individual elements to a function. */ #include<stdio.h> #include<conio.h> struct student { char name[20]; char addr[30]; }; void print(char ch[]) { printf("\n%s",ch); } void main() { student std; clrscr(); printf("\nEnter the name : "); gets(std.name); printf("\nEnter the address : "); gets(std.addr); printf("\n\n"); print(std.name);

print(std.addr); getch(); } 7) /* Create a structure stores ( name , price, quantity ).Make a fuction that modify price & quantity and return the modified values . Make another function that calculates total amount (price * quantity) and return it . */ #include<stdio.h> #include<conio.h> struct stores { char name[15]; float price; int qty; }; typedef struct stores str; str modify(str s1) { int q; float p; printf("\nEnter new modified quantity : "); scanf("%d",&s1.qty); printf("\nEnter new modified price : "); scanf("%f",&s1.price); return(s1); } float amount(float p,int q) { float amt; amt=p*q; return(amt); } void main() { str st1; float am; clrscr(); printf("\nEnter prod name : "); gets(st1.name); printf("\nEnter the qty : "); scanf("%d",&st1.qty);

printf("\nEnter the price : "); scanf("%f",&st1.price); printf("\n Old Product Detail :\n\n"); printf("\n\tProduct Name : %s",st1.name); printf("\n\tQuantity : %d",st1.qty); printf("\n\tPrice : %f",st1.price); st1=modify(st1); am=amount(st1.price,st1.qty); printf("\n New Detail for the same Product:\n\n"); printf("\n\tProduct Name : %s",st1.name); printf("\n\tQuantity : %d",st1.qty); printf("\n\tPrice : %f",st1.price); printf("\n\tTotal Amount : %f",am); getch(); }

Practical-12 1) /* Write a program using pointer to read in the array ofintegers and print its elements in reverse order*/ #include<stdio.h> #include<conio.h> void main() { int a[5]; int i; int *p; clrscr(); p=a; for(i=0;i<5;i++) { printf("enter elements"); scanf("%d",(p+i)); } printf("elements in reserve order\n"); for(i=4;i>=0;i--) { printf("%d\n",*(p+i)); } getch();

} 2) /* Write a program using pointer to find the length of the character string.*/ #include<stdio.h> #include<conio.h> void main() { char str[80]; char *p; int len=0; clrscr(); p=str; printf("enter any string\n"); scanf("%s",str); while(*p !='\0') { len++; p++; } printf("length of given string =%d",len); getch(); } 3) /* Explain the difference between "pass by value " & "pass by address" with a example.*/ #include<stdio.h> #include<conio.h> int passbyvalue(int); int passbyref(int *) ; void main() { int a,b; clrscr(); a=10; b=20; printf("\n before \n"); printf("\n a=%d",a); printf("\n b=%d",b); printf("\n after"); passbyval(a); passbyref(&b); printf("\n a=%d",a); printf("\n b=%d",b); getch(); }

int passbyval(int x) { x=x+10; } int passbyref(int *y) { *y=*y+10; }

Você também pode gostar