unit ii c programming basics
DESCRIPTION
UNIT II C PROGRAMMING BASICS - PowerPoint PPT PresentationTRANSCRIPT
![Page 1: UNIT II C PROGRAMMING BASICS](https://reader033.vdocument.in/reader033/viewer/2022051216/56814cf8550346895dba05e1/html5/thumbnails/1.jpg)
UNIT II C PROGRAMMING BASICS
Problem formulation – Problem Solving - Introduction to ‘ C’
programming –fundamentals – structure of a ‘C’ program – compilation
and linking processes – Constants, Variables – Data Types – Expressions
using operators in ‘C’ – Managing Input and Output operations –
Decision Making and Branching – Looping statements – solving simple
scientific and statistical problems.
![Page 2: UNIT II C PROGRAMMING BASICS](https://reader033.vdocument.in/reader033/viewer/2022051216/56814cf8550346895dba05e1/html5/thumbnails/2.jpg)
Problem formulation
•Effective problem formulation is fundamental to the success of all analysis,
but particularly in Command and Control assessment because the problems are
often ill-defined and complex, involving many dimensions and a rich context.
•Problem formulation involves decomposition of the analytic problem into
appropriate dimensions such as structures, functions, mission areas.
•Problem formulation is an iterative process that evolves over the course of
the study.
•It is essential even for small studies or where time is short; it will
save time later and help ensure quality.
![Page 3: UNIT II C PROGRAMMING BASICS](https://reader033.vdocument.in/reader033/viewer/2022051216/56814cf8550346895dba05e1/html5/thumbnails/3.jpg)
•The problem formulation phase should identify the context of the
study and aspects of the problem related issues.
•There is no universally acceptable approach to problem
formulation. However, best practices exist that can be applied.
•First find out what the question is then find out what the real
question is.
![Page 4: UNIT II C PROGRAMMING BASICS](https://reader033.vdocument.in/reader033/viewer/2022051216/56814cf8550346895dba05e1/html5/thumbnails/4.jpg)
PROBLEM SOLVING
•When we starts reading these and wants to learn how to solve problem by
using a computer, it is first of all important to understand what the problem
is.
•We need to read the problem statement a number of times to ensure that
it understands what is asked before attempting to solve the problem.
Method of Problem Solving
1. Recognize and understand the problem.
2. Accumulate facts.
3. Select appropriate theory.
4. Make necessary assumptions.
5. Solve the problem.
6. Verify results.
![Page 5: UNIT II C PROGRAMMING BASICS](https://reader033.vdocument.in/reader033/viewer/2022051216/56814cf8550346895dba05e1/html5/thumbnails/5.jpg)
•Performing step 5 Solve the problem may involve a computer.
•The 5 steps in using a computer as a problem-solving tool
1. Develop an Algorithm and a Flowchart.
2. Write the program in a computer language.
3. Enter the program into the computer.
4. Test and' debug the program.
5. Run the program, input data, and get the results from the computer
![Page 6: UNIT II C PROGRAMMING BASICS](https://reader033.vdocument.in/reader033/viewer/2022051216/56814cf8550346895dba05e1/html5/thumbnails/6.jpg)
Introduction to ‘ C’ programming
•C was developed by Dennis Ritchie at Bell laboratory in 1972
•It is an upgrade version of languages B and BCPL.
Features of C
•It is a structured programming language.
•It is highly portable.
•It is a middle level language.
•It is a case sensitive language.
•It uses Top-Down approach.
•It is a Free form language.etc,.
![Page 7: UNIT II C PROGRAMMING BASICS](https://reader033.vdocument.in/reader033/viewer/2022051216/56814cf8550346895dba05e1/html5/thumbnails/7.jpg)
Steps in learning C
CharacterSet
ProgramsInstructionsTokens
![Page 8: UNIT II C PROGRAMMING BASICS](https://reader033.vdocument.in/reader033/viewer/2022051216/56814cf8550346895dba05e1/html5/thumbnails/8.jpg)
C Character Set
C Character Set
Execution Character Set
Source Character Set
SpecialCharacters
DigitsAlphabets EscapeSequence
WhiteSpaces
![Page 9: UNIT II C PROGRAMMING BASICS](https://reader033.vdocument.in/reader033/viewer/2022051216/56814cf8550346895dba05e1/html5/thumbnails/9.jpg)
C Character Set (Cont)
Source Character Set
It is used to construct the statements in the program.
Executable Character Set
These characters are employed at the time of execution i.e. they
have effects only when the program is being executed.
![Page 10: UNIT II C PROGRAMMING BASICS](https://reader033.vdocument.in/reader033/viewer/2022051216/56814cf8550346895dba05e1/html5/thumbnails/10.jpg)
Source Character SetLetters a to z ,A to Z
Digits 0 to 9
Special Characters ! @ # $ % ^ & * ( ) _ - + = \ | { } [ ] etc,.
White Spaces Blank Space ,Horizontal tab, New line, Vertical tab etc,.
![Page 11: UNIT II C PROGRAMMING BASICS](https://reader033.vdocument.in/reader033/viewer/2022051216/56814cf8550346895dba05e1/html5/thumbnails/11.jpg)
Special charactersComma , Period or dot .Semicolon ;Colon :Apostrophe ‘Quotation mark “Exclamation mark !Vertical bar | Back Slash \Tilde ~Underscore -Dollar $Question mark ?
![Page 12: UNIT II C PROGRAMMING BASICS](https://reader033.vdocument.in/reader033/viewer/2022051216/56814cf8550346895dba05e1/html5/thumbnails/12.jpg)
Ampersand &Caret ^Asterisk *Minus -Addition +Lesser than <Greater than >Parenthesis ()Bracket []Braces {}Percentage %Hash #Equal to =At the rate @
![Page 13: UNIT II C PROGRAMMING BASICS](https://reader033.vdocument.in/reader033/viewer/2022051216/56814cf8550346895dba05e1/html5/thumbnails/13.jpg)
Executable Character Set
Characters Escape Sequence
Back Space \b
Horizontal Space \t
Vertical Space \v
Newline \n
![Page 14: UNIT II C PROGRAMMING BASICS](https://reader033.vdocument.in/reader033/viewer/2022051216/56814cf8550346895dba05e1/html5/thumbnails/14.jpg)
C Tokens
The smallest element in the C language is the token.
It may be a single character or a sequence of characters.
![Page 15: UNIT II C PROGRAMMING BASICS](https://reader033.vdocument.in/reader033/viewer/2022051216/56814cf8550346895dba05e1/html5/thumbnails/15.jpg)
C Tokens (Cont)
C Tokens
Identifiers
Eg:main,avg
Keywords
Eg: int,for
operators
Eg: + -
Strings
Eg: “ab”
spIsymbolEg: #$ %
Constants
Eg:17,15.5
![Page 16: UNIT II C PROGRAMMING BASICS](https://reader033.vdocument.in/reader033/viewer/2022051216/56814cf8550346895dba05e1/html5/thumbnails/16.jpg)
Executing a C ProgramCreating the Program
Compilation
Linking
Execution
![Page 17: UNIT II C PROGRAMMING BASICS](https://reader033.vdocument.in/reader033/viewer/2022051216/56814cf8550346895dba05e1/html5/thumbnails/17.jpg)
Executing a C Program (Cont)
Enter the program in a C editor.
Save the program (File Save) or F2. Use the extension .c for saving
the file.
Eg: sample.c
Compile the program(Compile Compile) or Alt+F9.
Run the program(Run Run) or Ctrl+F9.
![Page 18: UNIT II C PROGRAMMING BASICS](https://reader033.vdocument.in/reader033/viewer/2022051216/56814cf8550346895dba05e1/html5/thumbnails/18.jpg)
Executing C program using UNIX
Enter the program in vi editor.
Save the file using :wq
Use the extension .c for saving the file.
Eg: sample.c
Compile the program.
Eg: cc sample.c (or) gcc sample.c
Run the program using a.out.
![Page 19: UNIT II C PROGRAMMING BASICS](https://reader033.vdocument.in/reader033/viewer/2022051216/56814cf8550346895dba05e1/html5/thumbnails/19.jpg)
Structure of C programDOCUMENTATION SECTION
PREPROCESSOR SECTION
DEFINITION SECTION
GLOBAL DECLARATION SECTION
main() {
Declaration part;Executable Part;
} sub program section {
Body of the subprogram; }
![Page 20: UNIT II C PROGRAMMING BASICS](https://reader033.vdocument.in/reader033/viewer/2022051216/56814cf8550346895dba05e1/html5/thumbnails/20.jpg)
Documentation Section
It contains the comment lines.
Preprocessor Section
It is used to link library files.
Global Declaration Section
The Global declaration section comes at the beginning of the
program and they are visible to all parts of the program.
Declaration Section
It describes the data to be used within the function.
Executable Part
It contains the valid statements.
![Page 21: UNIT II C PROGRAMMING BASICS](https://reader033.vdocument.in/reader033/viewer/2022051216/56814cf8550346895dba05e1/html5/thumbnails/21.jpg)
C Programs
· C program may have many functions.
· One and only one of the functions MUST BE named main.
· main is the starting point for the program.
· main and other functions in a program are divided into two
sections, declaration section and statement section.
![Page 22: UNIT II C PROGRAMMING BASICS](https://reader033.vdocument.in/reader033/viewer/2022051216/56814cf8550346895dba05e1/html5/thumbnails/22.jpg)
Preprocessor Directives
Special instructions to the preprocessor that tells how to prepare the
program for compilation
E.g: include : tells the processor to include information from
selected libraries known as header files e.g. <stdio.h>
![Page 23: UNIT II C PROGRAMMING BASICS](https://reader033.vdocument.in/reader033/viewer/2022051216/56814cf8550346895dba05e1/html5/thumbnails/23.jpg)
Comments (Program documentation)
The compiler simply ignores comments when it translates the
program into executable code.
To identify a comments, C uses opening /* and closing */
comment tokens.
![Page 24: UNIT II C PROGRAMMING BASICS](https://reader033.vdocument.in/reader033/viewer/2022051216/56814cf8550346895dba05e1/html5/thumbnails/24.jpg)
Comments (Cont)
Comments can appear anywhere in a program.
Comments are also found wherever it is necessary to explain a point
about a code.
Comments cannot be nested in C i.e. you cannot have comments
inside comments.
![Page 25: UNIT II C PROGRAMMING BASICS](https://reader033.vdocument.in/reader033/viewer/2022051216/56814cf8550346895dba05e1/html5/thumbnails/25.jpg)
C program/* Example program in C*/ Comments
# include <stdio.h> Preprocessor Section
Global Declaration
void main ()
{ Local declaration
printf (“Hello World! \n”); Statements
}
Output :
Hello World
![Page 26: UNIT II C PROGRAMMING BASICS](https://reader033.vdocument.in/reader033/viewer/2022051216/56814cf8550346895dba05e1/html5/thumbnails/26.jpg)
C Tokens
Identifiers
Keywords
Constants
Operators
Special symbols
![Page 27: UNIT II C PROGRAMMING BASICS](https://reader033.vdocument.in/reader033/viewer/2022051216/56814cf8550346895dba05e1/html5/thumbnails/27.jpg)
Identifiers
Identifiers are names given to various program elements such as
variables, functions and arrays etc,.
Eg: #define N 10
#define a 15
Here N and a are user defined identifiers.
![Page 28: UNIT II C PROGRAMMING BASICS](https://reader033.vdocument.in/reader033/viewer/2022051216/56814cf8550346895dba05e1/html5/thumbnails/28.jpg)
Rules for naming identifier
First character must be alphabetic or underscore.
Must consist only of alphabetic characters, digits, or underscores.
Only the first 31 characters of an identifier are significant and are
recognized by the compiler.
Cannot use a keywords or reserved word (e.g. main, include, printf &
scanf etc.).
No space are allowed between the identifiers etc,.
C is case sensitive, e.g. My_name my_name.
![Page 29: UNIT II C PROGRAMMING BASICS](https://reader033.vdocument.in/reader033/viewer/2022051216/56814cf8550346895dba05e1/html5/thumbnails/29.jpg)
Examples of Valid and Invalid Names
Valid Names Invalid Names
a a1 $sum /* $ is illegal */
student_name stdntNm 2names /* Starts with 2 */
_aSystemName _anthrSysNm stdnt Nmbr /* no spaces */
TRUE FALSE int /* reserved word */
![Page 30: UNIT II C PROGRAMMING BASICS](https://reader033.vdocument.in/reader033/viewer/2022051216/56814cf8550346895dba05e1/html5/thumbnails/30.jpg)
Variables
Variable is an identifier that is used to represent some specified type
of information.
Eg: x=3
Here x is variable.
![Page 31: UNIT II C PROGRAMMING BASICS](https://reader033.vdocument.in/reader033/viewer/2022051216/56814cf8550346895dba05e1/html5/thumbnails/31.jpg)
Keywords
reserved words.
Cannot be used for anything else.
Examples:
int
while
for etc,.
![Page 32: UNIT II C PROGRAMMING BASICS](https://reader033.vdocument.in/reader033/viewer/2022051216/56814cf8550346895dba05e1/html5/thumbnails/32.jpg)
KeywordsAuto register ContinueDouble typedef ForInt Char signedStruct extern void Break return Default Else union GotoLong Const sizeof Switch Float doCase short IfEnum unsignedStatic While
![Page 33: UNIT II C PROGRAMMING BASICS](https://reader033.vdocument.in/reader033/viewer/2022051216/56814cf8550346895dba05e1/html5/thumbnails/33.jpg)
ConstantsIt is an entity whose value does not changes during the execution.
Eg: x=3
Here 3 is a constant.
![Page 34: UNIT II C PROGRAMMING BASICS](https://reader033.vdocument.in/reader033/viewer/2022051216/56814cf8550346895dba05e1/html5/thumbnails/34.jpg)
Types
Numeric constants
Character constant
![Page 35: UNIT II C PROGRAMMING BASICS](https://reader033.vdocument.in/reader033/viewer/2022051216/56814cf8550346895dba05e1/html5/thumbnails/35.jpg)
Constants
Constants
Character Constants Numeric Constants
RealConstant
IntegerConstant
String Constant
SingleCharacter Constant
![Page 36: UNIT II C PROGRAMMING BASICS](https://reader033.vdocument.in/reader033/viewer/2022051216/56814cf8550346895dba05e1/html5/thumbnails/36.jpg)
Numeric constantsInteger constants
It is formed using a sequence of digits.
Decimal - 0 to 9 .
Octal - 0 to 7.
Hexa - 0 to 9 ,A to F
Eg: 10,75 etc.
![Page 37: UNIT II C PROGRAMMING BASICS](https://reader033.vdocument.in/reader033/viewer/2022051216/56814cf8550346895dba05e1/html5/thumbnails/37.jpg)
Rules for defining Integer Constant
It must have at least one digit.
Decimal point are not allowed.
No blank space or commas are allowed.
It can be either positive or negative. Etc,.
![Page 38: UNIT II C PROGRAMMING BASICS](https://reader033.vdocument.in/reader033/viewer/2022051216/56814cf8550346895dba05e1/html5/thumbnails/38.jpg)
Numeric constants
Real constants
It is formed using a sequence of digits but it contain decimal point.
length, height, price distance measured in real number
Eg: 2.5, 5.11, etc.
![Page 39: UNIT II C PROGRAMMING BASICS](https://reader033.vdocument.in/reader033/viewer/2022051216/56814cf8550346895dba05e1/html5/thumbnails/39.jpg)
Character constants
Single character constant
A character constant is a single character they also represented
with single digit or a single special symbol which is enclosed in
single quotes.
Eg: ‘a’, ‘8’,’_’etc.
![Page 40: UNIT II C PROGRAMMING BASICS](https://reader033.vdocument.in/reader033/viewer/2022051216/56814cf8550346895dba05e1/html5/thumbnails/40.jpg)
Character constants
String constants
String constant are sequence of characters enclosed with in double
quote.
Eg: “Hello” ,”444”,”a” etc,.
![Page 41: UNIT II C PROGRAMMING BASICS](https://reader033.vdocument.in/reader033/viewer/2022051216/56814cf8550346895dba05e1/html5/thumbnails/41.jpg)
Operators
An operator is a symbol that specifies an operation to be performed
on the operands.
Eg: a + b
+ is an operator.
a,b are operands.
![Page 42: UNIT II C PROGRAMMING BASICS](https://reader033.vdocument.in/reader033/viewer/2022051216/56814cf8550346895dba05e1/html5/thumbnails/42.jpg)
Data Types
· A Data type is the type of data that are going to access within the
program.
![Page 43: UNIT II C PROGRAMMING BASICS](https://reader033.vdocument.in/reader033/viewer/2022051216/56814cf8550346895dba05e1/html5/thumbnails/43.jpg)
Standard Data Types
•These Standard type can be used to build more complex data types
called Derived Types (e.g. pointers, array, union etc.).
![Page 44: UNIT II C PROGRAMMING BASICS](https://reader033.vdocument.in/reader033/viewer/2022051216/56814cf8550346895dba05e1/html5/thumbnails/44.jpg)
Data types
Data type Size(bytes) Range Format string
Char 1 -128 to 127 %c
int 2 -32,768 to 32,767 %d
Float 4 3.4 e-38 to 3.4 e+38 %f
Double 8 1.7 e-308 to 1.7 e+308 %lf
![Page 45: UNIT II C PROGRAMMING BASICS](https://reader033.vdocument.in/reader033/viewer/2022051216/56814cf8550346895dba05e1/html5/thumbnails/45.jpg)
integer
A number without a fraction part : integral number.
C supports three different sizes of the integer data type :
short int
int
long int
![Page 46: UNIT II C PROGRAMMING BASICS](https://reader033.vdocument.in/reader033/viewer/2022051216/56814cf8550346895dba05e1/html5/thumbnails/46.jpg)
Floating Point
A floating-point type is a number with a fractional part, e.g. 56.78
Floating point numbers are stored using
4 Byte.
Types
Float
Double
long double
![Page 47: UNIT II C PROGRAMMING BASICS](https://reader033.vdocument.in/reader033/viewer/2022051216/56814cf8550346895dba05e1/html5/thumbnails/47.jpg)
character
Character are generally stored using 8 bits(1 Byte) of the internal
storage.
Character ASCII code value
a 97(decimal) or 01100001(binary)
x 120(decimal) or 01111000(binary)
![Page 48: UNIT II C PROGRAMMING BASICS](https://reader033.vdocument.in/reader033/viewer/2022051216/56814cf8550346895dba05e1/html5/thumbnails/48.jpg)
void The void type has no values and no operations.
Both the set of values and the set of operations are empty.
![Page 49: UNIT II C PROGRAMMING BASICS](https://reader033.vdocument.in/reader033/viewer/2022051216/56814cf8550346895dba05e1/html5/thumbnails/49.jpg)
Variable’s Declaration
To create a variable, you must specify the type and then its
identifier :
float price;
int a,b;
char code;
![Page 50: UNIT II C PROGRAMMING BASICS](https://reader033.vdocument.in/reader033/viewer/2022051216/56814cf8550346895dba05e1/html5/thumbnails/50.jpg)
Entire Data types in c:
Data type Size(bytes) Range Format string
Char 1 128 to 127 %c
Unsigned char 1 0 to 255 %c
Short or int 2 -32,768 to 32,767 %i or %d
Unsigned int 2 0 to 65535 %u
Long 4 -2147483648 to 2147483647 %ld
Unsigned long 4 0 to 4294967295 %lu
Float 4 3.4 e-38 to 3.4 e+38 %f or %g
Double 8 1.7 e-308 to 1.7 e+308 %lf
Long Double 10 3.4 e-4932 to 1.1 e+4932 %lf
![Page 51: UNIT II C PROGRAMMING BASICS](https://reader033.vdocument.in/reader033/viewer/2022051216/56814cf8550346895dba05e1/html5/thumbnails/51.jpg)
Types of Operator
Arithmetic operator
Relational operator
Logical operator
Assignment operator
Increment or decrement operator(unary)
Bitwise operator
Conditional operator
![Page 52: UNIT II C PROGRAMMING BASICS](https://reader033.vdocument.in/reader033/viewer/2022051216/56814cf8550346895dba05e1/html5/thumbnails/52.jpg)
Arithmetic operator
It is used to carry out arithmetic operations like addition,
subtraction etc,
Eg: + , - , * , / etc,
![Page 53: UNIT II C PROGRAMMING BASICS](https://reader033.vdocument.in/reader033/viewer/2022051216/56814cf8550346895dba05e1/html5/thumbnails/53.jpg)
Sample program#include<stdio.h> // Header File
#include <conio.h>
int b=10; //Global Declaration
void main ( ) /* main is the starting of every c program */
{
int a,c; //Local Declaration
clrscr( );
scanf(“%d”,&a);
printf(“ \n The sum of the two values:”);
c = a+b;
printf(“%d”,c);
getch( );
}
![Page 54: UNIT II C PROGRAMMING BASICS](https://reader033.vdocument.in/reader033/viewer/2022051216/56814cf8550346895dba05e1/html5/thumbnails/54.jpg)
Division operator on Different Data Type
Operation Result Exampleint/int int 5/2 = 2int/real real 5/2.0 = 2.5real/int real 5.0/2 = 2.5
real/real real 5.0/2.0 = 2.5
![Page 55: UNIT II C PROGRAMMING BASICS](https://reader033.vdocument.in/reader033/viewer/2022051216/56814cf8550346895dba05e1/html5/thumbnails/55.jpg)
Sample program
#include<stdio.h>#include <conio.h>void main ( ){int a=10,b=4,c;float d=3,e;clrscr( );c = a/b;printf(" \n value a/b is:%d",c);e = a/d;printf("\n value a/d is:%f",e);getch( );}
![Page 56: UNIT II C PROGRAMMING BASICS](https://reader033.vdocument.in/reader033/viewer/2022051216/56814cf8550346895dba05e1/html5/thumbnails/56.jpg)
Output
value a/b is:2
value a/d is:3.333333
![Page 57: UNIT II C PROGRAMMING BASICS](https://reader033.vdocument.in/reader033/viewer/2022051216/56814cf8550346895dba05e1/html5/thumbnails/57.jpg)
Relational operator
It is used to compare two or more operands.
Eg :< , > , <= , >=, != etc,.
5 < 9 which will return 1
![Page 58: UNIT II C PROGRAMMING BASICS](https://reader033.vdocument.in/reader033/viewer/2022051216/56814cf8550346895dba05e1/html5/thumbnails/58.jpg)
Logical operator
It is used to combine the result of two or more condition.
AND(&&)
OR (||)
NOT (!) are Logical operators.
Eg: (i>10)&&(j>5).
(i>10)||(j>5) etc,.
![Page 59: UNIT II C PROGRAMMING BASICS](https://reader033.vdocument.in/reader033/viewer/2022051216/56814cf8550346895dba05e1/html5/thumbnails/59.jpg)
Sample program#include<stdio.h>
#include <conio.h>
void main ( ){
int a=10,b=3,c=5,e;
clrscr( );
if(a>b) // relational operator
{
printf(" \n a is bigger than b");
}
if((a>b)&&(a>c)) //Logical operator
{
printf(" \n a is biggest");
}
getch( );
}
![Page 60: UNIT II C PROGRAMMING BASICS](https://reader033.vdocument.in/reader033/viewer/2022051216/56814cf8550346895dba05e1/html5/thumbnails/60.jpg)
Output
a is bigger than b
a is biggest
![Page 61: UNIT II C PROGRAMMING BASICS](https://reader033.vdocument.in/reader033/viewer/2022051216/56814cf8550346895dba05e1/html5/thumbnails/61.jpg)
Assignment operator
It is used to assign a value or expression etc to a variable.
Eg: a =10.
a = b
a = b + c etc,.
![Page 62: UNIT II C PROGRAMMING BASICS](https://reader033.vdocument.in/reader033/viewer/2022051216/56814cf8550346895dba05e1/html5/thumbnails/62.jpg)
Assignment operator(Cont)
Compound operator
It is also used to assign a value to a variable.
Eg: x + = y means x = x + y
Nested operator
It is used for multiple assignment.
Eg: i = j = k = 0;
![Page 63: UNIT II C PROGRAMMING BASICS](https://reader033.vdocument.in/reader033/viewer/2022051216/56814cf8550346895dba05e1/html5/thumbnails/63.jpg)
Sample program#include<stdio.h>
#include <conio.h>
int b=10;
void main ( )
{
int a=3,b=5;
clrscr( );
a+=b; // a= a+b
printf(" \n The sum of the two values:%d",a);
getch( );
}
![Page 64: UNIT II C PROGRAMMING BASICS](https://reader033.vdocument.in/reader033/viewer/2022051216/56814cf8550346895dba05e1/html5/thumbnails/64.jpg)
Output
The sum of the two values:8
![Page 65: UNIT II C PROGRAMMING BASICS](https://reader033.vdocument.in/reader033/viewer/2022051216/56814cf8550346895dba05e1/html5/thumbnails/65.jpg)
Increment or decrement operator(Unary)
It is used to Increment or decrement an operand.
Eg: ++x (Pre Increment),
x++ (Post Increment),
--x (Pre Decrement),
x-- (Post Decrement).
![Page 66: UNIT II C PROGRAMMING BASICS](https://reader033.vdocument.in/reader033/viewer/2022051216/56814cf8550346895dba05e1/html5/thumbnails/66.jpg)
Sample Program
#include<stdio.h>
#include <conio.h>
void main ( )
{
int a=5;
clrscr( );
printf(" \n Post increment Value:%d",a++);
printf(" \n Pre increment Value:%d",++a);
printf(" \n Pre decrement Value:%d",--a);
printf(" \n Post decrement Value:%d",a--);
getch( );
}
![Page 67: UNIT II C PROGRAMMING BASICS](https://reader033.vdocument.in/reader033/viewer/2022051216/56814cf8550346895dba05e1/html5/thumbnails/67.jpg)
Output
Post increment Value:5
Pre increment Value:7
Pre decrement Value:6
Post decrement Value:6
![Page 68: UNIT II C PROGRAMMING BASICS](https://reader033.vdocument.in/reader033/viewer/2022051216/56814cf8550346895dba05e1/html5/thumbnails/68.jpg)
Bitwise operator
It is used to manipulate data at bit level.
Eg: a=5 i.e 0000 0101
b=4 i.e 0000 0100
Then a & b = 0000 0100
a | b = 0000 0101 etc,.
![Page 69: UNIT II C PROGRAMMING BASICS](https://reader033.vdocument.in/reader033/viewer/2022051216/56814cf8550346895dba05e1/html5/thumbnails/69.jpg)
Sample program
#include<stdio.h>
#include <conio.h>
void main ( )
{
int a=5,b=4,c;
//char a=5,b=4,c;
clrscr( );
c = a&b;
printf(" \n value a&b is:%d",c);
getch( );
}
![Page 70: UNIT II C PROGRAMMING BASICS](https://reader033.vdocument.in/reader033/viewer/2022051216/56814cf8550346895dba05e1/html5/thumbnails/70.jpg)
Output
value a&b is:4
![Page 71: UNIT II C PROGRAMMING BASICS](https://reader033.vdocument.in/reader033/viewer/2022051216/56814cf8550346895dba05e1/html5/thumbnails/71.jpg)
Conditional Operator (or) Ternary Operator
It is used to checks the condition and execute the statement
depending on the condition.
Eg: C = a > b ? a:b
![Page 72: UNIT II C PROGRAMMING BASICS](https://reader033.vdocument.in/reader033/viewer/2022051216/56814cf8550346895dba05e1/html5/thumbnails/72.jpg)
Sample Program
#include<stdio.h>
#include <conio.h>
void main ( )
{
int a=5,b=8,c;
clrscr( );
c = a>b?a:b; //Conditional operator
printf(" \n The Larger Value is%d",c);
getch( );
}
![Page 73: UNIT II C PROGRAMMING BASICS](https://reader033.vdocument.in/reader033/viewer/2022051216/56814cf8550346895dba05e1/html5/thumbnails/73.jpg)
Output
The Larger Value is 8
![Page 74: UNIT II C PROGRAMMING BASICS](https://reader033.vdocument.in/reader033/viewer/2022051216/56814cf8550346895dba05e1/html5/thumbnails/74.jpg)
Special Operatorcomma operator ( , )sizeof operatorpointer operator (& , *) etc,.
![Page 75: UNIT II C PROGRAMMING BASICS](https://reader033.vdocument.in/reader033/viewer/2022051216/56814cf8550346895dba05e1/html5/thumbnails/75.jpg)
#include<stdio.h>
#include <conio.h>
void main ( )
{
int c;
clrscr( );
printf(" \n size of int is:%d",sizeof c);
getch( );
}
![Page 76: UNIT II C PROGRAMMING BASICS](https://reader033.vdocument.in/reader033/viewer/2022051216/56814cf8550346895dba05e1/html5/thumbnails/76.jpg)
Output
size of int is: 2
![Page 77: UNIT II C PROGRAMMING BASICS](https://reader033.vdocument.in/reader033/viewer/2022051216/56814cf8550346895dba05e1/html5/thumbnails/77.jpg)
Expression
An expression represent data item such as variable, constant are
interconnected using operators.
Eg:
Expression C Expression
a + b + c a + b + c
a2+b2 a*a + b*b
![Page 78: UNIT II C PROGRAMMING BASICS](https://reader033.vdocument.in/reader033/viewer/2022051216/56814cf8550346895dba05e1/html5/thumbnails/78.jpg)
Operator Precedence & Associativity
The arithmetic expressions evaluation are carried out based on the
precedence and associativity.
The evaluation are carried in two phases.
First Phase: High Priority operators are
evaluated.
Second Phase: Low Priority operators are
evaluated.
![Page 79: UNIT II C PROGRAMMING BASICS](https://reader033.vdocument.in/reader033/viewer/2022051216/56814cf8550346895dba05e1/html5/thumbnails/79.jpg)
Precedence Operator
High * , / , %
Low + , -
![Page 80: UNIT II C PROGRAMMING BASICS](https://reader033.vdocument.in/reader033/viewer/2022051216/56814cf8550346895dba05e1/html5/thumbnails/80.jpg)
Example5 - 20/4 + 3*3 – 1 = 5 - 5 + 9 – 1 = 0 + 9 – 1 = 9 – 1 = 8
![Page 81: UNIT II C PROGRAMMING BASICS](https://reader033.vdocument.in/reader033/viewer/2022051216/56814cf8550346895dba05e1/html5/thumbnails/81.jpg)
Example5 – (20/4) + 3*(3 – 1)
= 5 - 5 + 3*2
= 5 - 5 + 6
= 6
![Page 82: UNIT II C PROGRAMMING BASICS](https://reader033.vdocument.in/reader033/viewer/2022051216/56814cf8550346895dba05e1/html5/thumbnails/82.jpg)
Type Conversion
Converting the type of an expression from one type to another type.
Eg: x = (int)10.45
![Page 83: UNIT II C PROGRAMMING BASICS](https://reader033.vdocument.in/reader033/viewer/2022051216/56814cf8550346895dba05e1/html5/thumbnails/83.jpg)
Sample Program
#include<stdio.h>
#include <conio.h>
void main ( )
{
int c;
clrscr( );
c=(int)10.45;
printf("\nOutput is:%d",c);
getch( );
}
Output is:10
![Page 84: UNIT II C PROGRAMMING BASICS](https://reader033.vdocument.in/reader033/viewer/2022051216/56814cf8550346895dba05e1/html5/thumbnails/84.jpg)
Input/output Function
Input/Output Function
Unformatted Formatted
Output
printf()fprintf()
Input
scanf()fscanf()
Input
getc()gets()
getchar()
Output
putc()puts()
putchar()
![Page 85: UNIT II C PROGRAMMING BASICS](https://reader033.vdocument.in/reader033/viewer/2022051216/56814cf8550346895dba05e1/html5/thumbnails/85.jpg)
Formatted Input/output
C uses two functions for formatted input and output.
Formatted input : reads formatted data from the keyboard.
Formatted output : writes formatted data to the monitor.
![Page 86: UNIT II C PROGRAMMING BASICS](https://reader033.vdocument.in/reader033/viewer/2022051216/56814cf8550346895dba05e1/html5/thumbnails/86.jpg)
Formatted Input and Output
![Page 87: UNIT II C PROGRAMMING BASICS](https://reader033.vdocument.in/reader033/viewer/2022051216/56814cf8550346895dba05e1/html5/thumbnails/87.jpg)
Standard Output
The standard output file is the monitor.
Like the keyboard, it is a text file.
When you need to display data that is not text, it must be
converted into to the text before it is written to the screen.
![Page 88: UNIT II C PROGRAMMING BASICS](https://reader033.vdocument.in/reader033/viewer/2022051216/56814cf8550346895dba05e1/html5/thumbnails/88.jpg)
Format of printf Statement
![Page 89: UNIT II C PROGRAMMING BASICS](https://reader033.vdocument.in/reader033/viewer/2022051216/56814cf8550346895dba05e1/html5/thumbnails/89.jpg)
Formatted Input (scanf)
• The standard formatted input function in C is scanf (scan formatted).
• scanf consists of : a format string . an address list that identifies where data are to be placed in
memory.
scanf ( format string, address list );
(“%c….%d…..%f…..”, &a,….&i,…..,&x…..)
![Page 90: UNIT II C PROGRAMMING BASICS](https://reader033.vdocument.in/reader033/viewer/2022051216/56814cf8550346895dba05e1/html5/thumbnails/90.jpg)
Format of scanf Statement
![Page 91: UNIT II C PROGRAMMING BASICS](https://reader033.vdocument.in/reader033/viewer/2022051216/56814cf8550346895dba05e1/html5/thumbnails/91.jpg)
getchar() Example#include<stdio.h>
#include<conio.h>
#include<ctype.h>
void main()
{
char x;
printf("enter the character:");
x=getchar();
if(islower(x))
putchar(toupper(x));
else
putchar(tolower(x));
getch();
}
Output:
enter the character:ABC
a
![Page 92: UNIT II C PROGRAMMING BASICS](https://reader033.vdocument.in/reader033/viewer/2022051216/56814cf8550346895dba05e1/html5/thumbnails/92.jpg)
getche() Example
#include <stdio.h>
#include <conio.h>
void main()
{
char c ;
clrscr();
printf("\nInput a string:");
c = getche();
printf("\nstring is:");
putch(c);
getch();
}
Output:
Input a string:k
string is:k
![Page 93: UNIT II C PROGRAMMING BASICS](https://reader033.vdocument.in/reader033/viewer/2022051216/56814cf8550346895dba05e1/html5/thumbnails/93.jpg)
Getch() Example#include <stdio.h>
#include <conio.h>
void main()
{
char c;
clrscr();
printf("\nInput a string:");
c = getch();
printf("\nstring is:");
putch(c);
getch();
}
Output:
Input a string:
string is:h
![Page 94: UNIT II C PROGRAMMING BASICS](https://reader033.vdocument.in/reader033/viewer/2022051216/56814cf8550346895dba05e1/html5/thumbnails/94.jpg)
getc Example
#include<stdio.h>
#include<conio.h>
#include<ctype.h>
void main()
{
char x;
printf("enter the character:");
x=getc(stdin);
if(islower(x))
putc(toupper(x),stdout);
else
putc(tolower(x),stdout);
getch();
}
Output:
enter the character:abc
A
![Page 95: UNIT II C PROGRAMMING BASICS](https://reader033.vdocument.in/reader033/viewer/2022051216/56814cf8550346895dba05e1/html5/thumbnails/95.jpg)
gets() Example#include <stdio.h>
#include<conio.h>
void main()
{
char c[80];
clrscr();
printf("Input a string:");
gets(c);
printf("The string is:");
puts(c);
getch();
}
Output:
Input a string:qwerty
The string is:qwerty
![Page 96: UNIT II C PROGRAMMING BASICS](https://reader033.vdocument.in/reader033/viewer/2022051216/56814cf8550346895dba05e1/html5/thumbnails/96.jpg)
Character Test Function
It is used to test the character taken from the input.
isalpha(ch)
isdigit(ch)
islower(ch)
isupper(ch)
tolower(ch)
toupper(ch) etc,.
![Page 97: UNIT II C PROGRAMMING BASICS](https://reader033.vdocument.in/reader033/viewer/2022051216/56814cf8550346895dba05e1/html5/thumbnails/97.jpg)
Decision Making
It is used to change the order of the program based on condition.
Categories:
Sequential structure
Selection structure
Iteration structure
Encapsulation structure
![Page 98: UNIT II C PROGRAMMING BASICS](https://reader033.vdocument.in/reader033/viewer/2022051216/56814cf8550346895dba05e1/html5/thumbnails/98.jpg)
Decision Making (cont)Sequential structure
In which instructions are executed in sequence.
Selection structure
In which instruction are executed based on the result of some
condition.
Iteration structure
In which instruction are executed repeatedly.
Encapsulation structure
In which some compound structure are used.
![Page 99: UNIT II C PROGRAMMING BASICS](https://reader033.vdocument.in/reader033/viewer/2022051216/56814cf8550346895dba05e1/html5/thumbnails/99.jpg)
SELECTION STRUCTURE
It allows the program to make a choice from alternative paths.
C provide the following selection structures
IF statement
IF … ELSE statement
Nested IF … ELSE statement
IF … ELSE ladder
![Page 100: UNIT II C PROGRAMMING BASICS](https://reader033.vdocument.in/reader033/viewer/2022051216/56814cf8550346895dba05e1/html5/thumbnails/100.jpg)
IF StatementSyntax
IF (condition is true)
{
Statements;
}
If condition
False
True
Statements
![Page 101: UNIT II C PROGRAMMING BASICS](https://reader033.vdocument.in/reader033/viewer/2022051216/56814cf8550346895dba05e1/html5/thumbnails/101.jpg)
Example
#include<stdio.h>#include <conio.h>void main ( ){int a;clrscr( );printf("\nEnter the number:");scanf("%d",&a);
if(a>10){
printf(" \n a is greater than 10");}
getch( );}
![Page 102: UNIT II C PROGRAMMING BASICS](https://reader033.vdocument.in/reader033/viewer/2022051216/56814cf8550346895dba05e1/html5/thumbnails/102.jpg)
Output
Enter the number: 12
a is greater than 10
![Page 103: UNIT II C PROGRAMMING BASICS](https://reader033.vdocument.in/reader033/viewer/2022051216/56814cf8550346895dba05e1/html5/thumbnails/103.jpg)
IF…ELSE Statement
SyntaxIF (condition) {
True statements;}ELSE{
False statements;}
If Condition
True False
True statements
False statements
![Page 104: UNIT II C PROGRAMMING BASICS](https://reader033.vdocument.in/reader033/viewer/2022051216/56814cf8550346895dba05e1/html5/thumbnails/104.jpg)
#include<stdio.h>#include <conio.h>void main ( ){int a;clrscr( );printf("\nEnter the number:");scanf("%d",&a);
if(a>10){
printf(" \n a is greater than 10");}
else{
printf(" \n a is less than 10");}
getch( );}
![Page 105: UNIT II C PROGRAMMING BASICS](https://reader033.vdocument.in/reader033/viewer/2022051216/56814cf8550346895dba05e1/html5/thumbnails/105.jpg)
NESTED IF… ELSE
If Condition
2
True False
True statements
False statements
IfCondition
1False
StatementsTrue
![Page 106: UNIT II C PROGRAMMING BASICS](https://reader033.vdocument.in/reader033/viewer/2022051216/56814cf8550346895dba05e1/html5/thumbnails/106.jpg)
NESTED IF… ELSESyntaxIF (condition1) {
IF (condition2) {
True statements;}ELSE{
False statements;}
}ELSE{
False statements;}
![Page 107: UNIT II C PROGRAMMING BASICS](https://reader033.vdocument.in/reader033/viewer/2022051216/56814cf8550346895dba05e1/html5/thumbnails/107.jpg)
IF…ELSE LADDER
Condition1
Statements
Condition2
Statements
Condition3
Statements Statements
TRUE
TRUE
TRUE
FALSE
FALSE
FALSE
![Page 108: UNIT II C PROGRAMMING BASICS](https://reader033.vdocument.in/reader033/viewer/2022051216/56814cf8550346895dba05e1/html5/thumbnails/108.jpg)
IF…ELSE LADDERSyntaxIF (condition1) {statements;}
else if (condition2) {statements;}
else if (condition3){statements;}
else{statements;}
![Page 109: UNIT II C PROGRAMMING BASICS](https://reader033.vdocument.in/reader033/viewer/2022051216/56814cf8550346895dba05e1/html5/thumbnails/109.jpg)
Example#include<stdio.h>#include<conio.h>void main(){ int m1,m2,m3; float avg; printf("\nEnter the marks:"); scanf("%d%d%d",&m1,&m2,&m3); avg=(m1+m2+m3)/3; printf("\n The average is:%f",avg); printf("\n The Grade is:"); if(avg>=60) {
printf("First class"); }
![Page 110: UNIT II C PROGRAMMING BASICS](https://reader033.vdocument.in/reader033/viewer/2022051216/56814cf8550346895dba05e1/html5/thumbnails/110.jpg)
else if(avg>=50) {
printf("Second class"); } else if(avg>=35) {
printf("Thrid class"); } else {
printf("Fail"); }
getch();}
![Page 111: UNIT II C PROGRAMMING BASICS](https://reader033.vdocument.in/reader033/viewer/2022051216/56814cf8550346895dba05e1/html5/thumbnails/111.jpg)
OutputEnter the marks:65
75
70
The average is:70.000000
The Grade is: First class
![Page 112: UNIT II C PROGRAMMING BASICS](https://reader033.vdocument.in/reader033/viewer/2022051216/56814cf8550346895dba05e1/html5/thumbnails/112.jpg)
Looping
It is used to execute some instructions several time based on
some condition.
WHILE
Do…WHILE
For
![Page 113: UNIT II C PROGRAMMING BASICS](https://reader033.vdocument.in/reader033/viewer/2022051216/56814cf8550346895dba05e1/html5/thumbnails/113.jpg)
WHILE Loop Syntax
.
WHILE (condition){
.Body of the loop;
. }
Body of The loop
conditionFalse
True
![Page 114: UNIT II C PROGRAMMING BASICS](https://reader033.vdocument.in/reader033/viewer/2022051216/56814cf8550346895dba05e1/html5/thumbnails/114.jpg)
Example#include<stdio.h>#include<conio.h>void main(){ int i=1,fact=1,n; printf("\nEnter the Number:"); scanf("%d",&n); while(i<=n) {
fact =fact *i; i++; // i=i+1
} printf("\n The value of %d! is:%d",n,fact); getch();}
![Page 115: UNIT II C PROGRAMMING BASICS](https://reader033.vdocument.in/reader033/viewer/2022051216/56814cf8550346895dba05e1/html5/thumbnails/115.jpg)
Output
Enter the Number:3
The value of 3! is: 6
![Page 116: UNIT II C PROGRAMMING BASICS](https://reader033.vdocument.in/reader033/viewer/2022051216/56814cf8550346895dba05e1/html5/thumbnails/116.jpg)
DO…WHILE Loop Syntaxdo
{
Body of the loop
}while (condition);
Body of The loop
condition
False
True
![Page 117: UNIT II C PROGRAMMING BASICS](https://reader033.vdocument.in/reader033/viewer/2022051216/56814cf8550346895dba05e1/html5/thumbnails/117.jpg)
for loop Syntaxfor (initialization; test condition;
Increment/Decrement){
Body of the loop
}
![Page 118: UNIT II C PROGRAMMING BASICS](https://reader033.vdocument.in/reader033/viewer/2022051216/56814cf8550346895dba05e1/html5/thumbnails/118.jpg)
for loop
Initialization
condition False
Body of the loop
Inc / Decrement
![Page 119: UNIT II C PROGRAMMING BASICS](https://reader033.vdocument.in/reader033/viewer/2022051216/56814cf8550346895dba05e1/html5/thumbnails/119.jpg)
Example#include<stdio.h>#include<conio.h>void main(){ int i,fact=1,n; printf("\nEnter the Number:"); scanf("%d",&n); for(i=1;i<=n;i++) {
fact =fact *i; } printf("\n The value of %d! is:%d",n,fact); getch();}
![Page 120: UNIT II C PROGRAMMING BASICS](https://reader033.vdocument.in/reader033/viewer/2022051216/56814cf8550346895dba05e1/html5/thumbnails/120.jpg)
OutputEnter the Number:3
The value of 3! is: 6
![Page 121: UNIT II C PROGRAMMING BASICS](https://reader033.vdocument.in/reader033/viewer/2022051216/56814cf8550346895dba05e1/html5/thumbnails/121.jpg)
Nested for loop Syntaxfor (initi; cond; Inc/Dec)
{
for (initi; cond; Inc/Dec){
Body of the loop
}
}
![Page 122: UNIT II C PROGRAMMING BASICS](https://reader033.vdocument.in/reader033/viewer/2022051216/56814cf8550346895dba05e1/html5/thumbnails/122.jpg)
CASE structure
Case 1
Case 2
Defaultcase
Switch
![Page 123: UNIT II C PROGRAMMING BASICS](https://reader033.vdocument.in/reader033/viewer/2022051216/56814cf8550346895dba05e1/html5/thumbnails/123.jpg)
CASE structure
Syntaxswitch (expression){case constant 1:
block1;break;
case constant 2:block2;break;..
default :default block;break;
}
![Page 124: UNIT II C PROGRAMMING BASICS](https://reader033.vdocument.in/reader033/viewer/2022051216/56814cf8550346895dba05e1/html5/thumbnails/124.jpg)
Example#include<stdio.h>#include<conio.h>void main(){ int i,n; printf("\nEnter the Number:"); scanf("%d",&n);
switch(n) {
case 1:{printf("\n Its in case 1");break;}
![Page 125: UNIT II C PROGRAMMING BASICS](https://reader033.vdocument.in/reader033/viewer/2022051216/56814cf8550346895dba05e1/html5/thumbnails/125.jpg)
case 2:{printf("\n Its in case 2");break;}
default:{printf("\n Its in default");break;}
} getch();}
![Page 126: UNIT II C PROGRAMMING BASICS](https://reader033.vdocument.in/reader033/viewer/2022051216/56814cf8550346895dba05e1/html5/thumbnails/126.jpg)
OutputEnter the Number:2
Its in case 2
![Page 127: UNIT II C PROGRAMMING BASICS](https://reader033.vdocument.in/reader033/viewer/2022051216/56814cf8550346895dba05e1/html5/thumbnails/127.jpg)
break StatementIt is used to terminate the loop
When a break statement is encountered inside a loop, then the
loop is terminated.
![Page 128: UNIT II C PROGRAMMING BASICS](https://reader033.vdocument.in/reader033/viewer/2022051216/56814cf8550346895dba05e1/html5/thumbnails/128.jpg)
Loops with break Statementwhile(cond)
{
…………
if(cond)
break;
…………
}
![Page 129: UNIT II C PROGRAMMING BASICS](https://reader033.vdocument.in/reader033/viewer/2022051216/56814cf8550346895dba05e1/html5/thumbnails/129.jpg)
do{
…………if(cond)break;…………
} while(cond);
![Page 130: UNIT II C PROGRAMMING BASICS](https://reader033.vdocument.in/reader033/viewer/2022051216/56814cf8550346895dba05e1/html5/thumbnails/130.jpg)
for (initi; condt; Inc/Dec){…………if(cond)break;…………
}
![Page 131: UNIT II C PROGRAMMING BASICS](https://reader033.vdocument.in/reader033/viewer/2022051216/56814cf8550346895dba05e1/html5/thumbnails/131.jpg)
Continue StatementWhen a continue statement is encountered inside a loop, the
control is transferred to the beginning.
![Page 132: UNIT II C PROGRAMMING BASICS](https://reader033.vdocument.in/reader033/viewer/2022051216/56814cf8550346895dba05e1/html5/thumbnails/132.jpg)
Loops with continue Statementwhile(cond)
{
…………
if(cond)
continue;
…………
}
![Page 133: UNIT II C PROGRAMMING BASICS](https://reader033.vdocument.in/reader033/viewer/2022051216/56814cf8550346895dba05e1/html5/thumbnails/133.jpg)
do
{
…………
if(cond)
continue;
…………
} while(cond);
![Page 134: UNIT II C PROGRAMMING BASICS](https://reader033.vdocument.in/reader033/viewer/2022051216/56814cf8550346895dba05e1/html5/thumbnails/134.jpg)
for (initi; condt; Inc/Dec)
{
…………
if(cond)
continue;
…………
}
![Page 135: UNIT II C PROGRAMMING BASICS](https://reader033.vdocument.in/reader033/viewer/2022051216/56814cf8550346895dba05e1/html5/thumbnails/135.jpg)
goto StatementWhen a goto statement is encountered inside a loop, the control is
transferred to the beginning.
![Page 136: UNIT II C PROGRAMMING BASICS](https://reader033.vdocument.in/reader033/viewer/2022051216/56814cf8550346895dba05e1/html5/thumbnails/136.jpg)
Syntax for goto Statementlabel:
…………
…………
…………
goto label;
…………
![Page 137: UNIT II C PROGRAMMING BASICS](https://reader033.vdocument.in/reader033/viewer/2022051216/56814cf8550346895dba05e1/html5/thumbnails/137.jpg)
goto label;
…………
…………
…………
label:
…………
![Page 138: UNIT II C PROGRAMMING BASICS](https://reader033.vdocument.in/reader033/viewer/2022051216/56814cf8550346895dba05e1/html5/thumbnails/138.jpg)
Example
#include<stdio.h> #include<conio.h> void main() {
int a,b,c,n;clrscr();printf("\nEnter the value of a,b:");scanf("%d%d",&a,&b);printf("\nMENU");printf("\n1.ADD\n2.SUB\n3.MULTIPLY\n0.EXIT");printf("\nEnter the choice:");scanf("%d",&n);
![Page 139: UNIT II C PROGRAMMING BASICS](https://reader033.vdocument.in/reader033/viewer/2022051216/56814cf8550346895dba05e1/html5/thumbnails/139.jpg)
switch(n){ case 1:
c=a+b;printf("\nThe result of Addition is:%d",c);break;
case 2:c=a-b;printf("\nThe result of Subtraction is:%d",c);break;
case 3:c=a*b;printf("\nThe result of Multiplication is:%d",c);break;
case 0:exit(0);break;
}getch();
}
![Page 140: UNIT II C PROGRAMMING BASICS](https://reader033.vdocument.in/reader033/viewer/2022051216/56814cf8550346895dba05e1/html5/thumbnails/140.jpg)
OutputEnter the value of a,b:56MENU1.ADD2.SUB3.MULTIPLY0.EXITEnter the choice:1The result of Addition is:11
![Page 141: UNIT II C PROGRAMMING BASICS](https://reader033.vdocument.in/reader033/viewer/2022051216/56814cf8550346895dba05e1/html5/thumbnails/141.jpg)
Finding Armstrong No
#include<stdio.h>#include<conio.h>void main(){ int r=0,sum=0,n,a; printf("\nEnter the number:"); scanf("%d",&n); a=n;
while(n>0){
r=n%10;sum=sum+r*r*r;n=n/10;
}
![Page 142: UNIT II C PROGRAMMING BASICS](https://reader033.vdocument.in/reader033/viewer/2022051216/56814cf8550346895dba05e1/html5/thumbnails/142.jpg)
if(a==sum){ printf("\nIt is an armstrong number");}else{ printf("\nIt is not an armstrong number");}
getch();}
![Page 143: UNIT II C PROGRAMMING BASICS](https://reader033.vdocument.in/reader033/viewer/2022051216/56814cf8550346895dba05e1/html5/thumbnails/143.jpg)
Output
Enter the number:153
It is an armstrong number
![Page 144: UNIT II C PROGRAMMING BASICS](https://reader033.vdocument.in/reader033/viewer/2022051216/56814cf8550346895dba05e1/html5/thumbnails/144.jpg)
Sum of the Digits
#include<stdio.h>#include<conio.h>void main(){ int r=0,sum=0,n; printf("\nEnter the no:"); scanf("%d",&n); while(n>0) { r=n%10;
sum=sum+r;
n=n/10;
}
printf("sum of the digits is:%d",sum);
}
![Page 145: UNIT II C PROGRAMMING BASICS](https://reader033.vdocument.in/reader033/viewer/2022051216/56814cf8550346895dba05e1/html5/thumbnails/145.jpg)
Output
Enter the no:156
sum of the digits is:12
![Page 146: UNIT II C PROGRAMMING BASICS](https://reader033.vdocument.in/reader033/viewer/2022051216/56814cf8550346895dba05e1/html5/thumbnails/146.jpg)
Reverse of a number#include<stdio.h>#include<conio.h>void main(){ int r=0,sum=0,n; printf("\nEnter the no:"); scanf("%d",&n); while(n>0)
{
r=n%10;
sum=sum*10+r;
n=n/10;
}
printf("Reverse of the number is:%d",sum);
getch();
}
![Page 147: UNIT II C PROGRAMMING BASICS](https://reader033.vdocument.in/reader033/viewer/2022051216/56814cf8550346895dba05e1/html5/thumbnails/147.jpg)
Output
Enter the no:567
Reverse of the number is:765
![Page 148: UNIT II C PROGRAMMING BASICS](https://reader033.vdocument.in/reader033/viewer/2022051216/56814cf8550346895dba05e1/html5/thumbnails/148.jpg)
Fibonacci Series#include<stdio.h>
#include<conio.h>
void main()
{
int f=0,f1=-1,f2=1,n,i;
printf("\nEnter the number:");
scanf("%d",&n);while(f<n)
{ f=f1+f2;
f1=f2; f2=f;
printf("\t%d",f);}
getch();}
![Page 149: UNIT II C PROGRAMMING BASICS](https://reader033.vdocument.in/reader033/viewer/2022051216/56814cf8550346895dba05e1/html5/thumbnails/149.jpg)
Output
Enter the number:5
0 1 1 2 3 5
![Page 150: UNIT II C PROGRAMMING BASICS](https://reader033.vdocument.in/reader033/viewer/2022051216/56814cf8550346895dba05e1/html5/thumbnails/150.jpg)
Swapping #include<stdio.h>#include <conio.h>void main ( ){int a,b,c;clrscr( );printf(" \nEnter the value of a:");scanf("%d",&a);printf(" \nEnter the value of b:");scanf("%d",&b);c=a;a=b;b=c;printf(" \nThe value of a is:%d",a);printf(" \nThe value of b is:%d",b);getch( );}
![Page 151: UNIT II C PROGRAMMING BASICS](https://reader033.vdocument.in/reader033/viewer/2022051216/56814cf8550346895dba05e1/html5/thumbnails/151.jpg)
Output:Enter the value of a:5Enter the value of b:4
The value of a is:4The value of b is:5
![Page 152: UNIT II C PROGRAMMING BASICS](https://reader033.vdocument.in/reader033/viewer/2022051216/56814cf8550346895dba05e1/html5/thumbnails/152.jpg)
Swapping without using third variable
#include<stdio.h>#include <conio.h>void main ( ){int a,b;clrscr( );printf(" \nEnter the value of a:");scanf("%d",&a);printf(" \nEnter the value of b:");scanf("%d",&b);a=a+b;b=a-b;a=a-b;printf(" \nThe value of a is:%d",a);printf(" \nThe value of b is:%d",b);getch( );}
![Page 153: UNIT II C PROGRAMMING BASICS](https://reader033.vdocument.in/reader033/viewer/2022051216/56814cf8550346895dba05e1/html5/thumbnails/153.jpg)
Output:Enter the value of a:5Enter the value of b:6
The value of a is:6The value of b is:5
![Page 154: UNIT II C PROGRAMMING BASICS](https://reader033.vdocument.in/reader033/viewer/2022051216/56814cf8550346895dba05e1/html5/thumbnails/154.jpg)
Quadratic Equation
#include<stdio.h>#include <conio.h>#include<math.h>void main ( ){int a,b,c,d,r1,r2;clrscr( );printf(" \nEnter the value of a:");scanf("%d",&a);printf(" \nEnter the value of b:");scanf("%d",&b);printf(" \nEnter the value of c:");scanf("%d",&c);d=b*b-4*a*c;
![Page 155: UNIT II C PROGRAMMING BASICS](https://reader033.vdocument.in/reader033/viewer/2022051216/56814cf8550346895dba05e1/html5/thumbnails/155.jpg)
if(d>=0){
r1=(-b+sqrt(d))/(2*a);r2=(-b-sqrt(d))/(2*a);
printf(" \nThe roots are %d,%d",r1,r2);}else{ printf(" \nThe roots are imaginary");}getch( );}
![Page 156: UNIT II C PROGRAMMING BASICS](https://reader033.vdocument.in/reader033/viewer/2022051216/56814cf8550346895dba05e1/html5/thumbnails/156.jpg)
Output
Enter the value of a:4
Enter the value of b:5
Enter the value of c:6
The roots are imaginary