CPL Week5
CPL Week5
Objective: Explore the full scope of different variants of “if construct” namely if-
else, nullelse, if-else if*-else, switch and nested-if including in what scenario
each one of them can be used and how to use them. Explore all relational and
logical operators while writing conditionals for “if construct”.
Suggested Experiments/Activities:
Tutorial 5: Branching and logical expressions:
Lab 5: Problems involving if-then-else structures.
i) Write a C program to find the max and min of four numbers using if-else.
ii) Write a C program to generate electricity bill.
iii) Find the roots of the quadratic equation.
iv) Write a C program to simulate a calculator using switch case.
v) Write a C program to find the given year is a leap year or not.
i) Write a C program to find the max and min of four numbers using if-else.
SOURCE CODE:
#include<stdio.h>
int main()
{
int a1, a2, a3, a4;
int max, min;
printf("Input four numbers: \n");
scanf("%d %d %d %d", &a1,&a2,&a3,&a4);
if (a1 >= a2 && a1 >= a3 && a1 >= a4)
max = a1;
else
if (a2 >= a1 && a2 >= a3 && a2 >= a4)
max = a2;
else
if (a3 >= a1 && a3 >= a2 && a3 >= a4)
max = a3;
else
max = a4;
if (a1 <= a2 && a1 <= a3 && a1 <= a4)
min = a1;
else
if (a2 <= a1 && a2 <= a3 && a2 <= a4)
min = a2;
else
if (a3 <= a1 && a3 <= a2 && a3 <= a4)
min = a3;
else
min = a4;
printf("Maximum is : %d\n", max);
printf("Minimum is : %d",min);
return 0;
}
OUTPUT:
ii) Write a C program to generate electricity bill.
Text Book Program-220
iii) Find the roots of the quadratic equation.
Text Book Program-169
iv) Write a C program to simulate a calculator using switch case.
SOURCE CODE:
#include <stdio.h>
int main()
{
int num1,num2;
float result=0;
char ch;
printf("Enter first number: ");
scanf("%d",&num1);
printf("Enter second number: ");
scanf("%d",&num2);
printf("Choose the operation(+,-,*,/,%):");
scanf(" %c",&ch);
switch(ch)
{
case '+':
result=num1+num2;
break;
case '-':
result=num1-num2;
break;
case '*':
result=num1*num2;
break;
case '/':
result=(float)num1/(float)num2;
break;
case '%':
result=num1%num2;
break;
default:
printf("Invalid operation.\n");
}
printf("Result: %d %c %d = %f\n",num1,ch,num2,result);
return 0;
}
OUTPUT:
v) Write a C program to find the given year is a leap year or not.
A leap year is exactly divisible by 4 except for century years (years ending with
00). The century year is a leap year only if it is perfectly divisible by 400.
1. If a year is evenly divisible by 4 means having no remainder then go to next
step. If it is not divisible by 4. It is not a leap year. For example: 1997 is not a
leap year.
2. If a year is divisible by 4, but not by 100. For example: 2012, it is a leap year. If
a year is divisible by both 4 and 100, go to next step.
3. If a year is divisible by 100, but not by 400. For example: 1900, then it is not a
leap year. If a year is divisible by both, then it is a leap year. So 2000 is a leap
year.
Below conditions are used to check that year is a leap year or not.
#include<stdio.h>
int main()
{
int year;
printf("Enter a year: ");
scanf("%d",&year);
if((year%400==0) || (year%100!=0) && (year%4==0))
{
printf("%d is a leap year", year);
}
else
{
printf("%d is not a leap year", year);
}
return 0;
}
OUTPUT