(XII) C for Lab Session

C Lab Work-1 C Lab Work-2
C Lab Work-3 C Lab Work-4
Lab works-1(Functions)
1. Write C program to find sum of two numbers using function named sum().
#include<stdio.h> int sum(int a, int b); main() { int a,b,c; printf("Enter two numbers:"); scanf("%d%d",&a,&b); c=sum(a,b); printf("Sum of two numbers = %d",c); } int sum(int a, int b) { int p; p=a+b; return (p); }
2. Write C program to find area and perimeter of a rectangle. Use function area_perimeter().
#include<stdio.h> void area_perimeter(int l, int b); int main() { int l,b; printf("Enter length and breadth :"); scanf("%d%d",&l,&b); area_perimeter(l,b); return 0; } void area_perimeter(int l, int b) { int area,perimeter; area=l*b; printf("Area of rectangle = %d",area); perimeter=2*(l+b); printf("\n Perimeter of rectangle = %d",perimeter); }
3. Write C program to know a number is even or odd using function named even_odd().
#include<stdio.h> void even_odd(int n); int main() { int n; printf("Enter a number :"); scanf("%d",&n); even_odd(n); return 0; } void even_odd(int n) { if(n%2==0) printf("Even"); else printf("Odd"); }
4. Write C program to print multiplication table of a number using function table().
#include<stdio.h> void table(int n); int main() { int n; printf("Enter a number :"); scanf("%d",&n); table(n); return 0; } void table(int n) { int i; for(i=1;i<=10;i++) { printf("%d\n",n*i); } }
5. Write C program to print the greatest value among three numbers using a function int great(). We have to use return statement.
#include<stdio.h> int great(int a, int, int c); int main() { int a,b,c,d; printf("Enter three numbers : "); scanf("%d%d%d",&a,&b,&c); d=great(a,b,c); printf("greatest number is %d",d); return 0; } int great(int a, int b, int c) { if(a>b && a>c) return a; else if (b>c) return b; else return c; }
6. Write C program to know a number is prime or not. Take the function name yourself.
#include<stdio.h> void test(int n); int main() { int n; printf("Enter a number :"); scanf("%d",&n); test(n); return 0; } void test(int n) { int i,c=0 ; for(i=1;i<=n;i++) { if(n%i==0) c++; } if(c==2) printf("Prime"); else printf("not prime or composite"); }
7. Write C program to find sum of series 1,2, 3,…..200 using function. Assume yourself function name. It returns a floating value.
#include<stdio.h> float sum(); int main() { printf("the sum is=%f",sum()); return 0; } float sum() { int s=0,i; for(i=1;i<=200;i++) { s=s+i; } return s; }
8. Write C program to input elements of an array and print them with their sum. Suppose, the array is one dimensional & is of void type and function to be used is array_elements().
#include<stdio.h> void array_elements(int a[]); int main() { int a[5],i,ans; for(i=0;i<5;i++) { printf("Enter num[%d] : ",i+1); scanf("%d",&a[i]); } array_elements(a); return 0; } void array_elements(int a[]) { int s=0,i; for(i=0;i<5;i++) { s=s+a[i]; } printf("Sum=%d",s); }
9. Suppose a function string_length(char st[]).Here, we have passed string as parameter. Use this function to find the length of string which must return the value.
#include<stdio.h> int string_length(char st[]); main() { char str[100]; printf(" Enter any string : "); scanf("%s",str); printf(" The length of string is %d",string_length(str)); } int string_length(char st[]) { int i; for(i=0;st[i]!='\0';i++); return (i); }
10. Suppose a function void matrix_sum(int a[][],int b[][]).Here, we have passed array as parameter. Use this function to find sum of matrices.
#include<stdio.h> void matrix_sum(int a[][3],int b[][3]); main() { int a[3][3],b[3][3],i,j; for(i=0;i<3;i++) { for(j=0;j<3;j++) { printf("Enter b[%d][%d] : ",i,j); scanf("%d",&a[i][j]); } } for(i=0;i<3;i++) { for(j=0;j<3;j++) { printf("Enter b[%d][%d] : ",i,j); scanf("%d",&b[i][j]); } } matrix_sum(a,b); } void matrix_sum(int a[][3],int b[][3]) { int c[3][3],i,j; printf("\n sum of two 3x3 matrices: \n"); for(i=0;i<3;i++) { for(j=0;j<3;j++) { c[i][j]=a[i][j]+b[i][j]; printf(" %d\t ",c[i][j]); } printf("\n"); } }
11. An array contains some numbers(integer type).WAP to sort numbers using a function void sort(a[]).We have to pass that array from main function.
#include<stdio.h> void sort(int num[],int n); main() { int num[100],n,i; printf(" Enter the array size not more than 100 : "); scanf("%d",&n); for(i=0;i<n;i++) { printf(" Enter num[%d] : ", i+1); scanf("%d",&num[i]); } sort(num,n); } void sort(int num[],int n) { int i,j,temp; for(i=0;i<n-1;i++) { for(j=i+1;j<n;j++) { if(num[i]>num[j]) { temp=num[i]; num[i]=num[j]; num[j]=temp; } } } printf(" Numbers in ascending order \n "); for(i=0;i<n;i++) printf(" %d\t ",num[i]); }
12. Write C program to sort 'n' number of strings using function. Pass strings as parameter.
#include<stdio.h> #include<string.h> void sort(char str[][20],int n); main() { char str[50][20]; int i,n; printf(" How many strings : "); scanf("%d",&n); for(i=0;i<n;i++) { printf(" Enter str[%d] : ",i+1); scanf("%s",str[i]); } sort(str,n); } void sort( char str[][20],int n) { int i,j; char temp[20]; for(i=0;i<n-1;i++) { for(j=i+1;j<n;j++) { if(strcmp(str[i],str[j])>0) { strcpy(temp,str[i]); strcpy(str[i],str[j]); strcpy(str[j],temp); } } } printf(" Sorted strings are : \n"); for(i=0;i<n;i++) printf(" %s\n ",str[i]); }
13. WAP to find factorial value of a number using recursive function.
#include<stdio.h> int fact(int); main() { int n,ans; printf( "Enter any number : "); scanf("%d",&n); ans=fact(n); printf(" Factorial of %d is %d ",n,ans); } int fact(int n) { if(n<=1) return (1); else return(n*fact(n-1)); }
14. WAP to print Fibonacci series 1, 1, 2, 3 ..... nth using recursive function.
#include<stdio.h> int fibo(int n); main() { int i,n; printf( " Enter any number : "); scanf("%d",&n); for(i=1;i<=n;i++) { printf(" %d\t ",fibo(i)); } } int fibo(int n) { if(n==0) return 0; else if(n==1) return 1; else return (fibo(n-1)+fibo(n-2)); }


