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

C Programming

Uploaded by

Ayush Meraki
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
50 views

C Programming

Uploaded by

Ayush Meraki
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 160

C PROGRAMMING

COMPUTER SCIENCE & APPLICATION

The Professional's Point


NET/GATE Classes
Behind Bajrang Complex,
Beside Khandelwal Bhawan, Imlipara Road
Old Bus Stand Bilaspur (C.G.)
Contact: VIKAS TIWARI (Mo. 7828033728)
❖ Program - dksbZ Hkh program software gksrk gSA ftlesa set of instruction fdlh special task dks
djus ds fy, use fd;k tkrk gSA
❖ Programming Language - Programming language ,d coding language gSA ftldk use
computer program ;k source code dks fy[kus ds fy, fd;k tkrk gSA

tSls :- C- language ,C++, Java etc.

❖ Machinary language :- ;g computer ds actual language gSAftls computer directaly


understand djrk gSA bls Binary language Hkh dgrs gSA blesa dksbZ Hkh data ;k instruction directaly
‘0’ and ‘1’ esa fy[kk tkrk gSA ftlds fy, dqN standard coding ka use fd;k tkrk gSA

tSls :- ASCII (American Standard Code for Information Interchange)

❖ ASCII = 7 bit
❖ Binary = 2=7² = 128
❖ A = 65 , a = 97
❖ Example:- SARITA

❖ Translator :- program ke contest es translator dqN special software gksrs gSA ftuds dk;Z source
code dks translate djds mldk machine ode generat djus dk gksrk gSA
Types of translator
➢ Assembler
➢ Compailer
➢ Interpreter
➢ Assembler - Assembly language esa fy[ks x;s program ko machine language esa cnyus ds fy,
computer ftl software ko translate ds :i esa use djrk gSA Assembler dgykrk gSA

➢ Compailer - High level program language esa fy[ks x;s program ko


➢ Machine language esa cnyus ds fy, computer ftl soft-
➢ ka use djrk gzSA Compailer dgykrk gSA

➢ Interpriter :- Interpiter ,d program gksrk gS A tks high level language


➢ esa fy[ks x;s program ds ,d & ,d instruction dks ckjh & ckjh ls
➢ Machine language esa ifjofrZr djds execute djrk gSA ;g high level langu-
➢ age ke program ds program dks ,d lkFk Machine language esa translate

ugh djrk gSA

Source code :- fdlh Hkh program language esa fy[ks x;s program ke code
ko source code dgrs gSA
1. tSls & C-language esa fy[ks x;s program.
C-language source code.
#include<stdio.h>
void main ( )
{
printf(“Naveen Collage Hasoud”);
}
2. C++ ds fy, source code.
#include<stdio.h>
void main ( )
{
Cout<<(“Welcome”);
}
3. java ds fy, source code.
Imoprt java lang * ;
class A
{
public static void main(string a[ ]);
{
System-out-print(“Welcome”);
}
}
;fn source code Machinary language esa ugh gksrk rks mls computer system esa directly execute
ugh dj ldrs gSA D;ksafd computer Machinary language dks gh le>rk gSA

How to program any problem by using C-language.

Problem --------→program
1. Fid logic/Algorithm of solution of problem.
2. Coding any languge.
Q. 1 Write a program in c-language to perform addition on 2 (tow)
number.
Ans.- Algorithum
Step 1 - Input A,B
Step 2 - C ˂̶ A+B
Step 3 - Output C
Flow chart

Programming
#include<stdio.h>
#include<conio.h>
void main( )
{
int a,b,c;
clrscr ( );
printf(“Enter the 2 number”);
scanf(“%d%d”,&a,&b);
c = a+b;
printf(“%d”,c);
getch ( );

Q. 2 Write a program in c-language to perform subtruction on 2


number.
Ans.- Algorithum
Step 1 - Input A,B

Step 2 - C ˂̶ A-B

Step 3 - Output C

Flow chart

Programming
#include<stdio.h>
#include<conio.h>
void main( )
{
int a,b,c;
clrscr ( );
printf(“Enter the 2 number”);
scanf(“%d%d”,&a,&b);
c = a-b;
printf(“%d”,c);
getch ( );
}

Q. 3 Write a program in c-language to perform multiplication on for


number.
Ans.- Algorithum
Step 1 - Input A,B,C,D
Step 2 - E ˂̶ A*B*C*D
Step 3 - Output E
Flow chart

Programming
#include<stdio.h>
#include<conio.h>
void main( )
{
int a,b,c,d,e;
clrscr ( );
printf(“Enter the number”);
scanf(“%d%d%d%d”,&a,&b,&c,&d);
e = a*b*c*d;
printf(“%d”,e);
getch ( );
}
Q. 4 Write a program in c-language to perform division on 2 (tow)
number.
Ans.- Algorithum
Step 1 - Input A,B
Step 2 - C ˂̶ A/B
Step 3 - Output C
Flow chart

Programming
#include<stdio.h>
#include<conio.h>
void main( )
{
int a,b,c;
clrscr ( );
printf(“Enter the 2 number”);
scanf(“%d%d”,&a,&b);
c = a/b;
printf(“%d”,c);
getch ( );
}

Q. 5 Write algorithum and program to calculet area of Rectangle.


Ans.- Algorithum
Step 1 - Input L,B
Step 2 - area = L*B
Step 3 - Output = area
Flow chart

Programming
#include<stdio.h>
#include<conio.h>
void main( )
{
int length,breadth,area;
clrscr ( );
printf(“Enter length and breadth of Rectangle”);
scanf(“%d%d”,&length,&breadth);
area = length*breadth;
printf(“%d”,area);
getch ( );
}

Q. 6 Write algorithum and program to calculate SI (Simple Interest).


Ans.- Algorithum
Step 1 - Input P,RT
Step 2 - SI = P*R*T
Step 3 - Output = SI
Flow chart

Programming
#include<stdio.h>
#include<conio.h>
void main( )
{
int p,r,t,si;
clrscr ( );
printf(“Enter the valve of p,r,t”);
scanf(“%d%d%d”,&p,&r,&t);
si = p*r*t;
printf(“simple interest = %d”,si);
getch ( );
}
Features of C-language –
1. C-language middle level language gS tks fd low level language tSls
assembly language ,oa high level language tSls C++ bR;knh ds fy,
features proviod djrk gSA
2. C-language Machine independent language gSAvFkkZr fdlh Hkh compu-
ter machine ij blds }kjk programming dh tk ldrh gSA
3. C-languag esa compaile fd;s x, program plate form dependent gksrk
gSA vFkkZr ftl operating system environment esa compaile gksrk gSA og
mlh plate form ij execute gksrk gSA
4. C-language case-sensitive language gksrs gSa ftlesa character ko ASCII
code ds }kjk represent fd;k tkrk gSA ,oa izR;sd character dk ASCII code
deffereant gksrk gSA
5. C-languag esa coding esa use gksus okys code small leater esa fy[ks tkrs gSA
6. C-languag esa fy[ks x, program dks ;k source code dks computer ij
Direactly execute ugha fd;k tk ldrk ,oa blesa fy[ks x, source code dks tra
- nslate djus dh vko”;drk gksrh gSA ftlds fy, compailer tks fd ,d transle-
ter software gksrk gSA iz;ksx fd;k tkrk gSA
C –Token’s :- C programming esa use gksus okys smallest and atomic

element dks C-Token dgrs gSA

Example :- void main ( )


{
printf(“Hello”);
}
Used token : void,main(,),{,printf,(“Hello”);}
Types of C-Token’s -
1. Keyword’s
2. Identifiers
3. Constant/literals
4. Special symbol
5. Operator
1. Keyword’s – C-language esa use gksus okys reserve ds code dks keyword
dgrs gSA D;ksafd ;s reserve code gS blfy, bls fdlh nwljs izdkj ls use ugha fd;k tk
ldrk gSA
Example :- int ,d keyword gS , ftls data type keyword dgrs gSA bldk use
computer memory esa space reserve djus ds fy, fd;k tkrk gSA
tks fdlh variable name ls 2 Byte memory space create djrk gSA
int a,b,c;
C-language esa dqy 32 keyword use fd;s tkrs gSAtSls & auto,breck,char,do, enum,for,goto,if etc.

2. Identifier – programming esa fdlh Hkh function vFkok variable ds identi-


fecation ds fy;s tks code use fd;s tkrs gSaA mUgsa identifier dgrs gSaA
Example :- int a,b,c;

dksbZ Hkh variable named memory address gksrk gSA tks fd ,d identifier ds :i esa use fd;k tkrk gSA
Programming esa ge dbZ lkjs function use djrs gSA vkSj function ds identifecation
ds fy, Hkh dqN name use gksrs gSA tks ,d izdkj ls identifier gSA
tSls & printf,scanf,main.
Naming rouls of Identifier-
1. fdlh Hkh identifier ko create le; bl ckr dk /;ku j[kuk pkfg, fd og reser-
ve code (Key-word) uk gksA
tSls & int fi (√)
int if (x)
2. fdlh Hkh identifier ko create le; ;g /;ku j[kuk pkfg, fd mlds lq:okr esa dksbZ

Disit (Number) uk gksA

tSls & int a1 (√)


int 1a (x)
3. ;nh identifier ,d ls vf/kd word dk gS]rks ml fLFkfr esa word ds chp esa space
ugh gksuk pkfg,A ysfdu nksuksa ds chp Underscors (-) yxk ldrs gSA

tSls & int first_number (x)

4. identifier ds fy, tks name e fd;s tkrs gSA mlds lq:okr esa special symbol

tSls & $,# bR;kfn dk iz;ksx ugh djuk gSA


tSls & int #a; (√)

5. fdlh Hkh scope esa ,d gh identifier ,d ls vf/kd ckj use ugh fd;k tk ldrk gSA
tSls & {
int a,b.c; (√)
int a; (x) -----because a already declared.
}
6. fdlh variable dks ;k identifier dks create djrs le; Underscors dk iz;ksx dgha Hkh fd;k tk ldrk
gS
tSls & int_a ; (√)
int_123; (√)
7. Number of character used in identifier sould be limited.
3. Constant/Lierals :- program esa use gksus okys fixed data dks cons-
tand dgrs gS A

Example :-
Numeric constant ;k numeric value dks number literal Hkh dgrs gSA ,oa ,sls data dk use
Arithmetic (+,-,/,*etc.) operation djus ds esa fd;k tkrk gSA
Not:- constand data valu dks gh litera dgk tkrk gS A
Set of character dks strig dgk tkrk gS A bldk usefdlh name addrestSls data dks represent djus
ds fy, fd;k tkrk gSA
String ex :- ”Sarita ” ,” Junwani ”

4. Special Symol :- fdlh Hkh program esa use gksus okys punchuaters , ;

,oa (,) , {,}bR;kfn special symbol ds vUrxZr vkrk gS A

5.Opretar :- fdlh Hkh operation esa eq[;r: dksb Hkh operator ,d symbol

Tokengksrk gS A tks operands ij dqN task perform djrk gS A

Example :- Opretion Operand Operator Operand

Multiplication 10 * 10

Addtion 2 + 5
Operator of C - C-programming esa use gksus okys operator dks fuEufyf[kr izdkj ls classify fd;k
tk ldrk gSA

(1) Arithmetic operator (+,-,*,/,%)

(2) Assignment operator (=)

(3) Relational operator (<,< =,> ,>=,==,!=)

(4) Conditional operator (Ternary operator)

(5) Autoincreement ++/Autodecreement –operator

(6) Logical operator (&&,||,!)

(7) Bitwise operator (&,!,^,<<,>> etc.)

(8) Size of( ) operator

(1) Arithmetic operator :- ;s operator mathematical calculation esa use

fd;s tkrs gSA blds vUrxZr 5 operatorgSA

Persent (%) operator dk use floating point number ij ugh djuk pkfg, ;fn djsx
a s rks compailer
massage produse djsxkA

Example :-

Example :-
Num = 123
Example :- num = ABCDE

A1 = num%10 E

A2 = (num/10)%10 D

A3 = (num/100)%10 C

A4 = (num/1000)%10 B

A5 = (num/10000)%10 A

(2) Assignment operator :- Assignment operator fdlh Hkh variable ko

value assign djus ds fy;s use fd;k tkrk gSA

Example:- int a,b,c;

(3) Relational operator :- ;g operator dqN value ds chp esa relation

check djus ds fy;s usefd;s tkrs gSA vkSj relation check djus ds
ckn ;g relation true gksus ij ‘1’ provoid djrk gSA mlh izdkj relation falsegksus

ij ‘0’ provoid djrk gSAC-language esa eq[; N% relation operator use gksrs gSA

(4) Conditional operator :- bl operator ka use conditional task

perform ds fy, fd;k tkrk gSA bls Ternary operator Hkh dgrs gSA

;g operator condition ij on djrk gSA


Q. 1 Write a program to find greater number between tow number.

Ans. - Flow chart


Programming

#include<stdio.h>

#include<conio.h>

void main( )

int a,b;

clrscr ( );

printf(“Enter 2 number”);

scanf(“%d%d”,&a,&b);

a>b?printf(“%d”,a):printf(“%d”,b);

getch ( );

Q. 1 Write a program a check entered number is even or odd.

Ans. - Flow chart


Programming

#include<stdio.h>

#include<conio.h>

void main( )

int a;

clrscr ( );

printf(“Enter A number”);

scanf(“%d”,&a);

(a%2==0)?printf(“even”):printf(“odd”);

getch ( );

(5) Autoincreement ++/Autodecreement - - operator :- ;s operator

unary operators gSA ftlesa ‘++’ gks mls auto-increement dgrs gSA
,oa ‘- -‘ ko auto-decreement dgrs gSA

Example :- Q. 1 Write a program to enter a number and print its

previous and next number?

Ans. - Flow chart

Programming

#include<stdio.h>

#include<conio.h>

void main( )

int n,next,pre;

clrscr ( );

printf(“Enter A number”);

scanf(“%d”,&n);

next = n+1;

pre = n-1;
or

next = n;

next ++;

pre = n;

pre- -;

printf(“\n number = %d”,n);

printf(“\n previous = %d”,pre);

printf(“\n next = %d”,next);

getch ( );

2. #include<stdio.h>

#include<conio.h>

void main( )

int a,b,c;

clrscr ( );

a = 10;

b = a;

c = a;

c ++;

a ++;

a ++;

b ++;

c - -;

printf(“%d%d%d”,a,b,c);//12,11,10

getch ( );

}
3. #include<stdio.h>

#include<conio.h>

void main( )// starting of program

{ // opening delimeter

int a,b,c,d; /* creating for variable each with 2Byte*/

clrscr ( );// clear the screen

a = 10;

b = 20 +a;

c = a+b;

d = a+c;

d - -;

c ++;

a ++;

b - -;

printf(“%d’,a);//11

printf(“%d’,b);//29

printf(“%d’,c);//41

printf(“%d’,d);//69

getch ( );

bu operator dks nks defferent mode esa usefd;k tkrk gSA


4. #include<stdio.h>

#include<conio.h>

void main( )

int a;

clrscr ( );

a = 10;

printf(“%d’,a++);//10

printf(“%d’,++a);//12

printf(“%d’,++a);//13

printf(“%d’,a++);//13

printf(“%d”,a);//14

getch ( );
}

5. #include<stdio.h>

#include<conio.h>

void main( )

int a = 5;

clrscr ( );

printf(“%d%d%d%d%d%”,++a,a,a++,++a);// 8, 7, 6,6

getch ( );

Note :- Comment - Comment program esa fy[ks x;s oks statement gSA tks

show gksrs gSA ysfdu execute ugha gksrs gSA


;s nks izdkj ds gksrs gSA

1. Single line comment (//)

2. Multi-line comment /* ------------------

------------------*/

6. Logical operator :- Logical operator condition ko combine djus ds fy, use fd;s tkrs gSA

C- language esa eq[; rhu izdkj ds logical operator use gksrs gSA

(1) Logical AND “&&”


(2) Logical OR “¦¦”
(3) Logical NOT “!”

(1) Logical AND “&&” - ;g operator leLr condition ds true gksus ij ‘1’

provide djrk gSA

Condition1&&Condition2&&Condition3

1 if all condition true.


0 if any condition found false.

Q. 1 Write a program a to check entered number is divisible by tow

AND three both or not.

Ans. - Flow chart

Programming

#include<stdio.h>

#include<conio.h>

void main( )

int n;

clrscr ( );

printf(“Enter A number”);

scanf(“%d”,&n);

(n%2==0)&&(n%3==0)?printf(“yes”):printf(“no”);
getch ( );

(2) Logical OR (¦¦) - ;g operator nh xbZ condition esa ls fdlh Hkh ,d ;k ,d ls

vf/kd condition true gksus ij ‘1’ provide djrk gSA vkSj ;nh leLr condition

false gksus ij ‘0’ provide djrk gSA

Condition1¦¦Condition2¦¦Condition3

1 if any condition found true.

0 if all condition found false.

Q. 1 Write a program a to check entered number is divisible by tow

OR three both or not.

Ans. - Flow chart

Programming

#include<stdio.h>
#include<conio.h>

void main( )

int n;

clrscr ( );

printf(“Enter A number”);

scanf(“%d”,&n);

(n%2==0)¦¦(n%3==0)?printf(“yes”):printf(“no”);

getch ( );

(3) Logical NOT (!) - ;g ,d unary operator gksrs gSA ftldk dk;Z condition

ds true gksus ij false vkSj rk gSA vkSj false gksus ij true gksus dk dk;Z djrk gSA

Syntax - ! Condition

#include<stdio.h>

void main ( )

printf(“%d%d”,!0,!1); // 1,0

7. Bitwise operator :- Bitwise operator fn;s x;s data ds binary bits ij

work djrk gSA

Bitwise operator ds vUrxZr 5 izdkj ds operator vkrs gSA

[1] Bitwise AND (&)

[2] Bitwise OR (|)

[3] Bitwise X-OR (^)

[4] Bitwise Left Shift (<<)

[5] Bitwise Right Shift (>>)


[1] Bitwise AND (&) :- ;g operator fn;s x;s data ds correspoinding

Position bits ds high 1 gksus ij 1 produce djrk gS A vkSj ;fn corre

-spoindin bits esa ls dksb Hkh bits 0 gks rks ;g 0 produse djrk gS A

Bitwise and (&)

a= 63;

b= 45;

c= a & b

Flow chart –

Programming-

#include<stdio.h>

#include<conio.h>

void main ( )

{
int a = 63,b = 45;

clrscr ( );

printf(“bitwise AND (a&b) :”,a&b);

getch ( );

[2] Bitwise or (|) :- ;g operator fn;s x;s data ds correspoinding

Position bits esa ls fdlh Hkh ,d bit ds 1 gksus ij 1 produce gksxk A

Ex :- int a,b,c

a= 63;

b= 48;

c= a|b;

Flow chart –

Programming-
#include<stdio.h>

#include<conio.h>

void main ( )

int a = 63,b = 45;

clrscr ( );

printf(“bitwise OR (a|b) :”,a|b);

getch ( );

[3] Bitwise X-or (^) :- ;g operator fn;s x;s data bits ds correspoinding

Position bits esa ls fdlh Hkh ,d bit ds diffrent gksus ij 1 produce djrk
gSA

Ex :- int a,b,c

a= 62;

b= 49;

c= a^b;

Flow chart –
Programming-

#include<stdio.h>

#include<conio.h>

void main ( )

int a = 62,b = 49;

clrscr ( );

printf(“bitwise x-or (a^b) :”,a^b);

getch ( );

[4] Bitwise left shift (<<) :- ;g operator fn;s x;s data ds bits dks

0 ds }kjk left dh vksj shift djrk gS A

Ex :- int a,b,c,

a= 17;

b= a<<3;
Flow chart –

Programming-

#include<stdio.h>

#include<conio.h>

void main ( )

int a = 17,b;

clrscr ( );

printf(“bitwise left shift (a<<b) :”,a>>b);

getch ( );

[5] Bitwise wright shift (>>) :- ;g operator fn;s x;s data ds bits dks

0 ds }kjk wright dh vksj shift djrk gS A


Ex :- int a,b,c,

a= 29;

b= a>>2;

Flow chart –

Programming-

#include<stdio.h>

#include<conio.h>

void main ( )

int a = 29,b;

clrscr ( );

printf(“bitwise right shift (a>>b) :”,a>>b);

getch ( );

}
[8] Size of () operator :- size of operation dk use fdlh data

Type key – word ;k fdlh constand dk size find djus ds fy, fd;k
Tkkrk gS A

Ex:- printf (“%d”, size of (int) );

O / p :- 2

Control statement :- programming esa use gksus okys os statement tks pro

-gram ds execution ds flow dks contro djrs gSA mUgs control statement

dgrs gS A ;s control statement condition ds vuqlkj execution dks control

djrs gS A

C- programming esa rhu izdkj ds control statement use fd;s tkrs gS A

[1] Condition statement :- ;s control statement condition ds vuqlkj

fn;s x;s statement dks ;k rks excute djrs gS ;k mls skip (NksM ) nsrs gS A

;s fuEufyf[kr izdkj ds gksrs gS –

[1] if ( ) statement

[2] if else statement


[3] else if ladder statement

[4] nested if else statement

[5] switch case statement

[1] if ( ) statement :-

Syntax :-

If ( condition )

------------- Single statement --------------

or

If ( condition )

----------------------

----------------------

----------------------

Flow chart
Q. 1 Write a program to enter a number and check is it even or

odd by using if ( ) statement.

Ans. - Flow chart

Programming

#include<stdio.h>

#include<conio.h>

void main( )

int n;

clrscr ( );
printf(“Enter A number”);

scanf(“%d”,&n);

if (n%2==0)

printf(“even”);

if (n%2!=0)

printf(“odd”);

getch ( );

[2] if else statement :-

Syntax :-

If ( condition )

-------------------

-------------------

-------------------

else

--------------------

--------------------

-------------------

Or

If ( condition )

----------------------

else

--------------------
Q. 1 Write a program to enter a number and check is it even or

odd by using if else statement.

Ans. - Flow chart

Programming

#include<stdio.h>

#include<conio.h>

void main( )

int n;

clrscr ( );

printf(“Enter A number”);

scanf(“%d”,&n);

if (n%2==0)
printf(“even”);

else

printf(“odd”);

getch ( );

Q. 2 Write a program to enter a number and check is it even or

odd by using if else statement.

Ans. - Flow chart

Programming

#include<stdio.h>

#include<conio.h>

void main( )

int y;

clrscr ( );
printf(“Enter the year”);

scanf(“%d”,&y);

if ( (y%400=0)||(y%100!=0)&&(y%4==0)

printf(“leap year”);

else

printf(“not leap year”);

getch ( );

Q. 3 Write a program to calculate SI (Simple Interest) according

the even condition.

Ans. - SI = (P*R*T)/100

Condition1 if (t <= 5)

R = 10%

Condition2 if (t > 5)

R = 12%

Flow chart
Programming

#include<stdio.h>

#include<conio.h>

void main( )

float p,r,t,si;

clrscr ( );

printf(“Enter the value of p and t”);

scanf(“%f%f”,&p,&t);

if (t <= 5)

r = 10;
else

r = 12;

si = (p*r*t)/100

printf(“simple interest = %f”,si);

getch ( );

Q. 4 Write a program to calculate SI (Simple Interest) according

the even condition.

Ans. - SI = (P*R*T)/100

Condition1 if (p <= 10000)

R = 10%

Condition2 if (p > 10000)

R = 15%

Flow chart
Programming

#include<stdio.h>

#include<conio.h>

void main( )

float p,r,t,si;

clrscr ( );

printf(“Enter the value of p and t”);

scanf(“%f%f”,&p,&t);

if (p <= 10000)

r = 10;
else

r = 15;

si = (p*r*t)/100

printf(“simple interest = %f”,si);

getch ( );

Q. 5 Write a program to enter a number and check is it divisible

by 10 or not.

Ans. - Flow chart

Programming

#include<stdio.h>

#include<conio.h>
void main( )

int n;

clrscr ( );

printf(“Enter A number”);

scanf(“%d”,&n);

if (n%10==0)

printf(“yes”);

else

printf(“no”);

getch ( );

[3] else if ladder statement :- conditionl statement dk use rc fd;k

Tkkrk gSA tc mutipal condition even gks vkSj condition dks dqN bl

Izkdkj chak fd;k tkuk gks fd next condition rHkh check gks tc previus

condition false gks A

Syntax :-

If ( condition )

Execution 1

else if ( condition)

Execution 2

Else if ( condition)

{
Execution 3

Flow chart –

Q. 1 Write a program to enter a letter and print any word if it is A

or B or C otherwise print other ?

Ans. - Flow chart


Programming

#include<stdio.h>

#include<conio.h>

void main( )

char ch;

clrscr ( );

printf(“Enter the any charecter”);

scanf(“%c”,&ch);

if (ch== ‘A’||ch== ‘a’)

print(“apple”);
else if (ch== ‘B’||ch== ‘b’)

printf(“boy”);

else if (ch== ‘C’||ch== ‘c’)

printf(“cat”);

else

printf(“other”);

getch ( );

Q. 2 Write a program to enter a letter and print any word if it is

age otherwise print other ?

Ans. - Flow chart

Programming

#include<stdio.h>

#include<conio.h>

void main( )

{
int n;

clrscr ( );

printf(“Enter the any number”);

scanf(“%d”,&n);

if (age < 18)

print(“age lass than 18”);

else if (age < 30)

printf(“age between 18 to 29”);

else

printf(“age more than 30”);

getch ( );

[4] Nested if – else statement :- ;g control statement esa fdlh conditio

-n ds trues ;k faise gksus ij fdlh nqljh condition dks chak fd;k tkrh gS A bl

control statemen dks condition inside another condition chak dgrs


gS A

Syntax :-

if ( condition)

if ( condition)

---------------------

---------------------

else

--------------------
--------------------

else

If ( condition )

------------------

-----------------

else

------------------

-------------------

Flow chart

Example :- Flow chart


Programming

#include<stdio.h>

#include<conio.h>

void main( )

int a,b,c;

clrscr ( );

printf(“Enter the three number”);

scanf(“%d%d%d”,&a,&b,&c);

if (a > b)

if (a >c)

print(“%d”,a);

else

printf(“%d”);

}
else

if (b > c)

printf(“%d”,b);

else

printf(“%d”,c);

getch ( );

Q. 2 write a program use C-language to calculate SI according the following the


condition same is the flow chart ?

P = <= 20000

T = <= 5

R = <= 5/=7/8

R = <= 5/=9/10

Flow chart -

Programming
#include<stdio.h>

#include<conio.h>

void main( )

float p,r,t;

clrscr ( );

printf(“Enter the value of p and t”);

scanf(“%f%f”,&p,&t);

if (p <= 20000)

if (t <= 5)

r=7

else

r=8

else

if (t <= 5)

r=9

else

r = 10

getch ( );

Q. 3

Flow chart -
Programming

#include<stdio.h>

#include<conio.h>

void main( )

float p,q,r,s;

clrscr ( );

printf(“Enter the value of p,q,r,s”);

scanf(“%f%f%f%f”,&p,&q,&r,&s);

if (p > q)

if (p > r)

if (p > s)

printf(“big = p”);

else

printf(“big = s”);
else

if (r > s)

printf(“big = r”);

else

printf(“big = s”);

else

if (q > r)

if (q > s)

printf(“big = q”);

else

printf(“big = s”);

else

if (r > s)

printf(“big = r”);

else

printf(“big = s”);

getch ( );

[5] Switch case statement :- bl statement dk use menu ;k choice be

-st operation djus ds fy, fd;k tkrk gS A

Syntax :-

Switch ( choice )

Case: // statement break ;

case: // statement break ;


case: // statement break ;

default :

Note :- * switch caseesa case dk dksbZ seqince ugh gksrk vr: fdlh Hkh case dks

Randony dgh Hkh ] fdHkh Hkh fy[kk tk ldrk gS A

* izR;sd case ds ckn break ky – wor dk use djuk pkfg, vkSj ;fn break key word case ke last
me apply ugh fd;k tkrk rks next cases Hkh excute gks tkrk gS A

* Swith case dk default case rc excute gksrk gS A tc dksbZ Hkh case match

Ukgh gksrk ,oa default dks use djuk must ugh gksrk A

* Switch case esa choise or option me floating point value dk use ugh
djuk pkfg, A

Example :- #include<stdio.h>

#include<conio.h>

void main ( )

char ch;

printf(“Enter any character”);

scanf(“%c”,&c);

switch (ch)

case ‘A’ :

case ‘a’ : printf(“apple”); break;

case ‘B’ :

case ‘b’ : printf(“boy”); break;

case ‘C’ :

case ‘c’ : printf(“cat”); break;

default : printf(“other”);

getch ( );
}

[2] Looping statement :- C- programming esa looping statement dk

use fn;s gq, statement dks ckj ckj execute djus ds fy, fd;k tkrk gS A c esa

use gksus okys loop fuEufyf[kr gS –

1 . For loop

2 . While loop / entry contol loop

3 . Do – while loop / exit control loop

1 . For loop :- ;g loop entry controlled loop gS A blesa statement rc

rd excute gksrk gS tc rd dh condition true ugha gks tkrh A

Flow chart –

Syntax :-

For (initialization ; condition ; reassignment )

Execution

}
Q. 1 Write a program to print welcome 20 times.

Programming –

#include<stdio.h>

#include<conio.h>

void main ( )

int a;

clrscr ( );

for (a = 1 ; a <= 20 ; a++)

printf(“\n%d Welcome”,a);

getch ( );

Q. 2 Write a program to print to counting from 1 to 100.

Programming –

#include<stdio.h>

#include<conio.h>

void main ( )

int a;

clrscr ( );

for (a = 1 ; a <= 100 ; a++)

printf(“\n%d”,a);

getch ( );
}

Q. 3 Write a program to print to counting from 20 to 1.

Programming –

#include<stdio.h>

#include<conio.h>

void main ( )

int a;

clrscr ( );

for (a = 20 ; a >= 1 ; a++)

printf(“\n%d”,a);

getch ( );

Q. 4 Write a program to print 10,20,30,40,50,60,70,80,90,100.

Programming –

#include<stdio.h>

#include<conio.h>

void main ( )

int a;

clrscr ( );

for (a = 10 ; a <= 100 ; a = a+100)

printf(“\n%d”,a);

}
getch ( );

Note :- ;fn for loop ke last me statement (;) ka use fd;k tk, rks loop

ki body, loop ds vUrxZr execute ugh gksrh D;ksfd og loop ls vyx gksrk gS A vkSj

loop ke comp gksus ds ckn dsoy ,d gh ckj execute gksrh gS A

Example :- Write a program to print to counting from 1 to 100.

Programming –

#include<stdio.h>

#include<conio.h>

void main ( )

int a;

clrscr ( );

for (a = 1 ; a <= 10 ; a = a+1)

printf(“\n%d”,a);

getch ( );

} // output 11

Q. 5 Write a program to print to add all the number from 1 to 10.

Programming –

#include<stdio.h>

#include<conio.h>

void main ( )

int a,sum=0;

clrscr ( );
for (a = 1 ; a <= 10 ; a = a+1)

sum = sum + a;

printf(“sum of all number from1 to 10 = %d”,sum);

getch ( );

Q. 6 Write a program to print to add first n number .

Programming –

#include<stdio.h>

#include<conio.h>

void main ( )

int a,sum=0,n;

clrscr ( );

printf(“Enter the limit :”);

scanf(“%d”,&n);

for (a = 1 ; a <= n ; a = a+1)

sum = sum + a;

printf(“sum of all numbers from1 to 10 = %d”,n,sum);

getch ( );

Q. 7 Write a program to print to add all number given in any range .

Programming –

#include<stdio.h>
#include<conio.h>

void main ( )

int a,sum=0,start,end

clrscr ( );

printf(“Enter the range : start and end”);

scanf(“%d%d”,&start,&end);

for (a = start ; a <= end ; a+a = 1)

sum = sum + a;

printf(“sum of all numbers for %d to %d %d”,start,end,sum);

getch ( );

Programming –

#include<stdio.h>

#include<conio.h>

void main ( )

int a,sum=0,end;

clrscr ( );

printf(“Enter the limit :”);

scanf(“%d”,&end);

for (a = 2 ; a <= end ; a = a+1)

{
sum = sum + pow(a,a);

printf(“sum = %d”,sum);

getch ( );

Programming –

#include<stdio.h>

#include<conio.h>

void main ( )

float a,sum=0,n;

clrscr ( );

printf(“Enter the number :”);

scanf(“%d”,&n);

for (a = 2 ; a <= n ; a = a+2)

sum = sum + pow(2,a);

printf(“sum = %f”,sum);

getch ( );

2 . While loop :- ;g ,d entry controlled loop gS A blesa statement rc rd

Ykxkrkj execute gksrk jgrk gSA tc rd dh condition true ugh gks tkrh A ;g

lcls igys condition check djrk gS A mlds ckn statement dks execute
djrk gS A

Flow chart –
Syntax:-

while (codition )

// execution

Q. 1 Tish program while print different color on the search until key

- board button press .

Programming –

#include<stdio.h>

#include<conio.h>

void main ( )

int a = 1;

clrscr ( );

while (! kbhit ( ));

text background (a);//conio.h to set background color

clrscr ( ); // conio.h printing the screen with set color


delay(1000); //slow down the spead of execution by 1 set

a++; // changing the color numbers

getch ( );

Q. 2 Write a program to print counting from 1 to 10 by using while

loop .

Programming –

#include<stdio.h>

#include<conio.h>

#include<dos.h>

void main ( )

int a = 1;

clrscr ( );

while (a <= 10);

printf(“%d”,a);

delay (1000);

a++;

getch ( );

3 . do – while loop :- ;g loop ,d controlled loop gS A blesa blesa lcls igys

statement dks execute djrk gS mlds ckn condition check djrk gS A

Flow chart –
Syntax :-

do

// execution

while ( condition );

Q. 1 Write a program to print counting from 1 to 10 by using while

loop .

Programming –

#include<stdio.h>

#include<conio.h>

void main ( )

int a = 1;
clrscr ( );

do ;

printf(“%d Welcome”,a)

a++;

while(a<=10);

getch ( );

Logic how to separate digit of any number.

num = 123

while(num!=0)

digit = num%10,123%10(3)12%10(2)1%10(1)

num = num

Q. 1 Write a program to print each digit of entered number

separately?

Programming –

#include<stdio.h>

#include<conio.h>

void main ( )

int num,digit;

clrscr ( );

num = 123;

while (num!=0)
{

digit = num%10;

printf(“\n%d”,digit);

num = num/10;

getch ( );

Q. 2

Programming –

#include<stdio.h>

#include<conio.h>

void main ( )

int num,digit;

clrscr ( );

num = 123;

printf(“Enter A number :”);

scanf(“%d”,&num);

while (num!=0)

digit = num%10;

printf(“\n%d”,digit);

num = num/10;

getch ( );

}
Q. 3 Write a program to print each digit of entered any number and

print digit of number digit times.

Example :- 123

Output = 333

22

Programming –

#include<stdio.h>

#include<conio.h>

void main ( )

int num,digit,a;

clrscr ( );

num = 123;

printf(“Enter a number”);

scanf(“%d”,&num);

while (num!=0)

digit = num%10;

for (a = 1 ; a<=digit ; a++)

printf(“%d”,digit);

printf(“ \n ”);

num = num/10;

getch ( );
}

Q. 4 Write a program to find the sum of each digit of entered number .

Programming –

#include<stdio.h>

#include<conio.h>

void main ( )

int num,digit, sum = 0;

clrscr ( );

printf(“Enter a number”);

scanf(“%d”,&num);

while (num!=0)

digit = num%10;

sum = sum+digit;

num = num/10;

printf(“sum of number digit = %d ”,sum);

getch ( );

Q. 5 Write a program to reserve any entered number.

Programming –

#include<stdio.h>

#include<conio.h>

void main ( )

int num,digit, rev = 0;


clrscr ( );

printf(“Enter a number”);

scanf(“%d”,&num);

while (num!=0)

digit = num%10;

rev = rev * 10 * digit;

num = num/10;

printf(“reverse of numbers digit = %d ”,rev);

getch ( );

Q. 6 Write a program to print entered number and its

reverse.

Programming –

#include<stdio.h>

#include<conio.h>

void main ( )

int num,digit, rev = 0,save;

clrscr ( );

printf(“Enter a number”);

scanf(“%d”,&num);

save = num;

while (num!=0)

digit = num%10;
rev = rev * 10 + digit;

num = num/10;

printf(“numbers = %d reverse = %d ”,save,rev);

getch ( );

Q. 7 Write a program to check entered number is palindrome or

not?

Programming –

#include<stdio.h>

#include<conio.h>

void main ( )

int num,digit, rev = 0,save;

clrscr ( );

printf(“Enter a number”);

scanf(“%d”,&num);

save = num;

while (num!=0)

digit = num%10;

rev = rev * 10 * digit;

num = num/10;

if (rev ==save)

printf(“number is palindrome”);

else
printf(“number is not palindrome");

getch ( );

Q. 8 Write a program to check entered number is armstrong

number of not .

Armstrong :- ,d ,slk number ftlds gj digit dks rhu ckj xq.kk djds vkil

esa tksM fn;k tk, rks mls Armstrong dgk tkrk gS A

Programming –

#include<stdio.h>

#include<conio.h>

void main ( )

int num,digit, sum = 0,save;

clrscr ( );

printf(“Enter a number”);

scanf(“%d”,&num);

save = num;

while (num!=0)

digit = num%10;

sum = sum + digit + digit + digit;

num = num/10;

printf(“\nnumber = %d sum of cube digit = %d”,save,sum);

if (sum ==save)

printf(“\nnumber is armstrong”);
else

printf(“number is not armstrong ");

getch ( );

Q. 9 Write a program to print all the digit of number from starting

to end.

Ex.- I/P : 123

O/P : 1

Programming –

#include<stdio.h>

#include<conio.h>

void main ( )

int num,digit,d, rev = 0;

clrscr ( );

printf(“Enter a number”);

scanf(“%d”,&num);

while (num!=0)

digit = num%10;

rev = rev * 10 * digit;

num = num/10;

while (rev!=0)

{
d = rev%10;

printf(“\n%d”,d);

rve = rev/10;

getch ( );

Nested loop :- loop ds vanj fdlh nqljs loop ds use dks nested loop dgk
Tkkrk gS A

Example :-

for(b = 1 ; b<=3 ; b++)

for(a = 1 ; a<=10 ; a++)

printf(“ * ”);

printf(“ \n ”);

1. Programming –

#include<stdio.h>

#include<conio.h>

void main ( )

int a,b;

clrscr ( );

for(a = 1 ; a<=5 ; a++)

for(b = 1 ; b<=10 ; b++)


{

printf(“ * ”);

printf(“ \n ”);

getch ( );

2. Programming –

#include<stdio.h>

#include<conio.h>

void main ( )

int a,b;

clrscr ( );

for(a = 1 ; a<=2 ; a++)//outer loop

for(b = 1 ; b<=5 ; b++)//Inner loop

printf(“ Welcome ”);

printf(“ \n ”);

getch ( );

3. Write a program to print following paitern .


Programming –

#include<stdio.h>

#include<conio.h>

void main ( )

int a,b;

clrscr ( );

for(a = 1 ; a<=3 ; a++)

for(b = 1 ; b<=3 ; b++){printf(“ * ”);}

for(b = 1 ; b<=2 ; b++){printf(“ $ ”);}

for(b = 1 ; b<=3 ; b++){printf(“ * ”);}

printf(“ \n ”);

getch ( );

4. Write a program to print following paitern .

Programming –

#include<stdio.h>

#include<conio.h>
void main ( )

int a,b;

clrscr ( );

for(a = 1 ; a<=5 ; a++)

for(b = 1 ; b<=a ; b++){printf(“ $ ”);

printf(“ \n ”);

getch ( );

5. Write a program to the printing following paitern .

Programming –

#include<stdio.h>

#include<conio.h>

void main ( )

int a,b;

clrscr ( );

for(a = 1 ; a<=5 ; a++)

for(b = 1 ; b<=5-a ; b++){printf(“ ”);}

for(b = 1 ; b<=a ; b++){printf(“ * ”);}


printf(“ \n ”);

getch ( );

6. Write a program to the printing following paitern .

Programming –

#include<stdio.h>

#include<conio.h>

void main ( )

int a,b;

clrscr ( );

for(a = 1 ; a<=5 ; a++)

for(b = 1 ; b<=5-a ; b++){printf(“ ”);}

for(b = 1 ; b<=a*2-1 ; b++){printf(“ * ”);}

printf(“ \n ”);

getch ( );

Jumping statement :-

[1] go to statement
[2] break statement

[3] continue statement

[4] retwrn statement

Exit (0) function

[1] go to statement:- program ds execution ds sequenc dks change


djus dk dk;Z djrk gS A

syntax :-

go to lable 1;

----------// skip

----------// skip

----------// skip

Lable1 :

(1) program -

#include<stdio.h>

#include<conio.h>

viod main ( )

clrscr ( );

goto x;

x : prntf(“ 1 ”); goto end;

y : prntf(“ 2 ”); goto z;

z : prntf(“ 3 ”); goto y;

end :

getch ( );

O/P = 321

2. Write a program to the print welcome 10 times without using loop .


Programming –

#include<stdio.h>

#include<conio.h>

void main ( )

int count = 1;

clrscr ( );

printf(“\n%d Welcome”,count);

count++;

if (count <=10) goto X;

getch ( );

3. Write a program to the print 5 stars in 5 lines by using 10 to

break .

Programming –

#include<stdio.h>

#include<conio.h>

void main ( )

int a,b;

clrscr ( );

a = 1;

y:

b = 1;

x:

textcolor (a);
printf(“ * ”);

b++;

if (b<=5) goto x;

a++;

printf(“ \n ”);

if (a<=5) goto y;

getch ( );

[2] break statement :- bl statement dk use switch case and

looping esa fd;k tkrk gS A ;g programming ds flow dks break djds

mls book ds ckgj send djrk gS A

Example:-

Programming –

#include<stdio.h>

#include<conio.h>

void main ( )

int a;

clrscr ( );

for(a = 1 ; a<=10 ; a++)

printf(“%d”,a);

if (a==5)

break ;

getch ( );

}
O/P = 1,2,3,4,5

[3] continue statement :- ;g jumping statement loop ds stateme

- nt dks tks fd blds uhps fy[ks x;s gksrs gS] mUgs skip djrs gq, loop dks vkxs

dh vksj execute djrk gS ,ao bldk use dsoy looping esa fd;k tkrk gS A

Example:-

Programming –

#include<stdio.h>

#include<conio.h>

void main ( )

int a;

clrscr ( );

for(a = 1 ; a<=10 ; a++)

if (a==5||a==7)

continue;

printf(“%d”,a);

getch ( );

O/P = 1,2,3,4,6,8,9,10.

[4] return statement :- retwrn statement dk use fdlh function d s

+}kjk dksbZ value ko retarn djus ds fy, fd;k tkrk gS A

Example :-

Programming –
#include<stdio.h>

#include<conio.h>

int data ( )

return 1;

viod main ( )

int a;

clrscr ( );

a = data ( );

pritf(“%d”,a);

getch ( );

Array in C-language –

1. Array ,d linear data structure gSA ftlesa homogenous set of element ,d sequence esa
store fd;k tkrk gSA

2. programming esa array blfy, useful gS A D;ksfd fdlh common var –

iable name ds lkFk index dk use djds n element dks index [0] se

Index ( n – 1 ) rd store fd;s tk ldrs gS A

3. Array dk lcls important advantage ;g gS fd single declaration

element store djus ds fy, reserve dh tk ldrh gS A

Syntax :- (1)

[data type array name [size] [size] ]

Example:- 1. int A[10]


2. float B[5]

Q. 1 Enter 10 number into the array and print that.

Programming

#include<stdio.h>

#include<conio.h>

void main ( )

int A[10];

clrscr ( );

printf(“Enter 10 number”);

scanf(“%d%d%d%d%d%d%d%d%d%d”,&[0],&[1],&[2],&[3],

&[4],&[5],&[6]&[7],&[8],&[9]);

getch ( );

Q. 1 Enter 10 number into the array and print that by using loop.

Programming

#include<stdio.h>

#include<conio.h>

void main ( )
{

int A[10],i;

clrscr ( );

printf(“Enter 10 number”);

for(i = 0 ; i <= 9 ; i++)

scanf(“%d”,A[i]);

for(i = 0 ; i <= 9 ; i++)

scanf(“%d”,A[i]);

getch ( );

Benefits of array :-

1. Single declaration esa bloc of memory dk allocation


2. Single variable esa maltipal data store djus dh faiciltiy
3. Easy to mango

Type of array :-

Subscript ds vk/kkj ij array dks fuEufyf[kr izdkj ls classify fd;k tk ldrk gS A


[1] one – dimensional array :- ;fn a array dks declear djrs le; dsoy ,d

gh subseript dk use fd;k tk, rks ,slh array dks one dimensional

array dgrs gS A ,ao tc dHkh element access djus dh erquirment gksrh

gS rks single index dk use fd;k tkrk gS A

Example:- Memory representation of array

int A[10];

[2] multi dimensional array :- ;fn a array dks creat djrs le; dsoy ,d

Lks vf/kd subseript dk use fd;k tk, rks ,slh array dks multidimensio

-nal array dgrs gS A ,ao element dks access djus ds fy, mrus gh index dk

use fd;k tkrk gS ftrus dh subseript mlesa gksrs gS A

Example:- Memory representation of A[3][2] tow dimensional array

int A[3][2];
#include<stdio.h>

#include<conio.h>

void main ( )

int N[5],[2],i,j;

clrscr ( );

printf(“Enter 10 number”);

for(i = 0 ; i <= 4 ; i++)

for(i = 0 ; i <= 1 ; i++)

scanf(“%d”,N[i] [j]);

for(i = 0 ; i <= 4 ; i++)

for(i = 0 ; i <= 1 ; i++)

printf(“%d”,N[i] [j]);

printf(“ \n ”);

getch ( );

Q. 1 Write a program to enter 10 number in any array and search

for specific element .

Program
#include<stdio.h>

#include<conio.h>

void main ( )

int A[10],index,se,flag=0;

clrscr ( );

printf(“Enter 10 number”);

for (index = 0 ; index <= 9 ; index++)

scanf(“%d”,&A[i]);

printf(“Enter number number to search”);

scanf(“%d”,&se);

for (index = 0 ; index <= 9 ; index++)

flag = 1;

break;

flag == 0?printf(“not found”):printf(“element found”);

getch ( );

Q. 2 Write a program to find out greater number among 100

number.

Program

#include<stdio.h>

#include<conio.h>

void main ( )
{

int A[100],index,greater;

clrscr ( );

printf(“Enter 100 number”);

for (i = 0 ; i <= 99 ; i++)

scanf(“%d”,&A[i]);

greater = A[0];

for (i = 0 ; i <= 99 ; i++)

if (greater < A[i])

printf(“greater number = %d”,greater);

getch ( );

Q. 3 Write a program to find out smaller number among 100

number.

Program

#include<stdio.h>

#include<conio.h>

void main ( )

int A[100],index,smaller;

clrscr ( );

printf(“Enter 100 number”);

for (i = 0 ; i <= 99 ; i++)


{

scanf(“%d”,&A[i]);

smaller = A[0];

for (i = 0 ; i <= 99 ; i++)

if (smaller > A[i])

printf(“smaller number = %d”,smaller);

getch ( );

Q. 4 Write a program to perform buble sort on 10 number.

program

#include<stdio.h>

#include<conio.h>

void main ( )

int A[10],i,j,ex;

clrscr ( );

printf(“Enter 10 number”);

for(i = 0 ; i <= 9 ; i++)

scanf(“%d”,&A[i]);

for(i = 0 ; i <= 9 ; i++)

for(j = 0 ; j <= 9- 1 ; j++)


{

if (A[i] > A[ j +1 ])

ex = A[ j ];

A [ j ] = A [ j + 1 ];

A[ j + 1] = ex;

printf(“\n sorted data \n”);

for(i = 0 ; i <= 9 ; i++)

printf(“%d”,A[i]);

getch ( );

Q. 5 Write a program to enter 100 number and print all different

ways.

Program

#include<stdio.h>

#include<conio.h>

void main ( )

int A[100],i;

clrscr ( );

printf(“Enter 100 number”);

for (i = 0 ; i <= 99 ; i++)


scanf(“%d”,&A[i]);

for (i = 0 ; i <= 99 ; i++)

printf(“%d”,A[ i ]);

getch ( );

Matrix :- fdlh Hkh tow dimensional array dks matrix dgrs gS Aftlesa data

dks row and column ds form esa arrange djds j[kk tkrk gS A

syntax :- data type array name [ size – row ] [size – column ];

Example:- int M[5][6];

Tow dimensional array esa first subscript row ko indicate djrh gSA ,oa second subscript
column ko represent djrh gSA tow dimensional array matrix ka logical representation gSA

#include<stdio.h>

#include<conio.h>

void main ( )

int M[5][5],i,j;

clrscr ( );
printf(“Enter 25 number for 5*5 M \n”);

for (i = 0 ; i <= 4 ; i++)

for (j = 0 ; j <= 4 ; j++)

scanf(“%d”,M[ i ][ j ]);

for (i = 0 ; i <= 4 ; i++)

for (j = 0 ; j <= 4 ; j++)

printf(“%d”,M[ i ] [ j ]);

printf(“ \n ”);

getch ( );

Q. 2 Write a program to perform addition on tow matrix .

Addition on matrix is possible only if three order is same.

M1 [ r1 ] [ c1]

M2 [ r2 ] [ c2 ]

Step 1 Enter order of tow matrix r1,c1,r2,c2

Step 2 check r1 == r2&&c1 == c2

if true – 1A – fill the data in M1

1B – fill the data in M2

1C – M3[ r ] [c ] = M1[ r ] [ c ] + M2[ r ] [c ]

1D – printf M1
1E – printf M2

1F – printf M3

if false

error msg.

#include<stdio.h>

#include<conio.h>

void main ( )

int M1[100][100], M2[100][100], M3[100][100],r,c,r1,c1,r2,c2;

clrscr ( );

printf(“Enter order of first matrix \n”);

scanf(“%d%d”,&r1,&c1);

if(r1 == r2 && c1 == c2)

printf(“Enter %d number for matrix1 \n”,r1*c1);

for(r == 0 ; r <= r1 ; r++)

for(c == 0 ; c <= c1 ; c++)

scanf(“%d”,&M1[r][c]);

printf(“Enter %d number for matrix2 \n”,r2*c2);

for(r == 0 ; r <= r2 ; r++)

for(c == 0 ; c <= c2 ; c++)

{
scanf(“%d”,&M2[r][c]);

printf(“Enter %d number for matrix3 \n”,r1*c2);

for(r == 0 ; r <= r1 ; r++)

for(c == 0 ; c <= c2 ; c++)

M3[r][c] = M1[r][c] + M2[r][c];

printf(“data of first matrix \n”);

for(r == 0 ; r <= r1 ; r++)

for(c == 0 ; c <= c1 ; c++)

scanf(“%d”,M1[r][c]);

printf(“ \n ”);

printf(“data of second matrix \n”);

for(r == 0 ; r <= r2 ; r++)

for(c == 0 ; c <= c2 ; c++)

scanf(“%d”,M2[r][c]);

}
printf(“ \n ”);

printf(“data of answer matrix \n”);

for(r == 0 ; r <= r1 ; r++)

for(c == 0 ; c <= c2 ; c++)

scanf(“%d”,M3[r][c]);

printf(“ \n ”);

else

printf(“order mismatched”);

getch ( );

Q. 3 Write a program to perform substruction on tow matrix .

#include<stdio.h>

#include<conio.h>

void main ( )

int M1[100][100], M2[100][100], M3[100][100],r,c,r1,c1,r2,c2;

clrscr ( );

printf(“Enter order of first matrix \n”);

scanf(“%d%d”,&r1,&c1);

if(r1 == r2 && c1 == c2)


{

printf(“Enter %d number for matrix1 \n”,r1*c1);

for(r == 0 ; r <= r1 ; r++)

for(c == 0 ; c <= c1 ; c++)

scanf(“%d”,&M1[r][c]);

printf(“Enter %d number for matrix2 \n”,r2*c2);

for(r == 0 ; r <= r2 ; r++)

for(c == 0 ; c <= c2 ; c++)

scanf(“%d”,&M2[r][c]);

printf(“Enter %d number for matrix3 \n”,r1*c2);

for(r == 0 ; r <= r1 ; r++)

for(c == 0 ; c <= c2 ; c++)

M3[r][c] = M1[r][c] - M2[r][c];

printf(“data of first matrix \n”);

for(r == 0 ; r <= r1 ; r++)


{

for(c == 0 ; c <= c1 ; c++)

scanf(“%d”,M1[r][c]);

printf(“ \n ”);

printf(“data of second matrix \n”);

for(r == 0 ; r <= r2 ; r++)

for(c == 0 ; c <= c2 ; c++)

scanf(“%d”,M2[r][c]);

printf(“ \n ”);

printf(“data of answer matrix \n”);

for(r == 0 ; r <= r1 ; r++)

for(c == 0 ; c <= c2 ; c++)

scanf(“%d”,M3[r][c]);

printf(“ \n ”);

else
printf(“order mismatched”);

getch ( );

Matrix multiplication :- fdlh Hkh nks matrix dk multiplication rHkh

possible gksrk gS tc igyh matrix ds row ds cjkcj gks A

Example :- M1 = r1*c1

M2 = r2*c2

Matrix multiplication is possible only if

Step of multiplication

Step 1 Enter order of matrix M1(r1*c1)

Step 2 Enter order of matrix M2(r2*c2)

Step 3 check if (c1==r2)

Then
(A) Input for M1(r1*c1)
(B) Input for M2(r2*c2)
(C) Multiplication M3 (M1*M2) (r1 * c2)
(D) Display M1
(E) Display M2
(F) Display M3

else

multiplication not possible.

#include<stdio.h>

#include<conio.h>

void main ( )

int M1[100][100];

int M2[100][100];

int M3[100][100];

int r1,c1,r2,c2,r,c,k;

clrscr ( );

printf(“Enter order of first matrix ”);

scanf(“%d%d”,&[r1],&[c2]);

if(r1 == r2)

//A printf(“Enter %d number for matrix1 \n”,r1*c1);

for(r == 0 ; r <= r1 ; r++)

for(c == 0 ; c <= c1 ; c++)

scanf(“%d”,&M1[r][c]);

//B printf(“Enter %d number for matrix2 \n”,r2*c2);

for(r == 0 ; r <= r2 ; r++)


for(c == 0 ; c <= c2 ; c++)

scanf(“%d”,&M2[r][c]);

//C for(r == 0 ; r <= r1 ; r++)

for(c == 0 ; c <= c2 ; c++)

M3[r][c] = 0

for(k = 0; k <= c1; k++)

M3[r][c]+= M1[r][k] * M2[k][c];

printf(“data of first matrix \n”);

for(r == 0 ; r <= r1 ; r++)

for(c == 0 ; c <= c1 ; c++)

scanf(“%d”,M1[r][c]);

printf(“ \n ”);

printf(“data of second matrix \n”);

for(r == 0 ; r <= r2 ; r++)

for(c == 0 ; c <= c2 ; c++)

scanf(“%d”,M2[r][c]);

}
printf(“ \n ”);

printf(“data of answer matrix \n”);

for(r == 0 ; r <= r1 ; r++)

for(c == 0 ; c <= c2 ; c++)

scanf(“%d”,M3[r][c]);

printf(“ \n ”);

else

printf(“multiplication not possible”);

getch ( );

Transpose :- transpose ,d ,slk matrix gS A ftlesa row and colom

cjkcj gksrk gS A rFkk ftlesa row dks colom me and colom dks row

esa transpose fd;k tkrk gS A transpose dgykrk gS A

String :- programming esa use fd;s tkus okyk fdlh Hkh character array

dks string dgrs gS A string dk use group of character ko store djus

ds fy, fd;k tkrk gS A fdlh Hkh string me name address phone number

etc. dks store djus ds fy, fd;k tk ldrk gSA izR;sd string ke end esa ,d special character
include fd;k tkrk gSA tks string ds end ko specify djrk gSA ftls NULL (‘\0’) character dgk
tkrk gSA

Syntax :- [char string name[size];]

String representation // string initialization

char name [ ] = “JOJO” ;


or

char name [10 ] = “JOJO” ;

Example :-

#include<string.h>

#include<stdio.h>

#include<conio.h>

void main( )

char name [40];//string declaration

clrscr ( );

printf(“Enter your name :”);

scanf(“%s”,name);//KAJAN

printf(“\n original %s”,name);//original KAJAN

strrev (name);

printf(“\n reverse %s”,name);//reverse KAJAN

getch ( );

Q. 2 Write a program to enter and print your name and name of you

pairant .

program –

#include<string.h>

#include<stdio.h>
#include<conio.h>

void main( )

char name [40],fname[40],mname[40];

clrscr ( );

printf(“Enter your name :”);

gets (name);

printf(“Enter your fname”);

gets (fname);

printf(“Enter your mname”);

gets (mname);

printf(“\n your name : %s”,name);

printf(“\n your fname : %s”,fname);

printf(“\n your mname : %s”,mname);

getch ( );

String function of <string.h>

(1) strrev ( ) - ;g function string ko reverse djus ds fy, usefd;k tkrk gSA

Syntax - strrev (char str[ ]);


(2) strlen ( ) - ;g function string ko length find djus ds fy, usefd;k tkrk

gSA rFkk ;g integer return djrk gSA

(3) strupr ( ) - ;g function string ke character dks upper case eas convert dj nsrk gSA

Syntax - strupr (char str[ ]);

(4) strlwr ( ) - ;g function string ke character dks lower case eas convert dj nsrk gSA

Syntax - strlwr (char str[ ]);

(5) strcmp ( ) - ;g nks string dks compare djrk gSA vkSj same gksus ij zero

return djrk gSA ;g function case sensitive function gSA

Syntax - strcmp (char s1[ ],char s2[ ]);

(6) strcmpi ( ) - ;g nks string dks compare djrk gSA vkSj same gksus ij zero

return djrk gSA ;g function case sensitive function ugh gksrk gSA

Syntax - strcmpi (char s1[ ],char s2[ ]);

(7) strcat ( ) - ;g function nks string dks joint/concate djus ds fy, use
fd;k tkrk gSA

Syntax - strcat (char d[ ],char s[ ]);

(8) strcpy ( ) - ;g function fdlh ,d string dks nqljs character array esca opy djus ds fy, use
fd;k tkrk gSA

Syntax - strcpy (char d[ ],char s[ ]);

Example:-

#include<string.h>

#include<stdio.h>

#include<conio.h>

void main( )

int size;

char n1 [50],n2[50],n3[100];

clrscr ( );
strcpy(n1, “PURNIMA”);

strcpy(n2, “PooJa”);

strcpy(n3, “sarita”);

size = strlen(n1);

printf(“\n %s having %d character”,n1,size);

strlwr(n3);

puts(n3);

strupr(n3);

puts(n3);

if (strcmp(n1,n2)==0)

printf(“same”);

else

printf(“not same”);

if (strcmpi(n1,n2)==0)

printf(“same”);

else

printf(“not same”);

strrev(n3);

puts (n3);//atiras

strcat(n2,n3);

puts(n2);

getch ( );

Q.1 Write a program to enter password and check is it “SP10”.

#include<string.h>

#include<stdio.h>

#include<conio.h>
void main( )

char password[20];

clrscr ( );

printf(“Enter your password : ”);

gets(password);

if (password, (“SP10”)==0)

printf(“valid password”);

else

printf(“not valid password”);

getch ( );

Structure :- Structure ,d user ds }kjk create fd;k x;k derived data

type gS A ftls “struct” key – word ds }kjk declare djrs gS A program

ing me structure dk use hetrogenous ( fofHkUu izdkj ) data element

dks logically combine djus ds fy, fd;k tkrk gS A

Syntax :- struct name of structure

// declare of data set

};

Ex. (1) struct my data

int a;

flout b;

};

struct my data v1 v2 v3
ex. (2) :- struct employce

int id , age

char name [50] address [100];

Struct emploce e1, e2, e3, e4

* fdlh Hkh structure ds variable dk size mlesa ekStqn data set ds izR;sd

element ds size ds sum ds cjkcj gksrk gS A

* structure ds data number dks access djus ds fy, (.) dot – operator

dk use djrs gS A

Example:-

#include<stdio.h>

#include<conio.h>
struct student

char name[40],address[60];

int age;

void main ( )

struct student s;

printf(“\n Enter name : ”);

gets(s . name);

printf(“\n Enter address : ”);

gets(s . address);

printf(“\n Enter age : ”);

scanf(“%d”,&s.age);

printf(“\n %s%s%d”,s.name,s.address,s.age);

getch ( );

Union :- “union” user ds }kjk create fd;k x;k ,slk derived data type

gS A ftls union key – word ke use ls cuk;k tkrk gS A fdlh Hkh union me hetrogenous
collection of element gksrs gS A ,ao fdlh Hkh le; dsoy ,d gh

element use gks ldrk gS A union ka size mlesa ekStqn biggest element

ke size ij depend djrk gS A

ex. :- union data

int a ;

float b ;

}
union data v;

#include<stdio.h>

#include<conio.h>

union data

int a;

float b;

void main ( )

union data v;

v.a = 10;

v.b = 25.79f;

printf(“%d”,v.a);//garbage

printf(“%f”,v.b);//25.79

v.a = 25;

printf(“%d ”,v.a);//25

printf(“%d”v.b);//garbage

getch ( );

Pointer in C-language :-,d special variable gS A tks fdlh nqljs variable ;k

address ko store djus dk dk;Z djrk gS A


Syntax :- data type *…….* pointer ;

Exampe:- int a,*p,**pp;

dksbZ Hkh pointer variable ka address store djrk gS A mldk data type and pointer ka data
type same gksuk pkfg, A

Example:- ;fn dksbZ variable integer type dk gSA rks mldk pointer Hkh integer type dk gh gksuk
pkfg, A

#include<stdio.h>

#includeconio.h>

void main ( )

int a = 10, *b = 20;

int *pa = &a,*pb = &b;

printf(“\n value of a = %d b = %d”,a,b);//10,20

printf(“\n address of a = %u b = %u”,&a,&b);//2000,2002

printf(“\n address of a = %u b = %u”,pa,pb);

printf(“\n address of a = %u b = %u”,&pa,&pb);

getch ( );

pointer of pointer :- og variable tks Lo;a esa pointer gksrs gq, fdlh nqljs
pointer ks address store djrk gS mls pointer of pointer dgrs gS A

ex. :- int *p , **a;

printer of pointer <- Q = &P;

dksbZ Hkh pointer ftl pointer dks point djrk gS mls pointer to pointer dgrs gS A,oa ,sls pointer esa
,d T;knk “*” (star) gksrk gS A

Example:-

#include<stdio.h>

#includeconio.h>

void main ( )

int a = 10, *p = &a,**p = &p;

printf(“\n value of a = %d ”,a);//10

printf(“\n %u”,&a);//address of a

printf(“\n %u”,p);//address of a

printf(“\n %u”,&p);//address of p

printf(“\n %u”,q);//address of p

printf(“\n %u”,&q);//address of q

getch ( );

pointer dereferencing :- fdlh Hkh pointer ij “*” astrick apply djds

mlds process dks pointer dereferencing dgrs gS A


Example:-

#include<stdio.h>

#includeconio.h>

void main ( )

int a = 10, *p = &a,**q = &p,***r = &q;

printf(“%d%u%d%u%d”,*&a,p,*p,*q,**q);

printf(“%u%u%d”,*&r,**r,***r);

getch ( );

void pointer :- void pointer ,d ,slk pointer gS A ftls void keyword ds

lkFk declare djrs gS og void pointer dgykrk gS A vkSj ,slk pointer

fdlh Hkh data type variable dks address store dj ldrk gS A


Q.1 Write a program to perform addition on 2 number with the help

of pointer .

#include<stdio.h>

#include<conio.h>

void main( )

int a,b*pa,*pb;

pa = &a;

pb = &b;

printf(“Enter 2 number”);

scanf(“%d%d”,&pa,&pb);

printf(“%d”,*pa,*pb);

getch ( );

}
Q.2 Write a program to perform swaping on 2 number with the help

of pointer .

#include<stdio.h>

#include<conio.h>

void main( )

int a,b*pa,*pb,z;

pa = &a;

pb = &b;

printf(“Enter 2 number”);

scanf(“%d%d”,&a,&b);

z = *pa;

*pa = *pb;

*pb = z;

printf(“after swap a = %d%d”,a,b);

getch ( );

Memory allocation :- computer me data processing ds fy, use gksus okys

data lcls igys memory me store fd;s tkrs gS A tk¡gk ls

processing ds fy, mUgs Hkstk tkrk gS A

Memory allocation eq[; nks izdkj ls fd;k tkrk gS A

1. Static memory allocation / compile time memory allocation


2. Dynamic memory allocation/ run time memory allocation
1. Static memory allocation:- bl mode esa create fd x;h memory

space fixd size esa gksrh gS A tks program dks compile djrs le; gh

desaid gks tkrk gS A ,ao blds size dks change ugh fd;k tk ldrk A

Ex. :- Array int A[10];

2. Dynamic memory allocation :- program ds execution ds time

ij allocate fd;s tkus okys memory ftl izdkj ls allocate fd tkrh gSA mls

Dynamic memory allocation dgrs gSA vFkkZr run time allocation dynamic

allocation dgykrk gS A ;g allocation mode esa gesa flexibility proved djrk

gS ftlds }kjk allocate gksus okys memory ds size dks increce ;k decrece

fd;k tk ldrk gS A

C – language esa dynamic memory allocation ds fy, dqN function use

fd;s tkrs gS A ftUgs dynamic memory allocation function dgrs gS A ;s

function fuEufyf[kr gS A

<stdlip.h><alloc.h>
(c) memory resize

P = (int *) realloc (pointer,new size);

P = (int *) malloc (size_in_byte);

Note:- p is a integer pointer

Q = (float *) malloc (size_in_byte); Note:- Q is a float pointer

Ex.- p = (int *)malloc(50); or p = (int *) malloc (size of (int) *25);

p = (int *)calloc(number element ,size of each element);

Ex. :- p = (int *) calloc (15,2);

Q.1 write a program to enter and print n number.

Program –

#include<stdlip.h>

#include<stdio.h>

#include<conio.h>

void main( )

int I,n,*p;

printf(“How many number do you want”);

scanf(“%d”,&n);

[ p = (int *) calloc (n,2) : //p[n] ]

printf(“Enter %d number”n);
for (i = 0 ; i<n; i++)

scanf(“%d”,&p[i]);

printf(“\n number of array :”);

for (i = 0 ; i<n; i++)

printf(“%d”,p[i]);

free(p);

getch ( );

malloc :- ;g dynamic memory allocation function gS A ftlesa single

parameter gksrk gS A ;g function fn;s x;s parameter ds vuqlkj mrus

gh byte dh memory space allocation djrk gS A

Exmple:- int *p = (int *) malloc (24);

char *p = (char *) malloc(20);


Calloc :- ;g dynamic memory allocation function gS A ftlesa memory

allocate djus ds fy, nks parameter pass fd;s tkrs gS A ftlesa igyk

parameter number of element ,o nqljk parameter size of element dks

represent djrk gS A

Exmple:- float *p = (float *) calloc (2,4);

char *p = (char *) calloc(20,1);


realloc :- ;g function dynamically allocated memory dks resize ds fy,

fd;k tkrk gS A

eg. – datatype.1 *p = (datatype 1*) malloc(n* x);

p = (datatype realloc (p,m*x);

free function :- ;g function fdlh pointer dks dynamically allocate dh

x;h memory space dks free ;k deallocate djus ds fy, fd;k tkrk gS A

syntax :- free (pointer )

function :- function is group of instruction vFkkZr fdlh Hkh function esa

set of instruction gksrs gS tks dqN special operation djus ds fy, fd;k tkrk gSA

programming esa use gkus okys function dks eq[; nks izdkj ls classify fd;k x;k gS A
[1] pre defined function :- pre defined function os function gksrs gS ftlesa

function igys ls cuk gksrk gS ftls change ugh fd;k tk ldrk gS A pre defined

function dgykrk gS A

tSls :- printf ,scanf, getch etc.

Q.1 Write a program to add 2 number with the help pre define

function.

#include<stdio.h>

#include<conio.h>

void add( )

int a,b,c;

printf(“Enter 2 number”);

scanf(“%d%d”,&a,&b);

c = a + b;

printf(“\n sum of %d and %d is %d”,a,b,c);

}
void main ( )

clrscr ( );

add ( );

add ( );

add ( );

getch ( );

[2] user defined function :- user defined function os function gksrs gS

ftlesa user vius vko”;drkuqlkj function ;k statement defined djrk gS A

Q.1 Write a program to add 2 number with the help usre define

function.

#include<stdio.h>

#include<conio.h>

void clrscr( )

int a,b,c;

printf(“I cunt clear the screen”);

void main ( )

clrscr ( );

getch ( );

Function defination :- fdlh Hkh function esa fy[ks x;s set of instruction dks

ml function dk function defination dgrs gS A

void add (int x, int y)


{

int ans;

ans = x + y;

printf(“sum = %d”,ans);

void main ( )

add (2,5);

formula parameter :- function difenation ds LFkku ij mi;ksx gksus okys

function dks formal parameter dgrs gS A

Actual parameter :- calling place ij function esa send fd;k data actu

-al parameter dgykrk gS A

Function prototype :- structure or format ko function ka prototype


dgk tkrk gS A

syntax :- returntype function,name(list parameter );

void fun(void);

void add(int,int,int);

Example:-

#include<stdio.h>

#include<conio.h>

void multiply (int,int,int,int);//prototype compulsory because

function defined below main

multiply(2,3,4,9);

multiply(7,3,1,7);

}
void multiply (int a,int b,int c,int d);

int ans;

ans = a*b*c*d;

printf(“%d”,ans);

getch ( );

(2)

#include<stdio.h>

void multiply (int a,int b,int c,int d);

int ans;

ans = a*b*c*d;

printf(“ans = %d”,ans);

void main ( )

multiply(2,3,4,9);

multiply(7,3,1,7);

(3)

#include<stdio.h>

int y ( )

return 2;

int x ( )
{

return 5*y( );

void main ( )

int v;

v = x( );

printf(“%d”,v);//10

(4)

#include<stdio.h>

#include<conio.h>

void add(int x,int y )

return x + y;

void main ( )

int a,b,ans;

printf(“Enter 2 number”);

scanf(“%d%d”,&a,&b);

ans = add(a,b);

printf(“sum of %d and %d = %d”,a,b,ans);

getch ( );

}
classification of function according prototype :-

prototype ds vuqlkj function dks pkj izdkj ls classify fd;k tk ldrk gS -

[1] no argument and no return type

Void fun name (void) ; void add (void) ;

[2] argument and no return type

Void fun name (list of arg); void add (int , int);

[3] no argument and but return type

Return type fun name (void) ; int add (void);

[4] argument and return type both

Return type fun name (list of arg ); int add (int , int);

[1] no argument and no return type -

#include<stdio.h>

#include<conio.h>

void add(void);//prototype

void add(void)

int a,b,ans;

printf(“Enter 2 number”);

scanf(“%d%d”,&a,&b);
ans = a + b;

printf(“ %d ”,ans);

void main ( )

add ( );

getch ( );

[2] argument and no return type-

#include<stdio.h>

#include<conio.h>

void add(int,int);

void add(int x,int y)

int ans;

ans = x+y;

printf(“%d”,ans);

void main ( )

add (2,3);

add (5,7 );

add(15,20);

getch ( );

[3] no argument and but return type-


#include<stdio.h>

#include<conio.h>

int add(void);

int add(void)

int a,b,ans;

printf(“Enter 2 number”);

scanf(“%d%d”,&a,&b);

ans = a + b;

return ans;

void main ( )

int a;

a = add ( );

printf(“%d”,a);

getch ( );

Call by value –

#include<stdio.h>

#include<conio.h>

void add(int,int);

void add(int x,int y)

int ans;

ans = x+y;

printf(“%d”,ans);
}

void main ( )

int a,b;

printf(“Enter 2 number”);

scanf(“%d%d”,&a,&b);

add(a,b);

getch ( );

Call by address –

#include<stdio.h>

#include<conio.h>

void add(int,int);

void add(int x,int y)

int ans;

ans = *x + *y;

printf(“%d”,ans);

void main ( )

int a,b;

printf(“Enter 2 number”);

scanf(“%d%d”,&a,&b);

ans = add(&a,&b);

getch ( );

}
[4] argument and return type both –

#include<stdio.h>

#include<conio.h>

void add(int,int);

void add(int x,int y)

int ans;

ans = x+y;

return ans;

void main ( )

int a,b,ans;

printf(“Enter 2 number”);

scanf(“%d%d”,&a,&b);

ans = add(a,b);

printf(“%d”,ans);

getch ( );

(2)

#include<stdio.h>

#include<conio.h>

int add(int,int);

int add(int x,int y)

int ans;
ans = *x + *y;

return ans;

void main ( )

int a,b,ans;

printf(“Enter 2 number”);

scanf(“%d%d”,&a,&b);

ans = add(&a,&b);

printf(“%d”,ans);

getch ( );

Methods of function calling :-

[1] function dks call djrs le; mlesa pass fd;s x;s actual parameter ks

size ke according function calling dks eq[; nks izdkj ls classify fd;k
x;k gS A

1. call by value

2. call by address

1. call by value :- bl izdkj ds function calling me function ke calling

place ij actual parameter ds :Ik esa dksbZ value ;k constand literal

function me pass fd;k tkrk gS A function ke definition place ij pass

fd;s x;s data ke according formal parameter use fd;s tkrs gA vkSj ;fn

formal parameter esa dksbZ Hkh changing fd;k tkrk gS rc bldk dksbZ Hkh effect

actual parameter variable ij ugh gksxk A

Example:- fn;s x;s example esa actual parameter ds :i esa main function ke variable a dh
value function ds formal parameter x ko increment djus ij actual parameter a dh value esa
dksbZ change ugh gks jgkA

(1)
#include<stdio.h>

#include<conio.h>

void fun (int x)

x++;

void main ( )

int a = 10;

printf(“%d”,a);//10

fun(a);

printf(“%d”,a);//11

getch ( );

(2) Write a program to define function to perform swapping the data of passed variable
by using call by value (not possible)

* call by value ds }kjk swapping possible ugh gksrk gSA tSlk dh example esa show fd;k x;k gSA

#include<stdio.h>

#include<conio.h>

void swap (int x,int y)

int z;

z = x;

x = y;

y = z;

void main ( )
{

int a = 10,b = 20;

swap (a,b);

printf(“a = %d b = %d”,a,b);

getch ( );

2. call by address – bl izdkj ds function calling esa function ds calling place ij actual
parameter ds :i esa variable ke address ko pass fd;k tkrk gSA tks fd pass fd;s x;s variable ds
address ko store djrk gSA ,oa dereferencing ds }kjk store fd;s x;s dsta ko access dj ldrk gSA
ftlls ;g lEHko gS fd fdlh Hkh variable ko scope ds ckgj Hkh access fd;k tk ldrk gSA

Example :- fn;s x;s program esa main function ds variable a dh address function defination esa
pointer dereferencing ds }kjk a dh value dks increment fd;k tkrk gSA

#include<stdio.h>

#include<conio.h>

void fun(int *p);

*p = *p+1;

void main ( )

int a = 10;

printf(“%d”,a);//10

fun(&a);

printf(“%d”,a);//11

getch ( );

2.

#include<stdio.h>
#include<conio.h>

void swap (int *x,int *y)

int z;

z = *x;

*x = *y;

*y = z;

void main ( )

int a = 10,b = 20;

swap (a,b);

printf(“a = %d b = %d”,a,b); //a = 20,b =10

getch ( );

Macro – Macro small set of micro instruction gksrs gSA ftls #define preprocessor directiveds
lkFk difinefd;k tkrk gSA ;s macro compile time process gks tkrs gSA ,oa used placeij replace gks
tkrs gSA

Q.1 Write a program to define square (x²).

#include<stdio.h>

#include<conio.h>

#define sqr (x) (x*x)

void main ( )

int ans;

ans = sqr(2);

printf(“%d”,ans);

getch ( );
}

Q.2 Write a program to define square (x³).

#include<stdio.h>

#include<conio.h>

#define cube x, (x*x*x)

void main ( )

int ans;

ans = cube(3);

printf(“%d”,ans);

getch ( );

Q.3 Write a program to define addition (a+b).

#include<stdio.h>

#include<conio.h>

#define add (a,b) (a+b)

void main ( )

int ans;

ans = add(a,b);

printf(“%d”,ans);

getch ( );

Pointer of structure :- fdlh Hkh structure ds pointer }kjk store fd;s

x;s structure ds variable ds element dks access djus ds fy, ‘→’ ;k

‘ *’ operator dk use fd;k tkrk gS A

Syntax – Access by pointer


Example:-

#include<stdio.h>

#include<conio.h>

struct demo

int a;

float b;

void main ( )

struct demo s,*p;

p = &s;

s.a = 10;

s.b = 20.25;

printf(“%d%f%d%f”,s.a,s.b,p → a,p → b);

getch ( );

Pointer to / of array :- array dk ,d pointer array fdlh Hkh index

element dk address store dj ldrk gS A

array ds first element ;k index [0] ds starting address dks base

address dgrs gS A

ex. int x [0]

base address &x [0]


base address x

pointer array dk address store djus ds ckn ;fn increment fd;k tkrk gS

rks ml fLFkfr esa og next address of index ij igq¡p tkrk gS A vkSj pointer ds

}kjk array ds element dks access djus ds fy, dereferencing ;k index

dk use fd;k tkrk gS A

Example:- int x[10];

int *p;

p = &x[0]; //p = x

p[index] or x[index]*p

p[0] = 10; or *p = 10;

x[1] = 20; p++;

*p = 25;

int x[10];

int *p;

p = x;

for (a=1; a<=10; a++)


{

scanf(“%d”,p);

p++;

for (a=1; a<=10; a++)

scanf(“%d”,p);

p++

Ex.- int num[ ] = {1,7,9,3,2,4,40};

int *p;

p = num ; or p = &num[o];

int i;

1. for (i=0; i<=6; i++)

printf(“%d%d”,num[0],p[0]);

2. for (i=0; i<=7; i++)

printf(“%d”,*p);

p++;

}
file handalling c- language :-

file :- fdlh Hkh secondary storage esa create fd xbZ space tgk¡ ij data

store fd;k tkrk gS mls file dgrs gS A fdlh Hkh file ls related operation dks

djus ds fy, det of function use fd;s tkrs gS A ftls file handalling dgrs gS A

dksbZ Hkh device tks data produs consume ;k store djrh gS mls Hkh file dgrs

gS A tSls :- key – bord , monitor etc .

(a) How to open file/How to create any file :- fopen(“path”, “mode”);

(b) How to close any opened file :- fclose (file *)

(c) How to write data into the file :-(putc ( ),fprintf ( ),fwrite ( ),etc.

(d) How to read data from the file :- getch ( ),fscanf ( ),fread ( )etc.

(e) How to work on file pointer :- ftell ( ),fseek ( ).

file pointer :- file pointer predefine structure “ file” dk ,d pointer gS


ftldk use “f open” function dks store djus ds fy, fd;k tkrk gS A

syntax – [file *p;]

eg. – FILE *P;

p = fopen(“D:\\folder\\data.txt”, “w”);

or

p = fopen(“data.txt”, “r”);

or

p = fopen(“newdata.txt”, “w”);

(1)fopen ( ) function :- bl function dk use fdlh Hkh file dks create djus ;k

fQj ml ij vyx - vyx operation ds fy, open djus gsrq fd;k tkrk

gS A bl function esa eq[; nks parameter pass fd;s tkrs gS A

first parameter fdlh file dk path ,ao name gS vkSj second parameter file

dk mode gSA tgk¡ mode file ds open fd;s tkus ds objective dks specify

djrk gS fd file dks read operation ;k write ds fy, open fd;k tkrk gSA ;g

fopen function file ds open gksus ij mldk address provide djrk gSA ;fn

file open ;k create ugh gksrh rc ;g “ NULL” provide djrk gS A

syntax – fopen(“file name with path”, “mode of file opening”);

eg. – (1) fopen(“data.txt”, “w”);

;g ,d fresh file “data.txt”, name dh root direactry esa create AdjsxkA

(2) fopen(“F:\\data.txt”, “w”);

(2) fclose ( ) function :- f close function fdlh operand file dks close

djus ds fy, fd;k tkrk gS A fdlh file dks close djus ds fy, mlds file

pointer dks f lose function esa pass fd;k tkrk gS A

Syntax – [fclose (FILE*)]

Example :- FILE *p;

P = (“path and name”, “mode”);

--------------------------------------------
--------------------------------------------

fclose (p);

Q. 1 Write any name into the file.

Program

#include<stdio.h>

void main ( )

FILE *p;

char name[50];

p = fopen (“data.txt”, “w”);

if(p==NULL)

printf(“sorry file is not opened”);

else

printf(“Enter your name”);

scanf(“%s”,name );

fprintf(p, “%s”,name);

fclose(p);
}

2.

Program

#include<stdio.h>

#include<conio.h>

void main ( )

FILE *p;

p = fopen (“data.txt”, “w”);

if(p==NULL)

printf(“sorry file is not opened”);

else

printf(“p, “%s”, “JOJO”);

fclose(p);

getch ( );

Q. 3 Write a program to write your name age any file.

Program

#include<stdio.h>

#include<conio.h>

void main ( )

{
FILE *p;

p = fopen (“details.txt”, “w”);

if(p!==NULL)

printf(“p, “%s%d”, “KHAGESH”, “5”);

fopen (p);

else

printf(“sorry file not opened”);

getch ( );

Q. 4 Write a program to read any name from the file.

Program

#include<stdio.h>

#include<conio.h>

void main ( )

FILE *p;

p = fopen (“deta.txt”, “r”);

if(p!==NULL)

fscanf(“p, “%s”,name);

printf(“%s”,name);

fclose (p);

}
else

printf(“sorry file not opened”);

getch ( );

file :- C- language esa fdlh Hkh file dks open djrs le; f open function esa

file name ds lkFk file mode dk Hkh use fd;k tkrk gS A

;s mode fuEufyf[kr gS :-

Mode Discription

(1) “r” fdlh file dks reading mode esa open djus ds fy, use gksrk gSA

(2) “w” fdlh file dks cukus ds fy, mls write mode esa open djus ds fy, use gksrk gSA

(3) “a” fdlh file dks “append” mode me open djus ds fy, use gksrk gSA

(4) “r+” ;g mode fdlh file ij read and write nkukssa gh operation djus ds fy, file

opening me use gksrk gSA

(5) “w+” ;g mode fdlh file ij write operation djus ds lkFk lkFk read operation

djus ds fy, “w+” mode me opened fd;k tkrk gSA

fprint f () function :- bl function ds }kjk fdlh Hkh file esa formatted data

write fd;k tkrk gS A

syntax – fprintf(FILE *p,char *control string set_of_data_variable);

Exmple :- printf(p, “%s%d”,name,age);

fscanf () function :- bl function dk use fdlh file ls formatted data dks

read djus ds fy, fd;k tkrk gS A

syntax – fscanf(FILE *p,char *control string set_of_data_variable);


Exmple :- scanf(p, “%s%d”,name,&age);

get () function :- bl function dk use fdlh file ds data dks character by

character read djus ds fy, fd;k tkrk gS A

syntax :- [ getc ( FILE *p ); ]

getc function fdlh file ds data dks character by character read djrk gS

vkSj file pointer dks next character ij move djrk gS A,ao file ds end ij

igq¡pus ds ckn ;g ,d special character return djrk gS A ftls end of file

(EOF) dgrs gS A

Q. 1 Write a program to read any file tell end of file character by

character .

Program

#include<stdio.h>

#include<conio.h>

void main ( )

char ch ( );

FILE *p;

p = fopen (“deta.txt”, “r”);

if(p!==NULL)

whaile((ch = getc(fp)) ! = EOF)

putch(ch); // printf(“%c”,ch);

fclose (fp);

else
{

printf(“sorry file not opened”);

getch ( );

putc () function :- ;g function fdlh Hkh file esa data character by

character write djus ds fy, iz;ksx fd;k tkrk gS A

syntax – [putc(char, FILE *);]

Q. 1 Write a program to copy the data of one file into the another

file.

Program

#include<stdio.h>

#include<conio.h>

void main ( )

char ch ( );

FILE *fp,*cp;

fp = fopen (“deta.txt”, “r”);

cp = fopen (“copy.txt”, “w”);

if(fp!==NULL&&cp!= NULL)

whaile((ch = getc(fp)) ! = EOF)

putc(ch,cp);

fclose (fp);

fclose (cp);
}

else

printf(“ file not opened”);

getch ( );

Q. 2 Write a program to find the size of file.

Program

#include<stdio.h>

#include<conio.h>

void main ( )

int ,count = 0;

FILE *fp;

fopen (“deta.txt”, “r”);

if(fp!==NULL)

whaile((ch = getc(fp)) ! = EOF)

count++;

printf(“ file size = %dby byte”,count);

fclose (fp);

else

{
printf(“ file not opened”);

getch ( );

Note :- ;fn nks word ds chp esa space ds vykok ‘ _’ ;k ‘,’ bR;kfn dk use fd;k

tk, rks ml fLFkfr esa nh xbZ logic dk use djsxsa A

[if (ch = = ‘ ’ || ch = = ‘ ,’)count++]

Q.1 Write a program to count number of vowel and consonant a

variable in any file.

Programming-

#include<stdio.h>

#include<conio.h>

void main ( )

int v = 0,c = 0;

FILE *fp;

fp = fopen (“data.txt”, “r”);

if(fp!==NULL)

whaile((ch = getc(fp)) ! = EOF)

if((ch>= ‘A’&&ch<= ‘Z’)||( ch>= ‘a’&&ch<= ‘z’))

if(ch>= ‘a’&&ch<= ‘A’||ch>= ‘e’||ch<= ‘E’||ch>‘a’&&ch<=

‘i’||ch>= ‘I’||ch<= ‘o’||ch>= ‘O’&&ch<= ‘u’||ch>= ‘U’)

w++;

else
c++;

printf(“ total v = %d c = %d”,v,c);

getch ( );

Q.2 Write a program to count number of vowel and consonant a

variable in any file.

Programming-

#include<stdio.h>

#include<conio.h>

void main ( )

int count = 0;

FILE *fp;

fp = fopen (“data.txt”, “r”);

if(fp!==NULL)

whaile((ch = getc(fp)) ! = EOF)

if((ch == ‘ ’) count++;

printf(“total words = %d”,count+1);

fclose(fp);

else
printf(“ file not opend”);

getch ( );

fwrite ( ) function :- f write function dk use fdlh structure ds }kjk

record dks write djus ds fy, fd;k tkrk gS A blds vykok other data typedks

Hkh tSls string bR;kfn dks Hkh function dh help ls write fd;k tk ldrk gS A

Syntax :- [ fwrite(count *ptr,size_tsize,size_tmember,FILE *P);]

ptr ,d pointer gS tks ml array dk base address provide djrk gS ftldk data write djuk gS A

struct details d;

size byte esa element ds size dks represent djrk gS ftls write djuk gSA

number of records ;k data dks show djrk gSA FILE bl file dk pointer gksrk gS ftlesa record
fy[krk gS A

Example:- 1. struct details

char name[40];

int age;

struct details d;

struct (d name “PRAKASH KUMAR”);

d age = 28;

fwrite(&d,sizeof(d),p); //p is a file pointerof any opend file

Example :- 2. char name[ ] = (“PRAKASH KUMAR”);

fwrite(name,1,sizeof(name),p);

fread ( ) function :- f read function fdlh Hkh file ls record dk array ;k

string read ds fy, fd;k tkrk gSA bl function esa dqy pkj parameter use
fd;s tkrs gS A

syntax -
[fread (const vord *ptr,size_tsize,size_t_member,FILE,FILE *p);]

ptr ,d pointer gS tks ml array dk base address provide djrk gS ftldk

data read djuk gSA size type esa element ds size dks represent djrk gSA ftls

read djuk gSA number of records ;k data dks show djrk gSA FILE * ml file

dk pointer gksrk gS ftlesa record fy[krk gS A

Example:- 1. struct details

char name[40];

int age;

struct details d;

FILE *p = fopen(“file”, ‘r’);

fread(&d,sizeof(d),1,p);

printf(“%s%d”,d.name,d.age);

file pointer function :- ;g function file pointer ls lacaf/kr dk;Z djus ds fy,

use fd;s tkrs gS A

[1] fseek ( )

[2] ftell ( )

[3] rewind ( )

[1] fseek ( ) function :- ;g function file pointer dks fdlh specific loca-

tion rd move djus ds fy, fd;k tkrk gS A ,d parameter file pointer gksrk gS

vkSj nqljk parameter file of f set gksrk gS A ,ao rhljk parameter of f set rel-

ative position dks show djrk gS A

syntax – [fseek(FILE *,int offset,int from_whare)]

Example:- (p,5,SEEK,CUR);

Or

SEEK_SET
Or

SEEK_END

Q.1 Write a program to print dateof any file from 5 character to the

Program

#include<stdio.h>

void main ( )

char ch;

FILE *p;

p = fopen(“data.txt”, “r”);

if(p!=NULL)

fseek(p,5,SEEK_SET);

while(1)

ch = get(p);

if(ch == EOF) break;

putc (ch);

else

printf(“Error”);

ftell :- ;g function file pointer dh position dks find djus ds fy, fd;k tkrk gSA

,ao ;g ,d integer value return djrk gS tks file pointer dh position gksrh gSA

fd og fide ds beginig ls fdruk byte vkxs gS A

syntax - [ftell ( FILE *)]


Example :- int pos;

FILE *p = fopen(“file.txt”, “r”);

pos = ftell(P);

printf(“%d”,pos); //0

fseek(p,5,SET_CUR);

pos = ftell (P);

printf(“%d”,pos); //5

maths function :- < math.h > file esa dqN predefine void function ekStqn

gSA ftudk use dqN fo”ks’k operation djus ds fy, fd;k tkrk gS A

#include<math.h>

#include<stdio.h>

#include<conio.h>

void main ( )

int a,b;

a = pow(12,3);

printf(“%d”,a);

b = sqrt (30);

printf(“%d”,b);

getch ( );

Example :- (1) aᵇ eg. 12³

Pow(a,b);

Date and time function :-

[1] get data function :- bl function dk use c- ds program es system

dh date dks chack djus ds fy, fd;k tkrk gSA function esa parameter ds :Ik

esa struct data dk variable addres pass fd;k tkrk gSA ,ao function use gksus
ds ckn struct data variable ls mlds data member ds :Ik esa day month and

year dks print fd;k tk ldrk gS A

struct data

int da_year; /*current year */

char da_day; /* day of the month */

char da_mon; /* month (1 = jan) */

};

Syntax – [ getdate (struct date *);]

Example :- Write a program to get date function?

Program

#include<stdio.h>

#include<conio.h>

#include<dos.h>

void main ( )

char month [20];

struct date d;

clrscr ( );

get date (&d);

switch (d,da_mon)

case1 : strcpy (mon “JANUARY”); break;

case2 : strcpy (mon “FEBRUARY”); break;

case3 : strcpy (mon “MARCH”); break;

case4 : strcpy (mon “APRIL”); break;

case5 : strcpy (mon “MAY”); break;


case6 : strcpy (mon “JUN”); break;

case7 : strcpy (mon “JULY”); break;

case8 : strcpy (mon “AUGUST”); break;

case9 : strcpy (mon “SEPTEMBER”); break;

case10 : strcpy (mon “OCTOBER”); break;

case11 : strcpy (mon “NOVEMBER”); break;

case12 : strcpy (mon “DECEMBER”); break;

default :

printf(“\n year = %d”,d.da_year);

printf(“\n mon = %s”,mon);

printf(“\n day = %d”,d.da_day);

getch ( );

sat date function :- ;g function system dh data dks change djus ;k mls

update djus ds fy, use fd;k tkrk gSAbl function esa struct data ke varia –

ble dk address pass fd;k tkrk gSA ftlesa data dh tkudkjh store gksrh gS A

syntax – setdate(struct date *);

Example :-

#include<stdio.h>

#include<conio.h>

#include<dos.h>

void main ( )

struct date ds,dg;

clrscr ( );

ds.da_day = 25;
ds.da_mon = 3;

ds.da_year = 2026;

getdate (&dg);

printf(“\n year = %d”,dg.da_year);

printf(“\n year = %d”,dg.da_mon);

printf(“\n year = %d”,dg.da_day);

getch ( );

get time function :- ;g function system find dh time dks chack ;k find

djus ds fy, use fd;k tkrk gSA blesa struct time ds variable

ds address pass fd;k tkrk gS A

syntax :- gettime ( struct time * );

set time function :- ;g function system ds time dks change ds fy, ;k

update djus ds fy, use fd;k tkrk gS A

syntax :- settime ( struct time * );

Example :-

#include<stdio.h>

#include<conio.h>

#include<dos.h>

void main ( )

struct time ts,tg;

clrscr ( );

gettime (&tg);

printf(“\n original time ………… \n”);

printf(“\n HOURS = %d”,tg.ti_hour);


printf(“\n MIN = %d”,tg.ti_min);

printf(“\n SEC = %d”,tg.ti_sec);

ts.ti_hour = 2;

ts.ti_hour = 2;

ts.ti_min = 30;

ts.ti_sec = 0;

settime (&ts);

gettime (&tg);

printf(“\n UPDATED time ………….. \n”);

printf(“\n HOURS = %d”,tg.ti_hour);

printf(“\n MIN = %d”,tg.ti_min);

printf(“\n SEC = %d”,tg.ti_sec);

getch ( );

Command line argument :- command user interface mode esa fdlh

program dks execute djrs le; tks parameter program

name ds lkFk main function esa Hkstk tkrk gS Amls line argument dgrs gS A

Q.1 write a program to perform addition on tow number by using

command line argument.

Program-

#include<stdio.h>

#include<conio.h>

void main (int c,char *d[ ])

int ans;

ans = atoi(d[1] + atoi(d[2]);

printf(“ans = %d”,ans);
getch ( );

You might also like