Nothing Special   »   [go: up one dir, main page]

CBasic

Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 37

What is C?

=============
C is a Structural programming language Developed by Dennie Richie in 1971 at AT and
T Bell Laboratory in USA.
Programming Language
--------------------
It is special type of Language through which user interact with Computer by
writing some programs.
Exp:-Java,C,C++,Visual Basic etc.
Program
--------
Group of instructions such as Keyword,Syntax,User data written in a File called
Program or Program File.
COMPILER
---------
It is a system software or mechanism which converted user defined program code into
machine understandable code.
So all the program must compiled,at the time of compilation if any Error occur then
it show error message.
INTERPRETER
-----------
Like compiler it is another software or mechanism which interprete program line by
line.
EXECUTER
----------------
It is another mechanism or system software which converted our compiled file into
Object File or .EXE file then provide output.
So it is compulsory to Run or Execute each program , if there is 0 Error and 0
Warning.
NOTE
---------
The extension of C program file is .c
REMARK
---------------
In C ,3 types of window will apear
1.Program Window
2.Output Window
3.Error Window
Program Window
-------------------------
When open C then a program window will appear inside which we write program.This
window contain Compile and Run option.
ERROR WINDOW
--------------
When compile the program if any Error or Mistake occur then this mistake will
appear in Error Window.
OUTPUT WINDOW
----------------
After compiling if No mistake found then Run it.When Run then Output window will
apear.
STRUCTURAL EXPLANATION OF C
---------------------------
/*Write a program to accept any two Numbers then display product of these Numbers
aftermultiplication? */
//This program by Mr.Adhikari
#include<stdio.h>
#include<conio.h>
void main()
{
int x,y,z;
clrscr();
printf("Enter 1st Number\n");
scanf("%d",&x);
printf("Enter 2nd Number\n");
scanf("%d",&y);
z=x*y;
printf("Product of %d and %d is %d\n",z);
getch();
}
Explanation
------------------
COMMENT ENTRY
-----------------------------
It is the non executable lines in a program,because these lines never touch by
system control or processor head.So comment entry are optional.
Comment entry are 2 types
1.Single Line Comment Entry
2.Multi Line Comment Entry
Single Line Comment Entry
-------------------------
Each line of Comment start with // called single line comment Entry.
Multi Line Comment Entry
-------------------------
Multiple Lines of comments Written within /* and */ called multi line comment
Entry.
STATEMENTS
---------------------
Statements are 2 types
1.Single Line Statements
2.Multi Line or Block Statements
Single Line Statements
----------------------
Inside a program each line End with ; called Single line statement or Executable
statement.
Block Statement
---------------
Group of single line statements written within { and } called block
statements or Multi Line statements.
#include
------------
Here # is a preprocessor and include is a Folder or Directory under which all the
header files(Files with Extension .h) are already present. # will execute contains
of header files at first before entering to main().
stdio.h
-------
Here stdio -> standard Input/Output
This header file contain code , responsible for inputting and outputting data.
conio.h
---------
conio-Console input/Output
This header file support to console of system such as keyboard , Monitor etc.
void main()
---------------
main() is the compulsory for all C program because it is the gateway or entrance
point of any C program.
Here void means it return any type of value.
VARIABLE
-------------------
Variable is a named memory location which reserve or allocate a fixed amount of
memory area in RAM or main memory.It is dynamic in nature.
Here no1,no2 and sum are 3 variables.
DATA TYPE
-------------------
It is the characterstic of a variable which indicate to the system only particular
declared type of data should be store to this variable.
Each data type reserved only specified size in main memory.
Some common data types are
DATA TYPE SIZE IN BYTES PURPOSE
========= ============ ========
int 2 Bytes store small
No upt 4 digits.
long int 4 Bytes store Number upto 6 digits No.
char 1 Byte Store Single character
such as a letter.
float 4 Byte Store Floating point No.
upto 3 precession.
double 8 Bytes Store floating point No.
upto 6 prescession No.
signed int 2 Bytes same as int
unsigned int 2 Bytes same as int but store
+ve No.only.
clrscr()
---------
This function is used for clearing screen of output window.
printf()
------------
This function is used for displaying double Quotation String(") or contains of
variable using specified format specifier.
Some common format specifires are
FORMAT SPECIFIERS DATA TYPE
----------------------------- ----------------
%d int
%c char
%s char (when Multiple characters)
%f float
%ld long int
%lf double
%u unsigned int
scanf()
--------
This fuction is used for blinking and when user enter any data from keyboard , it
entered to the specified variable through the format specifiers.
All the Format specifiers used in printf() are also used in scanf().

Example:-
Write a program to accept Name,City,Installment fee and marks in 4 subject of a
student.Then display name,City,Fee and %ge of this student?
Ans:-
#include<stdio.h>
#include<conio.h>
void main()
{
int m1,m2,m3,m4,tot,per;
float fee;
char nm[20],ct[20];
tot=per=0;
clrscr();
printf("Enter Name of the Student\n");
scanf("%s",nm);
printf("Enter City of the Student\n");
scanf("%s",ct);
printf("Enter Installment Fee\n");
scanf("%f",&fee);
printf("Enter marks in Subject1\n");
scanf("%d",&m1);
printf("Enter marks in Subject2\n");
scanf("%d",&m2);
printf("Enter marks in Subject3\n");
scanf("%d",&m3);
printf("Enter marks in Subject4\n");
scanf("%d",&m4);
tot=m1+m2+m3+m4;
per=tot/4;
printf("Name is %s\n",nm);
printf("City is %s\n",ct);
printf("Percenatge in 4 subjects is %d\n",per);
printf("InstallMent fee is %0.2f\n",fee);
getch();
}
OPERATORS
-----------------
Operators are symbols which create a new Expression after combining multiple
Expressions.
Operators are 6 types
1.Relational Operators
2.Logical Operators
3.Assignment Operators
4.SizeOf Operators
5.ArithmaticOperators
6.Tertiary Operators
RELATIONAL OPERATORS
---------------------
The operators those are established relation between two operands or Expression
called relational operator.
Some common relational operators are
> greater than
< Less than
>= Greater Than or Equal To
<= Less than or Equal To
!= Not Equal
== Equality
ASSIGNMENT OPERATORS
------------------------------------
The = symbol is called assignment operator because it assigned or stored value to
the variable.
Exp:
int x=10;
int y=10;
Here x==y returned true value.
Logical Operator
----------------
In generally 3 types of Logical Operators are used
1. && - And
2. || - Or
3. ! - Not
ARITHMATIC OPERATOR
--------------------
These operators made arithmatic calculation.
Some common arithamtic operators are
+ - Add
- - Substract
* - Product
/ - Result after Division
% - Remainder after division
Q:-Write a Program to accept any 2 Numbers then display
addition,Substraction,Product,Result and Remainder after division?
Ans:-
#include<stdio.h>
#include<conio.h>
void main()
{
int x,y;
int x1,x2,x3,x4,x5;
printf("Enter 1st Number\n");
scanf("%d",&x);
printf("Enter 2nd Number\n");
scanf("%d",&y);
x1=x+y;
x2=x-y;
x3=x*y;
x4=x/y;
x5=x%y;
printf("Sum of these Nos is %d\n",x1);
printf("Substarction of these Nos is %d\n",x2);
printf("Product of these Nos is %d\n",x3);
printf("Result after Division is %d\n",x4);
printf("Remainder after Division is %d\n",x5);
getch();
}
Exp
----
Write a program to Display 4000 Secs is how many Hour,Minutes and Seconds?
Ans:-
#include<stdio.h>
#include<conio.h>
void main()
{
int min,hr,sec,rem;
clrscr();
hr=4000/3600;
rem=4000%3600;
min=rem/60;
sec=rem%60;
printf("4000 secs=%d Hour,%d Minutes and %d Seconds\n",hr,min,sec);
getch();
}
TYPES OF ARITHMATIC OPERATOR
---------------------------------------------------------
Arithmatic operators are two types
1.Unary Operator
2.Binary Operator
Binary Operator
-----------------------
THe operator having two operands called binary operator.
Exp:
x+y=10
y-z=9;
Here + and - are two binary operators.
Unary Operator
------------------------
The Operator having single operands called unary operator.
Unary Operators are two types
1)Increment
2)Decrement
Increment or Decrement operators are two types
1.Prefix
2.Postfix
Prefix
-------
In prefix the value of the variable at first increment or decrement then assign or
stored in the variable.
Exp-
int x=10;
x++ => x=x+1 =>x+=1
int y=10;
y-- => y=y-1 => y-=1
Postfix
-----------
In postfix the value of the variable at first stored then increment or decrement
takes place.
Exp:
int x,y;
x=10;
++x => x+1=x
--y => y-1=y
TERTIARY OPERATOR/CONDITIONAL Operator
-------------------------------------------------------
SYNTAX
=======
variable=(condition?Expresion1:Expresion2);
Here ? and : are tertiary operators, condition will check the condition,if
condition satisfy then Expression1 will stored in variable but If condition not
satisfy then Expression2 will stored in variable.
Q:-Write aprogram to accept any 2 Nos then Display larger Value?
Ans:-
#include<stdio.h>
#include<conio.h>
void main()
{
int x,y,z;
clrscr();
printf("Enter No.1\n");
scanf("%d",&x);
printf("Enter No.2\n");
scanf("%d",&y);
z=(x>y?x:y);
printf("Larger number %d\n",z);
getch();
}
Q:-Write a program to enter a Number then display absolute value of it?
Ans:-
#include<stdio.h>
#include<conio.h>
void main()
{
int num,abs;
clrscr();
printf("Enter any Number\n");
scanf("%d",&num);
abs=(num>0?num:-num);
printf("Absolute value of %d is %d\n",num,abs);
getch();
}
SIZEOF OPERATOR
----------------------------
This operator return size of particular data type in Bytes.
Example:
Write an Example for Sizeof operator?
Ans:
#include<stdio.h>
#include<conio.h>
void main()
{
int x;
int boki=200;
x=sizeof(boki);
printf("Size of int data type is %d and value of boki is %d\n",x,boki);
getch();
}
Ex
Press a Capital Letter but display in lower case?
-----
#include<stdio.h>
#include<conio.h>
void main()
{
char ch;
clrscr();
printf("Press a Capital Letter\n");
scanf("%c",&ch);
printf("The Letter in Lower case is %c\n",ch+32);
getch();
}
FLOW OF CONTROL
-----------------------------
When execute a program the control fallow certain path this path is called Flow of
Control.
Control Flow are 3 types
1.Conditional Statements
2.Looping or Iterative Statements
3.Unconditional Statements
Conditional Statements
---------------------------------
The statements fallow certain condition called conditioanal Statements.
It is of three types
i)if....else
ii)if....else if
iii)switch...case
if......else
=======
When a single condition arise in this case this statements is used.If condition
satisfy then control enter to if part otherwise control move to else part.
SYNTAX
------------
if(condition)
{
statements;
}
else
{
statements;
}
Q:Accept any two Nos then display Smaller one?
Ans:
#include<stdio.h>
#include<conio.h>
void main()
{
int x,y;
clrscr();
printf("Enter 1st Number\n");
scanf("%d",&x);
printf("Enter 2nd Number\n");
scanf("%d",&y);
if(x<y)
{
printf("%d is Smaller No\n",x);
}
else
{
printf("%d is Smaller No\n",y);
}
getch();
}
Q44:-Write aprogram to accept a Number then display No. is Even or Odd?
Ans:
#include<stdio.h>
#include<conio.h>
void main()
{
int num;
clrscr();
printf("Enter a Number\n");
scanf("%d",&num);
if(num%2==0)
{
printf("%d is Even No\n",num);
}
else
{
printf("%d is Odd No\n",num);
}
getch();
}

