0% found this document useful (0 votes)
23 views24 pages

UNIT-2 Ppt Part 2_Conditional Statements Ppt

Uploaded by

ryangosling2707
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views24 pages

UNIT-2 Ppt Part 2_Conditional Statements Ppt

Uploaded by

ryangosling2707
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 24

School of CSE

Course Delivery

B.Tech (CSE) - II Semester


By

Dr.. Raghavendra Reddy


Assistant Professor
[email protected]
UNIT -2

•Conditional statements: if statement, if-else statement,


nested if, switch statement.
BRANCHING CONSTRUCTS:
•The statements that transfer the control from one place to other place in the
program with condition are called branching or condition statements. These
statements alter the sequence of instructions.

•2 types of conditional statements are:


• Conditional branch statements
• Un-conditional branch statements

•Conditional branch statements: It is classified as:


• if
• if-else
• Nested if
• else if ladder
• Switch (multi-way selector)
1.If statement
2.If-else statement
2.If-else statement
•Implement a Program to find whether a given integer number is even or
odd.
#include <stdio.h>
int main()
{
int n;
printf("Enter an integer\n");
scanf("%d", &n);
if (n%2 == 0)
printf("Even\n");
else
printf("Odd\n");
return 0; }
2.If-else statement
•Examples:
•1. Program to check for leap year.
•2. Program to find the largest of 2 numbers
2.If-else statement
Largest of 2 numbers

#include <stdio.h>
int main( )
{
int n1, n2;
printf("Enter two numbers\n ");
scanf("%d%d", &n1, &n2);
if(a>b)
printf(“a is large\n”);
else if(b>a)
printf(“b is large\n”);
else
printf(“a and b are same\n”);
return 0;
}
3.Nested if statements
4.if-else-if statement / if-else ladder
4.if-else-if statement / if-else ladder
Examples:
1. Program to find the largest of 3 numbers
2. Check the given number is zero, +ve or negative number
4.if-else-if statement
Write a c program to find the largest of 3 numbers.
#include<stdio.h>
int main()
{
int a,b,c;
printf(“enter the values of a,b and c\n”);
scanf(“%d%d%d”, &a,&b,&c);
if(a>b && a>c)
printf(“ a is larger\n”);
else if (b>a && b>c)
printf(“ b is larger\n”);
else
printf(“ c is larger\n”);
return 0; }
5.Switch statement
• The switch statement is a control statement used to make a selection
between many alternatives. Here choice value can be integer or
character data type.
5.Switch statement
5.Switch statement
#include <stdio.h>
int main()
{
int num1,num2,sum,sub,mul,mod, ch;
float div;
printf("Enter the values of a and \n");
scanf("%d%d",&num1, &num2);
printf("enter your choice\n ");
scanf("%d",&ch);
switch(ch)
{
case 1:
sum=num1+num2;
printf("sum= %d\n",sum);
break;
case 2:
sub=num1-num2;
printf("sub= %d\n",sub);
break;
5.Switch statement
case 3:
mul=num1*num2;
printf("mul= %d\n",mul);
break;
case 4:
if(num2!=0)
{
div=(float) (num1/num2);
printf("div= %f\n",div);
}
else
printf("Division by zero is not allowed\n");
break;
case 5:
mod=num1%num2;
printf("mod= %d\n",mod);
break;
5.Switch statement
default:
printf("Invalid operation.\n");
}
return 0;
}
5.Switch statement

#include <stdio.h>
int main()
{
float num1, num2, result;
char op;
printf("Enter two integer numbers: \n");
scanf("%f%f", &num1, &num2);
printf("Enter the operation (+, -, *, /): ");
scanf(" %c", &op);
switch (op)
{
5.Switch statement
case '+':
result = num1 + num2;
printf("Sum: %f\n", result);
break;
case '-':
result = num1 - num2;
printf("Difference: %f\n", result);
break;
case '*':
result = num1 * num2;
printf("Product: %f\n", result);
break;
5.Switch statement

case '/':
// Check for division by zero
if (num2!= 0)
{
result = num1 / num2;
printf("Result: %f\n", result);
}
else
printf("Division by zero is not allowed.\n");

break;
5.Switch statement
default:
printf("Invalid operator\n");
}
return 0;
}
2.If-else statement
1. Program to check for leap year.
#include <stdio.h>
int main()
{
int year;
printf("Enter a year to check if it is a leap year\n");
scanf("%d", &year);
if (( year%4 == 0 && year%100 != 0)||(year%400==0)
printf("%d is a leap year\n", year);
else
printf("%d is not a leap year\n", year);
return 0;
}
2.If-else statement
1. Implement a Program to display the grade of a student based on the marks obtained.
2. Implement a Program to read the nonzero coefficients of a quadratic equation and compute
and display its roots.
3. Implement a Program to simulate a simple calculator by using switch
4. WRITE A PROGRAM TO CHECK WHETHER AN ENTERED CHARACTER IS
VOWEL OR NOT USING SWITCH CASE.
5. WRITE A PROGRAM TO PRINT DAY OF THE WEEK by using switch
Thank You

You might also like