c programming :session 2

25
APTITUDE TRAINING (Technical Part) (Technical Part) At 6 At 6 th th Semester of Graduate Programme Semester of Graduate Programme in all Branches of Engineering / Technology in all Branches of Engineering / Technology Instruc tor : Dr . Somsubhra Gupta, IT Dept. JISCE Instruc tor : Dr . Somsubhra Gupta, IT Dept. JISCE V enue: Dr . B.C. Roy Auditorium. V enue: Dr . B.C. Roy Auditorium. T echnical Apt itude Trai ning JISCE 2015 1

Upload: ashutosh-mohanty

Post on 19-Feb-2018

215 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: C programming :Session 2

7/23/2019 C programming :Session 2

http://slidepdf.com/reader/full/c-programming-session-2 1/25

APTITUDE TRAINING

(Technical Part)(Technical Part)

At 6At 6thth Semester of Graduate ProgrammeSemester of Graduate Programme

in all Branches of Engineering / Technologyin all Branches of Engineering / Technology

Instructor : Dr. Somsubhra Gupta, IT Dept. JISCEInstructor : Dr. Somsubhra Gupta, IT Dept. JISCE

Venue: Dr. B.C. Roy Auditorium.Venue: Dr. B.C. Roy Auditorium.

Technical Aptitude Training JISCE 2015 1

Page 2: C programming :Session 2

7/23/2019 C programming :Session 2

http://slidepdf.com/reader/full/c-programming-session-2 2/25

Technical Session 2Technical Session 2. .. .

 

Technical Aptitude Training JISCE 2015 2

Page 3: C programming :Session 2

7/23/2019 C programming :Session 2

http://slidepdf.com/reader/full/c-programming-session-2 3/25

Session Objective

•as c pro em-so v ng ec n ques.• To develop algorithms through the process of op- own, s epw se re nemen .

• To use the if selection statement and...e se se ec on s a emen oselect actions.

Technical Aptitude Training JISCE 2015 3

Page 4: C programming :Session 2

7/23/2019 C programming :Session 2

http://slidepdf.com/reader/full/c-programming-session-2 4/25

e e ect on tatement

 if ( grade >= 60 )

printf( "Passed\n" );

C code corresponds closely to the pseudocode

Diamond symbol (decision symbol)Indicates decision is to be made

Contains an ex ression that can be true or false

Test the condition, follow appropriate path

Technical Aptitude Training JISCE 2015 4

Page 5: C programming :Session 2

7/23/2019 C programming :Session 2

http://slidepdf.com/reader/full/c-programming-session-2 5/25

e ..e se statement

if

 

if…else

 condition is true and when it is false

 If student’s grade is greater than or equal to 60Print “Passed”

elsePrint “Failed”

Technical Aptitude Training JISCE 2015 5

 

Page 6: C programming :Session 2

7/23/2019 C programming :Session 2

http://slidepdf.com/reader/full/c-programming-session-2 6/25

  … ..

C code:if ( grade >= 60 )

printf( "Passed\n");

else

printf( "Failed\n");

ernary con ona opera or :  Takes three arguments (condition, value if true, value if false)

Our pseudocode could be written:pr nt %s n , gra e >= 60 ? Passe : Fa e ;

Or it could have been written:grade >= 60 ? printf( “Passed\n” ) : printf( “Failed\n”

Technical Aptitude Training JISCE 2015 6

Page 7: C programming :Session 2

7/23/2019 C programming :Session 2

http://slidepdf.com/reader/full/c-programming-session-2 7/25

Technical Aptitude Training JISCE 2015 7

Page 8: C programming :Session 2

7/23/2019 C programming :Session 2

http://slidepdf.com/reader/full/c-programming-session-2 8/25

  …

 Nested if…else statements

Test for multiple cases by placing if…elseselection statements inside if…else selections a emen

Once condition is met, rest of statements skipped eep n entat on usua y not use n pract ce

Technical Aptitude Training JISCE 2015 8

Page 9: C programming :Session 2

7/23/2019 C programming :Session 2

http://slidepdf.com/reader/full/c-programming-session-2 9/25

  ..Pseudocode for a nested if…else statement If student’s grade is greater than or equal to 90

Print “A”else

 I student’s rade is reater than or e ual to 80Print “B”

else I student’s rade is reater than or e ual to 70

Print “C”else

 I student’s rade is reater than or e ual to 60

Print “D”else

Print “F”

Technical Aptitude Training JISCE 2015 9

Page 10: C programming :Session 2

7/23/2019 C programming :Session 2

http://slidepdf.com/reader/full/c-programming-session-2 10/25

Practice 

Code

• Palindrome program in C

• Find the Cube values of a Number

• Find vowel or consonant using switch statement

• Get month name from month number 

Technical Aptitude Training JISCE 2015 10

Page 11: C programming :Session 2

7/23/2019 C programming :Session 2

http://slidepdf.com/reader/full/c-programming-session-2 11/25

Assignment : Day 2

1. Generate all the terms of Fibonacci Series up to the input NGenera term n F onacc Ser es s as o ows:-

F[i] = i if i<2F[i-1]+F[i-2] if i >=2 The series is 0,1,1,2,3,5,8,13,21,34,55 etc..)

