UNIT-2 Ppt Part 2_Conditional Statements Ppt
UNIT-2 Ppt Part 2_Conditional Statements Ppt
Course Delivery
#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