Docsity

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

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