Unit 2 18CSS101J Programming for Problem Solving, Slides of Programming for Engineers

Unit 2 18CSS101J Programming for Problem Solving Detailed lecture slides covering all topics in detail as prescribed by the curriculum for the academic term. Important Exam Material.

Typology: Slides

2017/2018

Uploaded on 02/20/2022

vasundhara-jhobta
vasundhara-jhobta 🇺🇸

4.6

(8)

8 documents

1 / 117

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
18CSS101J Programming for Problem Solving
Unit II
SRM
INSTITUTE OF SCIENCE AND TECHNOLOGY,
CHENNAI.
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b
pf2c
pf2d
pf2e
pf2f
pf30
pf31
pf32
pf33
pf34
pf35
pf36
pf37
pf38
pf39
pf3a
pf3b
pf3c
pf3d
pf3e
pf3f
pf40
pf41
pf42
pf43
pf44
pf45
pf46
pf47
pf48
pf49
pf4a
pf4b
pf4c
pf4d
pf4e
pf4f
pf50
pf51
pf52
pf53
pf54
pf55
pf56
pf57
pf58
pf59
pf5a
pf5b
pf5c
pf5d
pf5e
pf5f
pf60
pf61
pf62
pf63
pf64

Partial preview of the text

Download Unit 2 18CSS101J Programming for Problem Solving and more Slides Programming for Engineers in PDF only on Docsity!

18CSS101J – Programming for Problem Solving

Unit II

SRM

INSTITUTE OF SCIENCE AND TECHNOLOGY,

CHENNAI.

UNIT II

Relational and logical Operators - Condition Operators,

Operator Precedence - Expressions with pre / post increment

Operator - Expression with conditional and assignment

operators - If statement in expression - L value and R value in

expression - Control Statements – if and else - else if and

nested if, switch case - Iterations, Conditional and

Unconditional Branching - For loop - While loop - do while,

goto, break, continue – Array - Initialization and Declaration-

SRM

INSTITUTE OF SCIENCE AND TECHNOLOGY,

CHENNAI.

2. 1 Operators in C

a) Relational Operators

b) Logical Operators

c) Conditional Operators

SRM

INSTITUTE OF SCIENCE AND TECHNOLOGY,

CHENNAI.

2. 1 Operators in C Contd…

a) Relational Operators

 Binary Operators (or) Boolean Operators  Produces an integer result  Condition True : Integer value is 1  Condition False : Integer value is 0  Compares  Values between two variables  Values between variables and constants

SRM

INSTITUTE OF SCIENCE AND TECHNOLOGY,

CHENNAI.

2. 1 Operators in C Contd…

a) Relational Operators Contd…

 Consider a = 10 and b =4. The relational expression returns the following integer values

SRM

INSTITUTE OF SCIENCE AND TECHNOLOGY,

CHENNAI.

Relational Expression Result Return Values a < b False 0 a > b True 1 a < = b False 0 a > = b True 1 a = = b False 0 a! = b True 1

/* Program for Relational Operations*/

#include<stdio.h> int main( ) { int a,b; printf("Enter the two Values \n "); scanf("%d%d", &a, &b); printf(“a>b is %d \n “, (a>b)); printf(“a<b is %d \n “, (a<b)); printf(“a>=b is %d \n “, (a>=b)); printf(“a<=b is %d \n “, (a<=b)); printf(“a==b is %d \n “, (a==b)); printf(“a!=b is %d \n “, (a!=b)); return 0 ; }

2. 1 Operators in C Contd…

b) Logical Operators

 Combines two or more relations  Used for testing one or more conditions

SRM

INSTITUTE OF SCIENCE AND TECHNOLOGY,

CHENNAI.

2. 1 Operators in C Contd…

b) Logical Operators Contd…

Logical Expression / Compound Relational Expression : An expression which combines two or more relational expression

SRM

INSTITUTE OF SCIENCE AND TECHNOLOGY,

CHENNAI.

Op1 Op2 Op1 && Op2 Op1 || Op F (0) F (0) F (0) F (0) F (0) T (1) F (0) T (1) T (1) F (0) F (0) T (1) T (1) T (1) T (1) T (1)

/* Program for Logical Operations*/

#include<stdio.h> int main( ) { int age,height; printf("Enter Age of Candidate: \n "); scanf("%d", &age); printf(“Enter Height of Candidate: \n “); scanf("%d", &height); if ((age>= 18 ) && (height>= 5 )) printf(“The Candidate is Selected”); else printf(“Sorry, Candidate not Selected”); return 0 ; }

Output 1 Enter Age of Candidate: 18 Enter Height of Candidate: 6 The Candidate is Selected

Output 2 Enter Age of Candidate: 19 Enter Height of Candidate: 4 Sorry, Candidate not Selected

#include<stdio.h> int main( ) { int x, y; scanf("%d", &x); y=(x > 5? 3 : 4 ); printf(“%d”, y); return 0 ; }

#include<stdio.h> int main( ) { int x, y; scanf("%d", &x); if(x > 5 ) y= 3 ; else y= 4 ; printf(“%d”, y); return 0 ; }

SRM

INSTITUTE OF SCIENCE AND TECHNOLOGY,

CHENNAI.

/* Program for Addition (or) Multiplication/ #include<stdio.h> int main( ) { int a, b, result, choice; printf(“Enter first number \n”); scanf(“%d”,&a); printf(”Enter second number\n”); scanf(“%d”,&b); printf(“Enter 1 for addition or 2 for multiplication\n”); scanf(“%d”,&choice); result = (choice==1)?a+b:(choice==2)?ab:printf(“Invalid”); if(choice==1||choice==2) printf(“The result is %d\n\n”,result); return 0; }

/* Program to find the maximum of 3 Numbers*/

#include <stdio.h>

int main( ) { int a, b, c, max; printf("Enter three numbers: "); scanf("%d%d%d",&a, &b, &c); max = (a > b && a > c)? a : (b > c)? b : c; printf("\n Maximum between %d, %d and %d = %d", a, b, c, max); return 0;

}

Output

Enter three numbers: 30 10 40

Maximum between a, b and c = 40