Lab works-2(Structure and unions)
1. Write C program to input and print student id, name and grade. Use structure concept.
#include<stdio.h> struct student { int id; char name[30]; int grade; } s; int main() { printf("Enter student id,name and grade : "); scanf("%d%s%d",&s.id,s.name,&s.grade); printf("%d\n %s\n %d\n",s.id,s.name,s.grade); return 0; }
2. Write C program to input id, name and grade for 10 students. Then print them. Use array of structure concept.
#include<stdio.h> struct student { int id; char name[30]; int grade; } s[10]; int main() { int i; for(i=0;i<10;i++) { printf("Enter student id,name and grade : "); scanf("%d%s%d",&s[i].id,s[i].name,&s[i].grade); } printf("-------Output-------\n"); printf("ID\t Name\t Grade\n"); for(i=0;i<10;i++) { printf("%d\t %s\t %d\n",s[i].id,s[i].name,s[i].grade); } return 0; }
3. Write C program to input 20 employees name, post and salary. Then search a record of an employee on the basis of name.
#include<stdio.h> #include<string.h> struct employee { char name[30]; char post[30]; int salary; } e[20]; int main() { int i; char search_name[30]; for(i=0;i<20;i++) { printf("Enter employee name,post and salary : "); scanf("%s%s%d",e[i].name,e[i].post,&e[i].salary); } printf("Enter name to search: "); scanf("%s",search_name); for(i=0;i<20;i++) { if(strcmp(e[i].name,search_name)==0) printf("%s\t %s\t %d\n",e[i].name,e[i].post,e[i].salary); } return 0; }
4. We have two structures as given below. struct employee { char ename[100]; char eaddress[100]; }; struct salarydetails { char pos[100]; float salary; struct employee detail; }payinfo; Write C program to input employees’ details with salary then print them. Use nested struct concept.
#include<stdio.h> struct employee { char ename[100]; char eaddress[100]; }; struct salarydetails { char post[100]; float salary; struct employee detail; }payinfo; int main() { printf("Enter employee name,address,post and salary : "); scanf("%s%s%s%f",payinfo.detail.ename,payinfo.detail.eaddress,payinfo.post,&payinfo.salary); printf("Employee Name : %s\n",payinfo.detail.ename); printf("Address : %s\n",payinfo.detail.eaddress); printf("Post: %s\n",payinfo.post); printf("Salary: %f",payinfo.salary); return 0; }
5. Write C program to input id, name and address of 20 students using struct. Then print them in sorted format on the basis of name.
#include<stdio.h> #include<string.h> struct student { int id; char name[30]; char address[30]; }s[20]; int main() { struct student temp; int i,j; for(i=0;i<20;i++) { printf("Enter id, name and address: "); scanf("%d%s%s",&s[i].id,s[i].name,s[i].address); } for(i=0;i<19;i++) { for(j=i+1;j<20;j++) { if(strcmp(s[i].name,s[j].name)>0) { temp=s[i]; s[i]=s[j]; s[j]=temp; } } } printf("Records in Sorted order by Name \n"); for(i=0;i<20;i++) { printf("%d\t %s\t %s\n ",s[i].id,s[i].name,s[i].address); } return 0; }
6. Write C program to input teacher’s id, name, address and subject(any ten records). Then print them on the basis of id. Use structure concept.
#include<stdio.h> struct teacher { int id; char name[30]; char address[30]; char subject[30]; }t[10]; int main () { int i,search_id,flag=0; for(i=0;i<20;i++) { printf("Enter teacher's id,name,address and subject : "); scanf("%d%s%s%s",&t[i].id,t[i].name,t[i].address,t[i].subject); } printf("Enter id to search: "); scanf("%d",&search_id); for(i=0;i<20;i++) { if(t[i].id==search_id) { flag=1; break; } } if(flag==1) printf("%d\t %s\t %s\t %s\n",t[i].id,t[i].name,t[i].address,t[i].subject); else printf("Record not found "); return 0; }
7. Write C program to input any 10 student’s name, grade, gender and marks in 5 subjects. Then print all students’ records with their name, grade, gender, total and percentage. Also print names of those students whose gender is ‘female’.
#include<stdio.h> #include<string.h> struct student { char name[30]; int grade; char gender[10]; int sub1,sub2,sub3,sub4,sub5,total; float per; }s[10]; int main() { int i; for(i=0;i<10;i++) { printf("Enter name,grade,gender, marks in five subjects : "); scanf("%s%d%s%d%d%d%d%d",s[i].name,&s[i].grade,s[i].gender,&s[i].sub1,&s[i].sub2,&s[i].sub3,&s[i].sub4,&s[i].sub5); } for(i=0;i<10;i++) { s[i].total=s[i].sub1+s[i].sub2+s[i].sub3+s[i].sub4+s[i].sub5; s[i].per=(float)s[i].total/5; } for(i=0;i<10;i++) { printf("Name: %s\n",s[i].name); printf("Grade:%d\n",s[i].grade); printf("Gender:%s\n",s[i].gender); printf("Total:%d\n",s[i].total); printf("Percentage:%0.2f\n",s[i].per); } printf("\n Female Records only \n"); for(i=0;i<10;i++) { if(strcmp(s[i].gender,"female")==0) { printf("Name: %s\n",s[i].name); printf("Grade:%d\n",s[i].grade); printf("Gender:%s\n",s[i].gender); printf("Total:%d\n",s[i].total); printf("Percentage:%0.2f\n",s[i].per); } } return 0; }
8. Using function and structure to calculate sum of two distances measured in terms of kilometers and meters. For example, If we input following data Kilometer meter 23 445 45 756
69 201 The output would be 69 km and 201 meters.
#include<stdio.h> struct distance { int km; int m; }d1,d2,total; void sum(struct distance dis1,struct distance dis2); int main() { printf("Enter first distance : "); scanf("%d%d",&d1.km,&d1.m); printf("Enter second distance : "); scanf("%d%d",&d2.km,&d2.m); sum(d1,d2); return 0; } void sum(struct distance dis1,struct distance dis2) { total.m=(dis1.m+dis2.m)%1000; total.km=dis1.km+dis2.km+(dis1.m+dis2.m)/1000; printf("Total km=%d m=%d",total.km,total.m); }
9. Write C program to input student id, name and grade and print them. Use union concept.
#include<stdio.h> union student { int id; char name[30]; int grade; } s; int main() { printf("Enter student id: "); scanf("%d",&s.id); printf("Student ID: %d\n ",s.id); printf("Enter name : "); scanf("%s",s.name); printf("Name : %s\n",s.name); printf("Enter grade : "); scanf("%d",&s.grade); printf("Grade : %d\n",s.grade); return 0; }


