View
191
Download
1
Category
Preview:
Citation preview
C-Programming
Walchand Institute of Technology, Solapur Page 1
HANDOUT#4
Assignment/Program Statement:
Write C programs using decision control statements such as if, if-else, else_if
ladder and nested if-else.
Learning Objectives:
Students will be able to
- explain decision control statements in C
- write C code using if statement in C
- write C code using if_else statement in C
- write C code using else_if ladder in C
- write C code using nested if-else in C
- draw flowchart for decision making solutions
Theory:
A decision control instruction can be implemented in C using following decision
control constructs:
1) The if statement
2) The if - else statement
3) Ladder else_if (or switch-case statement)
4) Nested if_else
4-a) Write a C program using if statement.
4-b) Write a C program using if_else statement.
4-c) Write a C program using else_if ladder.
4-d) Write a C program using nested if-else.
C-Programming
Walchand Institute of Technology, Solapur Page 2
Assignment#4a
Program Statement:
Write a C program using if statement - to display message if given number is
greater than 10 using if statement.
Learning Objectives:
Students will be able to
- explain decision control statements in C
- write C code using if statement in C
- draw flowchart for decision making solutions
Theory:
The if statement:
� The code inside “if” body executes only when the condition defined by the
“if” statement is “true”.
� If the condition is “false” then compiler skips the statement enclosed in if’s
body.
� We can have any number of if statements in a C program.
The general form of if statement looks like this:
if (this condition is true)
{
execute this statement;
}
Example:
C code to check given number in ‘n’ variable is smaller than 10.
if (n<10)
{
printf(“\n N is smaller than 10”);
}
C-Programming
Walchand Institute of Technology, Solapur Page 3
Flow-Diagram for if statement:
Algorithm:
Step1: Start
Step2: Read value of n from user
Step3: If ‘N’ > 10 then
print “N is greater than 10
End If
Step4: Stop
Flowchart:
C-Programming
Walchand Institute of Technology, Solapur Page 4
Program:
#include<stdio.h>
#include<conio.h>
void main()
{
int n;
clrscr();
printf(“\n Enter number (N):”);
scanf(“%d”, &n);
if(n>10){
printf(“\n %d is greater than 10”);
}
}
Input:
Enter number (N): 5
Output:
5 is greater than 10
Practice Problem Statements:
Write a program using if statement
i) To calculate area of circle if given radius value is not equal to zero.
ii) To display message if given two numbers addition is equal to 35.
Conclusion:
Thus C program using if statement - to display message if given number is greater
than 10 using if statement is implemented
Assignment Outcome:
At the end of this assignment, students are be able to
- explain decision control statements in C
- write C code using if statement in C
- draw flowchart for decision making solutions
C-Programming
Walchand Institute of Technology, Solapur Page 5
Assignment#4b
Program Statement:
Write a C program using if-else statement, to display greater number amongst
given two numbers a & b.
Learning Objectives:
Students will be able to
- explain decision control statements in C
- write C code using if-else statement in C
- draw flowchart for decision making solutions
Theory:
The if_else statement:
� If we have two blocks of statements then we use if_else statement.
� If the condition results “true” then “if” block gets in execution otherwise
statements in “else” block executes.
� The “else” cannot exist without if statement.
� If the condition is “true”, it executes one set of statements and if the
condition is “false”, it executes another set of instructions.
The general form of if_else statement
if(condition)
{
statements;
}
else
{
statements;
}
Example:
C code to check greater number amongst a & b.
C-Programming
Walchand Institute of Technology, Solapur Page 6
if (a>b)
{
printf(“\n a is greater”);
}
else
{
printf(“\n b is greater”);
}
Flow-Diagram for if-else statement:
Algorithm:
Step1: Start
Step2: Read value of A and B from user
Step3: If A > B then
Print “A is greater number”
goto step4
Else
Print “B is greater number”
Gtoto step4
End If
Step4: Stop
C-Programming
Walchand Institute of Technology, Solapur Page 7
Flowchart:
Program:
#include<stdio.h>
#include<conio.h>
void main()
{
int a, b;
clrscr();
printf(“\n Enter value of ‘A’:”);
scanf(“%d”, &a);
printf(“\n Enter value of ‘B’:”);
scanf(“%d”, &b);
if(a>b)
{
printf(“\n %d is greater number”,a);
}
C-Programming
Walchand Institute of Technology, Solapur Page 8
else
{
printf(“\n %d is greater number”,b);
}
}
Input:
Enter value of ‘A’: 25
Enter value of ‘B’: 50
Output:
50 is greater number
Practice Problem Statements:
Write a program using if_else statement
i) To read person’s age and decide whether that person is eligible for voting
or not and accordingly display messages.
ii) To check that user entered number is positive or negative and
accordingly display messages.
iii) To decide given number is EVEN or ODD.
Conclusion:
Thus a C program using if-else statement, to display greater number amongst given
two numbers a & b is implemented.
Learning Outcome:
At the end of this assignment, students are able to
- explain decision control statements in C
- write C code using if-else statement in C
- draw flowchart for decision making solutions
C-Programming
Walchand Institute of Technology, Solapur Page 9
Assignment#4c
Program Statement:
Write a C program using else-if ladder statement, to display grades accordingly
marks.
(For example if marks obtained is less than 40 then fail, if marks is in between 40
to 59 then C grade, if marks is in between 60 to 74 then B grade, if marks is in
between 75 to 90 then A grade and marks in between 90 to 10 then A+ grade
otherwise invalid marks.
Learning Objectives:
Students will be able to
- explain decision control statements in C
- write C code using else-if ladder in C
- draw flowchart for decision making solutions
Theory:
Ladder else_if statements:
� If we are having different - different test conditions with different - different
statements, then for these kind of programming we need else if leader.
The general form of else_if statements
if(test_condition1)
{
statement 1;
}
else if(test_condition2)
{
statement 2;
}
else if(test_condition3)
{
statement 3;
}
C-Programming
Walchand Institute of Technology, Solapur Page 10
else // at last, we use only else.
{
statement x;
}
In above syntax there are four test conditions with four different - different
statements.
Example:
C code to check greater number amongst three numbers a, b & c.
if (a>b && a>c)
{
printf(“\n %d is greater number” ,a);
}
else if(b>a && b>c)
{
printf(“\n %d is greater number”, b);
}
else
{
printf(“\n %d is greater number”,c);
}
Flow-Diagram for if-else statement:
C-Programming
Walchand Institute of Technology, Solapur Page 11
Algorithm:
Step1: Start
Step2: Read ‘marks’
Step3: If marks<40 Then
Print “Fail”
goto step4
Else If marks >=40 and marks<60 Then
Print “Grade-C”
goto step4
Else If marks>=60 and marks<75 Then
Print “Grade-B”
goto step4
Else If marks>=75 and marks<90 Then
Print “Grade-A”
goto step4
Else If marks>=90 Then
Print “Grade-A+”
goto step4
Else
Print “Invalid”
End If
Step4: Stop
C-Programming
Walchand Institute of Technology, Solapur Page 12
Flowchart:
C-Programming
Walchand Institute of Technology, Solapur Page 13
Program:
#include<stdio.h>
#include<conio.h>
void main()
{
int marks;
clrscr();
printf(“\n Enter marks:”);
scanf(“%d”, &marks);
if (marks<40)
{
printf(“\n Fail”);
}
else if(marks>=40 && marks<60)
{
printf(“\n Grade-C”);
}
else if(marks >=60 && marks <75)
{
printf(“\n Grade-B”);
}
else if(marks>=75 && marks<90)
{
printf(“\n Grade-A”);
}
else if(marks>90)
{
printf(“\n Grade-A+”);
}
else
{
printf(“\n Invalid”);
}
C-Programming
Walchand Institute of Technology, Solapur Page 14
getch();
}
Input:
Enter marks: 65
Output:
Grade-B
Practice Problem Statements:
Write a program to enter the temperature and print the following message
according to the given temperature by using else_if ladder statement.
1. T<=0 "Its very very cold".
2. 0 < T < 0 "Its cold".
3. 10 < T < =20 "Its cool out".
4. 20 < T < =30 "Its warm".
5. T>30 "Its hot".
2) Write a program to perform arithmetic operation based on user choice. First read
two integer numbers from user and operator symbol e.g. +, -, *, / and %. Based on
user choice perform arithmetic operation and display result. Here use ladder else_if
construct.
Input:
Enter two numbers: 12 13
Enter operation: +
Output:
Addition of two numbers 12 & 13 is equal to 25
Conclusion:
Thus a C program using else-if ladder statement, to display grades accordingly
marks is implemented
C-Programming
Walchand Institute of Technology, Solapur Page 15
Learning Outcomes:
At the end of this assignment, students are able to
- explain decision control statements in C
- write C code using else-if ladder in C
- draw flowchart for decision making solutions
C-Programming
Walchand Institute of Technology, Solapur Page 16
Assignment#4d
Program Statement:
Write a C program using nested if_else statement, to display greater number
amongst given three numbers a, b, & c grades accordingly marks.
Learning Objectives:
Students will be able to
- explain decision control statements in C
- write C code using else-if ladder in C
- draw flowchart for decision making solutions
Theory:
Nested if_else construct:
� In Nested if ..... else, the if and else part can contain one or more if else
statements.
General Form:
if(conditional-expression)
{
if(conditional-expression)
{
statements;
}
else
{
statements;
}
statements;
}
else
{
if(conditional-expression)
{
C-Programming
Walchand Institute of Technology, Solapur Page 17
statements;
}
statements;
}
Example:
C code to check greater number amongst three given numbers a, b & c.
if (a>b)
{
if(a>c)
{
printf(“%d is greater number”,a);
}
else
{
printf(“%d is greater number”,c);
}
}
else
{
if(b>c)
{
printf(“%d is greater number”,b);
}
else
{
printf(“%d is greater number”,c);
}
}
C-Programming
Walchand Institute of Technology, Solapur Page 18
Flow-Diagram for if-else statement:
Algorithm:
Step1: Start
Step2: Read two numbers A & B
Step3: If A > B Then
If A > C Then
Print “A is greater number”
Else
Print “C is greater number”
End If
Else
If B> C Then
Print “B is greater number”
Else
Print “C is greater number”
End If
End If
Step4: Stop
C-Programming
Walchand Institute of Technology, Solapur Page 19
Flowchart:
Program:
#include<stdio.h>
#include<conio.h>
void main()
{
int a, b, c;
clrscr();
printf(“\n Enter three numbers:”);
scanf(“%d %d %d”, &a, &b, &c);
if (a>b)
{
C-Programming
Walchand Institute of Technology, Solapur Page 20
if(a>c)
{
printf(“\n %d is greater number.”,a);
}
else
{
printf(“\n %d is greater number.”,c);
}
}
else
{
if(b>c)
{
printf(“\n %d is greater number.”,a);
}
else
{
printf(“\n %d is greater number.”,c);
}
}
}
Input:
Enter three numbers: 10 30 20
Output:
30 is greater number.
Practice Problem Statements:
Write a C program to decide given number is divisible by 3 and 2 or 4 and 2
using nested if_else statement.
C-Programming
Walchand Institute of Technology, Solapur Page 21
Conclusion:
Thus a C program using nested if_else statement, to display greater number
amongst given three numbers a, b, & c grades accordingly marks is implemented.
Learning Outcomes:
At the end of this assignment, students are able to
- explain decision control statements in C
- write C code using else-if ladder in C
- draw flowchart for decision making solutions
Recommended