Top Banner
1. //Wap to print simple message. #include<stdio.h> #include<conio.h> void main() { printf(" Get Well Soon"); clrscr(); getch(); } Output:- 1
94

Practical File of C Language

Apr 14, 2017

Download

Education

RAJWANT KAUR
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Practical File of C Language

1. //Wap to print simple message.#include<stdio.h>

#include<conio.h>

void main()

{

printf(" Get Well Soon");

clrscr();

getch();

}

Output:-

1

Page 2: Practical File of C Language

2. //Wap to add two numbers .#include<stdio.h>

#include<conio.h>

void main()

{

int a,b,c;

clrscr();

printf("enter the values of a and b");

scanf("%d%d",&a,&b);

c=a+b;

printf("sum=%d",c);

getch();

}

Output:-

2

Page 3: Practical File of C Language

3. //Wap to calculate average of 5 numbers.#include<stdio.h>

#include<conio.h>

void main()

{

int a,b,c,d,e,avg;

clrscr();

printf("enter the values of a, b, c, d, and e");

scanf("%d%d%d%d%d",&a,&b,&c,&d,&e);

avg=(a+b+c+d+e)/5;

printf("avg=%d",avg);

getch();

}

Output:-

3

Page 4: Practical File of C Language

4. //Wap to multiply three numbers#include<stdio.h>

#include<conio.h>

void main()

{

int a=5,b=8,c=6,d;

clrscr();

d=a*b*c;

printf("mul=%d",d);

getch();

}

Output:-

4

Page 5: Practical File of C Language

5. //Wap to print area of a circle.#include<stdio.h>

#include<conio.h>

void main()

{

int r;

float pie=3.14;

float area;

printf("Enter the value of r");

scanf("%d",&r);

area=(pie*r*r);

printf("Area=%f",area);

getch();

}

Output:-

5

Page 6: Practical File of C Language

6. //Wap to print the biggest of three numbers#include<stdio.h>

#include<conio.h>

void main()

{

int a,b,c;

clrscr();

printf("Enter the values of a,b,and c");

scanf("%d%d%d",&a,&b,&c);

if (a>b&&a>c)

{

printf(" A is Biggest");

}

if (b>a&&b>c)

{

printf("B is Biggest");

}

if (c>a&&c>b)

{

printf("C is Biggest");

}

getch();

}

6

Page 7: Practical File of C Language

Output:-

7

Page 8: Practical File of C Language

7. //Wap to check given number is even or odd.#include<stdio.h>

#include<conio.h>

void main()

{

int a;

clrscr();

printf("enter the value of a");

scanf("%d",&a);

if(a%2==0)

{

printf("Number is Even");

}

else

{

printf("Number is Odd");

}

getch();

}

Output:-

8

Page 9: Practical File of C Language

8. //Wap to check given number is positive or negative.#include<stdio.h>

#include<conio.h>

void main()

{

int a;

clrscr();

printf("enter the value of a");

scanf("%d",&a);

if (a>0)

{

printf("Number is Positive");

}

else

{

printf("Number is Negative");

}

getch();

}

9

Page 10: Practical File of C Language

9. //Wap to check given year is leap year or not?#include<stdio.h>

#include<conio.h>

void main()

{

int a;

printf("Enter tha value of a");

scanf("%d",&a);

if (a%4==0)

{

printf("This year is Leap Year");

}

else

{

printf("This is not Leap Year");

}

getch();

}

Output:-

10

Page 11: Practical File of C Language

10. //Wap to print the name of day according to user choice.#include<stdio.h>

#include<conio.h>

void main()

{

int day;

clrscr();

printf("Enter the value for day 1 to 7");

scanf("%d",&day);

if (day==1)

{

printf("\n Monday");

}

else if (day==2)

{

printf("\n Tuesday");

}

else if (day==3)

{

printf("\n Wednesday");

}

else if (day==4)

{

11

Page 12: Practical File of C Language

printf("\n Thursday");

}

else if (day==5)

{

printf("\n Friday");

}

else if (day==6)

{

printf("\n Saturday");

}

else if (day==7)

{

printf("\n Sunday");

}

else

{

printf("Please Enter Correct Value");

}

getch();

}

12

Page 13: Practical File of C Language

