Вы находитесь на странице: 1из 23

# 1. WRITE A PROGRAM TO FIND AREA & CIRCUMFERENCE OF A CIRCLE.

#include <stdio.h>
#include <conio.h>
#define PI 3.14
void main()
{
float circum,r,area;
rintf(!enter the value of radius " !);
scanf(!#f!,\$r);
area % PI & r & r;
circum % ' & PI & r;
rintf(!area of (iven circle " #f)n !,area);
rintf(!circumference of (iven circle is "#f!,circum);
(etch();
*
+he main inut for calculatin( area and erimeter of a circle is radius of the circle.,e use the datat-e float to
store the radius of the circle.+he formula to find the area of the circle is 3.14&r&r and the formula to find
erimeter of the circle is '&3.14&radius..fter calculatin( the area and erimeter /e need to disla- the results
to the user.
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
rintf(!)n2nter the radius of the circle"!);
rintf(!)n.rea of the (iven cirlce is"#f!,area);
rintf(!)n)nPerimeter of the (iven circle is"#f!,erimeter);
(etch();
*
2. C Program to Simulate a Simple Calulator
+his 3 Pro(ram simulates a simle calculator. +his ro(ram erforms arithmatic oerations li4e addtion,
su1raction, multilication \$ division. .ssume that the ' num1ers a \$ 1 are (iven. 5or the (iven element /e
need to erform addition, su1traction, multilication \$ division.
0& 3 ro(ram to simulate a simle calculator to erform arithmetic oerations li4e addition, su1traction,
multilication and division &0
#include <stdio.h>
void main()
{
char oerator;
float num1, num', result;
rintf(!6imulation of a 6imle 3alculator)n!);
rintf(!&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&)n!);
rintf(!2nter t/o num1ers )n!);
scanf(!#f #f!, \$num1, \$num');
fflush(stdin);
rintf(!2nter the oerator 78,9,&,0: )n!);
scanf(!#s!, \$oerator);
s/itch(oerator)
{
case ;8;" result % num1 8 num';
1rea4;
case ;9;" result % num1 9 num';
1rea4;
case ;&;" result % num1 & num';
1rea4;
case ;0;" result % num1 0 num';
1rea4;
default " rintf(!2rror in oerationn!);
1rea4;
*
rintf(!)n #<.'f #c #<.'f % #<.'f)n!, num1, oerator, num', result);
*
= cc (m.c
= a.out>
6imulation of a 6imle 3alculator
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
2nter t/o num1ers
' 3
2nter the oerator 78,9,&,0:
8
'.?? 8 3.?? % <.??
= a.out
6imulation of a 6imle 3alculator
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
2nter t/o num1ers
<? 4?
2nter the oerator 78,9,&,0:
&
<?.?? & 4?.?? % '???.??
= a.out
6imulation of a 6imle 3alculator
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
2nter t/o num1ers
<?? 1@
2nter the oerator 78,9,&,0:
0
<??.?? 0 1@.?? % 'A.41
= a.out
6imulation of a 6imle 3alculator
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
2nter t/o num1ers
B<??? 4@??
2nter the oerator 78,9,&,0:
9
B<???.?? 9 4@??.?? % B?3??.??
3. C Program to Fi!" t#e Large\$t Num%er Amo!g T#ree Num%er\$
In this ro(ram, user is as4ed to enter three num1ers and this ro(ram /ill find the lar(est num1er amon( three
num1ers entered 1- user. +his ro(ram can 1e solved in more than one /a-.
6ource 3ode 1
0& 3 ro(ram to find lar(est num1er usin( if statement onl- &0
#include <stdio.h>
int main(){
float a, 1, c;
rintf(!2nter three num1ers" !);
scanf(!#f #f #f!, \$a, \$1, \$c);
if(a>%1 \$\$ a>%c)
rintf(!Car(est num1er % #.'f!, a);
if(1>%a \$\$ 1>%c)
rintf(!Car(est num1er % #.'f!, 1);
if(c>%a \$\$ c>%1)
rintf(!Car(est num1er % #.'f!, c);
return ?;
*
6ource 3ode '
0& 3 ro(ram to find lar(est num1er usin( if...else statement &0
#include <stdio.h>
int main(){
float a, 1, c;
rintf(!2nter three num1ers" !);
scanf(!#f #f #f!, \$a, \$1, \$c);
if (a>%1)
{
if(a>%c)
rintf(!Car(est num1er % #.'f!,a);
else
rintf(!Car(est num1er % #.'f!,c);
*
else
{
if(1>%c)
rintf(!Car(est num1er % #.'f!,1);
else
rintf(!Car(est num1er % #.'f!,c);
*
return ?;
*
6ource 3ode 3
0& 3 Pro(ram to find lar(est num1er usin( nested if...else statement &0
#include <stdio.h>
int main(){
float a, 1, c;
rintf(!2nter three num1ers" !);
scanf(!#f #f #f!, \$a, \$1, \$c);
if(a>%1 \$\$ a>%c)
rintf(!Car(est num1er % #.'f!, a);
else if(1>%a \$\$ 1>%c)
rintf(!Car(est num1er % #.'f!, 1);
else
rintf(!Car(est num1er % #.'f!, c);
return ?;
*
+hou(h the techniDue to solve this ro1lem is different in these three eEamles, outut of all these ro(ram is
same.
2nter three num1ers" 1'.'
13.4<'
1?.1A3
Car(est num1er % 13.4<
4. C Program to C#e& W#et#er a Num%er i\$ E'e! or O""
Fum1ers erfectl- divisi1le 1- ' are 4no/n even num1ers and num1ers /hich are not divisi1le 1- ' are called
odd num1ers.+his ro(ram ta4es an inte(er from user and chec4s /hether that num1er is even or odd and
disla-s the result.
#include <stdio.h>
int main(){
int num;
rintf(!2nter an inte(er -ou /ant to chec4" !);
scanf(!#d!,\$num);
if((num#')%%?) 0& 3hec4in( /hether remainder is ? or not. &0
rintf(!#d is even.!,num);
else
rintf(!#d is odd.!,num);
return ?;
*
Gutut 1
2nter an inte(er -ou /ant to chec4" '<
'< is odd.
Gutut '
2nter an inte(er -ou /ant to chec4" 1'
1' is even.
In this ro(ram, user is as4ed to enter an inte(er /hich is stored in varia1le num. +hen, the remainder is found
/hen that num1er is divided 1- ' and chec4ed /hether remainder is ? or not. If remainder is ? then, that
num1er is even other/ise that num1er is odd. +his tas4 is erformed usin( if...else statement in 3
ro(rammin( and the result is disla-ed accordin(l-.+his ro(ram also can 1e solved usin( conditional
oerator7 H" : /hich is the shorthand notation for if...else statement.
5. C program to #e& (#et#er a! i!teger i\$ o"" or e'e! u\$i!g o!"itio!al operator
#include <stdio.h>
int main(){
int num;
rintf(!2nter an inte(er -ou /ant to chec4" !);
scanf(!#d!,\$num);
((num#')%%?) H rintf(!#d is even.!,num) " rintf(!#d is odd.!,num);
return ?;
*
6. C Program to Fi!" Fatorial o) a Num%er
5or an- ositive num1er n, its factorial is (iven 1-"
factorial % 1&'&3&4....n
If a num1er is ne(ative, factorial does not eEist and factorial of ? is 1.
+his ro(ram ta4es an inte(er from a user. If user enters ne(ative inte(er, this ro(ram /ill disla- error
messa(e and if user enters non9ne(ative inte(er, this ro(ram /ill disla- the factorial of that num1er.
#include <stdio.h>
int main()
{
int n, count;
unsi(ned lon( lon( int factorial%1;
rintf(!2nter an inte(er" !);
scanf(!#d!,\$n);
if ( n< ?)
rintf(!2rrorIII 5actorial of ne(ative num1er doesn;t eEist.!);
else
{
for(count%1;count<%n;88count) 0& for loo terminates if count>n &0
{
factorial&%count; 0& factorial%factorial&count &0
*
rintf(!5actorial % #lu!,factorial);
*
return ?;
*
Gutut 1
2nter an inte(er" 9<
2rrorIII 5actorial of ne(ative num1er doesn;t eEist.
Gutut '
2nter an inte(er" 1?
5actorial % 3B'>>??
Jere the t-e of factorial varia1le is declared as" unsi(ned lon( lon(. It is 1ecause, the factorial is al/a-s
ositive, so unsi(ned 4e-/ord is used and the factorial of a num1er can 1e rett- lar(e. 5or eEamle" the
factorial of 1? is 3B'>>?? thus, lon( lon( 4e-/ord is used. +o solve this ro(ram, /e have used for loo 1ut,
this ro(ram also can 1e solved in similar /a- usin( /hile loo.
7. " Program to )i!" a!" \$um a!" a'erage o) ! !um%er\$ (#ere ! i\$ t#e u\$er "e)i!e" !o.
#include<stdio.h>
void main()
{
int no7'?:, sum % ?, av( % ?, i, n;
rintf(!Jo/ man- num1ers do /ant to add" )n!);
scanf(!#d!,\$n);
rintf(!2nter the num1ers" )n!);
for(i%?,i<n;i88)
scanf(!#d!,\$no7i:);
sum % sum 8 no7i:;
av( % sum 0n;
rintf(!+he sum of #d num1er is"#d!,n,sum);
rintf(!+he avera(e of #d num1ers is;#d!,n,av();
(etch();
*
8. C Program to Co!'ert temperature )rom "egree e!tigra"e to Fa#re!#eit
#include<stdio.h>
#include<conio.h>
void main()
{
float celsius,fahrenheit;
clrscr();
rintf(!n2nter tem in 3elsius " !);
scanf(!#f!,\$celsius);
fahrenheit % (1.> & celsius) 8 3';
rintf(!n+emerature in 5ahrenheit " #f !,fahrenheit);
(etch();
*
Gutut "
2nter tem in 3elsius " 3'
+emerature in 5ahrenheit " >A.<AAA>
nclude<stdio.h>
int main()
{
int fahr,cel,choice;
rintf(!,hat do -ou /ant to doH(10'))n1. fahrenheit to 3elsius)n'. 3elsius to 5ahrenheit)n!);
scanf(!#d!,\$choice);
s/itch(choice)
{
case 1"
rintf(!2nter fahrenheit value)n!);
scanf(!#d!,\$fahr);
cel%(fahr93')&<0A;
rintf(!3elsius of #d de(rees fahrenheit is #d de(rees celsius)n)n!,fahr,cel);
case '"
rintf(!2nter fahrenheit value)n!);
scanf(!#d!,\$cel);
fahr%cel&<0A 8 3';
rintf(!5ahrenheit of #d de(rees 3elsius is #d de(rees fahrenheit)n)n!,cel,fahr);
*
*
9. C program to pri!t I!teger\$ )rom *+*,,-E'e! !um%er\$ )rom *+*,,-O"" !um%er\$ )rom *+*,,
#include<stdio.h>
main()
{
int a,1,c;
rintf(!)n)n)t a.Inte(ers from 191?? )n)n!);
for (a%1;a<%1??;a88)
{
rintf(! #d!,a);
*
rintf(!)n)n)t 1.2ven num1ers from 191??)n)n!);
for (1%1;1<%1??;188)
{ if (1#'%%?)
rintf(! #d!,1);
*
rintf(!)n)n)t c.Gdd num1ers from 191??)n)n!);
for (c%1;c<%1??;c88)
{ if (c#'I%?)
rintf(!#d !,c);
*
rintf(!)n!);
*
10. WRITE A PROGRAM TO CON.ERT DA/S TO MONT0S AND DA/S.
main ()
{
int months, da-s ;

rintf(!2nter da-s)n!) ;
scanf(!#d!, \$da-s) ;
months % da-s 0 3? ;
da-s % da-s # 3? ;
rintf(!Konths % #d La-s % #d!, months, da-s) ;
*

Gutut
2nter da-s
'B<
Konths % > La-s % '<

2nter da-s
3B4
Konths % 1' La-s % 4

2nter da-s
4<
Konths % 1 La-s % 1<
11. Write a C program to alulate \$ale\$ma!1\$ \$alar2.
#include<stdio.h>
#include<conio.h>
#define MN6.C 13<?.??
#define MGFO6 1>?.??
#define 3GKKI66IGF ?.?3
void main()
{
int Dt-;
float (Nsal,rice;
float 1onus,comm;
rintf(P6ecif- num1er of items sold \$ the rice")nQ);
rintf(P#d #f!,\$Dt-,\$rice);
1onus%MGFO6&Dt-;
comm%3GKKI66IGF&Dt-&rice;
(N sal%M N 6.C 81onus8comm;
rintf(P)nQ);
rintf(PMonus %#B.'f)nQ,1onus);
rintf(P3ommission %#B.'f)nQ,comm);
rintf(PRross salar- %#B.'f)nQ,(Nsal);
(etch();
*

GO+PO+"
6ecif- num1er of items sold \$ the rice" @ '?<<?
Monus %1'B?
3ommission %431<.<
Rross salar- %BA'<.<
12. Fi%o!ai \$erie\$ i! programmi!g3
c ro(ram for 5i1onacci series /ithout and /ith recursion. Osin( the code 1elo/ -ou can rint as man-
num1ers of terms of series as desired. Fum1ers of 5i1onacci seDuence are 4no/n as 5i1onacci num1ers. 5irst
fe/ num1ers of series are ?, 1, 1, ', 3, <, > etc, 2Ecet first t/o terms in seDuence ever- other term is the sum
of t/o revious terms, 5or eEamle > % 3 8 < (addition of 3, <). +his seDuence has man- alications in
mathematics and 3omuter 6cience.
#include<stdio.h>
int main()
{
int n, first % ?, second % 1, neEt, c;
rintf(!2nter the num1er of terms)n!);
scanf(!#d!,\$n);
rintf(!5irst #d terms of 5i1onacci series are "9)n!,n);
for ( c % ? ; c < n ; c88 )
{
if ( c <% 1 )
neEt % c;
else
{
neEt % first 8 second;
first % second;
second % neEt;
*
rintf(!#d)n!,neEt);
*
return ?;
*
13. C program to pri!t patter!\$ o) \$tar\$
+hese ro(ram rints various different atterns of num1ers and stars. +hese codes illustrate ho/ to create
various atterns usin( c ro(rammin(. Kost of these c ro(rams involve usa(e of nested loos and sace. .
attern of num1ers, star or characters is a /a- of arran(in( these in some lo(ical manner or the- ma- form a
seDuence. 6ome of these atterns are trian(les /hich have secial imortance in mathematics. 6ome atterns
are s-mmetrical /hile other are not. Please see the comlete a(e and loo4 at comments for man- different
atterns.
&
&&&
&&&&&
&&&&&&&
&&&&&&&&&
,e have sho/n five ro/s a1ove, in the ro(ram -ou /ill 1e as4ed to enter the num1ers of ro/s -ou /ant to
rint in the -ramid of stars.
#include <stdio.h>
int main()
{
int ro/, c, n, tem;
rintf(!2nter the num1er of ro/s in -ramid of stars -ou /ish to see !);
scanf(!#d!,\$n);
tem % n;
for ( ro/ % 1 ; ro/ <% n ; ro/88 )
{
for ( c % 1 ; c < tem ; c88 )
rintf(! !);
tem99;
for ( c % 1 ; c <% '&ro/ 9 1 ; c88 )
rintf(!&!);
rintf(!)n!);
*
return ?;
*
14. Write a program to ge!erate )ollo(i!g )igure o) ! li!e\$. I!put ! )rom &e2%oar".
NNNNNN?
NNNN1 ? 1
NN' 1 ? 1 '
3 ' 1 ? 1 ' 3 consider sace in lace of line.
#include<stdio.h>
#include<conio.h>
void main()
{
int i,n,S;
clrscr();
rintf(!)n Please Rive +he Talue of F" !);
scanf(!#d!,\$n);
for(i%?;i<n;i88)
{
for(S%?;S<n9i;S88)
rintf(! !);
for(S%i;S>%?;S99)
rintf(! #d!,S);
for(S%1;S<%i;S88)
rintf(! #d!,S);
rintf(!)n!);
*
(etch();
*
&&&&&&&&&&&&&&&&&&&&& GO+PO+ &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
Please Rive +he Talue of F" A
?
1 ? 1
' 1 ? 1 '
3 ' 1 ? 1 ' 3
4 3 ' 1 ? 1 ' 3 4
< 4 3 ' 1 ? 1 ' 3 4 <
B < 4 3 ' 1 ? 1 ' 3 4 < B
@ B < 4 3 ' 1 ? 1 ' 3 4 < B @
> @ B < 4 3 ' 1 ? 1 ' 3 4 < B @ >
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
15. C Program to Compute t#e Sum o) Digit\$ i! a gi'e! I!teger
+his 3 Pro(ram comutes the sum of di(its in a (iven inte(er. +his ro(ram m accets inte(er. +hen adds all
the di(its of a (iven inte(er, that 1ecomes the sum of di(its of inte(er.
#include <stdio.h>
void main()
{
lon( num, tem, di(it, sum % ?;
rintf(!2nter the num1er )n!);
scanf(!#ld!, \$num);
tem % num;
/hile (num > ?)
{
di(it % num # 1?;
sum % sum 8 di(it;
num 0% 1?;
*
rintf(!Riven num1er % #ld)n!, tem);
rintf(!6um of the di(its #ld % #ld)n!, tem, sum);
*
= cc (m>1.c
= a.out
2nter the num1er
3??
Riven num1er % 3??
6um of the di(its 3?? % 3

= a.out
2nter the num1er
1B@>A
Riven num1er % 1B@>A
6um of the di(its 1B@>A % 31
16. C program to re'er\$e a !um%er
3 Pro(ram to reverse a num1er "9 +his ro(ram reverse the num1er entered 1- the user and then rints the
reversed num1er on the screen. 5or eEamle if user enter 1'3 as inut then 3'1 is rinted as outut. In our
ro(ram /e use modulus(#) oerator to o1tain the di(its of a num1er. +o invert num1er loo4 at it and /rite it
from oosite direction or the outut of code is a num1er o1tained 1- /ritin( ori(inal num1er from ri(ht to
left. +o reverse or invert lar(e num1ers use lon( data t-e or lon( lon( data t-e if -our comiler suorts it, if
-ou still have lar(e num1ers then use strin(s or other data structure.
#include <stdio.h>
int main()
{
int n, reverse % ?;

rintf(!2nter a num1er to reverse)n!);
scanf(!#d!,\$n);
/hile (n I% ?)
{
reverse % reverse & 1?;
reverse % reverse 8 n#1?;
n % n01?;
*
rintf(!Ueverse of entered num1er is % #d)n!, reverse);
return ?;
*
17. C Program to Sear# a! eleme!t i! Arra2

#include<stdio.h>
#include<conio.h>
void main()
{
int a73?:,E,n,i;
0&a9 for storin( of data, E 9element to 1e searched, n 9no of elements in the arra-, i 9scannin( of the arra-&0
rintf(!n2nter no of elements "!);
scanf(!#d!,\$n);
0& Ueadin( values into .rra- &0
rintf(!n2nter the values "!);
for(i%?;i < n;i88)
scanf(!#d!,\$a7i:);
0& read the element to 1e searched &0
rintf(!n2nter the elements to 1e searched!);
scanf(!#d!,\$E);
0& search the element &0
i%?; 0& search starts from the Veroth location &0
/hile(i < n \$\$ EI%a7i:)
i88;
0& search until the element is not found i.e. EI%a7i: search until the element could still 1e found i.e. i n &0
if(i < n) 0& 2lement is found &0
rintf(!found at the location %#d!,i81);
else
(etch();
*
18. C Program to Sort t#e Arra2 i! a! A\$e!"i!g Or"er
0& 3 ro(ram to accet F num1ers and arran(e them in an ascendin( order &0
#include <stdio.h>
void main()
{
int i, S, a, n, num1er73?:;
rintf(!2nter the value of F )n!);
scanf(!#d!, \$n);
rintf(!2nter the num1ers )n!);
for (i % ?; i < n; 88i)
scanf(!#d!, \$num1er7i:);
for (i % ?; i < n; 88i)
{
for (S % i 8 1; S < n; 88S)
{
if (num1er7i: > num1er7S:)
{
a % num1er7i:;
num1er7i: % num1er7S:;
num1er7S: % a;
*
*
*
rintf(!+he num1ers arran(ed in ascendin( order are (iven 1elo/ )n!);
for (i % ?; i < n; 88i)
rintf(!#d)n!, num1er7i:);
*
2nter the value of F B
2nter the num1ers
3
@>
A?
4<B
@>?
'??
+he num1ers arran(ed in ascendin( order are (iven 1elo/
3
@>
A?
'??
4<B
@>?
19. C Program to I!\$ert a! eleme!t i! a! Arra2
#include<stdio.h>
int main()
{
int arr73?:,element,num,i,location;
rintf(!n 2nter no of elements "!);
scanf(!#d!,\$num);
for(i%? ; i < num ; i88)
scanf(!#d!,\$arr7i:);
rintf(!n 2nter the element to 1e inserted "!);
scanf(!#d!,\$element);
rintf(!n 2nter the location!);
scanf(!#d!,\$location);
0& create sace at the secified location &0
for(i % num ;i >% location ; i99)
arr7i: % arr7i91:;
num88;
arr7location91: % element;
0& Print out the Uesult of Insertion &0
for(i % ? ;i < num ;i88)
rintf(!n #d!,arr7i:);
return(?);
*
Gutut of the Pro(ram "
2nter no of elements " <
1 ' 3 4 <
2nter the element to 1e inserted " B
2nter the location " '
1 B ' 3 4 <
20. C program to "elete a! eleme!t )rom t#e arra2
Lata structureWs 3 ro(ram to delete an element from the arra-. 2nter the arra- siVe and then itWs elements.
.fter this enter the location of the element -ou /ant to delete and the item /ill 1e deleted. Melo/ is the code of
the a1ove ro(ram
0& Pro(ram in 3 to delete an element from the arra- &0
#include<stdio.h>
void main()
{
int a7'?:, n, loc, item,i;

rintf(!)n2nter siVe of an arra-" !);
scanf(!#d!, \$n);
rintf(!)n2nter elements of an arra-")n!);
for(i%?; i<n; i88)
{
scanf(!#d!, \$a7i:);
*

rintf(!)n2nter location of deletion" !);
scanf(!#d!, \$loc);
item % a7loc91:;
for(i%loc91; i<n; i88)
{
a7i: % a7i81:;
*
n99;
rintf(!)nI+2K deleted" #d!, item);

rintf(!)n)n.fter deletion")n!);
for(i%?; i<n; i88)
{
rintf(!)n#d!, a7i:);
*
(etch();
*
Inut 9
2nter arra- siVe X <
2nter arra- elements X < ' 1< <? '3
2nter Cocation of element to 1e deleted X 4
Gutut9
.rra- after deletion is 9 < ' 1< '3
21. C Program to A"" T(o Matrie\$ u\$i!g 4D Arra2
.s a 3 Pro(rammer ,-ou need to deal /ith data structures li4es an arra-.Pro(rammers often needs to use a
Kultidimensional arra- in the form of matriE to do mathematical 3alculations.+he Pro(ram 1elo/
Lemonstrates the addition of an- t/o user secified matrices to(ether and o1tain the result in a ne/ KatriE.
+he 3omlete 6ource 3ode to imlement matriE addition 1- usin( 'L Inte(er .rra- is (iven Melo/.
#include<stdio.h>
#include<conio.h>
void main()
{
int .7<?:7<?:,M7<?:7<?:,37<?:7<?:;
int i,S,r1,r',c1,c';
clrscr();
rintf(! & KatriE .ddition& !);
rintf(!)n2nter +he 6iVe Gf 5irst KatriE"!);
scanf(!#d#d!,\$r1,\$c1);
rintf(!)n2nter +he 2lements Gf 5irst KatriE"!);
for(i%?;i<r1;i88)
for(S%?;S<c1;S88)
scanf(!#d!,\$.7i:7S:);
rintf(!)n)n2nter +he 6iVe Gf 6econd KatriE"!);
scanf(!#d#d!,\$r',\$c');
rintf(!)n2nter +he 2lements Gf 6econd KatriE"!);
for(i%?;i<r';i88)
for(S%?;S<c';S88)
scanf(!#d!,\$M7i:7S:);
if(r1I%r'YYc1I%c')
{
rintf(!)n)n+he KatriE 3ant Me .ddedIII!);
(etch();
eEit(?);
*
for(i%?;i<r1;i88)
for(S%?;S<c1;S88)
37i:7S:%.7i:7S:8M7i:7S:;
rintf(!)n)n+he 6um Gf Katrices Is"!);
for(i%?;i<r1;i88)
{ rintf(!)n!);
for(S%?;S<c1;S88)
rintf(!#d !,37i:7S:);
*
(etch();
*
22. Multipliatio! o) t(o matrie\$ u\$i!g t(o "ime!\$io!al arra2 i! C5C66
#include<stdio.h>
int main()
{
int r1, c1, r', c', matriE171?:71?:, matriE'71?:71?:, result71?:71?:;
int i, S, 4;
rintf(!2nter the ro/ and column of the first matriE" !);
scanf(!#d#d!,\$r1,\$c1);
rintf(!2nter the ro/ and column of the second matriE" !);
scanf(!#d#d!,\$r',\$c');
if(c1 I% r'){
rintf(!KatriE multilication imossi1le!);
*
rintf(!2nter the first matriE" )n!);
for(i % ?; i <r1; i88)
for(S % ?; S < c1; S88)
scanf(!#d!, \$matriE17i:7S:);
rintf(!2nter the second matriE" )n!);
for(i % ?; i <r'; i88)
for(S % ?; S < c'; S88)
scanf(!#d!, \$matriE'7i:7S:);
for(i % ?; i < r1; i88 ){
for(S % ?; S < c'; S88){
result7i:7S: % ?;
for(4 % ?; 4 < c1; 488){
result7i:7S: 8% matriE17i:74: & matriE'74:7S:;
*
*
*
rintf(!+he multilication of the matriE is" )n!);
for(i % ?; i < r1; i88){
for(S % ?; S < c'; S88){
rintf(!#d!, result7i:7S:);
rintf(! !);
*
rintf(!)n!);
*
return ?;
*
23. Write a program to tra!\$po\$e a matri7.
#include<stdio.h>
#include<conio.h>
main()
{
int a71?:71?:,171?:71?:;
int i,S,m,n;
clrscr();
rintf(! )n enter the ro/ and column of matriE!);
scanf(!)n #d#d!,\$m,\$n);
rintf(!)n enter the elements of matriE!);
for(i%?;i<m;i88)
{
for(S%?;S<n;S88)
{
scanf(!#d!,\$a7i:7S:);
*
*
for(i%?;i<m;i88)
{
for(S%?;S<n;S88)
{
rintf(!)n matriE %#d!,a7i:7S:);
*
*
for(i%?;i<m;i88)
{
for(S%?;S<n;S88)
{
17i:7S:%?;
*
*
for(i%?;i<m;i88)
{
for(S%?;S<n;S88)
{
17i:7S:%a77S:7i:;
rintf(!)n tranceosed matriE %#d!,17i:7S:);
*
*
return (?);
*
outut"9 enter the ro/ and column of matriE %3&3
enter the element of matriE7a:%1,',3,4,<,B,@,>,A
matriE7a:% 1 ' 3
4 < B
@ > A
transose of matriE71:% 1 4 @
' < >
3 B A
24. C Program to Fi!" Le!gt# o) Stri!g Wit#out u\$i!g Li%rar2 Fu!tio!
It is easier to find the len(th of the strin( usin( (iven li1rar- function, 1ut in this ro(ram /e are findin( the
len(th of the strin( /ithout usin( li1rar- function.
#include<stdio.h>
#include<conio.h>
void main()
{
char str71??:;
int len(th;
rintf(!n2nter the 6trin( " !);
(ets(str);
len(th % ?; 00 Initial Cen(th
/hile(str7len(th:I%;;)
len(th88;
rintf(!nCen(th of the 6trin( is " #d!,len(th);
(etch();
*
2Elanation of Pro(ram "In the a1ove ro(ram /e have acceted the strin( from the user.
rintf(!n2nter the 6trin( " !);
(ets(str);
.fter that /e have initialiVed the len(th varia1le /ith Vero. Plen(thQ varia1le is used to 4ee trac4 of the
num1er of character accessed.
len(th % ?; 00 Initial Cen(th
Initiall- len(th is ?. Fo/ /e are accessin( ver- first character. If it is eDual to FOCC then /e are terminatin(
the loo else /e are incrementin( the len(th.
/hile(str7len(th:I%;;)
len(th88;
Lr- Uun "
3onsider inut strin( X Pmum1aiQ.
,hile loo Iteration len(th str7len(th:
Mefore ,hile Coo ? m
.fter Iteration 1 1 u
.fter Iteration ' ' m
.fter Iteration 3 3 1
.fter Iteration 4 4 a
.fter Iteration < < i
.fter Iteration B B ;;
.fter Iteration @ B Coo +erminated
Fo/ last ste is to rint the len(th of the strin( 9
rintf(!nCen(th of the 6trin( is " #d!,len(th);
25. Write a 8C9 program to op2 o!e \$tri!g i!to a!ot#er \$tri!g (it#out u\$i!g \$ta!"ar" )u!tio!
#include<stdio.h>
#include<conio.h>
void main()
{
int i%?;
char s171?:,s'71?:;
clrscr();
rintf(!)n enter the strin( 1 )n!);
(ets(s1);
rintf(!)n enter the strin( ')n!);
(ets(s');
rintf(!)n strin( 1"9#s!,s1);
rintf(!)n strin( '"9#s!,s');
/hile(s17i:I%FOCC)
{
s'7i:%s17i:;
i88;
*
s'7i:%;)?;;
rintf(!)n .fter co- the strin('% #s!,s');
(etch();
*
26. Write a program to o!'ert t#e \$tri!g )rom lo(er a\$e to upper a\$e
#include<stdio.h>
int main(){
char str7'?:;
int i;
rintf(!2nter an- strin(9>!);
scanf(!#s!,str);
rintf(!+he strin( is9>#s!,str);
for(i%?;i<%strlen(str);i88){
if(str7i:>%A@\$\$str7i:<%1'')
str7i:%str7i:93';
*
rintf(!)n+he strin( in lo/ercase is9>#s!,str);
return ?;
*
.l(orithm" .63II value of ;.; is B< /hile ;a; is A@. Lifference 1et/een them is A@ X B< % 3'
6o if /e /ill add 3' in the .63II value of ;.; then it /ill 1e ;a; and if /ill /e su1tract 3' in .63II value of ;a;
it /ill 1e ;.;. It is true for all alha1ets.
In (eneral rule" Oer case character % Co/er case character X 3'
Co/er case character % Oer case character 8 3'
27. Co!ate!atio! o) t(o \$tri!g\$ i! programmi!g la!guage
#include<stdio.h>
int main(){
int i%?,S%?;
char str17'?:,str'7'?:;
uts(!2nter first strin(!);
(ets(str1);
uts(!2nter second strin(!);
(ets(str');
rintf(!Mefore concatenation the strin(s are)n!);
uts(str1);
uts(str');
/hile(str17i:I%;)?;){
i88;
*
/hile(str'7S:I%;)?;){
str17i88:%str'7S88:;
*
str17i:%;)?;;
rintf(!.fter concatenation the strin(s are)n!);
uts(str1);
return ?;
*
28. C Program to Store I!)ormatio!:!ame- roll a!" mar&\$; o) a Stu"e!t U\$i!g Struture
In this ro(ram, a structure(student) is created /hich contains name, roll and mar4s as its data mem1er. +hen, a
structure varia1le( s ) is created. +hen, data (name, roll and mar4s) is ta4en from user and stored in data
mem1ers of structure varia1le s. 5inall-, the data entered 1- user is disla-ed.
#include <stdio.h>
struct student{
char name7<?:;
int roll;
float mar4s;
*;
int main(){
struct student s;
rintf(!2nter information of students")n)n!);
rintf(!2nter name" !);
scanf(!#s!,s.name);
rintf(!2nter roll num1er" !);
scanf(!#d!,\$s.roll);
rintf(!2nter mar4s" !);
scanf(!#f!,\$s.mar4s);
rintf(!)nLisla-in( Information)n!);
rintf(!Fame" #s)n!,s.name);
rintf(!Uoll" #d)n!,s.roll);
rintf(!Kar4s" #.'f)n!,s.mar4s);
return ?;
*
Gutut
29. C Program to Ae\$\$ Eleme!t\$ o) a! Arra2 U\$i!g Poi!ter
+his ro(ram declares the arra- of five element and the elements of that arra- are accessed usin( ointer.
#include <stdio.h>
int main(){
int data7<:, i;
rintf(!2nter elements" !);
for(i%?;i<<;88i)
scanf(!#d!,data8i);
rintf(!Zou entered" !);
for(i%?;i<<;88i)
rintf(!#d)n!,&(data8i));
return ?;
*
Gutut
2nter elements" 1
'
3
<
4
Zou entered" 1
'
3
<
4
30. C program to a"" t(o !um%er\$ u\$i!g poi!ter\$
+his ro(ram erforms addition of t/o num1ers usin( ointers. In our ro(ram /e have t/o t/o inte(er
varia1les E, - and t/o ointer varia1les and D. 5irstl- /e assi(n the addresses of E and - to and D
resectivel- and then assi(n the sum of E and - to varia1le sum. Fote that \$ is address of oerator and & is
#include <stdio.h>
int main()
{
int first, second, &, &D, sum;
scanf(!#d#d!, \$first, \$second);
% \$first;
D % \$second;
sum % & 8 &D;
rintf(!6um of entered num1ers % #d)n!,sum);
return ?;
*
31. Write a 8C9
Program to ompute t#e \$um
o) all eleme!t\$ \$tore" i! a!
arra2 u\$i!g poi!ter\$
#include
#include
void main() {
int num.rra-71?:;
int i, sum % ?;
int &tr;
rintf(!)n2nter 1? elements " !);
for (i % ?; i < 1?; i88)
scanf(!#d!, \$num.rra-7i:);
tr % num.rra-; 0& a%\$a7?: &0
for (i % ?; i < 1?; i88) {
sum % sum 8 &tr;
tr88;
*
rintf(!+he sum of arra- elements " #d!, sum);
*
Gutut "
2nter 1? elements " 11 1' 13 14 1< 1B 1@ 1> 1A '?
+he sum of arra- elements is 1<<
2Elanation of Pro(ram ".ccet the 1? elements from the user in the arra-.
for (i % ?; i < 1?; i88)
scanf(!#d!, \$num.rra-7i:);
,e are storin( the address of the arra- into the ointer.
tr % num.rra-; 0& a%\$a7?: &0
Fo/ in the for loo /e are fetchin( the value from the location ointer 1- ointer varia1le. Osin( Le9
referencin( ointer /e are a1le to (et the value at address. for (i % ?; i < 1?; i88) {
sum % sum 8 &tr;
tr88;
*
6uose /e have '??? as startin( address of the arra-. +hen in the first loo /e are fetchin( the value at '???.
i.e sum % sum 8 (value at '???) % ? 8 11 % 11
In the 6econd iteration /e /ill have follo/in( calculation 9
sum % sum 8 (value at '??') % 11 8 1' % '3
32. Pa\$\$i!g Argume!t to Fu!tio! 3
In 3 Pro(rammin( /e have different /a-s of arameter assin( schemes such as 3all 1- Talue and 3all 1-
Ueference. 5unction is (ood ro(rammin( st-le in /hich /e can /rite reusa1le code that can 1e called
/henever reDuire. ,henever /e call a function then seDuence of eEecuta1le statements (ets eEecuted. ,e can
ass some of the information to the function for rocessin( called ar(ument.
+/o ,a-s of Passin( .r(ument to 5unction in 3 Can(ua(e "
3all 1- Ueference 3all 1- Talue
A.Call %2 .alue 3
#include<stdio.h>
void interchan(e(int num1er1,int num1er')
{
int tem;
tem % num1er1;
num1er1 % num1er';
num1er' % tem;
*
int main() {
int num1%<?,num'%@?;
interchan(e(num1,num');
rintf(!)nFum1er 1 " #d!,num1);
rintf(!)nFum1er ' " #d!,num');
return(?);
*
Gutut "
Fum1er 1 " <?
Fum1er ' " @?
2Elanation " 3all 1- Talue
,hile Passin( Parameters usin( call 1- value , EeroE co- of ori(inal arameter is created and assed to the
called function..n- udate made inside method /ill not affect the ori(inal value of varia1le in callin(
function.In the a1ove eEamle num1 and num' are the ori(inal values and EeroE co- of these values is assed
to the function and these values are coied into num1er1,num1er' varia1le of sum function resectivel-.
.s their scoe is limited to onl- function so the- cannot alter the values inside main function.
3all 1- Talue in 3 Pro(rammin( 6cheme
M.3all 1-
Ueference0Pointer0.ddress "
#include<stdio.h>
void interchan(e(int &num1,int &num')
{
int tem;
tem % &num1;
&num1 % &num';
&num' % tem;
*
int main() {
int num1%<?,num'%@?;
interchan(e(\$num1,\$num');
rintf(!)nFum1er 1 " #d!,num1);
rintf(!)nFum1er ' " #d!,num');
return(?);
*
Gutut "
Fum1er 1 " @?
Fum1er ' " <?
2Elanation " 3all 1- .ddress
,hile assin( arameter usin( call 1- address scheme , /e are assin( the actual address of the varia1le to the
called function.
.n- udates made inside the called function /ill modif- the ori(inal co- since /e are directl- modif-in( the
content of the eEact memor- location.
3all 1- Pointer or .ddress or Ueference in 3 Pro(rammin( 6cheme
6ummar- of 3all
M- Talue and 3all
M- Ueference "
Point 3all 1- Talue 3all 1- Ueference
3o- Lulicate 3o- of Gri(inal Parameter is
Passed
.ctual 3o- of Gri(inal Parameter is Passed
Kodification Fo effect on Gri(inal Parameter after
modif-in( arameter in function
Gri(inal Parameter (ets affected if value of
arameter chan(ed inside function
%%%%%%
C Tutorial <
Call %2 .alue or
Call %2
Re)ere!e
In this 3
lan(ua(e
tutorial /e /ill ta4e
a loo4 at call 1-
value and call 1-
reference (also
4no/n as ass9 1-9
value and
ass91-9
reference). +hese methods are different /a-s of assin( (or callin() data to functions.
3all 1- Talue
If data is assed 1- value, the data is coied from the varia1le used in for eEamle main() to a varia1le used 1-
the function. 6o if the data assed (that is stored in the function varia1le) is modified inside the function, the
value is onl- chan(ed in the varia1le used inside the function. CetWs ta4e a loo4 at a call 1- value eEamle"
#include <stdio.h>
void callN1-Nvalue(int E) {
rintf(!Inside callN1-Nvalue E % #d 1efore addin( 1?.)n!, E);
E 8% 1?;
rintf(!Inside callN1-Nvalue E % #d after addin( 1?.)n!, E);
*
int main() {
int a%1?;
rintf(!a % #d 1efore function callN1-Nvalue.)n!, a);
callN1-Nvalue(a);
rintf(!a % #d after function callN1-Nvalue.)n!, a);
return ?;
*
+he outut of this call 1- value code eEamle /ill loo4 li4e this"
a % 1? 1efore function callN1-Nvalue.
Inside callN1-Nvalue E % 1? 1efore addin( 1?.
Inside callN1-Nvalue E % '? after addin( 1?.
a % 1? after function callN1-Nvalue.
G4, letWs ta4e a loo4 at /hat is haenin( in this call91-9value source code eEamle. In the main() /e create a
inte(er that has the value of 1?. ,e rint some information at ever- sta(e, 1e(innin( 1- rintin( our varia1le a.
+hen function callN1-Nvalue is called and /e inut the varia1le a. +his varia1le (a) is then coied to the
function varia1le E. In the function /e add 1? to E (and also call some rint statements). +hen /hen the neEt
statement is called in main() the value of varia1le a is rinted. ,e can see that the value of varia1le a isnWt
chan(ed 1- the call of the function callN1-Nvalue().
3all 1- Ueference
If data is assed 1- reference, a ointer to the data is coied instead of the actual varia1le as is done in a call 1-
value. Mecause a ointer is coied, if the value at that ointers address is chan(ed in the function, the value is
also chan(ed in main(). CetWs ta4e a loo4 at a code eEamle"
#include <stdio.h>
void callN1-Nreference(int &-) {
rintf(!Inside callN1-Nreference - % #d 1efore addin( 1?.)n!, &-);
(&-) 8% 1?;
rintf(!Inside callN1-Nreference - % #d after addin( 1?.)n!, &-);
*
int main() {
int 1%1?;
rintf(!1 % #d 1efore function callN1-Nreference.)n!, 1);
callN1-Nreference(\$1);
rintf(!1 % #d after function callN1-Nreference.)n!, 1);
return ?;
*
+he outut of this call 1- reference source code eEamle /ill loo4 li4e this"
1 % 1? 1efore function callN1-Nreference.
Inside callN1-Nreference - % 1? 1efore addin( 1?.
Inside callN1-Nreference - % '? after addin( 1?.
1 % '? after function callN1-Nreference.
CetWs eElain /hat is haenin( in this source code eEamle. ,e start /ith an inte(er 1 that has the value 1?.
+he function callN1-Nreference() is called and the address of the varia1le 1 is assed to this function. Inside the
function there is some 1efore and after rint statement done and there is 1? added to the value at the memor-
ointed 1- -. +herefore at the end of the function the value is '?. +hen in main() /e a(ain rint the varia1le 1
and as -ou can see the value is chan(ed (as eEected) to '?.
W#e! to U\$e Call %2 .alue a!" W#e! to u\$e Call %2 Re)ere!e=
Gne advanta(e of the call 1- reference method is that it is usin( ointers, so there is no dou1lin( of the
memor- used 1- the varia1les (as /ith the co- of the call 1- value method). +his is of course (reat, lo/erin(
the memor- footrint is al/a-s a (ood thin(. 6o /h- donWt /e Sust ma4e all the arameters call 1- referenceH
+here are t/o reasons /h- this is not a (ood idea and that -ou (the ro(rammer) need to choose 1et/een call
1- value and call 1- reference. +he reason are" side effects and rivac-. On/anted side effects are usuall-
caused 1- inadvertentl- chan(es that are made to a call 1- reference arameter. .lso in most cases -ou /ant
the data to 1e rivate and that someone callin( a function onl- 1e a1le to chan(e if -ou /ant it. 6o it is 1etter to
use a call 1- value 1- default and onl- use call 1- reference if data chan(es are eEected.
33. C Program to Create a File & Store I!)ormatio!
+his 3 Pro(ram creates a file \$ store information. ,e freDuentl- use files for storin( information /hich can
1e rocessed 1- our ro(rams. In order to store information ermanentl- and retrieve it /e need to use files
and this ro(ram demostrate file creation and /ritin( data in that.
0& 3 ro(ram to create a file called em.rec and store information a1out a erson, in terms of his name, a(e and
salar-. &0
#include <stdio.h>
void main()
{
5IC2 &ftr;
char name7'?:;
int a(e;
float salar-;
0& oen for /ritin( &0
ftr % foen(!em.rec!, !/!);
if (ftr %% FOCC)
{
rintf(!5ile does not eEists )n!);
return;
*
rintf(!2nter the name )n!);
scanf(!#s!, name);
frintf(ftr, !Fame % #s)n!, name);
rintf(!2nter the a(e)n!);
scanf(!#d!, \$a(e);
frintf(ftr, !.(e % #d)n!, a(e);
rintf(!2nter the salar-)n!);
scanf(!#f!, \$salar-);
frintf(ftr, !6alar- % #.'f)n!, salar-);
fclose(ftr);
*
2nter the name
raS
2nter the a(e
4?
2nter the salar-
4??????
34. C Program to Appe!" t#e Co!te!t o) File at t#e e!" o) A!ot#er
+his 3 Pro(ram aends the content of file at the end of another.
0& 3 Pro(ram to .end the 3ontent of 5ile at the end of .nother &0
#include <stdio.h>
#include <stdli1.h>
main()
{
5IC2 &fsrin(1, &fsrin(', &ftem;
char ch, file17'?:, file'7'?:, file37'?:;
rintf(!2nter name of first file !);
(ets(file1);
rintf(!2nter name of second file !);
(ets(file');
rintf(!2nter name to store mer(ed file !);
(ets(file3);
fsrin(1 % foen(file1, !r!);
fsrin(' % foen(file', !r!);
if (fsrin(1 %% FOCC YY fsrin(' %% FOCC)
{
error(!2rror has occured!);
rintf(!Press an- 4e- to eEit...)n!);
eEit(2[I+N5.ICOU2);
*
ftem % foen(file3, !/!);
if (ftem %% FOCC)
{
error(!2rror has occures!);
rintf(!Press an- 4e- to eEit...)n!);
eEit(2[I+N5.ICOU2);
*
/hile ((ch % f(etc(fsrin(1)) I% 2G5)
futc(ch, ftem);
/hile ((ch % f(etc(fsrin(') ) I% 2G5)
futc(ch, ftem);
rintf(!+/o files mer(ed #s successfull-.)n!, file3);
fclose(fsrin(1);
fclose(fsrin(');
fclose(ftem);
return ?;
*
2nter name of first file a.tEt
2nter name of second file 1.tEt
2nter name to store mer(ed file mer(e.tEt
+/o files mer(ed mer(e.tEt successfull-.
35. Write a program to a"" reor" i! a )ile- \$ear# reor" %2 !ame telep#o!e !um%er a!" "i\$pla2 all
reor"\$ )rom a )ile.
# include <stdio.h>
# include <conio.h>
# include <strin(.h>
struct teledir{
lon( teleno ;
char name7'?:;
*;
main()
{
struct teledir t;
5IC2 &f;
lon( tno;
char sname7'?:,c;
int ch;
/hile(1)
{
clrscr();
rintf(!)t)t)t)tK2FO)n!);
rintf(!1. +G .LL +J2 U23GUL IF +J2 5IC2.)n!);
rintf(!'. +G 62.U3J +J2 U23GUL MZ F.K2.)n!);
rintf(!3. +G 62U.3J +J2 U23GUL MZ +2C2PJGF2 FOKM2U.)n!);
rintf(!4. +G 622 .CC +J2 U23GUL.!);
rintf(!<. +G 2[I+.)n!);
rintf(!2F+2U ZGOU 3JGI32"9>!);
scanf(!#d!,\$ch);
s/itch(ch)
{
case 1"
f%foen(!telehon.tEt!,!a1!);
/hile (1)
{
clrscr();
rintf(!2F+2U +J2 +2C2PJGF2 FOKM2U"9>!);
scanf(!#ld!,\$t.teleno);
fflush(stdin);
rintf(!)n2F+2U +J2 F.K2 "9>!);
scanf(!#s!,t.name);
fflush(stdin);
rintf(!)n2F+2U +J2 .LLU266 "9>!);
f/rite(\$t,siVeof(t),1,f);
fflush(stdin);
rintf(!)n)n,I6J +G 3GF+IFO2H(Z0F)!);
scanf(!#c!,\$c);
if(c%%;n; YY c%%;F;)
1rea4;
*
fclose(f);
1rea4;
case '"
f%foen(!telehon.tEt!,!r1!);
clrscr();
rintf(!2F+2U +J2 F.K2 "9>!);
scanf(!#s!,sname);
{
if(strcm(sname,t.name)%%?)
{