Q3:-Write a program to press any letter from keyboard then check you press a Vowel
or Consonant?
#include<stdio.h>
#include<conio.h>
void main()
{
char ch;
clrscr();
printf("Press any letter from Keyboard\n");
scanf("%c",&ch);
if(ch=='a' || ch=='e' || ch=='i' || ch=='o' || ch=='u')
{
printf("It is a Vowel\n");
}
else
{
printf("It is a Cosonant\n");
}
getch();
}
If... Else If
===============
when multiple conditions arise in this case this statement is used.
SYNTAX
------------
if(condition)
{
statements;
}
else if(condition)
{
statements;
}
else if(condition)
{
.
.
.
}
else
{
statements;
}
Q1:- Write a program to accept any 4 Nos then Display larger one?
Ans-
#include<stdio.h>
#include<conio.h>
void main()
{
int x1,x2,x3,x4;
clrscr();
printf("Enter any 4 numbers\n");
scanf("%d",&x1);
scanf("%d",&x2);
scanf("%d",&x3);
scanf("%d",&x4);
if(x1>x2 && x1>x3 && x1>x4)
{
printf("%d is Larger\n",x1);
}
else if(x2>x1 && x2>x3 && x2>x4)
{
printf("%d is Larger\n",x2);
}
else if(x3>x2 && x3>x1 && x3>x4)
{
printf("%d is Larger\n",x3);
}
else
{
printf("%d is Larger\n",x4);
}
getch();
}
Q:-Write program to accept salary of an Employee then Display
HRA,DA,TA,PF,LIC,TAX,NET Salary to be paid by Employee on This month?
condition
-------------------
Salary >20000 HRA=20%
DA=40%
TA=20%
PF=10%
LIC=7%
TAX=3%
Salary>15000 HRA=15%
DA=30%
TA=15%
PF=8%
LIC=3%
TAX=2%
salary>10000 HRA=10%
DA=20%
TA=10%
PF=5%
LIC=200
NO TAX
Salary<10000 HRA=10%
DA=15%
TA=10%
NO PF
NO TAX
LIC=300
NET=Salary+HRA+DA+TA-PF-TAX-LIC
Ans:-
#include<stdio.h>
#include<conio.h>
void main()
{
float sal,hra,da,ta,pf,net,lic,tax;
clrscr();
printf("Enter salary of the Emplyee\n");
scanf("%f",&sal);
if(sal>=20000)
{
hra=sal*0.2;
da=sal*0.4;
ta=sal*0.2;
lic=sal*0.07;
pf=sal*0.1;
tax=sal*0.03;
}
else if(sal>=15000)
{
hra=sal*0.15;
da=sal*0.3;
ta=sal*0.15;
lic=sal*0.03;
pf=sal*0.08;
tax=sal*0.02;
}
else if(sal>=10000)
{
hra=sal*0.1;
da=sal*0.2;
ta=sal*0.1;
lic=200;
pf=sal*0.05;
tax=0.0;
}
else
{
hra=sal*(10/100);
da=sal*0.15;
ta=sal*0.1;
lic=300;
pf=0.0;
tax=0.0;
}
net=sal+hra+da+ta-pf-lic-tax;
printf("HRA=%0.2f\n",hra);
printf("DA=%0.2f\n",da);
printf("TA=%0.2f\n",ta);
printf("pf=%0.2f\n",pf);
printf("TAX=%0.2f\n",tax);
printf("LIC=%0.2f\n",lic);
printf("NET SALARY=%0.2f\n",net);
getch();
}
Nested if
------------
When one if statement present inside another if statement then it is called nested
if statement.
SYNTAX
-------------
if(condition)
{
statemnts;
if(condition)
{
statements;
}
}
else
{
statements;
}
Q:-Write a program to press any key from key board and check it digit or capital
letter or small letter or vowel or consonant or special character?
#include<stdio.h>
#include<conio.h>
void main()
{
char ch;
printf("Press any Key from Keyboard\n");
scanf("%c",&ch);
if(ch>='1' && ch<='9')
{
printf("U press a Digit\n");
}
else if(ch>='A' && ch<='Z')
{
printf("U press a capital Letter\n");
if(ch=='A' || ch=='E' || ch=='I' || ch=='O' || ch=='U')
{
printf("It is a Vowel\n");
}
else
{
printf("It is a Consonant\n");
}
}
else if(ch>='a' && ch<='z')
{
printf("U press a Small Letter\n");
if(ch=='a' || ch=='e' || ch=='i' || ch=='o' || ch=='u')
{
printf("It is a Vowel\n");
}
else
{
printf("It is a Consonant\n");
}
}
else
{
printf("U press a Special Character\n");
}
getch();
}
SWITCH CASE
=-----------=
It is similar as if elseif statement but difference is that in switch case the
control directly switch over to the particular case.
SYNTAX
---------------
switch(variable)
{
case 1:
statements;
break;
case 2:
statements;
break;
case 3:
statements;
break;
.
.
.
default:
statements;
break;
}
Q:-Write a program to Accept Day No. in a week then Display corresponding day Name?
#include<stdio.h>
#include<conio.h>
void main()
{
int day;
printf("Enter Day No:(1-7)\n");
scanf("%d",&day);
switch(day)
{
case 1:
printf("Day is Sunday\n");
break;
case 2:
printf("Day is Monday\n");
break;
case 3:
printf("Day is Tuesday\n");
break;
case 4:
printf("Day is Wednesday\n");
break;
case 5:
printf("Day is Thrushday\n");
break;
case 6:
printf("Day is Friday\n");
break;
case 7:
printf("Day is Saturday\n");
break;
default:
printf("Sorry enter Day No. 1 to 7 Only\n");
break;
}
getch();
}
LOOPING
---------------
When a particular statement or statements repeatedly Executed in this case Looping
is used.Looping are 3 types
1.while Loop
2.do while loop
3.for loop
While Loop
------------------------
In this statement the control at first check the condition then Enter to loop.This
Loop continue Upto condition not Satisfied or return false value.
SYNTAX
---------------------
while(condition)
{
statements;
}
Q:-Write a program to Display 1st 10 Nos?
#include<stdio.h>
#include<conio.h>
void main()
{
int ctr;
clrscr();
ctr=1;
printf("1st 10 Nos are \n");
while(ctr<=10)
{
printf("%d\n",ctr);
ctr++;
}
getch();
}
Q:-write a program to display 1st 15 even numbers?
Ans:-
#include<stdio.h>
#include<conio.h>
void main()
{
int ctr;
clrscr();
ctr=2;
printf("1st 15 Even Nos are \n");
while(ctr<=30)
{
printf("%d\n",ctr);
ctr=ctr+2;
}
getch();
}
Q:-write a program to accept any 15 numbers then display sum of these number?
Ans:-
#include<stdio.h>
#include<conio.h>
void main()
{
int ctr,sum,num;
clrscr();
ctr=1;
sum=0;
while(ctr<=15)
{
printf("Enter Number%d\n",ctr);
scanf("%d",&num);
sum=sum+num;
ctr++;
}
printf("Sum of the Nos is %d\n",sum);
getch();
}

