c programs_with answers
TRANSCRIPT
-
8/9/2019 c Programs_with Answers
1/40
SIMPLE PROGRAMS
1. Program to Find Sum and Average of Three Real Numer!
#include
main(){
float a, b, c, sum, avg;
printf("\nnter value of three numbers! ");
scanf("f f f", a, b, c);
sum $ a % b % c;
avg $ sum & ';
printf("\num $ .f", sum);
printf("\n*verage $ .f", avg);
getch();
+
. Program to Find Area of S"uare # $ir%umferen%e of a $ir%le
#include
#define - './
main()
{
float len, r, area, circum;
printf("\nnter length of a s0uare! ");
scanf("f", len);
area $ len 1 len;printf("\nnter radius of a circle! ");
scanf("f", r);
circum $ 1 - 1 r;
printf("\n*rea of s0uare $ .f", area);
printf("\n2ircumference of circle $ .f", circum);
getch();
+
&. Program to find S'here Surfa%e Area and (olume of a S'here
S'here Surfa%e Area ) * + PI + r + r (olume of S'here ) ,*-& + PI + r + r + r #include
#define - './
main()
{
float r, area, vol;
printf("\nnter radius of phere! ");
scanf("f", r);
area $ / 1 - 1 r 1 r;
-
8/9/2019 c Programs_with Answers
2/40
vol $ (/&') 1 - 1 r 1 r 1 r;
printf("\nphere urface *rea $ .f", area);
printf("\n3olume of phere $ .f", vol);
getch(); +
-++ Program to Find Area of a Triangle u!ing /ero ! Formula ++-
#include #include
main()
{
float a, b, c, s, area;
bac4!
printf("\nnter three sides of a triangle! ");
scanf("f f f", a, b, c);
if (a$$5 66 b$$5 66 c$$5)
{
printf("\n3alue of an7 side should not be e0ual to
8ero\n");goto bac4;
+
if (a%b
-
8/9/2019 c Programs_with Answers
3/40
printf("\n\n2ompound -nterest! .f", ci);
getch();
+
&1
rogram to find imple -nterest and 2ompound -nterest- $ (p 1 r 1 t) & 55
2- $ p 1 po9(( % r&55), t) : p
1&
#include
#include
main()
{
float p, r, t, si, ci;
printf("\nnter priciple, rate and time! ");
scanf("f f f", p, r, t);
si $ (p 1 r 1 t) & 55;ci $ p 1 po9(( % r&55), t) : p;
printf("\n\nimple -nterest! .f", si);
printf("\n\n2ompound -nterest! .f", ci);
getch();
+
&1
rogram to 2onvert ime in econds to ours, =inutes and econds
1&
#include main()
{
long sec, hr, min, t;
printf("\nnter time in seconds! ");
scanf("ld", sec);
hr $ sec&'55;
t $ sec'55;
min $ t&5;
sec $ t5;
printf("\n\nime is ld hrs ld mins ld secs", hr, min, sec);
getch();+
&1
rogram to 9ap 3alues of 9o 3ariables using hird 3ariable
1&
#include
main()
-
8/9/2019 c Programs_with Answers
4/40
{
int a, b, temp;
printf("\nnter an7 t9o numbers! ");
scanf("d d", a, b);
printf("\n\n?efore 9apping!\n");
printf("\na $ d\n", a);printf("\nb $ d\n", b);
temp $ a;
a $ b;
b $ temp;
printf("\n\n*fter 9apping!\n");
printf("\na $ d\n", a);
printf("\nb $ d\n", b);
getch();
+
&1rogram to 9ap 3alues of 9o 3ariables @ithout using 'rd 3ariable
1&
#include
main()
{
int a, b, temp;
printf("\nnter an7 t9o numbers! ");
scanf("d d", a, b);
printf("\n\n?efore 9apping!\n");
printf("\na $ d\n", a);printf("\nb $ d\n", b);
a $ a % b;
b $ a : b;
a $ a : b;
printf("\n\n*fter 9apping!\n");
printf("\na $ d\n", a);
printf("\nb $ d\n", b);
getch();+
&1?asic salar7 of an emplo7ee is input through the 4e7board. he A*
is B of the basic salar7 9hile the C* is B of the basic
salar7. rovident Dund is deducted at the rate of 5 of the gross
salar7(?%A*%C*).
rogram to 2alculate the Eet alar7.
1&
#include
-
8/9/2019 c Programs_with Answers
5/40
main()
{
float basicFsal, da, hra, pf, grossFsal, netFsal;
printf("\nnter basic salar7 of the emplo7ee! Cs. ");
scanf("f", basicFsal);
da $ (basicFsal 1 B)&55;hra $ (basicFsal 1 B)&55;
grossFsal $ basicFsal % da % hra;
pf $ (grossFsal 1 5)&55;
netFsal $ grossFsal : pf;
printf("\n\nEet alar7! Cs. .f", netFsal);
getch();
+
$ONTROL STATEMENTS
&111 rogram to Dind Gargest of hree Eumbers 111include
main()
{
int a, b, c;
printf("\nnter three numbers! ");
scanf("d d d", a, b, c);
if (a>b a>c)
printf("\n\nd is greater", a);
else if (b>a b>c)
printf("\n\nd is greater", b);else
printf("\n\nd is greater", c);
getch();
+
&1
rogram to 2hec4 @hether a 2haracter is a 3o9el or not b7 using
switch Statement
1&
#include
main(){
char ch;
printf("\nnter an7 character! ");
scanf("c", ch);
s9itch (ch)
{
case HaH!
-
8/9/2019 c Programs_with Answers
6/40
case H*H!
printf("\n\nc is a vo9el", ch);
brea4;
case HeH!
case HH!
printf("\n\nc is a vo9el", ch);brea4;
case HiH!
case H-H!
printf("\n\nc is a vo9el", ch);
brea4;999.9'professors.com Iursharan ingh atla age Eo.
case HoH!
case HJH!
printf("\n\nc is a vo9el", ch);
brea4;
case HuH!case HKH!
printf("\n\nc is a vo9el", ch);
brea4;
default!
printf("\n\nc is not a vo9el", ch);
+
getch();
+
&1 rogram to Dind the um of Dirst 55 ositive -ntegers 1include
main()
{
int i, sum$5;
printf("\n\tum of first 55 positive numbers\n");
for(i$5; i
-
8/9/2019 c Programs_with Answers
7/40
int i, sumven$5, sumJdd$5;
for (i$5; i 5)
{
digit $ n5;
n $ n&5;
sum $ sum % digit;
+
printf("\n\num of digits! d", sum);
getch();+
&111 rogram to Ceverse a Iiven Eumber 111&
#include
main()
{
long n;
int rev;
printf("\nnter an7 number! ");
scanf("ld", n);
printf("\nCeverse no. is!\n\n");9hile (n > 5)
{
rev $ n 5;
n $ n & 5;
printf("d", rev);
+
getch();
-
8/9/2019 c Programs_with Answers
8/40
+
&111 rogram to rint Dirst E rime Eumbers 111&
#include
main()
{int i, L, n;
printf("\nnter ho9 man7 prime numbers 7ou 9ant to print! ");
scanf("d", n);
printf("\n");
for (i$; i
-
8/9/2019 c Programs_with Answers
9/40
int n, temp, d, arm$5;
printf("\nnter an7 number! ");
scanf("d", n);
temp $ n;
9hile (temp > 5)
{d $ temp5;
temp $ temp&5;
arm $ arm % (d1d1d);
+
if (arm $$ n)
printf("\n\nd is an *rmstrong number\n", n);
else
printf("\n\nd is not an *rmstrong number\n", n);
getch();+
&1
rogram to rint the Eumbers, @hich are Aivisible b7 ' and B from
Dirst 55 Eatural Eumbers
1&
#include
main()
{
int i;
printf("\nDirst 55 numbers 9hich are divisible b7 ' and
B\n\n");
for (i$; i
-
8/9/2019 c Programs_with Answers
10/40
pal $ pal15 % M;
+
if (pal $$ n)
printf("\nd is palindrome", n);
else
printf("\nd is not palindrome", n);getch();+
&1 rogram to Dind Dactorial of a Eumber 9ithout using Cecursion 1&
#include
main()
{
int n, i;
long fact$;
printf("\nnter an7 number! ");
scanf("d", n);for (i$; i
-
8/9/2019 c Programs_with Answers
11/40
#include
int fibbo(int, int, int, int);
main()
{
int n, f, M$5, 7$, i$';
printf("\nnter value of n! ");scanf("d", n);
printf("\nd\td", M, 7);
fibbo(M, 7, n, i);
getch();
+
fibbo(int M, int 7, int n, int i)
{
int 8;
if (i 5)
{
rev $ n 5;
n $ n & 5;printf("d", rev);
+
getch();+
&1
rogram to Dind 3alue of sin(M) using Mpansion eries Iiven ?elo9!
-
8/9/2019 c Programs_with Answers
12/40
sin(M) $ M : M'&'N % MB&BN : MO&ON........
1&
#include
#include
main()
{float base, p9r, sum, c$, m$, i$', g, h;
printf("\nnter the base value! ");
scanf("f", base);
printf("\nnter the po9er value! ");
scanf("f", p9r);
sum $ base;
ab!
m $ m 1 i;
h $ po9(:, c);
g $ po9(base, i);
sum $ sum % (h 1 g) & m;i $ i % ;
c%%;
m $ m 1 (i : );999.9'professors.com Iursharan ingh atla age Eo.
if (i
-
8/9/2019 c Programs_with Answers
13/40
for (L$; L
-
8/9/2019 c Programs_with Answers
14/40
+
if (n $$ 5)
{
printf("\n2D of numbers is! d", m);
goto end;
+if (n > m)
{
temp $ m;
m $ n;
n $ temp;
+
0 $ ;
9hile (0 N$ 5)
{
0 $ m n;
if (0 N$ 5){
m $ n;
n $ 0;
+
+
printf("\n2D of number is! d", n);
end!
getch();
+
&111 rogram to Dind 3o9els in a tring 111&
#include
#include
main()
{
char strR5S;
int count$5, i$5;
printf("\nnter an7 string! ");
gets(str);
9hile (strRiS N$ H\5H)
{if (strRiS$$HaH 66 strRiS$$HeH 66 strRiS$$HiH 66
strRiS$$HoH 66 strRiS$$HuH)
count%%;
i%%;
+
printf("\nEo. of vo9els! d", count);
getch();
-
8/9/2019 c Programs_with Answers
15/40
+
&1
rogram to 2ount Eumber of @ords and Eumber of 2haracters in a
tring
1include
#include
main()
{
char strR5S;
int i$5, 9ord$5, chr$5;
printf("\nnter an7 string! ");
gets(str);
9hile (strRiS N$ H\5H)
{
if (strRiS $$ H H){
9ord%%;
chr%%;
+
else
chr%%;
i%%;
+
printf("\nEumber of characters! d", chr);
printf("\nEumber of 9ords! d", 9ord%);getch();
+
999.9'professors.com Iursharan ingh atla age Eo.
&111 rogram to -mplement brea4 tatement 111&
#include
main()
{
int i;
for (i$; i
-
8/9/2019 c Programs_with Answers
16/40
&111 rogram to -mplement continue tatement 111&
#include
main()
{
int i, n, a, s0;
printf("\n\trogram finds s0uare of positive numbers onl7\n");printf("\no9 man7 numbers 7ou 9ant to enter! ");
scanf("d", n);
for (i$; i
-
8/9/2019 c Programs_with Answers
17/40
printf("\n\n");
+
getch();
+
&111 rogram to *dd 9o =atrices 111include
main()
{
int aR5SR5S, bR5SR5S, cR5SR5S, i, L, ro9, col;
printf("\nnter number of ro9s and columns! ");
scanf("d d", ro9, col);
printf("\nnter elements of *rra7 *!\n");
for (i$5; i
-
8/9/2019 c Programs_with Answers
18/40
getch();
+
&1
rogram to multipl7 t9o matrices. he order and the elements of the
t9o matrices 9ill be entered b7 the user as input to the programand if multiplication is not possible then it should be reported to
the user
1&
#include
main()
{
int aR5SR5S, bR5SR5S, cR5SR5S, i, L, 4, r, r, c, c;
bac4!
printf("\nnter no. of ro9s and columns of =atriM *! ");
scanf("d d", r, c);
printf("\nnter no. of ro9s and columns of =atriM ?! ");scanf("d d", r, c);
if (c N$ r)
{
printf("\n\n=ultiplication is not possible\n");
goto bac4;
+
printf("\n\nnter elements of =atriM *!\n");
for (i$5; i
-
8/9/2019 c Programs_with Answers
19/40
+
for (i$5; i
-
8/9/2019 c Programs_with Answers
20/40
{
int aR-TS, n, i, b;
printf("\nnter si8e of arra7! ");
scanf("d", n);
printf("\nnter elements!\n");
for (i$5; i
-
8/9/2019 c Programs_with Answers
21/40
sum $ diagonalFsum(a, ro9, col);
printf("\num! d", sum);
getch();
+
int diagonalFsum(int MRCJ@SR2JGS, int r, int c)
{int i, L, s$5;
for (i$5; i
-
8/9/2019 c Programs_with Answers
22/40
&11111 rogram to ort an *rra7 using election ort 11111&
#include
void selectionFsort();
int aRB5S, n;
main()
{int i;
printf("\nnter si8e of an arra7! ");
scanf("d", n);
printf("\nnter elements of an arra7!\n");
for(i$5; i
-
8/9/2019 c Programs_with Answers
23/40
for (i$5; i Q)
printf("\n-tem doesnot eMist.");
getch();
+
STRINGS
&1111 rogram to 2oncatenate 9o trings using strcat() 1111&
#include
#include
main(){
char sR5S, sR5S;
printf("\nnter first string! ");
gets(s);
printf("\nnter second string! ");
gets(s);
strcat(s, s);
printf("\nhe concatenated string is! s", s);
getch();
+
&1 rogram to 2oncatenate 9o trings 9ithout using strcat() 1&
#include
#include
#include
main()
{
char stringR'5S, stringR5S;
int i, length$5, temp;
printf("nter the 3alue of tring! \n");gets(string);
printf("\nnter the 3alue of tring! \n");
gets(string);
for(i$5; stringRiSN$H\5H; i%%)
length%%;
temp $ length;
for(i$5; stringRiSN$H\5H; i%%)
-
8/9/2019 c Programs_with Answers
24/40
{
stringRtempS $ stringRiS;
temp%%;
+
stringRtempS $ H\5H;
printf("\nhe concatenated string is!\n");puts(string);
getch();
+
&111 rogram to 2ompare 9o trings using strcmp() 111&
#include
#include
main()
{
char sR5S, sR5S;
int result;printf("\nnter first string! ");
gets(s);
printf("\nnter second string! ");
gets(s);
result $ strcmp(s, s);
if (result $$ 5)
printf("\n?oth strings are e0ual");
else
printf("\n?oth strings are not e0ual");
getch();+
&11 rogram to 2ompare 9o trings @ithout using strcmp() 11&
#include
main()
{
char stringRBS,stringRBS;
int i,temp $ 5;
printf("nter the string value!\n");
gets(string);
printf("\nnter the tring value!\n");
gets(string);for(i$5; stringRiSN$H\5H; i%%)
{
if(stringRiS $$ stringRiS)
temp $ ;
else
temp $ 5;
+
-
8/9/2019 c Programs_with Answers
25/40
if(temp $$ )
printf("?oth strings are same.");
else
printf("?oth string not same.");
getch();
+
&111 rogram to 2op7 tring using strcp7() 111&
#include
#include
main()
{
char sR5S, sR5S;
printf("\nnter string into s! ");
gets(s);
strcp7(s, s);
printf("\ns! s", s);getch();
+
&1 rogram to 2op7 one tring to *nother @ithout Ksing strcp7() 1&
#include
#include
#include
main()
{
char stringR5S, stringR5S;int i;
printf("nter the value of C-EI! \n");
gets(string);
for(i$5; stringRiSN$H\5H; i%%)
stringRiS$stringRiS;
stringRiS$H\5H;
printf("\nhe value of C-EI is!\n");
puts(string);
getch();
+
&111 rogram to Dind Gength of a tring using strlen() 111&
#include
#include
main()
{
char sR5S;
int len;
-
8/9/2019 c Programs_with Answers
26/40
printf("\nnter an7 string! ");
gets(s);
len $ strlen(s);
printf("\nGength of string! d", len);
getch();
+
&111 rogram to Ceverse a tring using strrev() 111&
#include
#include
main()
{
char sR5S;
printf("\nnter an7 string! ");
gets(s);
strrev(s);
printf("\nCeverse of string! s", s);getch();
+
&1 rogram to Ceverse a tring 9ithout using strrev() 1&
#include
#include
#include
main()
{
char stringR5S, stringR5S;int i, length;
printf("nter an7 string!\n");
gets(string);
length $ strlen(string):;
for(i$5; stringRiSN$H\5H; i%%)
{
stringRlengthS$stringRiS;
length::;
+
stringRlengthS$H\5H;
printf("\nhe Ceverse of string is!\n");puts(string);
getch();
+
&111 rogram to -nput&Jutput trings using 2haracter Dunctions 111&
#include
main()
-
8/9/2019 c Programs_with Answers
27/40
{
char nameR5S, ch $ H\5H;
int i$5;
printf("\nnter 7our name! ");
9hile (ch N$ H\rH)
{ch $ getche();
nameRiS $ ch;
i%%;
+
printf("\nEame! ");
for (i$5; nameRiSN$H\5H; i%%)
putchar(nameRiS);
getch();
+
&11 rogram to -nput&Jutput trings using gets() and puts() 11include
main()
{
char nameR5S;
puts("\nnter 7our name! ");
gets(name);
puts("\nEame! ");
puts(name);
getch();
+
&11 rogram to -nput&Jutput trings using printf() and scanf() 11&
#include
main()
{
char nameR5S;
printf("\nnter 7our name! ");
scanf("s", name);
printf("\nEame! s", name);
getch();
+
&111 rogram to Dind Gength of a tring @ithout using strlen() 111&
#include
main()
{
char strR5S;
int i $ 5;
-
8/9/2019 c Programs_with Answers
28/40
printf("\nnter an7 string! ");
gets(str);
9hile (strRiS N$ H\5H)
i%%;
printf("\nGength of string! d", i);
getch();+
&111 rogram to Dind @hether a tring is alindrome or Eot 111&
#include
#include
main()
{
char sR5S, sR5S;
int result;
printf("\nnter an7 string! ");
gets(s);strcp7(s, s);
strrev(s);
result $ strcmp(s, s);
if(result $$ 5)
printf("\n-t is a palindrome string");
else
printf("\n-t is not a palindrome string");
getch();
+
&1 rogram to Dind @hether a tring is alindrome or Eot 9ithout
using tring Dunctions 1&
#include
#include
main()
{
char sR5S;
int i, L, len$5, flag$5;
printf("\nnter an7 string! ");
gets(s);
for (i$5; sRiSN$H\5H; i%%)len%%;
i $ 5;
L $ len:;
9hile (i < len)
{
if (sRiS N$ sRLS)
{
-
8/9/2019 c Programs_with Answers
29/40
flag $ ;
brea4;
+
i%%;
L::;
+if (flag $$ 5)
printf("\ntring is palindrome");
else
printf("\ntring is not palindrome");
getch();
+
F6N$TIONS
&1111 rogram to ho9 2all b7 3alue 1111include
s9ap (int, int);
main()
{
int a, b;
printf("\nnter value of a b! ");
scanf("d d", a, b);
printf("\n?efore 9apping!\n");
printf("\na $ d\n\nb $ d\n", a, b);
s9ap(a, b);printf("\n*fter 9apping!\n");
printf("\na $ d\n\nb $ d", a, b);
getch();
+
s9ap (int a, int b)
{
int temp;
temp $ a;
a $ b;
b $ temp;
+
&1111 rogram to ho9 2all b7 Ceference 1111&
#include
s9ap (int 1, int 1);
main()
{
int a, b;
-
8/9/2019 c Programs_with Answers
30/40
printf("\nnter value of a b! ");
scanf("d d", a, b);
printf("\n?efore 9apping!\n");
printf("\na $ d\n\nb $ d\n", a, b);
s9ap(a, b);
printf("\n*fter 9apping!\n");printf("\na $ d\n\nb $ d", a, b);
getch();
+
s9ap (int 1M, int 17)
{
int temp;
temp $ 1M;
1M $ 17;
17 $ temp;
+
RE$6RSION
&11 rogram to Dind Dactorial of a Eumber using Cecursion 11&
#include
long fact(int);
main()
{
int n;
long f;
printf("\nnter number to find factorial! ");scanf("d", n);
f $ fact(n);
printf("\nDactorial! ld", f);
getch();
+
long fact(int n)
{
int m;
if (n $$ )
return n;
else{
m $ n 1 fact(n:);
return m;
+
+
&111 rogram to rint Dibonacci eries using Cecursion 111&
-
8/9/2019 c Programs_with Answers
31/40
#include
int fibbo(int, int, int, int);
main()
{
int n, f, M$5, 7$, i$';
printf("\nnter value of n! ");scanf("d", n);
printf("\nd\td", M, 7);
fibbo(M, 7, n, i);
getch();
+
fibbo(int M, int 7, int n, int i)
{
int 8;
if (i
-
8/9/2019 c Programs_with Answers
32/40
POINTERS
&1111 rogram to -llustrate the 2oncept of ointers 1111&
#include
main(){
int a $ 5;
int 1p;
p $ a;
printf("\n*ddress of a! u", a);
printf("\n\n*ddress of a! u", p);
printf("\n\n*ddress of p! u", p);
printf("\n\n3alue of p! d", p);
printf("\n\n3alue of a! d", a);
printf("\n\n3alue of a! d", 1(a));
printf("\n\n3alue of a! d", 1p);getch();
+
STR6$T6RES
&11 rogram to -mplement tructure 11&
#include
struct student
{
char nameR5S;int rollno;
float mar4s;
+;
main()
{
struct student s $ {"abc", , /B5+;
struct student s;
printf("nter student Eame, Collno, =ar4s!\n");
scanf("sif", s.name, s.rollno, s.mar4s);
printf("\ntudent Eame\tCollno\t=ar4s\n");
printf("s\ti\tf", s.name, s.rollno, s.mar4s);
printf("\n");
printf("s\ti\tf",s.name,s.rollno,s.mar4s);
getch();
+
&11 rogram to -mplement tructure 9ith *rra7 11&
-
8/9/2019 c Programs_with Answers
33/40
#include
struct student
{
char nameR5S;
int rollno;
float mar4s;+;
struct student sR'S;
main()
{
int i;
printf("nter Eame, CollEo, =ar4s!\n");
for(i$5; i
-
8/9/2019 c Programs_with Answers
34/40
{
char nameR5S;
int rollno;
float mar4s;
+;
void sho9(struct student 1);main()
{
struct student s $ {"M78", , /B5+;
sho9(s);
getch();
+
void sho9(struct student 1ptr)
{
printf("tudent name\tCollno\t=ar4s\n");
printf("s\t\ti\tf",ptr:>name,ptr:>rollno,ptr:>mar4s);
+
SEAR$/ING AN7 SORTING
&1 rogram to earch an lement in the *rra7 using Ginear earch 1&
#include
main()
{
int aR5S, i, item;
printf("\nnter elements of an arra7!\n");
for (i$5; i
-
8/9/2019 c Programs_with Answers
35/40
main()
{
printf("\nnter si8e of an arra7! ");
scanf("d", n);
printf("\nnter elements of an arra7 in sorted form!\n");
for(i$5; i
-
8/9/2019 c Programs_with Answers
36/40
printf("\nd", aRiS);
getch();
+
void bubbleFsort()
{
int L, 4, temp;for(L$5; L
-
8/9/2019 c Programs_with Answers
37/40
temp $ aRiS;
aRiS $ aRminS;
aRminS $ temp;
+
+
FILE /AN7LING
&111 rogram to @rite and Cead a 2haracter from a Dile 111&
#include
main()
{
D-G 1fptr;
char ch;
printf("\nnter an7 character! ");
scanf("c", ch);
fptr $ fopen("abc.tMt", "9");putc(ch, fptr);
fclose(fptr);
fptr $ fopen("abc.tMt", "r");
ch $ getc(fptr);
printf("\n2haracter! c", ch);
getch();
+
&111 rogram to @rite and Cead an -nteger from a Dile 111&
#include main()
{
D-G 1fptr;
int a;
printf("\nnter an7 number! ");
scanf("d", a);
fptr $ fopen("abc.tMt", "9");
putc(a, fptr);
fclose(fptr);
fptr $ fopen("abc.tMt", "r");
a $ getc(fptr);printf("\n2haracter! d", a);
getch();
+
&111 rogram to @rite and Cead a tring from a Dile 1&
#include
#define -T B5
-
8/9/2019 c Programs_with Answers
38/40
main()
{
D-G 1fptr;
char sR-TS, sR-TS;
printf("\nnter an7 string! ");
gets(s);fptr $ fopen("abc.tMt", "9");
fputs(s, fptr);
fclose(fptr);
fptr $ fopen("abc.tMt", "r");
fgets(s, -T, fptr);
printf("\nUou entered!");
puts(s);
getch();
+
&111 rogram to @rite Aata into a Dile using fprintf() 111include
main()
{
D-G 1fptr;
int rollno;
char nameR5S;
float mar4s;
printf("\nnter roll no.! ");
scanf("d", rollno);
printf("\nnter name! ");fflush(stdin);
gets(name);
printf("\nnter mar4s! ");
scanf("f", mar4s);
fptr $ fopen("abc.tMt", "9");
fprintf(fptr, "d s f", rollno, name, mar4s);
fclose(fptr);
getch();
+
&111 rogram to Cead Aata from Dile using fscanf() 111include
main()
{
D-G 1fptr;
int rollno;
char nameR5S;
float mar4s;
-
8/9/2019 c Programs_with Answers
39/40
fptr $ fopen("abc.tMt", "r");
fscanf(fptr, "d s f", rollno, name, mar4s);
printf("\nColl Eo.! d", rollno);
printf("\n\nEame $ s", name);
printf("\n\n=*r4s $ .f", mar4s);
fclose(fptr);getch();
+
&111 rogram to 2op7 2ontents of Jne Dile to *nother 111&
#include
main()
{
D-G 1fp, 1fp;
char ch;
fp $ fopen("abc.tMt", "r");
fp $ fopen("M78.tMt", "9");9hile((ch $ getc(fp)) N$ JD)
putc(ch, fp);
fclose(fp);
fclose(fp);
getch();
+
&111 rogram to rint a Dile and 2ount Eumber of 2haracters 111&
#include
#includemain()
{
D-G 1fp;
char c;
int noc$5, nol$5, nob$5;
fp $ fopen("DileAispla7.2","r");
if (fp $$ EKGG)
{
printf("Dile canHt be opened");
return;
+9hile ((c $ fgetc(fp)) N$ JD)
{
putchar(c);
noc%%;
s9itch (c)
{
case H\tH!
-
8/9/2019 c Programs_with Answers
40/40
case H H!
nob%%;
brea4;
case H\nH!
nol%%;
+sleep(5);
+
fclose(fp);
printf("\n\n\nEo. of characters $ d",noc);
printf("\n\nEo. of lines $ d",nol);
printf("\n\nEo. of 9hite spaces $ d",nob);
getch();
+