11. //Wap to print the name of month.#include<stdio.h>

#include<conio.h>

void main()

{

int month;

clrscr();

printf("Enter the value for month 1 to 12");

scanf("%d",&month);

switch (month)

{

case 1:

printf("January");

break;

case 2:

printf("Febuary");

break;

case 3:

printf("March");

break;

case 4:

printf("April");

break;

case 5:

13

Page 14: Practical File of C Language

printf("May");

break;

case 6:

printf("June");

break;

case 7:

printf("July");

break;

case 8:

printf("August");

break;

case 9:

printf("September");

break;

case 10:

printf("October");

break;

case 11:

printf("November");

break;

case 12:

printf("December");

break;

default:

14

Page 15: Practical File of C Language

printf("Please Enter Correct Value");

break;

}

getch();

}

Output:-

12. //Wap to print 1 to 10 no by while loop15

Page 16: Practical File of C Language

#include<stdio.h>

#include<conio.h>

void main()

{

int i=1;

clrscr();

while(i<=10)

{

printf("\n %d",i);

i++;

}

getch();

}

Output:-

13. //Wap to print 1 to 10 Numbers by using do while.

16

Page 17: Practical File of C Language

#include<stdio.h>

#include<conio.h>

void main()

{

int i=1;

clrscr();

do

{

printf("\n %d",i);

i++;

}while(i<=10);

getch();

}

Output:-

14. //Wap to print 1 to 10 by using for loop17

Page 18: Practical File of C Language

#include<stdio.h>

#include<conio.h>

void main()

{

int i;

clrscr();

for(i=1;i<=10;i++)

{

printf("\n %d",i);

}

getch();

}

Output:-

15. //Wap to calculate factorial of a given number.

18

Page 19: Practical File of C Language

#include<stdio.h>

#include<conio.h>

void main()

{

int n,i,fact=1;

clrscr();

printf("Enter the value of n");

scanf("%d",&n);

for(i=n;i>=1;i--)

{

fact=fact*i;

}

printf("Factorial is=%d",fact);

getch();

}

Output:-

16. //Wap to print the fibonacci series.19

Page 20: Practical File of C Language

#include<stdio.h>

#include<conio.h>

void main()

{

int n,i,a=0,b=1,c;

clrscr();

printf("Enter the term of fibonacci series");

scanf("%d",&n);

printf("%d%d",a,b);

for(i=1;i<n;i++)

{

c=a+b;

printf("%d",c);

a=b;

b=c;

}

getch();

}

Output:-

17. //Wap to print the table of given no.

20

Page 21: Practical File of C Language

#include<stdio.h>

#include<conio.h>

void main()

{

int n,i;

clrscr();

printf("Enter a Number");

scanf("%d",&n);

for(i=1;i<=10;i++)

{

printf("\n %d*%d=%d",n,i,n*i);

}

getch();

}

Output:-

18. //Wap to print reverse of a number.21

Page 22: Practical File of C Language

#include<stdio.h>

#include<conio.h>

void main()

{

int num,n,rev=0,rem;

clrscr();

printf("Enter the Number");

scanf("%d",&num);

n=num;

while(n!=0)

{

rem=n%10;

n=n/10;

rev=rev*(10+rem);

}

printf("Old Number=%d",num);

printf("Reversed NO=%d",rev);

getch();

}

Output:-

19. //Wap to print given number is armstrong or not?22

Page 23: Practical File of C Language

#include<stdio.h>

#include<conio.h>

void main()

{

int num,n,rem,sum=0;

clrscr();

printf("Enter a Number");

scanf("%d",&num);

n=num;

while(n!=0)

{

rem=n%10;

sum=sum+(rem*rem*rem);

n=n/10;

}

if (sum==num)

{

printf("\n Number is Armstrong");

}

else

{

printf("\n Number is not Armstrong");

}

getch();

23

Page 24: Practical File of C Language

}

Output:-

20. //Wap to print odd numbers between 1 to 50.24

Page 25: Practical File of C Language

#include<stdio.h>

#include<conio.h>

void main()

{

int i=1;

while(i<=50)

{

if(i%2!=0)

printf("\n %d",i);

i++;

}

getch();

}

Output:-

21. //Wap to check given number is palindrome or not?25

