Decision making in �C
{C}
Programming
Programming for Problem Solving (PPS)
GTU # 3110003
USING
Prof. Nilesh Gambhava
�Computer Engineering Department,�Darshan Institute of Engineering & Technology, Rajkot
Need of decision making
if number is odd
{
/* code */
}
else number is even
{
/* code */
}
Prof. Nilesh Gambhava
#3110003 (PPS) – Decision Making
2
Decision Making or Conditional Statement
Prof. Nilesh Gambhava
#3110003 (PPS) – Decision Making
3
If statement
✓
if
if(condition)
{
// Body of the if
// true part
}
condition
True
False
…
Syntax
Flowchart of if
Prof. Nilesh Gambhava
#3110003 (PPS) – Decision Making
5
WAP to print Zero if given number is 0
#include<stdio.h>
void main()
{
int a;
printf("Enter Number:");
scanf("%d",&a);
if(a == 0)
{
printf("Zero");
}
}
1
2
3
4
5
6
7
8
9
10
11
Enter Number:0
Zero
Program
Output
Prof. Nilesh Gambhava
#3110003 (PPS) – Decision Making
6
WAP to print Positive or Negative Number
#include<stdio.h>
void main()
{
int a;
printf("Enter Number:");
scanf("%d",&a);
if(a >= 0)
{
printf("Positive Number");
}
if(a < 0)
{
printf("Negative Number");
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
Enter Number:5
Positive Number
Program
Output
Enter Number:-5
Negative Number
Output
Prof. Nilesh Gambhava
#3110003 (PPS) – Decision Making
7
Modulus Operator
Prof. Nilesh Gambhava
#3110003 (PPS) – Decision Making
8
If..else statement
✓
if...else
if(condition)
{
// true part
}
else
{
// false part
}
Syntax
Flowchart of if…else
condition
True
False
…
…
…
Prof. Nilesh Gambhava
#3110003 (PPS) – Decision Making
10
WAP to print Positive or Negative Number using if…else
#include<stdio.h>
void main()
{
int a;
printf("Enter Number:");
scanf("%d",&a);
if(a >= 0)
{
printf("Positive Number");
}
else
{
printf("Negative Number");
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
Program
Enter Number:5
Positive Number
Output
Enter Number:-5
Negative Number
Output
Prof. Nilesh Gambhava
#3110003 (PPS) – Decision Making
11
WAP to print Odd or Even Number using if…else
#include<stdio.h>
void main()
{
int a;
printf("Enter Number:");
scanf("%d",&a);
if(a%2 == 0)
{
printf("Even Number");
}
else
{
printf("Odd Number");
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
Enter Number:12
Even Number
Program
Output
Enter Number:11
Odd Number
Output
Prof. Nilesh Gambhava
#3110003 (PPS) – Decision Making
12
WAP to find largest number from given 2 numbers using if…else
#include<stdio.h>
void main()
{
int a, b;
printf("Enter Two Numbers:");
scanf("%d%d",&a,&b);
if(a > b)
{
printf("%d is largest", a);
}
else
{
printf("%d is largest", b);
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
Enter Two Numbers:4
5
5 is largest
Program
Output
Prof. Nilesh Gambhava
#3110003 (PPS) – Decision Making
13
{ }
if(a >= b)
printf("%d is largest", a);
if(a >= b)
{
printf("%d is largest", a);
}
Both are same
Prof. Nilesh Gambhava
#3110003 (PPS) – Decision Making
14
If…else if…else if…else �Ladder if
✓
If…else if…else if…else
if(condition-1)
statement-1;
else if(condition-2)
statement-2;
else
statement-3;
Syntax
Prof. Nilesh Gambhava
#3110003 (PPS) – Decision Making
16
if…else if…else ladder flowchart
condition1
True
False
…
condition2
True
False
…
Condition 3
True
False
…
…
Prof. Nilesh Gambhava
#3110003 (PPS) – Decision Making
17
WAP to print Zero, Positive or Negative Number
#include<stdio.h>
void main()
{
int a;
printf("Enter Number:");
scanf("%d",&a);
if(a > 0)
printf("Positive Number");
else if(a==0)
printf("Zero");
else
printf("Negative Number");
}
1
2
3
4
5
6
7
8
9
10
11
12
13
Program
Enter Number:5
Positive Number
Output
Enter Number:-5
Negative Number
Output
Prof. Nilesh Gambhava
#3110003 (PPS) – Decision Making
18
WAP for Below condition:
Marks Grade
90-100 : A
70-89 : B
50-69: C
Fail
Prof. Nilesh Gambhava
#3110003 (PPS) – Decision Making
19
#include<stdio.h>
void main()�{
int marks;
printf("Enter your marks between 0-100\n");
scanf("%d", &marks);
if(marks >= 90)
{
printf("YOUR GRADE : A\n");
}
else if (marks >= 70 && marks < 90)
{
printf("YOUR GRADE : B\n");
}
else if (marks >= 50 && marks < 70)
{
printf("YOUR GRADE : C\n");
}
else
{
printf("YOUR GRADE : Failed\n");
}
getch();
}
Nested if
✓
Nested if
if(condition-1)
{
if(condition-2)
{
statement-1;
}
else
{
statement-2;
}
}
else
{
statement-3;
}
Syntax
Prof. Nilesh Gambhava
#3110003 (PPS) – Decision Making
22
Nested if flowchart
condition1
True
False
Statement3
condition2
True
False
Statement 1
Statement 2
Next
Statement
Prof. Nilesh Gambhava
#3110003 (PPS) – Decision Making
23
WAP to print maximum from given three numbers
void main(){
int a, b, c;
printf("Enter Three Numbers:");
scanf("%d%d%d",&a,&b,&c);
if(a>b)
{
if(a>c)
printf("%d is max",a);
else
printf("%d is max",c);
}
else
{
if(b>c)
printf("%d is max",b);
else
printf("%d is max",c);
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
Program
Enter Three Numbers:7
5
9
9 is max
Output
Prof. Nilesh Gambhava
#3110003 (PPS) – Decision Making
24
Conditional Operator
✓
? : (Conditional Operator)
variable = Expression1 ? Expression2 : Expression3
False
True
Result value
Result value
Prof. Nilesh Gambhava
#3110003 (PPS) – Decision Making
26
Conditional operator flowchart
Expression1
True
False
Expression 3
Expression 2
Variable
Prof. Nilesh Gambhava
#3110003 (PPS) – Decision Making
27
WAP to find largest number from given 2 numbers using ? :
#include<stdio.h>
void main()
{
int a, b, max;
printf("Enter Two Numbers:");
scanf("%d%d",&a,&b);
max = a>b?a:b;
printf("%d is largest",max);
}
1
2
3
4
5
6
7
8
9
Enter Two Numbers:4
5
5 is largest
Program
Output
Prof. Nilesh Gambhava
#3110003 (PPS) – Decision Making
28
switch…case
✓
switch...case
switch (expression)
{
case constant1:
// statements
break;
case constant2:
// statements
break;
.
.
.
default:
// default statements
}
Syntax
Prof. Nilesh Gambhava
#3110003 (PPS) – Decision Making
30
WAP that asks day number and prints day name using switch…case
void main(){
int day;
printf("Enter day number(1-7):");
scanf("%d",&day);
switch(day)
{
case 1:
printf("Sunday");
break;
case 2:
printf("Monday");
break;
case 3:
printf("Tuesday");
break;
case 4:
printf("Wednesday");
break;
case 5:
printf("Thursday");
break;
case 6:
printf("Friday");
break;
Enter day number(1-7):5
Thursday
Output
case 7:
printf("Saturday");
break;
default:
printf("Wrong input");
}
}
Prof. Nilesh Gambhava
#3110003 (PPS) – Decision Making
31
Practice programs
Prof. Nilesh Gambhava
#3110003 (PPS) – Decision Making
32
Thank you
✓