Lab works-3(Pointers)
1. Write C program to perform arithmetic calculations (sum, difference, multiplication and division) of two numbers using pointers.
#include<stdio.h> int main() { int num1,num2,sum,diff,mul,div,*x,*y; printf("Enter two numbers : "); scanf("%d%d",&num1,&num2); x=&num1; y=&num2; sum=*x + *y; diff=*x - *y; mul=*x * *y; div=(*x) / (*y); printf("Sum=%d\n",sum); printf("Difference=%d\n",diff); printf("Multiplication=%d\n",mul); printf("Division=%d\n",div); return (0); }
2. Write C program to know a number is even or odd using pointer.
#include<stdio.h> int main() { int num,*n; printf("Enter any number: "); scanf("%d",&num); n=&num; if(*n % 2==0) printf("even"); else printf("odd"); return (0); }
3. Write C program to find sum and average of ‘n’ natural numbers using pointer.
#include<stdio.h> int main() { int *p; int n,i,sum=0,avg=0; p=&n; printf("Enter value of n: "); scanf("%d",&n); p=&n; for(i=1;i<=*p;i++) { sum=sum+i; } avg=sum/(*p); printf("Sum=%d\n",sum); printf("Average=%d\n",avg); return (0); }
4. Write C program to print 7, 22, 11, 34, 17 ..... 10th term using function. Pass pointer to function.
#include<stdio.h> void series (int *p); int main() { int *p; int n=10; p=&n; series(&n); return (0); } void series(int *p) { int a=7,i; for(i=1;i<=*p;i++) { printf("%5d",a); if(a%2==0) a=a/2; else a=a*3+1; } }
5. Write C program to find factorial of a number using pointer.
#include<stdio.h> int main() { int *p; int n; int i,fact=1; printf("enter a number\n"); scanf("%d",&n); p=&n; for(i=1;i<=*p;i++) { fact= fact*i; } printf("Factorial =%d",fact); return (0); }
6. Use array to input 10 elements and print them. Use array as pointer.
#include<stdio.h> int main() { int i,arr[10]; for(i=0;i<10;i++) { printf("Enter arr[%d] : ",i); scanf("%d",(arr+i)); } for(i=0;i<10;i++) { printf("%d\n",*(arr+i)); } return (0); }
7. Write C program to input 10 elements and print maximum and minimum value. Use array as pointer.
#include<stdio.h> int main() { int i,arr[10],max,min; for(i=0;i<10;i++) { printf("Enter arr[%d] : ",i); scanf("%d",(arr+i)); } max=*(arr+0); min=*(arr+0); for(i=0;i<10;i++) { if(*(arr+i)>max) max=*(arr+i); if(*(arr+i)<min) min=*(arr+i); } printf("Maximum number = %d \n Minimum number = %d",max,min); return (0); }
8. Write C program to swap two values using call by reference and call by value.
//program to show call by value #include<stdio.h> void swap(int a, int b); main() { int a = 10, b= 20; swap(a,b); printf("Call by value\n"); printf(" After swapping a = %d and b= %d ",a,b); } void swap(int a, int b) { int t; t = a; a = b; b = t; } //program to show call by reference #include<stdio.h> void swap(int *a, int *b); main() { int a = 10, b= 20; swap(&a,&b); // call by reference printf("Call by reference\n"); printf(" After swapping a = %d and b= %d ",a,b); } void swap(int *a, int *b) { int t; t = *a; *a = *b; *b = t; }
9. Write C program to sort 10 numbers stored in an array using pointer.
#include<stdio.h> int main() { int *p; int num[10],i,j,temp; for(i=0;i<10;i++) { printf("Enter num[%d] : ",i+1); scanf("%d",(num+i)); } for(i=0;i<9;i++) { for(j=i+1;j<10;j++) { if(*(num+i)>*(num+j)) { temp=*(num+i); *(num+i)=*(num+j); *(num+j)=temp; } } } printf("Sorted in Ascending Order \n"); for(i=0;i<10;i++) { printf("%d\n",*(num+i)); } return (0); }
10. Write C program to print factors of a number using pointer.
#include<stdio.h> int main() { int num,i; int *p ; printf(" Enter any number : ") ; scanf("%d",&num); p = &num; for(i=1;i<=*p;i++) { if(*p%i==0) printf(" %d\n ", i) ; } return (0); }
11. Write C program to print multiplication table of a number using pointer.
#include<stdio.h> int main() { int num,i; int *p ; printf(" Enter any number : ") ; scanf("%d",&num); p = &num; for(i=1;i<=10;i++) { printf(" %d\n ",(*p * i)) ; } return (0); }