Page 26: Practical File of C Language

#include<stdio.h>

#include<conio.h>

void main()

{

int n,rem,rev=0,num;

clrscr();

printf("Enter a Number");

scanf("%d",&n);

num=n;

while(n!=0)

{

rem=n%10;

n=n/10;

rev=((rev*10)+rem);

}

if(num==rev)

{

printf("Entered Number is palindrome");

}

else

{

printf("Entered Number is not Palindrome");

}

getch();

26

Page 27: Practical File of C Language

}

Output:-

27

Page 28: Practical File of C Language

22. //Wap to add two numbers using Functions.#include<stdio.h>

#include<conio.h>

int sum(int,int);

void main()

{

int a,b,c;

printf("enter the values of a and b");

scanf("%d%d",&a,&b);

c=sum(a,b);

printf("sum=%d",c);

getch();

}

int sum(int x,int y)

{

int z;

z=x+y;

return z;

}

Output:-

28

Page 29: Practical File of C Language

23. //Wap to calculate the factorial of given number.#include<stdio.h>

#include<conio.h>

void main()

{

int n,result;

clrscr();

printf("Enter the value of n");

scanf("%d",&n);

result=fact(n);

printf("factorial=%d",result);

getch();

}

int fact(int x)

{

if (x==0)

{

return(1);

}

else

{

return(x*fact(x-1));

}

}

29

Page 30: Practical File of C Language

Output:-

30

Page 31: Practical File of C Language

24. //Wap to print the fibonacci series.#include<stdio.h>

#include<conio.h>

int fib(int m);

void main()

{

int n,i;

clrscr();

printf("Enter the term of fibonacci series");

scanf("%d",&n);

printf("\n0");

for(i=1;i<=n;i++)

printf("\n &d",fib(i));

getch();

}

int fib(int m)