.3. Check whether a number is Armstrong number or not ( a number is Armstrong

if sum of the cubes of it digits, matches with the number. E.g. 153 =13

+33

+53

)

4. Check whether a number is Peterson number or not ( a number is Peterson ifsum of the factorials of it digits, matches the number. E.g.145 =1!+4!+ 5!)

5. Check whether a number is Perfect number or not ( a number is Perfect if sumof the factors (except itself), matches with the number. E.g. 28=1+2+4+7+14.

Technical Aptitude Training JISCE 2015 11

Page 12: C programming :Session 2

7/23/2019 C programming :Session 2

http://slidepdf.com/reader/full/c-programming-session-2 12/25

Page 13: C programming :Session 2

7/23/2019 C programming :Session 2

http://slidepdf.com/reader/full/c-programming-session-2 13/25

emory oncept• Variables

 – Variable names (identifiers) correspond to locations in the'

 – Every variable has a name, a type, a size and a value

 – Whenever a new value is laced into a variable throu hscanf, for example), it replaces (and destroys) the previous

value. (Destructive write) – ea ng var a es rom memory oes not c ange t em

• A visual representation (memory map)

36443

int

45i

4 bytes

addressvaluevariable

datatype size

Technical Aptitude Training JISCE 2015 12

Page 14: C programming :Session 2

7/23/2019 C programming :Session 2

http://slidepdf.com/reader/full/c-programming-session-2 14/25

Keywords

 

auto double int struct

 break else long switch

case enum register typedef

char extern return union

const float short unsigned 

continue for signed void

default goto sizeof volatile

 

Technical Aptitude Training JISCE 2015 13

Page 15: C programming :Session 2

7/23/2019 C programming :Session 2

http://slidepdf.com/reader/full/c-programming-session-2 15/25

Expressions

• Expressions are computations that return a value.

• Like variables, a value is an instance of a data type.• Examples of expressions are:

 – 45 (int)

 – 2+3 (int) – 2.0/5.0 (double)

 – “Hello” (string)

 – x (the current value of variable int x)

Technical Aptitude Training JISCE 2015 14

Page 16: C programming :Session 2

7/23/2019 C programming :Session 2

http://slidepdf.com/reader/full/c-programming-session-2 16/25

 + (addition) - (subtraction) * (multiplication)

/ (division) % (modulus, remainder) (no ** )

 

- (no + )Example:

int i=1, j=2, k=3, x;

x=i+2*j-22/k;

x=-1+j;

x=1+-j;

x=+i+j;

(x=1 + 4 -7 = -2)

(x= 1)

(x= -1)

x=22%k;

float f=1.5, g=0.5, y;

y=2.5*f+4.0*g;

Exercise: Try -5%3 -5%-3 5%-3

 

(x= 1, remainder)

(y=5.75)

(hint: -5/3=-1 -5/-3=1 5/-3=-1 and R=x-y*i)

 – Mixed data t es will be discussed later  Ans: -2 -2 2

  – Operators that have more than two operands use functional notation a = f(x,y,x).

Technical Aptitude Training JISCE 2015 15

Page 17: C programming :Session 2

7/23/2019 C programming :Session 2

http://slidepdf.com/reader/full/c-programming-session-2 17/25

Arithmetic Operators

C operation Arithmeticoperator

Algebraicexpression

C expression

 Addition + f + 7 f + 7

Subtraction - p – c  p - c

 

Division / x / y x / y

Modulus % r mod s r % s

Technical Aptitude Training JISCE 2015 16

Page 18: C programming :Session 2

7/23/2019 C programming :Session 2

http://slidepdf.com/reader/full/c-programming-session-2 18/25

Operator Precedence

Operator(s) Operation(s) Order of evaluation (precedence)

() Parentheses Evaluated first. If the parentheses are nested, theexpress on n t e nnermost pa r s eva uate rst. I t ereare several pairs of parentheses “on the same level” (i.e.,not nested), they are evaluated left to right.

