0% found this document useful (0 votes)
45 views

Coding Books

Thank you

Uploaded by

newarsahil34
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
0% found this document useful (0 votes)
45 views

Coding Books

Thank you

Uploaded by

newarsahil34
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 51
C programming Xl Question and Solution HSEB C PROGRAM SOLUTION 1. _ For any integer input through the keyboard, write a C program to find out whether itis odd or even. [HSEB 2062,2066,2068] include Hinclude void main() { int n; printf("\nEnter any number: "); scanf("%d",8n); if(n942==0) printf("9d is Even",n); else printf("9d is Odd",n); etch) , 2. Writea € program te gain or loss. [SEB 2066} iput cost price (CP) and selling price (SP) and determines whether there is fincludecstdio.h> fHinclude void main() { float cpp; printf("\nEnter Cost Price and Selling Price: "); scanf("36%t" 8p, &sp); iftsp>ep) printi("Rs. .2Fis Profit" sp-cp); else printf("Rs. .2F is Loss",cp-sp); getchi); } 3. Write aC program that reads three numbers and displays the largest among them. [HSEB 2065] Hinclude#include void main() { int a,b,c; printf("\nEnter any three numbers: "; scant("3id%d%d", a, &b 8c); iffa>d 88 arc) printf("%d is Grester",a}; else if(b>a && boc) printf("%ed is Grester",b); else printf("%d is Greater',c); getch(); ) 4. Write aC program that checks whether the numbered entered by the user is exactly divisible by 5 but not by 11.[HSEB 2065] Hinclude finclude void main() { int 9; printf("\nEnter any number: "); scanf("%d",8n); if{n96 =-0 88 ni111-0) printf("%d is exactly Divisible by 5 but not by 11",n); else printf("condition dissatisfied"); etch); , 5. Write a C program to find the commission amount on the basis of sales amount as per following conditions: Sales amount (Rs) Commission 0-100 5% 1001-2000 10% >2000 12% [HSEB 2066] Hincludecstdio.h> Hinclude void main() { float s,¢a; printf("\nEnter sales amount: "); scanf("94F" 8s); if(s>=0 && s<=1000) a=0.05%e; ca0.1%e; else ca=0.12%e; printf("Your Commission is Rs. %.2F",ca); getchi); } 6. Write a program to display name of the day on the basis of entered number 1 to 7. For example, 1 for Sunday. [HSEB 2066] Hincludecstdio.h> #includesconio.h> void main() printf("\nEnter number between 1 to 7"); scanf("36d",&n); ‘switch(n) { case 1 printf("\nSUNDAY");, break; case 2: printf("\nMONDAY" break; case 3: printf("\nTUESDAY break; case 4 printf("\nWEDNESDAY"); break; case 5: printf("\nTHURSDAY"); break; case 6: printf("\nFRIDAY");, break; case 7: printf("\nSATURDAY"); break; default: printf("\n Invalid Choice"); } etch); ) 7. Write aC program to display the sum of ‘n’ terms of even numbers. [HSEB 2063] Hincludesstdio.h> Hinclude void main() { int in,s=0,a=2; printf("\nEnter how many numbers? "); scanf{"%d",&n); forlie0;icn;i++) printf("\n Sum of dd terms of even numbers is dn.) etch); } 8 Write aC program to input a number and display its multiplication table. [HSEB 2958, 2061] Hinclude Hinclude void main() int ni; printf"\n Enter number"); scant("%d&n); for( O;i++) printf("\ns6d X %d = 9d!"\n,i.n*i]; eetch(); ) 9. Write aC program to read a positive number integer less than 20 and display its multiplication, table. [HSEB 2062] Hincludecstdio.h> Hinclude void main() { int printf("\n Enter positive number less than 20: scant("%d", 8); if(r>0 88 ne20) =10;ie+) printf("\ns6d X %d = 9d",n,i.n*i); } else printf("\n Invalid number"); getchi); } 10. Write a C program to print 10 terms of any series using FOR loop. [HSEB 2064] Hinclude #include void main() { int mja=5; printf("\n Enter how many numbers?"); scanf("%d",8n); ensit+) { printf("96d\t",a);, getchi); } 11. Write a C program to print 10 terms of the following series using FOR loop, 1, 5, 9, 13 [HSEB 2063] Hinclude Hinclude void main() forliz0;i<10;i++) { printf("96d\t",a);, aratd; ) getchi); } 12. Write a C program to read a four digit number and display it in reverse order. [HSEB 2055] Hincludesstdio.h> Hinclude void main() { int n.7,5=0; printf("\n Enter any 4 digit number: "); scanf("%d",&n); while(n!=0) it(ec=d) printf("\n Reversed number is %d",s); else printf("\n Itis not a 4 digit number"); getch(); ) 13. Write a C program to find the factorial of a given positive number. [HSEB 2066] #includesstdio.h> include void main() printf("\n Enter positive number: "); scanf("96d",&n); if(n ffincludesconio.h> void main() printf("\n Enter positive number: "); scanf("%6d" 8); if(n Hincludesconio.h> void main() printf("\nEnter any integer number: "); scanf("%d",8n); ifle==2) printf("%6d is prime number",n); else printf("9éd is not prime number",n); getch(); 16. Write aC program to input ‘n’ numbers and find out the largest and smallest number. [HSEB 2062] Hincludesstdio.h> Hinclude void main() { int n,,num{100),g,5; printf("\nEnter how many numbers?"); scanf("%d",&n); printf("\nEnter %d numbers',n); forlizO;ien;i++) seanf{"9%6d" &num|i)); g-num[0}; senum(0]; if (numfil>e) genumil; i{numtil Hincludecconio.h> void main() { int n,i,j,num[10],temp; printf("Enter how many numbers?"); scanf{"96d",8n); printf("\nEnter 96d numbers',n); forliz0;icn;i++) scanf("%éd" &num|i]}; forlie0;inumG) { temp=numti}; num|i}=numij); num{jJ=temp; i } 1 printf("\n The sorted numbers in ascending order are\n"); forliO;ien;i++) printf("%6d\",numfil); getchi); ) 18. Write a program to store ten different constant variables in an array and print out the greatest number. [HSEB 2064] Hincludecstdio.h> Hinclude void main() { int inum{0L.g; printf"\nnter 10 numbers for(i=0;i<10;i+4) scant("%d",&numti); g-num[0}; for ( it (nurnti>s) 10;i+) genumlil; ) printf("\nThe greatest number is %d",g); getch); ) 19. Write a program to sort integer variables in descending order. [HSEB 2063] fincludestdio.h> finclude void main() ( int n,,j,num[10],temp; printf("Enter how many numbers?"); scant("%6d", 8); printf("\nEnter %d numbers", n); for(i scanf("9%d",&num|i)}; niit+) forli Hincludecconio.h> void main() { int ic=0; float s{200}; printf("\nEnter salaries for 200 employees"); forli=0;i<200;i++) scanf("%",8sfil); for(i=0;i<200ji++) { if (sli>5000 && sfi}<10000) czet; } printf(*Total number of employees getting salary between 5000 and 10000 are %éd",c); getch(); 1 21. Write a program using C language to read the age of 100 persons and count the number of persons in the age group between 50 and 60, Use FOR and CONTINUE statement. [HSEB 2061] Hincludecstdio.h> Hinclude void main() 4 int i,e=0; float a[100}; printf("\nEnter age of 100 person: for{i=0;ic100;i++) scanfl"96" ali); for { if (ali>50 && afi}<60) i<100;i++) exert; else continue; } printf("Total number of persons aged between 50 and 60 are %d",); getchi); 1 22. Write a C program to read age of 40 students and count the number of students of the age between 15 and 22. [HSEB 2063] Hincludesstdio.h> Hinclude void main() { int ie=0; float {40}; printf("\nEnter age of 40 students"); for(i AO i++) scanfl"s6F", ali); for { if (al>15 && afi}<22) si #includesconio.h> void main() { int ni float [100]; printf("\nEnter how many students?"); seanf("%6d",&n); printf("\n Enter marks for %d students: ",n); forfi-O;icn;i++) scanf("96t", til}; forliz0;i70) else ifmlil<35) cfecf+1; printf("\n Total no. of students scoring more than 70 are %d ",c); printf"\nTotal no. of students who are fail are 4 "ef; getch(); } 24. Write a program to read elements of the two matrices of order 3 x 3 and perform the matrix addition, (SEB 2065] Hincludecstdio.h> Hinclude void main() { int a[3}{3], 6131131 S13)(3].j; printf("\n Enter elements for matrix A\n" forli=0;ic3;i++) { forlj=0;j<3;j++) { printf("\nEnter the number [%d] [%d] ".i]; scant("%d" &ali){i]);, 1 } printf("\n Enter the elements for matrix 8\n"); for(j=0;j<3;j++) { printf"\nEnter the number [%d] [%¢] scant("%d" &bITE): ) ) printf"\n The sum of two matrix s\n"); fo { forlj=0;)<3;i++) ( stall) +BUIE; ) ) for{i=O;i<3;i++) ( for(i-0<3si++) { printf("%d\e tis ) printf("\n"); ) getch(); i<3;it+) 1 25. Write a program to count the number of vowels and consonants in a given text. [HSEB 2064, 2066] Hinclude Hincludesconio.h> Hincludeestringh> void main() i char str[20]; int nv=0,ni printf("\nEnter any string"); gets(str); strupr(str); for 96d "snv); printf("\n No. of Consonants = %d ",nc); getch(); + 26. Write a program to read a line of text and convert it into uppercase. [HSEB 2068] finclude Hincludesconio.h> Hincludecstring h> void main() { char string[100]; printf("\nEnter any line of text in lowercase\n"); gets(string); strupr(string); printf("\n Enterd text converted into uppercase\n"); puts(string); getchi); } oR Hincludesstdio.h> Hinclude Hincludesstring.h> void main() { char string[100); inti; printf("\nEnter any line of text in lowercase\n"}; gets(string); for(i=0;stringli}!=0;i++) { if(stringli]>='a' && stringli) stringliJ=stringti]-32; } printf("\n Enterd text converted into uppercase\n"); puts(string); getch(); } 27. Write a program to input n names and sort them in alphabetical order. [HSEB 2062, 2068] Hinclude Hinclude #includesstring h> void main() { char name[S0]{20],temp|20); intings printf("\nEnter how many names: "}; scanf{" fan); printf("Enter %d names\n",n); for(i=O;isn;i++) scanf("%s",name[i}; forliicnyi++) { for(i=it1;jsn;j++) { if(stremp(namefi],name[j]}>0) { strepy(temp,namelil); strepy(name[i],namelj]); strepy(namefj},temp); ) } } printf("\nThe sorted names are\n"); for(i=0;iensi¢+) printf("\n%s",namefi} getch(); } 28. Write a C Program to enter name of students and age of ten different students in array and arrange them in descending order according to the age and print them. [HSEB 2057] Hincludecstdio.h> #includesconio.h> includecstring h> struct student { char name [25]; int age; Jstd[10); void main() { char temp|25); intij.tm; printf("Enter 10 names and age of students\n"); for { scanf{"%s",stdfi.name); ie LOsie+) scanf{"%sd",&stdlil.age); 1 ie LOsie+) { i+1;}<10;j+4) { if(stali].age Hincludecconio.h> Hincludecstring h> struct student { char name [25]; int mark; Jstd[10]; void main() & char temp[25]; intij.tm; printf("Enter names and marks for 20 students\n"); for { scanf{"%s",stali.name); I< 20:14) scanf{"96d",&stafi].mark); } forli=0;i<20;i++) { forji+1;j<20;j++) { if(stoli]. markestdlj]. mark) { tm=stafi]. mark; std]. mark=stalj]. mark; std. markstm; strepy(temp,std[i].name); strepy(stdli.name,stdljl.name); strepy(stdlj].name,temp); 1 } d printf("\nThe sorted names and marks in descending arder according to marks are\n"); for i<20;i+4) printf("\n%s\t%a', stdli].name, stafi]. mark); getch(); } 31. Write a C program to input a message from keyboard and display the menu ‘a. Print the message length in terms of characters. b. print the message in reverse order ¢. print the message in capital letters d. copy the message from one location of screen to another location. _[HSEB 2060] Hinclude Hinclude #includesstring h> void main() { char msg[100],msgi{100}; int i.ch,len,j; printf("\n Enter a message:"); eets(mse); printf("\n 1. Print the message length in terms of characters" printf("\n 2. Print the message in reverse order"); printf("\n 3. Print the message in capital letters"); printf("\n 4. Copy the message from one location to another"); printf("\n Enter your choice (1-4)"); scanf{"9%d",&ch); switch(ch) { case 1: len=0; while(msgflen]!="0') lent; printf("\n The string %s has %d characters\n",msg,len); break; case 2: len=strlen(msg); iF0; strepy(msg,msg1); printf("\n The reversed string is %s" msg); break; case 3: for(iO;msglili=\0'it#) { if(msgli}>='a' && msgli}<='2') imsetil=msglil-32;, } print("\n The message in uppercase %s",msg); break; case 4: fo hmmsgtil!=\O'i+) msglil=mselil; msgi{il=\0' printf"The copied string is %s ",msg); break; default: printf("\n Invalid choice"); ) getch(); ) 32. Write a program to find the sum of n integer numbers using function. {HSEB 2066] fincludecstdio.h> finclude int sumtint); void main() { int n,a; printf("\nEnter how many numbers: "); scanf{"3éd",8n}; assum(n); printf("\n Sum of %d numbers= %d",n,a); getch() , int sumtin n) { inti forliztji< s=sti; return s; } 33, Write a program to calculate the factorial of a given number using function. [HSEB 2063] Hinclude Hincludesconio.h> int fact(int); void main() { int na; printf("\n€nter any number: "); scanf{"%d",&n}; s=factin); printf("\n factorial= %6d",a); getch(); ) int fact(int n) { forlie cents) fef*i; return f; } 34. Write a program to calculate the factorial of a given number using recursive function. [HSEB 2064, 2068) Hincludeestdio.h> Hincludesconio.h> int fact(int); void main() { int na; printf("\nEnter any number: "); scanf{"%d",8n}; =fact(n); printf("\n factorial= %d",a); getchi); t int fact(int n) { it(nc=1) return 1; else return(n*fact(n-1)); # 35, Write a program using user defined function to calculate y raise to power x.[HSEB 2067] #includecstdio.h> Hinclude int powertint,int); void main() { int x5 printf("\n Enter values for y and x: "); seanf("%d%a" Buy, 8x); p=power(y,x); printf("\n y raise to power x= %d",p); getch(); + int power(int y, int x) pwepwty; return pw; } 36. Write a program that reads different names and addresses into the computer and rearrange them into alphabetical order using the structure variables. [HSEB 2061, 2064] fincludecstdio.h> fincludecconio.h> fincludecstringh> struct student { char name(30]; char add (301; Jstd[100); void main() { char tname[30],tadd[30]; int ijn; printf("\n Enter how many students: scanf("%d",&n); printf("Enter names and addresses for hd students: "n); forfiOjicnyit+) scant("%6s%s" stdfi].name, stai].add); forliO;ien;i+4) Aijenij+4) if (stremp(stdli].name,stalj].name]>0) { strepy(tname stal.name); strepy(std{il.name,stdlj.name); strepy(stdlj].name,tname); strepy(tadd,std[i].add); strepy(stall.add staf]. add); strepy(stdlj].add,tadd); ) i ) printf("\n Sorted names in alphabetical order according to names are:\n"}; forlicOjcnsi++) printi("\n %s\t %s" sti] name,staliladd); getch(); ) 237. Write a program to show data writing and reading operation to/from a data file. [HSEB 2066] fincludecstdio.h> Hinclude struct { int roll; char name[25}; float mark; Istd; void main() { int ni; FILE “fp; fp=fopen("4:\\epra\student.txt","wb"); printf("\n Enter how many records: "); scanf("%d",8n); printf("enter student number name and marks for %d students",n); for(i { scanf{"%d%6s%f", &std.rollstd.name,8std.mark); cei) fwrite( std sizeof(sta).1,fp); } fclose(tp); fp=fopen("d:\\cpre\student.txt”, printf("\nRoll\tName\tMarks Obtained\n"); while(fread(&std,sizeof(std),1,fp)) rintf("9%d\t%s\t96f\n",std.roll,std.name,std.mark); fclose(tp); getch(); } 38. Write a program to enter name, roll-number and marks of 10 students and store them ina file. [HSEB 2065) Hwincludecstdio.h> #includesconio.h> struct { int roll; char name[25}; float mark; Jstd; void main() { inti; FILE “fp; fp=fopen("d:\\cpre\student.txt","wb"); printf("enter student roll number name and marks for 10 students"); scanf{"%edM%4s8ef",&std.rollstd.name,&std.mark); fwrite(&std,sizeof(sta).1,fp); } fclose(tp); getchi); } 39. Write a program to store std-no, name and mark of ‘n’ students in a data file. Display the records in appropriate format reading from the file. [HSEB 2066) Hinclude #includesconio.h> struct { int roll; char name[25}; float mark; Istd; void main() { int ni; FILE “fp; fp=fopen("4:\\epre\student.txt","w"); printf("\n Enter how many records: "); scanf("%d",8n); printf("enter student number name and marks for %d students",n); for(i: { scanf{"%6d%6s%f", &std.rollstd.name,std.mark); cei) fprintf(fp,"s6d\t%s\taef\n" std.roll,std.name,std.mark);, } fclose(tp); fp=fopen("d:\\cpre\student.txt”, printf("\nRoll\tName\tMarks Obtained\n"); while(Fscant{fp,"%6d%s%6"",&std.roll,std.name, &std.mark)!=EOF) rintf("9%d\t%s\t96f\n",std.roll,std.name,std.mark); fclose(tp); getch(); } 40. Write a program using C language that reads successive records from the new data file and display each record on the screen in an appropriate format. [HSEB 2061, 2062] Hincludecstdio.h> #includesconio.h> struct { int roll; char name[25}; float mark; Jstd; void main() { int ni; FILE “fp; fp=fopen("d:\\cpre\student.txt","wb"); printf("\n Enter how many recort seanf("%d",&n); printf("enter student number name and marks for %d students",n); scanfl"%d%s%",&std.rollstd.name,&std.mark); fwrite(&std sizeof(sta).1,fp); } fclose(tp); fp=fopen("d:\\cpre\student. txt," printf("\nRoll\tName\tMarks Obtained\n"); while(fread(&std sizeof(std),1,fp)) printf("94d\t%s\t%f\n" stroll std.name,std.mark); felose(fe); getch); ) 41. Write a program to rename and delete a data file using rename and remove command. [HSEB 2064, 2067) Hinclude Hinclude void main() { char filename|20]; char oldfilename{20],newfilename[20]; printf("\n Enter the file name to be removed: "); gets(filename); if(remove(filename ) printi("File %s is eemoved" filename}; else printf("File %s cannot be removed" filename}; printf("\n Enter old file name: "); gets(oldfilename); printf("\n Enter new file name: "); gets(newflename}; if{rename(oldfilename,newfilename} 0) printf("\n File %s is renamed to %s",oldfilename,newfilename); else printf("\n file %s cannot be renamed” oldfilename); etch); , 42. Write a program to open a new file and read roll-no, name, address and phone number of students until the user says “no”, after reading the data, write itto the file then display the content of the file. (HSEB 2068) Hinclude #include struct { int roll; char name[25}; char ada{30]; long phone; Ista; void main() { char ch='y3 FILE “fp: fp=fopen("d:\\cpre\student.txt","w"); while(ch { printf("\n Enter roll number: scanf("%6d",&std.roll); printf("\n Enter name: scanf("%s",std.name); printf("\n Enter address: "); seanf("%s",std.add); printf("\n Enter phone number: ") scanf("96ld", &std.phone); fprintf(fp,"96d\t%s\t%s\t34Id\n",std.roll,std.name,std.add,std.phone); printf("DO you want to continue (Y/N)? ); ch=getche(); } fclose(fp); fp=fopen("d:\\cpra\student.txt’,"r") printf("\nRoll\tName\tAddress\tPhone\n"); while (fscanf(fp,"%d%s%s%d" &std.roll std.name,std.add,&std.phone)!=EOF) printf("94d\t%s\t%s\t%ld\n" std. roll td.name,std.add std. phone}; fclose(fp); getch(); ) Program HSEB Question Solved PART Il Write a program to read a line of text and convert it into uppercase. [HSEB 2068] Hinclude Hincludesconio.h> Hincludecstring.h> void main() { char string[100];, printf("\nEnter any line of text in lowercase\n"); gets(string); strupr(string); printf("\n Enterd text converted into uppercase\n"); puts(string); eetch(); } oR Hinclude Hinclude Hincludecstring.h> void main() { char stringl100}; inti; printf("\n€nter any line of text in lowercase\n"}; gets(string); for(i20;stringlil!=0;i++) { if(stringlil>='a! && stringlil<='2') string } printf("\n Enterd text converted into uppercase\n"); ringli)-32; puts(string); getch(); } 2. Writea program to input n names and sort them in alphabetical order. (HSEB 2062, 2068] Hinclude Hinclude Hincludecstring h> void main() { char name[50]|[20],temp|20]; intings printf("\nEnter how many names: "); scanf{"%d",&n); printf("Enter 9d names\n",n); forli=0;icn;i+4) scanf{"%s",namelil}; forli=Qyiensi++) { for Aijenije4) { if(stremp(nameli},nameti})>0) { strepy(temp,namefi); strepy(name[i},namelj)) strepy(nametj},temp); ) } } printf"\nThe sorted names are\n"); forli=0;ien;i+#) printf("\n%s",namefi}; geteh(); ) 3. Write a C Program to enter name of students and age of ten different students in array and arrange them in descending order according to the age and print them. [HSEB 2057] Hinclude Hinclude Hincludecstring.h> struct student { char name [25]; int age; Jstd10), void main() { char temp|25); int ij.tm; printf("Enter 10 names and age of students\n"); ie LOsie4) scanf{"%s",stali].name); scanf{"9éd",&stdlil.age); { if(stdfi).age Hincludecconio.h> Hincludecstring h> struct student t char name [25]; int mark; Jstd[10); void main() { char temp|25]; intij.tm; printf("Enter names and marks for 20 students\n"); forlict { scanf{"%s",staf].name); I< 20;i+4) scanf{"96d",&stali].mark); } forli=0;i<20;i++) { for(jsi+1;j<20;j++) { if(stali].mark #includesconio.h> int sum(int); void main() printf("\nEnter how many numbers: "); scanf("%d",&n}; a=sumi(n); print("\n Sum of 9d numbers= d'sna); getch(); } int sum{int n) s=sti; return s; y 6. Write a program to calculate the factorial of a given number using function. [HSEB 2063] Hinclude #include int fact(int); void main() printf("\nEnter any number: "); scanf{"%d",&n}; etn); printf("\n factorial=%d",a); getch(); } int fact(int n) { int if=1; forliza;ic=nji++) fefti; return f; y 7. Write a program to calculate the factorial of a given number using recursive function. [HSEB 2064, 2068] Hinclude finclude int fact(int); void main() { int na; printf("\n€nter any number: "); scanf{"%d",&n}; afact(n); printf("\n factorial= %d",a); getch(); + int fact(int n) { if(n<=1) return 1; else return(n*fact(n-1)); , 8 Write a program using user defined function to calculate y raise to power x.[HSEB 2067] Hinclude #includesconio.h> int power(int,int); void maint) { int x5 printf("\n Enter values for y and x: "); scanf("%6d%d",&y, 8x); p=power(y,x); printf("\n y raise to power x= %d",p); getch(); } int power(int y, int x) t int pwe1 i fori return pw; r 9. Write a program that reads different names and addresses into the computer and rearrange them into alphabetical order using the structure variables, [HSEB 2061, 2064] include Hinclude fincludeestringh> struct student { char name(30]; char add (301; Jstd[100); void main() { char tname[30],tadd[30]; printf("\n Enter how many students: scanf{"%d"&n); printf("Enter names and addresses for %d srudents: *,n); for{i=0;ien;i+4) scanf{"96s%s" stdfi].name, stdli]. add); forli=O;i0) { strepy(tname;stafi].name);, strepy(std_i].name,stdlj.name); strepy(stdlj].name,tname); strepy(tadd,stdfi).add); strepy(stdli].add,stalj].add); strepy(stdlj].add,tadd); y t } printf("\n Sorted name: alphabetical order according to names are:\n"); fortiOjensis+) printf("\n és\t %s",stoli].name,stali add); getchi); } 10. Write a program to show data writing and reading operation to/from a data file. [HSEB 2066] #includesstdio.n> fincludecconio.h> struct { int rol; char name[251; float mark; Jstd; void main() { int mi FILE *fp; fp=foper :\\cprg\student.txt", wb"); printf("\n Enter how many recors scanf("96d",&n); printfenter student number name and marks for éd students"); for(i=O;i Hinclude struct { int roll char name[25]; float mark; Istd; void main() { int; FILE "fp; fp=fopen("d:\\cpre\student.txt”, b"); printf("enter student roll number name and marks for 10 students"); forli0;i<10;i++) { scanf{"%d%s%eF",&std.rolltd.name,&std.mark); fwrite( &std,sizeof(sta),1,fp); } fclose(fp); getch(); } 12. Write a program to store std-no, name and mark of ‘n’ students in a data file, Display the records in appropriate format reading from the file. [HSEB 2066] ffinclude Hinclude struct { int roll; char name[25]; float mark; std; void main() { int ni FILE "fp; fp=fopen("d:\\cpre\student.ti printf("\n Enter how many recor seanf("%6d",8n); printf("enter student number name and marks for Séd students",n); forfiet { scanf("%d%s%F",&std.roll td.name,&std.mark); fenji++) {printi(fp,"96d\ts%s tr } fclose(fp); std.roll,std.name,std.mark); fp=fopen(*a:\\cpre\student.xt","F); printf"\nRoll\tName\tMarks Obtained\n"); while(Fscanf(fp,"%sd%s%t", std roll std.name Sistd.mark)!=EOF) printf("%d\t%s\t%6f\n?, sterol std.name, std. mark); feloselfp); getch); } 13, Write a program using C language that reads successive records from the new data file and display each record on the screen in an appropriate format. [HSEB 2061, 2062] Hincludecstdio.h> finclude struct { int roll; char name[25}; float mark; }std; void main() { int ni FILE “fp; fp=fopen("d:\\cpra\student.txt","wb");, printf("\n Enter how many records: "| scanf("%d",&n); printf("enter student number name and marks for %d students",n);, for(i=0;i Hinclude struct { int roll; char name[25]; char add[30]; long phone; Jstd; void main() { char ck FILE “fp; fp=fopen("¢:\\cpre\student.txt","w"); printf"\n Enter roll number: scanf("96d",&std.roll); printf("\n Enter name: "); seanf("%6s",std.name); printf("\n Enter address: scanf("%6s",std.add); printf("\n Enter phone number: "); scanf("¥éld",&std.phone); {printf(fp,"96d\t%s\t%s\154Id\n",std.roll,std.name,std.add,std.phone); printf("00 you want to continue (¥/N)? chegetche(); } felose(tp); fp=fopen("d:\\cpre\student.txt”, printf("\nRoll\tName\tAddress\tPhone\n"); while(Fscant{fp,"%d%s%s%4Id", &std.roltd.name,std.add, &std.phone) !=EOF) printf("94d\ts%s\t%s\t%id\n" std. roll std.name,std.add,std.phone); felose(tp); getch();

You might also like