{

static int a=0,b=1;

int c;

return(0);

if(m==1)

return(1);

else

{

31

Page 32: Practical File of C Language

c=a+b;

a=b;

b=c;

return(c);

}

Output:-

32

Page 33: Practical File of C Language

25. //Wap to calculate simple interest.#include<stdio.h>

#include<conio.h>

void main()

{

int p,t,r;

int splint;

clrscr();

printf("Enter the value of p r and t");

scanf("%d%d%d",&p,&r,&t);

splint=(p*r*t/100);

printf("Splint=%d",splint);

getch();

}

Output:-

33

Page 34: Practical File of C Language

26. //Wap to show concept of function no argument and with return.#include<stdio.h>

#include<conio.h>

int mul();

void main()

{

int result;

result =mul();

printf("Multiplication=%d",result);

getch();

}

int mul()

{

int a,b,c;

printf("Enter the value of a and b");

scanf("%d%d",&a,&b);

c=a*b;

return c;

}

34

Page 35: Practical File of C Language

27. //Wap to show the concept of function with argument and NO return.#include<stdio.h>

#include<conio.h>

void sub(int,int);

void main()

{

int a,b;

clrscr();

printf("Enter the value of a and b");

scanf("%d%d",&a,&b);

sub(a,b);

getch();

}

void sub(int x,int y)

{

int z; z=x-y;

printf("Result=%d",z);

}

Output:-

35

Page 36: Practical File of C Language

28.//Wap to show concept of function with no argument no return#include<stdio.h>

#include<conio.h>

void display();

void programinfo();

void main()

{

clrscr();

display();

programinfo();

display();

getch();

}

void display()

{

int i;

for(i=1;i<=20;i++)

printf("*");

printf("\n");

}

void programinfo()

{

printf(" No Argument NO Return \n");

36

Page 37: Practical File of C Language

}

Output:-

37

Page 38: Practical File of C Language

29. //Wap to swap two numbers using call by value.#include<stdio.h>

#include<conio.h>

void swap(int,int);

void main()

{

int a,b;

clrscr();

printf("Enter the value of a and b");

scanf("%d%d",&a,&b);

swap(a,b);

getch();

}

void swap(int x,int y)

{

int z;

z=x; x=y; y=z;

printf("%d%d",x,y);

}

Output:-

38

Page 39: Practical File of C Language

30. //Wap to swap two no by call by reference.#inclu30.de<stdio.h>

#include<conio.h>

void swap(int *,int *);

void main()

{

int a,b;

clrscr();

printf("Enter the value of a and b");

scanf("%d%d",&a,&b);

swap(&a,&b);

getch();

}

void swap(int *x, int *y)

{

int z; z=*x; *x=*y; *y=z;

printf("%d %d",*x,*y);

}

Output:-

39

Page 40: Practical File of C Language

31. //Wap to print input elements using One-D array.#include<stdio.h>

#include<conio.h>

void main()

{

int a[20],n,i;

printf("Enter Array Size ");

scanf("%d",&n);

for(i=0;i<n;i++)

{

printf("Enter the value of Elements %d",i);

scanf("%d",&a[i]);

}

printf("Array Elemnets are");

for(i=0;i<n;i++)

{

printf("\n %d",a[i]);

getch();

}

Output:-

40

Page 41: Practical File of C Language

32. //Wap to find the maximum value from array.#include<stdio.h>

#include<conio.h>

void main()

{

int a[50],i,n,max;

printf("Enter the array size");

scanf("%d",&n);

for(i=0;i<n;i++)

{

printf("\n Enter the values of elements %d",i);

scanf("%d",&a[i]);

}

max=a[50];

for(i=0;i<n;i++)

{

if(max<a[i])

{

max=a[i];

}

}

printf("Maximum value=%d",max);

getch()

}

41

Page 42: Practical File of C Language

Output:-

42

Page 43: Practical File of C Language

33. //Wap to find the minimum value from array.#include<stdio.h>

#include<conio.h>

void main()

{

int a[50],i,n,min;

printf("Enter the array size");

scanf("%d",&n);

for(i=0;i<n;i++)

{

printf("\n Enter the values of elements %d",i);

scanf("%d",&a[i]);

}

min=a[0];

for(i=0;i<n;i++)

{

if(min>a[i])

{

min=a[i];

}

}

printf("Minimum value=%d",min);

getch();

}

43

Page 44: Practical File of C Language

Output:-

44

Page 45: Practical File of C Language

34. //Wap to print the sum of all array elements.#include<stdio.h>

#include<conio.h>

void main()

{

int s[50],i,n,sum=0;

clrscr();

printf("Enter the array size");

scanf("%d",&n);

for(i=0;i<n;i++)

{

printf("\n Enter sum of student %d",i);

scanf("%d",&s[i]);

sum=sum+s[i];

}

{

printf("Sum of Student=%d",sum);

}

Output:-

45

Page 46: Practical File of C Language

35. //Wap to print input elements using Two-D array.#include<stdio.h>

#include<conio.h>

void main()

{

int a[5][5],j,i,r,c;

clrscr();

printf("Enter row and column size");

scanf("%d%d",&r,&c);

for(i=0;i<r;i++)

{

for(j=0;j<c;j++)

{

printf("Enter the value of Elements %d%d:",i,j);

scanf("%d",&a[i][j]);

}

}

printf("Array Elemnets are");

for(i=0;i<r;i++)

{

for(j=0;j<c;j++)

{

printf("\n %d",a[i][j]);

}

46

Page 47: Practical File of C Language

printf("\n");

}

getch();

}

Output:-

47

Page 48: Practical File of C Language

36. //Wap to add two matrices.#include<stdio.h>

#include<conio.h>

void main()

{

int a[5][5],b[5][5],c[5][5];

int i,j,r,col;

clrscr();

printf("Enter the row and column size");

scanf("%d%d",&r,&col);

printf("\n Enter the values of first matrix");

for(i=0;i<r;i++)

{

for(j=0;j<col;j++)

{

printf("\n Enter the values of elements %d%d",i,j);

scanf("%d",&a[i][j]);

}

}

printf("\n Enter the values of second matrix");

for(i=0;i<r;i++)

{

for(j=0;j<col;j++)

48

Page 49: Practical File of C Language

{

printf("\n Enter the values of elements %d%d",i,j);

scanf("%d",&b[i][j]);

}

}

for(i=0;i<r;i++)

{

for(j=0;j<col;j++)

{

c[i][j]=0;

c[i][j]=a[i][j]+b[i][j];

}

}

printf("\n After Addition \n");

for(i=0;i<r;i++)

{

for(j=0;j<col;j++)

{

printf("%d\t",c[i][j]);

}

printf("\n");

}

getch();

}

49

Page 50: Practical File of C Language

Output:-

50

Page 51: Practical File of C Language

37. //Wap to multiply two matrices.#include<stdio.h>

#include<conio.h>

void main()

{

int a[5][5],b[5][5],c[5][5];

int i,j,r,col;

clrscr();

printf("Enter the row and column size");

scanf("%d%d",&r,&col);

printf("\n Enter the values of first matrix");

for(i=0;i<r;i++)

{

for(j=0;j<col;j++)

{

printf("\n Enter the values of elements %d%d",i,j);

scanf("%d",&a[i][j]);

}

}

printf("\n Enter the values of second matrix");

for(i=0;i<r;i++)

{

for(j=0;j<col;j++)

{

51

Page 52: Practical File of C Language

printf("\n Enter the values of elements %d%d",i,j);

scanf("%d",&b[i][j]);

}

}

for(i=0;i<r;i++)

{

for(j=0;j<col;j++)

{

c[i][j]=0;

c[i][j]=a[i][j]*b[i][j];

}

}

printf("\n After Multiplication \n");

for(i=0;i<r;i++)

{

for(j=0;j<col;j++)

{

printf("%d\t",c[i][j]);

}

printf("\n\n");

}

getch();

}

52

Page 53: Practical File of C Language

Output:-

53

Page 54: Practical File of C Language

38. //Wap to subtract two matrices.#include<stdio.h>

#include<conio.h>

void main()

{

int a[5][5],b[5][5],c[5][5];

int i,j,r,col;

clrscr();

printf("Enter the row and column size");

scanf("%d%d",&r,&col);

printf("\n Enter the values of first matrix");

for(i=0;i<r;i++)

{

for(j=0;j<col;j++)

{

printf("\n Enter the values of elements %d%d",i,j);

scanf("%d",&a[i][j]);

}

}

printf("\n Enter the values of second matrix");

for(i=0;i<r;i++)

{

for(j=0;j<col;j++)

{

54

Page 55: Practical File of C Language

printf("\n Enter the values of elements %d%d",i,j);

scanf("%d",&b[i][j]);

}

}

for(i=0;i<r;i++)

{

for(j=0;j<col;j++)

{

c[i][j]=0;

c[i][j]=a[i][j]-b[i][j];

}

}

printf("\n After Subtraction \n");

for(i=0;i<r;i++)

{

for(j=0;j<col;j++)

{

printf("%d\t",c[i][j]);

}

printf("\n");

}

getch();

}

55

Page 56: Practical File of C Language

Output:-

56

Page 57: Practical File of C Language

39. //Wap to Transpose a matrix.#include<stdio.h>

#include<conio.h>

void main()

{

int a[5][5],i,j,r,c;

clrscr();

printf("Enter the row and column size");

scanf("%d%d",&r,&c);

printf("\n Enter the values of first matrix");

for(i=0;i<r;i++)

{

for(j=0;j<c;j++)

{

printf("\n Enter the values of elements %d%d",i,j);

scanf("%d",&a[i][j]);

}

}

printf("\n After Transpose \n");

for(i=0;i<r;i++)

{

for(j=0;j<c;j++)

{

printf("%d\t",a[i][j]);

57

Page 58: Practical File of C Language

}

printf("\n");

}

getch();

}

Output:-

58

Page 59: Practical File of C Language

40. //Wap to print diagonal elements of a matrix.#include<stdio.h>

#include<conio.h>

void main()

{

int a[5][5],i,j,r,c;

clrscr();

printf("Enter the row and column size");

scanf("%d%d",&r,&c);

for(i=0;i<r;i++)

{

for(j=0;j<c;j++)

{

printf("\n Enter the values of elements %d%d",i,j);

scanf("%d",&a[i][j]);

}

}

printf("\n Diagonal Elements are \n");

for(i=0;i<j;i++)

{

for(j=0;j<c;j++)

{

if(i==j)

{

59

Page 60: Practical File of C Language

printf("%d\t",a[i][j]);

}

}

}

getch();

}

Output:-

60

Page 61: Practical File of C Language

41. //Wap to print sum of diagonal elements of a matrix.#include<stdio.h>

#include<conio.h>

void main()

{

int a[5][5],i,j,r,c,sum=0;

clrscr();

printf("Enter the row and column size");

scanf("%d%d",&r,&c);

for(i=0;i<r;i++)

{

for(j=0;j<c;j++)

{

printf("\n Enter the values of elements %d%d",i,j);

scanf("%d",&a[i][j]);

}

}

for(i=0;i<r;i++)

{

for(j=0;j<c;j++)

{

if(i==j)

{

61

Page 62: Practical File of C Language

sum+=a[i][j];

}

}

printf("\n Sum of Diagonal Elements are=%d",sum);

getch();

}

62

Page 63: Practical File of C Language

42. //Wap to calculate % in four subjects And show rollno of students.#include<stdio.h>

#include<conio.h>

struct student

{

int rollno;

float sub1,sub2,sub3,sub4,per;

};

void main()

{

struct student s1;

clrscr();

printf("Enter the info for students");

printf("\n Enter the roll no for student:");

scanf("%d",&s1.rollno);

printf("Enter the marks for sub1,sub2,sub3,sub4");

scanf("%f%f%f%f",&s1.sub1,&s1.sub2,&s1.sub3,&s1.sub4);

s1.per=(s1.sub1+s1.sub2+s1.sub3+s1.sub4)/400*100;

printf("Info for student");

printf("\n Roll no of Student=%d",s1.rollno);

printf("\n Percentage of student=%f",s1.per);

getch();

63

Page 64: Practical File of C Language

}

Output:-

64

Page 65: Practical File of C Language

43. //Wap to print record of a school student.#include<stdio.h>

#include<conio.h>

struct student

{

int rollno;

int age;

char name;

float marks,sub1,sub2,per;

};

void main()

{

struct student s1;

clrscr();

printf("Enter the info for students");

printf("\n Enter the Name of student:");

scanf("%s",&s1.name);

printf("\n Enter the roll no for student:");

scanf("%d",&s1.rollno);

printf("Enter the Age of student:");

scanf("%d",&s1.age);

printf("Enter the marks for sub1,sub2");

scanf("%f%f",&s1.sub1,&s1.sub2);

65

Page 66: Practical File of C Language

s1.per=(s1.sub1+s1.sub2)/200*100;

printf("Info for student");

printf("\n Name of student %s",s1.name);

printf("\n Roll no of Student=%d",s1.rollno);

printf("\n Age of student:%d",s1.age);

printf("\n Marks of sub1 and sub2 of student:%f %f",s1.sub1,s1.sub2);

printf("\n Percentage of student=%f",s1.per);

getch();

}

Output:-

66

Page 67: Practical File of C Language

44. //Wap to print record of students using union.#include<stdio.h>

#include<conio.h>

union student

{

int rollno;

char name[20];

int age;

float marks; } s;

void main() {

printf("Enter RollNo of Student :");

scanf("%d",&s.rollno);

printf("Enter the age of student :");

scanf("%d",&s.age);

printf("Enter the name of student:");

scanf("%s",&s.name);

printf("Enter the marks of student :");

scanf("%f",&s.marks);

printf("\n RollNo=%d",s.rollno);

printf("\n age=%d",s.age);

printf("\n Name=%s",s.name);

printf("\n marks=%f",s.marks);

getch();

}

67

Page 68: Practical File of C Language

45. //Wap to print address & value of variable and address of pointer variable.#include<stdio.h>

#include<conio.h>

void main()

{

int * ptr;

int a=10;

clrscr();

ptr=&a;

printf("\n %d",a);

scanf("%ld",&ptr);

printf("%ld",*ptr);

printf("\n %ld",*ptr);

printf("\n %ld",&a);

getch();

}

68

Page 69: Practical File of C Language

Output:-

46. //Wap to show the concept of pointer to pointer.#include<stdio.h>

#include<conio.h>

void main()

{

int ** ptr;

int * p;

int a=10;

clrscr();

p=&a;

ptr=&p;

printf("\n Value of a=%d",a);

printf("\n Address of p=%ld",&p);

printf("\n Value of p=%ld",p);

printf("\n Content of a through p=%d",*p);

printf("\n Address of p through ptr=%ld",ptr);

69

Page 70: Practical File of C Language

printf("\n Content of a through ptr=%d",**ptr);

getch();

}

47. //Wap to show use of auto variable.#include<stdio.h>

#include<conio.h>

void func1();

void main()

{

func1();

func1();

}

void func1()

{

auto int i=2;

printf("%d\n",i);

i=i+2;

getch();

}

70

Page 71: Practical File of C Language

Output:-

48. //Wap to show use of static variable.#include<stdio.h>

#include<conio.h>

int func1();

int func2();

main()

{

static int i=1;

printf("%d",i);

printf("\n%d",func1());

printf("\n%d",func2()); }

int func1() {

static int i;

i=i+2;

return(i); }

int func2()

71

Page 72: Practical File of C Language

{

static int i;

i=i+3;

return(i);

getch(); }

Output:-

49. //Wap to show use of external variable.#include<stdio.h>

#include<conio.h>

int i=1;

int func1();

int func2();

void main()

{

printf("%d",i);

printf("\n%d",func1());

printf("\n%d",func2());

}

int func1()

{

i=i+10;

return(i);

72

Page 73: Practical File of C Language

}

int func2()

{

i=i+20;

return(i);

getch();

}

Output:---

73

Page 74: Practical File of C Language

50. //Wap to enter a mixed data into a file.#include<stdio.h>

#include<conio.h>

void main()

{

FILE *fp;

int rollno;

char name[20];

float marks;

fp=fopen("stud.txt","w");

clrscr();

if(fp==NULL)

{

printf("Cannot open file");

}

74

Page 75: Practical File of C Language

printf("Enter student name,rollno,marks");

scanf("%s %d %f",name,&rollno,&marks);

fprintf(fp,"%s %d %f",name,rollno,marks);

fclose(fp);

getch();

}

Output:-

75

Page 76: Practical File of C Language

Output:-

51. //Wap to read a mixed data from a file.#include<stdio.h>

#include<conio.h>

void main()

{

FILE *fp;

int rollno;

char name[20];

float marks;

fp=fopen("stud.txt","r");

clrscr();

if(fp==NULL)

76

Page 77: Practical File of C Language

{

printf("Cannot open file");

}

while(fscanf(fp,"%s %d %f",name,&rollno,&marks)!=EOF)

{

printf("%s %d %f",name,rollno,marks); }

fclose(fp);

getch(); }

Output

52. //Wap to enter a string data into a file.#include<stdio.h>

#include<conio.h>

void main()

{

FILE*fp;

char s[80];

fp=fopen("xyz.txt","w");

if(fp==NULL)

{

printf("Cannot open file");

}

77

Page 78: Practical File of C Language

printf("Enter string data and press enter");

while((strlen(gets(s))>0))

{

fputs(s,fp);

fputs("\n",fp);

}

close(fp);

}

Output:-

Output:-

78

Page 79: Practical File of C Language

53. //Wap to read a string data from a file.#include<stdio.h>

#include<conio.h>

void main()

{

FILE*fp;

char s[80];

clrscr();

fp=fopen("xyz.txt","r");

if(fp==NULL)

{

79

Page 80: Practical File of C Language

printf("Cannot open file");

}

while(fgets(s,80,fp)!=NULL)

puts(s);

fclose(fp);

getch();

}

Output:-

54. //Wap to write integer into a file.#include<stdio.h>

#include<conio.h>

void main()

{

FILE *fp;

int n;

fp=fopen("raj.txt","w");

clrscr();

80

Page 81: Practical File of C Language

if(fp==NULL)

{

printf("Cannot open file");

}

printf("Enter any integar value");

scanf("%d",&n);

putw(n,fp);

fclose(fp);

getch();

}

Output:-

Output:-

81

Page 82: Practical File of C Language

55. //Wap to read integer into a file.#include<stdio.h>

#include<conio.h>

void main()

{

FILE *fp;

int n;

fp=fopen("raj.txt","w");

clrscr();

if(fp==NULL)

{

82

Page 83: Practical File of C Language

printf("Cannot open file");

}

while((n=getw(fp))!=EOF)

{

printf("%d",n);

}

fclose(fp);

getch(); }

Output:-

83