C Programming
8 programs with Solution & Output
Rejaul Hoque Sarkar
Cont. No.- 9749189618 ; Email ID- srejaulhoque@gmail.com
------------------------------------------------------------------------------------------------------------------
------------------------------------------------------------------------------------------------------------------
------------------------------------------------------------------------------------------------------------------
1/ Write a C Program to create a text file.
Solution =>
#include<stdio.h>
#include<conio.h>
void main()
{
char ch;
FILE *fp;
fp = fopen("SAMPLE.TXT", "w");
clrscr();
printf("\n Type the text press enter key at end. \n\n");
while((ch = getchar())!= '\n')
put(ch,fp);
fclose(fp);
}
Output =>
--------------------------------------------------------------------------
Type the text press enter key at end.
computer programming in C language
is widely used for Science and
Engineering applications.
--------------------------------------------------------------------------
2/ Write a C Program to read a text file and also count the number of vowels present in the file.
Solution =>
#include<stdio.h>
#include<conio.h>
#include<ctype.h>
void main()
{
char ch;
int count=0;
FILE *fp;
fp = fopen("SAMPLE.TXT", "r");
clrscr();
printf("\n The content of the text file is : ")
while(!feof(fp))
{
ch = getch(fp);
printf("%c",ch);
switch(toupper (ch));
{
case 'A' :
case 'E' :
case 'I' :
case 'O' :
case 'U' : count++;
break;
}
}
printf("\n\n Number of vowels present = %d", count);
fclose(fp);
getch();
}
Output =>
-----------------------------------------------------------------------------
The content of the text file is :
Computer Programming in C language is
widely used for Science and Engineering
applications.
Number of vowels present = 31
-----------------------------------------------------------------------------
3/ Write a C Program to create a data file to store details of n students.
Solution =>
#include<stdio.h>
void main()
{
int rno, tot, i, n;
char sname[20];
FILE *fp;
fp = fopen("STUDENT.DAT", "w");
clrscr();
printf("\n How many students ? ");
scanf("%d", &n);
for(i = 0; i < n; i++)
{
printf("\n Student roll no., name, total ? ");
scanf("%d %s %d", &rno, sname, &tot);
fprintf(fp,"%d %s %d\n", rno, sname, tot);
}
fclose(fp);
}
Output =>
---------------------------------------------------------------------------------------
How many students ? 3
student roll no., name, total ? 101 ARUN 78
student roll no., name, total ? 102 DEEPAK 72
student roll no., name, total ? 222 WASIM 25
---------------------------------------------------------------------------------------
4/ Write a C Program to process a data file/read information from a data file.
Solution =>
#include<stdio.h>
#include<conio.h>
void main()
{
int rno, tot;
char sname[20];
FILE *fp;
fptr = fopen("STUDENT.DAT", "r");
clrscr();
printf("\n-----------------------------------------------");
printf("\n Roll No. Name Total");
printf("\n-----------------------------------------------");
while(!feof(fptr))
{
fscanf(fptr,"%d %s %d\n", &rno, sname, &tot);
if(tot >= 75)
printf("\n %6d \t %-20s %6d", rno, sname, tot);
}
printf("\n-----------------------------------------------");
fclose(fptr);
getch();
}
Output =>
_______________________________________________________________________
------------------------------------------------------------------------------------------------------------------
------------------------------------------------------------------------------------------------------------------
Thank You,
EduReja
Like us on Facebook : FB
Follow us on Twitter : TW
-----------------------------------------------------------------------------
-----------------------------------------------------------------------------
See Our Videos ==>> CLICK HERE
See Our Videos ==>> CLICK HERE
-----------------------------------------------------------------------------
See Our Videos ==>> CLICK HERE
See Our Videos ==>> CLICK HERE
-----------------------------------------------------------------------------
0 comments:
Post a Comment