Example:
------------
Program to display 1st 15 Even Nos as well as Sum and Averages of these Nos?
Ans:
#include<stdio.h>
#include<conio.h>
void main()
{
int ctr,sum,avg;
clrscr();
ctr=2;
sum=0;
avg=0;
printf("1st 15 Even Nos are\n");
while(ctr<=30)
{
printf("%d\n",ctr);
sum=sum+ctr;
ctr=ctr+2;
}
avg=sum/15;
printf("Sum of These Nos is %d\n",sum);
printf("Average of these Nos is %d\n",avg);
getch();
}
Q:- Write a program to accept any 10 Nos and display sum and averages of these Nos?
#include<stdio.h>
#include<conio.h>
void main()
{
int x,num,sum,avg;
sum=0;
avg=0;
x=1;
while(x<=10)
{
printf("Enter No%d\n",x);
scanf("%d",&num);
sum=sum+num;
x++;
}
avg=sum/10;
printf("Sum of these Nos is %d\n",sum);
printf("Averages of these Nos is %d\n",avg);
getch();
}
Q:-Enter any 11 numbers then display larger one as well as sum and averages of Even
Nos only?
Ans:-
#include<stdio.h>
#include<conio.h>
void main()
{
int x,num,sum,avg,large,y;
sum=0;
avg=0;
y=0;
x=1;
large=0;
while(x<=11)
{
printf("Enter No%d\n",x);
scanf("%d",&num);
if(num>large)
{
large=num;
}
if(num%2==0)
{
sum=sum+num;
y++;
}
x++;
}
avg=sum/y;
printf("Sum of Even Nos is %d\n",sum);
printf("Averages of Even Nos is %d\n",avg);
printf("Larger one is %d\n",large);
getch();
}

