c programming :session 2
TRANSCRIPT
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
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
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
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
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
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
7/23/2019 C programming :Session 2
http://slidepdf.com/reader/full/c-programming-session-2 7/25
Technical Aptitude Training JISCE 2015 7
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
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
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
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
7/23/2019 C programming :Session 2
http://slidepdf.com/reader/full/c-programming-session-2 12/25
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
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
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
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
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
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
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
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
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
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
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
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
7/23/2019 C programming :Session 2
http://slidepdf.com/reader/full/c-programming-session-2 25/25