




























































































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
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
1 / 117
This page cannot be seen from the preview
Don't miss anything!





























































































a) Relational Operators
b) Logical Operators
c) Conditional Operators
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
a) Relational Operators Contd…
Consider a = 10 and b =4. The relational expression returns the following integer values
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 ; }
b) Logical Operators
Combines two or more relations Used for testing one or more conditions
b) Logical Operators Contd…
Logical Expression / Compound Relational Expression : An expression which combines two or more relational expression
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 ; }
/* 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