*, /, or  %  Multiplication,Divi Evaluated second. If there are several, they aresion, Modulus evaluated left to right.

+ or  -  AdditionSubtraction

Evaluated last. If there are several, they areevaluated left to right.

Technical Aptitude Training JISCE 2015 17

Page 19: C programming :Session 2

7/23/2019 C programming :Session 2

http://slidepdf.com/reader/full/c-programming-session-2 19/25

Relational Operators• Binary Operators== != < > <= >=

 – Result is a inte er: 1 means TRUE

0 means FALSE – No logical type variable and constants

 –Example:

Meaning C Expression Result

equal == 5 == 3 0

not equal

greater 

less

!=

>

<

5 != 3

5 > 3

5 < 3

1

0

 

less equal <=

 

5 <= 3 0

10==0

int i=10, =10, k=1;

0i + j <= 3 + k

Technical Aptitude Training JISCE 2015 18

Page 20: C programming :Session 2

7/23/2019 C programming :Session 2

http://slidepdf.com/reader/full/c-programming-session-2 20/25

Standard algebraic C equality or Example of C Meaning of C

Relational Operators

equa y opera or or

relational operator

re a ona

operator

con on con on

 Equality Operators

=  x == y x s equa to y

not = != x != y x is not equal to y 

 Relational Operators

> >  x > y x is greater than y 

< <  x < y x is less than y 

>= >= >= 

equal to y 

<= <=  x <= y x is less than or

Technical Aptitude Training JISCE 2015 19

Page 21: C programming :Session 2

7/23/2019 C programming :Session 2

http://slidepdf.com/reader/full/c-programming-session-2 21/25

Logical (Boolean) Operators• B nary Operators&& (and) || (OR)

• Unar O erator  

! (not)• Operand must be i nt

 – se oa or ou e, e resu may no pre c a e

nonzero (TRUE) zero (FALSE)

• Result is i nt1 (TRUE) 0 (FALSE)

 – Express connected by && or || are evaluated from left to right, and evaluation stops as soon as thetruth or falsehood of the result is known. i.e. ‘expr1 && expr2’ is not equal to ‘expr2 &&ex r1’. This is called short-circuit evaluation.

 – ‘inward == 0’ normally be written as ‘!inward’

Example:

3 < 7 < 5(3 < 7) < 5 1 < 5 1

1 && 0 03 < 7 && 7 < 5

 

Technical Aptitude Training JISCE 2015 20

Page 22: C programming :Session 2

7/23/2019 C programming :Session 2

http://slidepdf.com/reader/full/c-programming-session-2 22/25

•  expr1 ? expr2 : expr3

 – If expr1  0, then execute expr2 and ignore expr3

 – If expr1 = 0, then execute expr3 and ignore expr2Example: x = i+j ? i+1 : j+1

Example:x = 5 ? 4 : 2; /* x = 4 */

Example:j = 4;

i = 2

x = i+j ? i+1 : j-1 /* x = 3 */

Example:l = a > b ? a : b; /* the larger of  a and  b */

xamp e: max =(a > b)?((a>c)?a:c):(b>c)?b:c);

/* the maximum number among a, b, and c */

Exam le:x = a > 0 ? a: -a; /* the absolute value of  a */

Technical Aptitude Training JISCE 2015 21

Page 23: C programming :Session 2

7/23/2019 C programming :Session 2

http://slidepdf.com/reader/full/c-programming-session-2 23/25

sizeof Operator• Syntax

sizeof ex r – The number of bytes occupied by expr

 – For most computerssizeof(3) 2 or 4 (bytes)(depending on16 bit CPU or 32 bit CPU), where 3 is an integer sizeof(3L) 4 (long int)

sizeof(3.0) 8 (double float)

double i;

 printf(“%d”,sizeof(i)); 8

 – Usually, this operator is used to get the size of an organized variable (like

struct, union, …) – This is one of a few functions that are built-in.  No #include is required.

Technical Aptitude Training JISCE 2015 22

Page 24: C programming :Session 2

7/23/2019 C programming :Session 2

http://slidepdf.com/reader/full/c-programming-session-2 24/25

• Syntax&var

Address Operator

 – Get the address of the variable

– & means the address of var

 – ype o var may e(a) fundamental data type

 b or anized data t e

RAM 100100210011000

1003

Example:

int i=100;

rintf “%d %d” &i i

10041005

 

Technical Aptitude Training JISCE 2015 23

Page 25: C programming :Session 2

7/23/2019 C programming :Session 2

http://slidepdf.com/reader/full/c-programming-session-2 25/25