do...while loop
--------------------
It is same as while loop but difference is that in do while loop the control check
at the End of Loop where as in While loop the control check before entering to
loop.
SYNTAX
------
do
{
statements;
}while(condition);
Q:-Write a program to Display 1st 12 Nos?
#include<stdio.h>
#include<conio.h>
void main()
{
int x=1000;
do
{
printf("%d\n",x);
x++;
}while(x<=12);
getch();
}
q:-Write a program to accept some Nos then Display sum and Averages of these Nos?
Ans:
#include<stdio.h>
#include<conio.h>
void main()
{
int ctr,num,sum,avg;
char papi='y';
clrscr();
ctr=0;
sum=avg=0;
do
{
printf("Enter Number%d\n",ctr+1);
scanf("%d",&num);
sum=sum+num;
ctr++;
printf("Are you continue?(y/n)\n");
papi=getch();
}while(papi=='y');
avg=sum/ctr;
printf("Sum of these Nos only is %d\n",sum);
printf("Average of these Nos is %d\n",avg);
getch();
}
FOR LOOP
--------------------
This loop is similar as while loop or do...while loop but difference is that in
this loop the initalisation,condition and incrementation or decrementation takes
place in a line.This loop executed for fixed No. of times.
SYNTAX
---------------
for(initialise;condition;Increment/Decrement)
{
statements;
}
Q:-Write a program to display 1st 10 Nos?
Ans:-
#include<stdio.h>
#include<conio.h>
void main()
{
int x;
clrscr();
printf("1st 10 Nos are\n");
for(x=1;x<=10;x++)
{
printf("%d\n",x);
}
getch();
}
Q:-Write a program to accept a no. then display factorial of it?
#include<stdio.h>
#include<conio.h>
void main()
{
int x,num,fact=1;
printf("Enter a Number\n");
scanf("%d",&num);
for(x=1;x<=num;x++)
{
fact=fact*x;
}
printf("The factorial of %d is %d\n",num,fact);
getch();
}

Q:-Accept a NO. then display this Number is prime or Composite?


#include<stdio.h>
#include<conio.h>
void main()
{
int num,x,y;
y=0;
clrscr();
printf("Enter a Number\n");
scanf("%d",&num);
for(x=1;x<=num;x++)
{
if(num%x==0)
{
y++;
}
}
if(y==2)
{
printf("%d is a Prime No\n",num);
}
else
{
printf("%d is a Composite No\n",num);
}
getch();
}
Q:-Write a program to accept a No. then display fibonacci Series upto that No?
Ans:-
#include<stdio.h>
#include<conio.h>
void main()
{
int ctr,num,x,y,z;
clrscr();
x=1;
y=1;
printf("Enter a No:\n");
scanf("%d",&num);
printf("First %d Fibonacci series are\n",num);
printf("%d,%d,",x,y);
for(ctr=1;ctr<=num-2;ctr++)
{
z=x+y;
printf("%d,",z);
x=y;
y=z;
}
getch();
}

