WEEK 5
5. a .Write a C program to calculate Electricity bill .
Conditions
For first 50 units – Rs. 3.50/unit
For next 100 units – Rs. 4.00/unit
For next 100 units – Rs. 5.20/unit
For units above 250 – Rs. 6.50/unit
You can use conditional statements.
int main()
{
float bill, units;
printf("Enter the units consumed=");
scanf("%f",&units);
if(units<=50 && units>=0)
{
bill=units*3.50;
printf("Electricity Bill=%f Rupees",bill);
}
else if(units<=100 && units>50)
{
bill=50*3.50+(units-50)*4;
printf("Electricity Bill=%f Rupees",bill);
}
else if(units<=250 && units>150)
{
bill=50*3.50+100*4+(units-150)*5.20;
printf("Electricity Bill=%f Rupees",bill);
else if(units>250)
{
bill=50*3.50+100*4+100*5.20+(units-250)*6.50;
printf("Electricity Bill=%f Rupees",bill);
}
else
{
printf("Please enter valid consumed units...");
}
return 0;
}
Input and output:
Enter the units consumed=278.90
Electricity Bill=1282.84 Rupees
// 5 .b program to find max and min numbers of 4 numbers using if else if
#include <stdio.h>
int main()
{
double a1, a2, a3, a4;
double max, min;
printf(" Enter Input four numbers: \n");
scanf("%lf%lf%lf%lf", & 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("Max num= %lf Min num=%lf \n", max,min);
return 0;
}
Input and output:
Enter input four numbers : 10 20 30 40
Max=40 Min =10
//5.c Program to Find Roots of a Quadratic Equation if else if
#include <math.h>
int main()
{
double a, b, c, discriminant, root1, root2, realPart, imagPart;
printf("Enter coefficients a, b and c: ");
scanf("%lf %lf %lf", &a, &b, &c);
discriminant = b * b - 4 * a * c;
if (discriminant > 0)
{
root1 = (-b + sqrt(discriminant)) / (2 * a);
root2 = (-b - sqrt(discriminant)) / (2 * a);
printf("root1 = %.2lf and root2 = %.2lf", root1, root2);
}
else if (discriminant == 0)
{
root1 = root2 = -b / (2 * a);
printf("root1 = root2 = %.2lf;", root1);
}
else
{
realPart = -b / (2 * a);
imagPart = sqrt(-discriminant) / (2 * a);
printf("root1 = %.2lf+%.2lfi and root2 = %.2f-%.2fi", realPart, imagPart, realPart, imagPart);
}
return 0;
}
Input and output:
Enter coefficients a, b and c: 2.3
4
5.6
root1 = -0.87+1.30i and root2 = -0.87-1.30i
// 5.d. Write a program to make a calculator to perform some basic operations using switch
case.
• If the operator is ‘+’, then print the addition operation on the given integer.
• If the operator is ‘-’, then print the subtraction operation on the given integer.
• If the operator is ‘*’, then print the multiplication operation on the given integer.
• If the operator is ‘/’, then print the division operation on the given integer.
int main ()
{
int num1, num2;
float result;
char ch;
printf ("Enter first number = ");
scanf ("%d", &num1);
printf ("Enter second number = ");
scanf ("%d", &num2);
printf ("Choose operator to perform operations = ");
scanf (" %c", &ch);
result = 0;
switch (ch)
{
Case '+': result = num1 + num2;
break;
case '-' : result = num1 - num2;
break;
case '*' : result = num1 * 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;
}
Input and Output
Enter first number = 6
Enter second number = 7
Choose operator to perform operations = +
Result: 6 + 7 = 13.000000
5. e. Program to Check Leap Year if else if
#include<stdio.h>
Int main()
{
int year;
printf("Enter a year: ");
scanf("%d", &year);
if (year % 400 == 0)
{
printf("%d is a leap year.", year);
}
elseif (year % 100 == 0)
{
printf("%d is not a leap year.", year);
}
elseif (year % 4 == 0)
{
printf("%d is a leap year.", year);
}
else
{
printf("%d is not a leap year.", year);
}
return0;
}
Run Code
Output 1
Enter a year: 1900
1900 is not a leap year.