Lab works-4(File Handling)
1. Write C program to input a sentence and store data in a data file “file.txt”. And print them on screen. Use getc() and putc() functions.
#include<stdio.h> int main() { FILE *fp; char ch; printf("\n Input data (to exit press ctrl+z)="); fp=fopen("f.txt","w"); while((ch=getchar())!=EOF){ putc(ch,fp); } fclose(fp); printf("\n Output data\n "); fp=fopen("f.txt","r"); while((ch=getc(fp))!=EOF){ putchar(ch); } fclose(fp); return (0); }
2. Write C program to store some ‘n’ natural numbers in a data file and print them on screen. Use getw() function. Also print their average.
#include<stdio.h> int main() { FILE *fp; int i,n,sum=0,avg=0; fp=fopen("file.txt","w"); printf("Enter the value of n: "); scanf("%d",&n); for(i=1;i<=n;i++) { putw(i,fp); } fclose(fp); printf("Press enter to view numbers :"); printf("\n Output data\n "); fp=fopen("file.txt","r"); while((i=getw(fp))!=EOF){ sum=sum+i; printf(" %d\n ",i); } avg=sum/n; fclose(fp); printf("\n Average = %d ",avg); return (0); }
3. Write C program to store student’s name and address in a data file “student.txt”. Use fprintf() function. Then read contents of data file and print them on screen. Use fscanf() function.
#include<stdio.h> int main() { char name[30],add[30]; FILE *fp; fp=fopen("student.txt","w"); printf("Enter name and address : "); scanf("%s%s",name,add); fprintf(fp,"%s\t %s",name,add); fclose(fp); fp=fopen("student.txt","r"); fscanf(fp,"%s\t %s",name,add); printf("\n -----Ouput data -----\n "); printf("%s \t %s",name,add); fclose(fp); return (0); }
4. Write C program to store book’s name, edition and price in a data file “book.txt” using yes/no options. It means the computer stores data until you say ‘n’. Then print them on screen.
#include<stdio.h> int main() { char b_name[30]; int b_edition; float b_price; char ch='y'; FILE *fp; fp=fopen("book.txt","w"); while(ch!='n') { printf("Enter book name : " ); scanf("%s",b_name); printf("Enter book edition : " ); scanf("%d",&b_edition); printf("Enter book price : " ); scanf("%f",&b_price); fprintf(fp,"%s\t %d\t %f\n",b_name,b_edition,b_price); printf("Add more records (y/n) : "); scanf(" %c",&ch); } fclose(fp); fp=fopen("book.txt","r"); while(fscanf(fp,"%s%d%f",b_name,&b_edition,&b_price)!=EOF) { printf(" %s\t %d\t %f\n ",b_name,b_edition,b_price); } fclose(fp); return (0); }
5. Write C program to store employee’s name, position and salary in a data file “employee.txt” using fwrite(). The computer stores data until you say ‘n’. Then read the contents of that file and print on screen using fread().
#include<stdio.h> struct employee{ char e_name[30]; char post[10]; float salary; }; int main(){ struct employee emp; char ch; FILE *fp; fp=fopen("employee.txt","w"); while(ch!='n'){ printf(" Enter employee name:\n "); scanf("%s",emp.e_name); printf(" Enter employee post:\n "); scanf("%s",emp.post); printf(" Enter employee salary:\n "); scanf("%f",&emp.salary); fwrite(&emp,sizeof(emp),1,fp); printf("\n Add more record (y/n): "); scanf(" %c",&ch); } fclose(fp); fp=fopen("employee.txt","r"); printf("\n Data from the file: \n"); printf("\n Name\t Post \t Salary"); while(fread(&emp,sizeof(emp),1,fp)) { printf("\n %s\t %s\t %f",emp.e_name,emp.post,emp.salary); } printf("\n"); fclose(fp); return (0); }
6. Write C program to input students’ name, grade and marks in five subjects. Then store these all data with total and percentage in a data file “student.dat”. Print all those data of students who have percentage >=80. You may use fscanf() or fwrite().
#include<stdio.h> struct student { char name[30]; int grade; int sub1,sub2,sub3,sub4,sub5,total; float per; }s[10]; int main() { int i,n; printf("How many records : "); scanf("%d",&n); FILE *fp; fp=fopen("student.dat","w"); for(i=0;i<n;i++) { printf("Enter name : "); scanf("%s",s[i].name); printf("Enter grade : "); scanf("%d",&s[i].grade); printf("Enter sub1 mark: "); scanf("%d",&s[i].sub1); printf("Enter sub2 mark: "); scanf("%d",&s[i].sub2); printf("Enter sub3 mark: "); scanf("%d",&s[i].sub3); printf("Enter sub4 mark: "); scanf("%d",&s[i].sub4); printf("Enter sub5 mark: "); scanf("%d",&s[i].sub5); s[i].total=s[i].sub1+s[i].sub2+s[i].sub3+s[i].sub4+s[i].sub5; s[i].per=(float)s[i].total/5; fprintf(fp,"%s %d %d %d %d %d %d %d %f\n",s[i].name,s[i].grade,s[i].sub1,s[i].sub2,s[i].sub3,s[i].sub4,s[i].sub5,s[i].total,s[i].per); } fclose(fp); printf("\n-----output-----\n"); fp=fopen("student.dat","r"); while(fscanf(fp,"%s %d %d %d %d %d %d %d %f\n ",s[i].name,&s[i].grade,&s[i].sub1,&s[i].sub2,&s[i].sub3,&s[i].sub4,&s[i].sub5,&s[i].total,&s[i].per)!=EOF) if(s[i].per>=80) { printf("\n Name: %s\n",s[i].name); printf("Grade:%d\n",s[i].grade); printf("Sub1 : %d\n",s[i].sub1); printf("Sub2 : %d\n",s[i].sub2); printf("Sub3 : %d\n",s[i].sub3); printf("Sub4 : %d\n",s[i].sub4); printf("Sub5 : %d\n",s[i].sub5); printf("Total:%d\n",s[i].total); printf("Percentage:%0.2f\n",s[i].per); } return (0); }
7. Write C program to show the concept of rename() and remove() functions.
#include<stdio.h> int main() { FILE *fp; char name[30]; fp=fopen("oldfile.txt","w"); printf("\n Enter your name : "); scanf("%s",name); fprintf(fp,"%s",name); fclose(fp); rename("oldfile.txt","newfile.txt"); remove("newfile.txt"); return (0); }
8. Write C program to show concept of ftell(), fseek() and rewind() functions.

// Program of ftell () and rewind()

#include<stdio.h> int main () { char name [30]; int age, length; FILE *fp; fp = fopen ("trinity.txt","w"); printf("Enter your name: \n"); scanf("%s",name); printf("Enter your age: \n"); scanf("%d",&age); fprintf (fp, "%s %d", name,age); length = ftell(fp); rewind (fp); fscanf (fp, "%s", name); fscanf (fp, "%d", &age); fclose (fp); printf ("Name= %s \n Age= %d \n",name,age); printf ("Total number of characters in file is %d \n", length); return (0); }

// Program of fseek ()

#include<stdio.h> int main () { FILE *fp; fp = fopen("test.txt", "r"); // Moving pointer to end fseek(fp, 0, SEEK_END); // Printing position of pointer printf("%ld", ftell(fp)); return (0); }
बधाई छ ! End of C Lab Session