NOTe
--------
In for loop the initialise part only once execute.
NESTED LOOP
----------------------------
When one loop present inside another loop called nested loop.
SYNTAX1
-----------------
for(--------)
{
for(----------)
{
--------
}
}
SYNTAX2
---------------
while(--------------)
{
for(--------)
{
statements;
}
}
EXAMPLE
----------------
Write a program to Display "Miwaoo" as
Miwaoo Miwaoo Miwaoo
Miwaoo Miwaoo Miwaoo
Miwaoo Miwaoo Miwaoo
?
Ans:
#include<stdio.h>
#include<conio.h>
void main()
{
int x,y;
char nm[20];
clrscr();
printf("Enter your Name:");
scanf("%s",nm);
for(x=1;x<=3;x++)
{
for(y=1;y<=3;y++)
{
printf("%s ",nm);
}
printf("\n");
}
getch();
}
Q:-Write a program to display a half pyramidal structure as
*
**
***
****
*****
******
Ans:-
#include<stdio.h>
#include<conio.h>
void main()
{
int x,y,ht;
clrscr();
printf("Enter height of half Piramid\n");
scanf("%d",&ht);
for(x=1;x<=ht;x++)
{
for(y=1;y<=x;y++)
{
printf("*");
}
printf("\n");
}
getch();
}
Q:-Display Half pyramida structure as
*
* *
* * *
* * * *
Ans:-
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c,ht;
clrscr();
printf("Enter height of half Pyramid\n");
scanf("%d",&ht);
for(a=1;a<=ht;a++)
{
for(b=1;b<=ht-a;b++)
{
printf(" ");
}
for(c=1;c<=a;c++)
{
printf("*");
}
printf("\n");
}
getch();
}
UNCONDITIONAL OR JUMPING STATEMENT
-------------------------------------------------------------
In generally 3 types of jumping statements are used
1.break
2.continue
3.goto
break
------
This statement used for terminating from the loop and proceed from last of loop.
Q-Write a progrmam to accept any 10 Nos and display sum of these Nos but entered
Nos not exceed more than 50?
Ans:-
#include<stdio.h>
#include<conio.h>
void main()
{
int num,sum,x;
sum=0;
for(x=1;x<=10;x++)
{
printf("Enter Number%d\n",x);
scanf("%d",&num);
if(num>50)
{
break;
}
sum=sum+num;
}
printf("Sum of these Nos is %d\n",sum);
getch();
}
continue
---------------
It similar as break statement but difference is that the continue without
terminating from loop proceed from next iteration.
Q-Write a progrmam to accept any 10 Non Negative Nos and display sum of +ve Nos
only?
#include<stdio.h>
#include<conio.h>
void main()
{
int num,sum,x;
sum=0;
for(x=1;x<=10;x++)
{
printf("Enter Number%d\n",x);
scanf("%d",&num);
if(num<0)
{
continue;
}
sum=sum+num;
}
printf("Sum of Positive Nos is %d\n",sum);
getch();
}
goto
------
It is similar as break but control directly jumped to specified label.
Q-Write a program to accept any 10 Nos and display sum of these Nos but entered Nos
not exceed than 50 if by mistake entered then without displaying output display
error message?
#include<stdio.h>
#include<conio.h>
void main()
{
int num,sum,x;
sum=0;
for(x=1;x<=10;x++)
{
printf("Enter Number%d\n",x);
scanf("%d",&num);
if(num>50)
{
goto Khasi;
}
sum=sum+num;
}
printf("Sum of these Nos is %d\n",sum);
Khasi:
printf("Sorry why U entered >50 No");
getch();
}
ARRAY
---------
Array is nothing but a subscripted variable is used for storing multiple location
consecutively.
Array are 2 types
1.Single Dimensional Array
2.Multi Dimensional Array
Single Dimensional Array
--------------------------------------
The Array having Single Row with multiple columns called Single Dimensional Arraay.
Exp:-
int No[10];
Q1:-Write a program to accept any 10 Nos one by one and then display these numbers
at a time?
#include<stdio.h>
#include<conio.h>
void main()
{
int no[10];
int x;
clrscr();
for(x=0;x<10;x++)
{
printf("Enter Number%d\n",x+1);
scanf("%d",&no[x]);
}
printf("All these 10 Nos are\n");
for(x=0;x<10;x++)
{
printf("%d\n",no[x]);
}
getch();
}
Q2:-Write a program to accept any 10 Nos one by one and then display these numbers
at a time in reverse order?
#include<stdio.h>
#include<conio.h>
void main()
{
int no[10];
int x;
clrscr();
for(x=0;x<10;x++)
{
printf("Enter Number%d\n",x+1);
scanf("%d",&no[x]);
}
printf("All these 10 Nos in reverse order are\n");
for(x=9;x>=0;x--)
{
printf("%d\n",no[x]);
}
getch();
}
STRING
-----------
It is nothing but a Character array where \0 play as important Role.
Exp
----
char str[100];
Here \0 is called terminator which indicate the string will end there.
Q:-Enter a String then Display length of it ?
Ans:-
#include<stdio.h>
#include<conio.h>
void main()
{
int x,len;
char str[100];
printf("Enter a string\n");
scanf("%s",str);
for(x=0;str[x]!='\0';x++)
{

}
len=x;
printf("The length ofstring is %d\n",len);
getch();
}
Q:-Enter a String then Display Reverse order ?
Ans:-
#include<stdio.h>
#include<conio.h>
void main()
{
int x,y;
char str[20];
printf("Enter a String\n");
scanf("%s",str);
for(x=0;str[x]!='\0';x++);
y=x-1;
printf("The string in Reverse order is ");
for(;y>=0;y--)
{
printf("%c",str[y]);
}
getch();
}
Q:-Enter a String then Display it is Palindome or Not?
Ans:-
#include<stdio.h>
#include<conio.h>
void main()
{
int x,len,flag=1;
char str[100];
printf("Enter a string\n");
scanf("%s",str);
for(x=0;str[x]!='\0';x++)
{
}
len=x-1;
for(x=0;str[x]!='\0';x++,len--)
{
if(str[x]==str[len])
{
flag=1;
}
else
{
flag=0;
break;
}
}
if(flag==1)
printf("It is a Palindrome\n");
else
printf("It is not a Palindrome\n");
getch();
}
TWo DIMENSIONAL ARRAY
---------------------
Array having multiple rows with multiple columns called 2 or Multi dimensional
Array.
Exp:-
char str[4][5];
Here 4 Rows and 5 Columns are created.
Exp1:-Write a program to Accept any 10 Names one by one then display at a time?
#include<stdio.h>
#include<conio.h>
void main()
{
char nm[10][20];
int x;
for(x=0;x<10;x++)
{
printf("Enter Name%d\n",x+1);
scanf("%s",nm[x]);
}
printf("All these 10 Names are\n");
for(x=0;x<10;x++)
{
printf("%s\n",nm[x]);
}
getch();
}
Exp2:-Write a program to Enter values to tabl1 and table2 then display these tese
values.Made addition of corresponding cell value of 1st table and 2nd table store
in corresponding cell value of third table,then display these values?
Ans:-
#include<stdio.h>
#include<conio.h>
void main()
{
int table1[5][5],table2[5][5],table3[5][5];
int x,y;
printf("Enter Values to table1\n");
for(x=0;x<5;x++)
{
printf("Enter values to Record%d\n",x+1);
for(y=0;y<5;y++)
{
scanf("%d",&table1[x][y]);
}
}
printf("Values in table1 are\n");
for(x=0;x<5;x++)
{
for(y=0;y<5;y++)
{
printf("%d ",table1[x][y]);
}
printf("\n");
}
printf("Enter Values to table2\n");
for(x=0;x<5;x++)
{
printf("Enter values to Record%d\n",x+1);
for(y=0;y<5;y++)
{
scanf("%d",&table2[x][y]);
}
}
printf("Values in table2 are\n");
for(x=0;x<5;x++)
{
for(y=0;y<5;y++)
{
printf("%d ",table2[x][y]);
}
printf("\n");
}
printf("ADD and STORED in Table3\n");
for(x=0;x<5;x++)
{
for(y=0;y<5;y++)
{
table3[x][y]=table1[x][y]+table2[x][y];
}
}
printf("Values in table3 are\n");
for(x=0;x<5;x++)
{
for(y=0;y<5;y++)
{
printf("%d ",table3[x][y]);
}
printf("\n");
}
getch();
}
Exp3:-Write a program to enter values to a matrix then display priginal matrix as
well as display after transposing matrix?
Ans:
#include<stdio.h>
#include<conio.h>
void main()
{
int mat[10][25];
int m,n,r,c;
clrscr();
printf("Enter No. of rows and columns of matrix\n");
scanf("%d",&r);
scanf("%d",&c);
puts("Enter values to Matrix\n");
for(m=0;m<r;m++)
{
for(n=0;n<c;n++)
{
scanf("%d",&mat[m][n]);
}
}
puts("Original Matrix\n");
for(m=0;m<r;m++)
{
for(n=0;n<c;n++)
{
printf("%d ",mat[m][n]);
}
printf("\n");
}
puts("Transpose of Matrix");
for(m=0;m<c;m++)
{
for(n=0;n<r;n++)
{
printf("%d ",mat[n][m]);
}
printf("\n");
}
getch();
}
FUNCTION
------------
Using function we create Multiple programs in a single file.
Functions are 2 types
1.Library function or standard function
2.User defined function
LIBRARY FUNCTION
-------------------------------
Some functions are already present in system and utilised by fallowing header files
called Library Function.
Some common Library functions are
1.printf()
2.scanf()
3.putchar()
stdio.h 4.getchar()
5.puts()
6.gets()
*************************************
conio.h 7.clrscr()
8.getch()
*************************************
9.pow()
math.h 10.sqrt()
11.abs()
*************************************
12.strlwr()
13.strupr()
string.h 14.strlen()
15.strcpy()
16.strcat()
17.strcmp()
-> getchar() accept a single character and assign to char variable.
->putchar() display contains of char variable which passed as parameter to this
function.
->gets() accept a string including blank space from keyboard and stored in string.
->puts() is dispplay double quotation string or string variable.
Exp:
-----
Write a program to accept a small letter but display in upper case?
Ans:-
#include<conio.h>
#include<stdio.h>
void main()
{
char ch;
clrscr();
puts("Press a small Letter");
ch=getchar();
printf("The character in upper case is %c\n",ch-32);
getch();
}
Q2.Write a program to accept your full name then display in upper case?

#include<conio.h>
#include<stdio.h>
#include<string.h>
void main()
{
char str[100];
puts("Enter your full name");
gets(str);
puts("Your full name in Upper case is ");
printf("%s",strupr(str));
getch();
}
->pow() will return power.
->sqrt() will return square root of a Number.
->abs() will return absolute value of number.
Exp:
------
Write a program to accept base and power the display result as well as display
sqare root of this number?
Ans:-
#include<conio.h>
#include<math.h>
#include<stdio.h>
void main()
{
int p,b,res1,res2;
puts("Enter power and base");
scanf("%d",&b);
scanf("%d",&p);
res1=pow(b,p);
res2=sqrt(res1);
printf("%d to the power %d is %d\n",b,p,res1);
printf("Square root of %d is %d\n",res1,res2);
getch();
}
->strupr() convert the string to upper case.
->strlwr() convert the string to lower case.
->strlen() return length of a string.
->strcpy() copy 1st string to 2nd string.
->strcat() append string1 to string2.
->strcmp() compare between two string and return int value.
If return +ve 1st string is Larger
If return -ve 2nd string is Larger
If return 0 then both the strings are equal
Q:-Write a program to accept any two strings in upper case then display in lower
case,length of 1st string and compare between these two string?
Ans:-
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
int x,len;
char str1[100],str2[100];
puts("Enter any two strings in upper case");
gets(str1);
gets(str2);
len=strlen(str1);
printf("Length of 1st String is %d\n",len);
printf("Both the strings in lower case are %s and %s\
n",strlwr(str1),strlwr(str2));
x=strcmp(str1,str2);
if(x>0)
printf("1st string is Larger\n");
if(x<0)
printf("2nd string is Larger\n");
if(x==0)
printf("Both the strings are Equal");
getch();
}
USER DEFINED FUNCTION
---------------------------------
The function defined by user called user defined function.For understanding user
defined functions some common terms should under stand
1.Function Declaration
2.Function Definition
3.Function prototype
4.Calling function
5.Called function
6.Function return type
7.Passing arguments
Example:-
--------
Write a program to accept any two Numbers then display sum of these two Numbers
useing user defined functions?
Ans:
#include<stdio.h>
#include<conio.h>
int Kutta(int a,int b);
void main()
{
int x,y,res;
printf("Enter 1st Number\n");
scanf("%d",&x);
printf("Enter 2nd Number\n");
scanf("%d",&y);
res=Kutta(x,y);
printf("Sum of %d and %d is %d\n",x,y,res);
getch();
}
int Kutta(int a,int b)
{
int c;
c=a+b;
return c;
}
Here main() is say Calling function but Kutta() is say called Function.
Here return keyword return back the calculated value to main().
Function prototype
--------------------
The function must declared before calling function this is called function
prototype.
->The variable declare inside Function declaration called Passing arguments or
Parameters.
->Kutta is say Called Function but main() say calling function.
Passing Arguments to a function are in two ways.
EXAMPLE
-----------
Write a program to accept a Number and Display Factorial of it.As well as accept
base and power , display result using user defined function?
Ans:-
#include<stdio.h>
#include<conio.h>
int Factorial(int no);
int Result(int a,int b );
void main()
{
int num,x,y,res1,res2;
printf("Enter a Number\n");
scanf("%d",&num);
printf("Enter Base and Power\n");
scanf("%d",&x);
scanf("%d",&y);
res1=Factorial(num);
res2=Result(x,y);
printf("Factorial of %d is %d\n",num,res1);
printf("%d to the power %d is %d\n",x,y,res2);
getch();
}
int Factorial(int no)
{
int a,fact=1;
for(a=1;a<=no;a++)
{
fact=fact*a;
}
return fact;
}
int Result(int b,int p)
{
int a,viki=1;
for(a=1;a<=p;a++)
{
viki=viki*b;
}
return viki;
}
PASSING ARGUMENTS TO FUNCTION
-------------------------------------------------------
We can pass arguments to function in two ways.
1.Call by Value
2.Call by Reference
Call By Value
--------------
In this mechanism the value of the variable after copied to called function the
reference between called and calling function not Exist so far ,so any changes
takes place in called function not effect on calling function.
Q:- Write a program to accept any two Nos then display after swapping?
Ans:-
#include<stdio.h>
#include<conio.h>
void swap(int a,int b);
void main()
{
int n1,n2;
printf("Enter any two Nos\n");
scanf("%d",&n1);
scanf("%d",&n2);
swap(n1,n2);
printf("After swapping values are %d and %d\n",n1,n2);
getch();
}
void swap(int a,int b)
{
int temp;
temp=a;
a=b;
b=temp;
printf("Values are %d and %d\n",a,b);
}
Call By Reference
--------------
In this mechanism the value of the variable after copied to called function the
reference between called and calling function still Exist so far ,so any changes
takes place in called function it will directly effect on calling function.
Q:- Write a program to accept any two Nos then display after swapping?
Ans:-
#include<stdio.h>
#include<conio.h>
void swap(int &a,int &b);
void main()
{
int n1,n2;
printf("Enter any two Nos\n");
scanf("%d",&n1);
scanf("%d",&n2);
swap(n1,n2);
printf("After swapping values are %d and %d\n",n1,n2);
getch();
}
void swap(int &a,int &b)
{
int temp;
temp=a;
a=b;
b=temp;
printf("Values are %d and %d\n",a,b);
}
POINTER
A pointer is nothing but a variable which pointing location of another variable.
->All the pointer variable declared with * symbol.
->In pointer & is used for address of a particular variable.
Exp:
int NUM;
int *ptr;
NUM=20;
.
ptr=&NUM;
EXAMPLE
-------------
Q:- Write a program for pointer arithmatic?
Ans:
#include<stdio.h>
#include<conio.h>
void main()
{
int num,*boki;
boki=&num;
printf("Enter a Number\n");
scanf("%d",&num);
printf("Value of num is %d\n",num);
printf("Value of num is also %d\n",*boki);
printf("Address of num is %u\n",&num);
printf("Address of num is also %u\n",boki);
printf("Value of *boki is %d\n",num);
printf("Value of *boki is also %d\n",*boki);
printf("Value of boki is %u\n",&num);
printf("Value of boki is also %u\n",boki);
*boki=100;
printf("Value of num is %d\n",num);
printf("Value of num is also %d\n",*boki);
printf("Address of num is %u\n",&num);
printf("Address of num is also %u\n",boki);
printf("Value of *boki is %d\n",num);
printf("Value of *boki is also %d\n",*boki);
printf("Value of boki is %u\n",&num);
printf("Value of boki is also %u\n",boki);
getch();
}
POINTER TO STRING or ARRAY
------------------------------------------------
In an array or String the pointer bydefault point to address location of 1st data
item.If user need then he can point to any other location.
e.g
char str[100];
char *p;
p=str;
OR
p=str[0];
Q:-Write a prgram to acept a string and display length of it using pointer?
#include<stdio.h>
#include<conio.h>
void main()
{
int x;
char str[100];
char *ptr;
ptr=str;
printf("Enter a String\n");
scanf("%s",str);
for(x=0;*ptr!='\0';x++,ptr++)
{

}
printf("Length is %d\n",x);
getch();
}
SCOPE OF VARIABLE
---------------------------------
Depends upon scope variables are 5 types
1.Local Variable
2.Global Variable
3.Auto Variable
4.Static Variable
5.Register Variable
LOCAL VARIABLE
------------------------------
The variable declared in the particular function only utilised inside that function
only but not used outside of this function.These variables are called local
variables.
GLOBAL VARIABLE
---------------------------
Any variable declared above of the functions these variable utilised every
functions.So these variables are called Global Variables.
EXAMPLE FOR LOCAL VARIABLE
--------------------------
#include<stdio.h>
#include<conio.h>
void function1();
void function2();
void function3();
int x;
void main()
{
x=10;
funtcion1();
printf("Value of x is %d\n",x);
getch();
}
void function1()
{
x=100;
funtcion2();
printf("Value of x is %d\n",x);
}
void function2()
{
x=1000;
funtcion3();
printf("Value of x is %d\n",x);
}
void function3()
{
x=1;
printf("Value of x is %d\n",x);
}
AUTO VARIABLE
----------------------------
The variables declared with auto keyword called auto variable.
All the local variable bydefault auto.The auto variable loose his scope in each
execution.
EXP:-Write program for Auto variable?
Ans:-
#include<stdio.h>
#include<conio.h>
void main()
{
int x;
for(x=1;x<=5;x++)
{
Bokita();
}
getch();
}
void Bokita()
{
auto int Q=1;
Q++;
printf("%d\n",Q);
}
STATIC VARIABLE
-----------------------------
The variable declared with static keyword called static variable.The Static
variable never loose scope until end of the Program.
EXP:-Write program for Static variable?
Ans:-
#include<stdio.h>
#include<conio.h>
void main()
{
int x;
for(x=1;x<=5;x++)
{
Bokita();
}
getch();
}
void Bokita()
{
static int Q=1;
Q++;
printf("%d\n",Q);
}
REGISTER VARIABLE
----------------------------------
In CPU some limited No. of Registers are present.The Register variable declared
with register keyword.Only char and int datatype are used for register variable.
Thease variables are mostly declared for system programming.
Exp:-
register int x1,x2,x3;
Since No. of Registers inside CPU are limited therefore if more variable declared
as Register then 1st variables are Register variable and other are say local
variable.
STRUCTURE
----------
It is a user defined datatype contain disimilar or similar variable of same or
different data type.
A Structure declared with struct key word.
syntax
------
struct <Sturcture Name>
{
datatype var1;
datatype var2;
datatype var3;
.
.
.
};
EXP
----
struct PQR
{
int a;
char b;
float c;
};
For accessing individual components of a structure we use dot(.) operator in case
of simple variable but pointer operator(->) in case of pointer variable.
NESTED STRUCTURE
----------------
since a structure is a Datatype therefore one structure may Exist inside
Another ..............................
000000000000000000000000structure.
SYNTAX
-------
struct <structure1>
{
datatype var1;
datatype var2;
struct <structure2>
{
datatype var3;
datatype var4;
.
.
.
};
};
EXAMPLE
-------
struct XYZ
{
int a1;
int c1;
};
struct PQR
{
int a1;
struct XYZ a2;
};
PQR x1,x2;
Exp:-Enter Employee details of two Employee then display details of Employee who
get more salary including date of joinin?
Ans:-
#include<stdio.h>
#include<conio.h>
struct Date
{
int d;
int m;
int y;
};
struct Employee
{
char nm[20];
char city[20];
int sal;
struct Date dt;
};
void main()
{
struct Employee p1,p2;
int ph1,ph2;
printf("Enter Name,City,Salary of 1st Employee\n");
scanf("%s",p1.nm);
scanf("%s",p1.city);
scanf("%d",&p1.sal);
printf("Enter Date of Joining of 1st Employee\n");
scanf("%d",&p1.dt.d);
scanf("%d",&p1.dt.m);
scanf("%d",&p1.dt.y);
printf("Enter Phone No. of 1st Employee\n");
scanf("%d",&ph1);
printf("Enter Name,City,Salary of 2nd Employee\n");
scanf("%s",p2.nm);
scanf("%s",p2.city);
scanf("%d",&p2.sal);
printf("Enter Date of Joining of 2nd Employee\n");
scanf("%d",&p2.dt.d);
scanf("%d",&p2.dt.m);
scanf("%d",&p2.dt.y);
printf("Enter Phone No. of 2nd Employee\n");
scanf("%d",&ph2);
if(p1.sal>p2.sal)
{
printf("Name of 1st Employee %s\n",p1.nm);
printf("City of 1st Employee %s\n",p1.city);
printf("Salary of 1st Employee %d\n",p1.sal);
printf("Date of Joining of 1st Employee is ");
printf("%d-%d-%d \n",p1.dt.d,p1.dt.m,p1.dt.y);
printf("Phone No. of 1st Employee %d\n",ph1);
}
else
{
printf("Name of 2nd Employee %s\n",p2.nm);
printf("City of 2nd Employee %s\n",p2.city);
printf("Salary of 2nd Employee %d\n",p2.sal);
printf("Date of Joining of 2nd Employee is ");
printf("%d-%d-%d \n",p2.dt.d,p2.dt.m,p2.dt.y);
printf("Phone No. of 2nd Employee %d\n",ph2);
}
getch();
}
FILE HANDLING
-------------------------
In generally 2 types of file are used
1.data File
2.Disk File
->Data file store data in main memory called RAM
->Disk file store data in Disk.
C supports No. of functions such as
1.Nameing File
2.Opening File
3.Reading from a File
4. Writing to the File
5. Closing File
C utilised No. of Library functions for doing all those, such as
fopen() - Create a new file for use.
- Open an Existing file for use.
getc() - Reads a character from a file.
putc() - Writes a character to a file.
fclose() - Closes a file which has been opened
fprintf() - Writes a set of data values to a file.
fscanf() - Reads a set of data values from afile.
getw() - Reads an integer from a file.
putw() - Writes an integer to a file.
fseek() - Sets the position to a desired point in the file.
ftell() - Gives the current position in the file.
rewind() - Sets the position to the begining of the file.
REMARK
----------------
For handling file in C, it support a special file structure called FILE.Before use
at first declare a pointer of FILE structure.
SYNTAX
------------
FILE *fp;
-------------
fp=fopen("filename","mode");
Here filename is any name
Here mode means in which mode it will opened.Some common modes are
r -> Open a file for reading only.
w -> Open a file for writing only.
a -> Open a file for appending data to it.
r+ -> Both reading and writing data to and from file
w+ -> Same as w except both for reading and writing.
a+ -> Same as a except both for reading and writing.
EXP
------
FILE *p1,*p2;
p1=fopen("data","r");
p2=fopen("result","w");

When a file is attempting for opening


1) When the mode is w, a new file created if file not exist.The contents deleted if
file already Exist.
2) When mode is a,the file is opened with current contents safe.If file not exist
then a new file will created.
3) When mode is r ,then the file is opened with current content safe,if file not
exist it show error message.
CLOSING FILE
-----------------------
For closing file fclose() is used .
SYNTAX
------------
FILE *p1;
p1=fopen("input","w");
----
----
----
fclose(p1);
EXAMPLE
------------------
Write a program to write and read in a file.
Ans:-
#include<stdio.h>
#include<conio.h>
void main()
{
FILE *f1;
char c;
printf("Data Input\n\n");
f1=fopen("INPUT","w");
while((c=getchar())!=EOF)
{
putc(c,f1);
}
fclose(f1);
printf("\nData Output\n");
f1=fopen("INPUT","r");
while((c=getc(f1))!=EOF)
{
printf("%c",c);
}
fclose(f1);
getch();
}

You might also like