Lab 5
Lab 5
#include <stdio.h>
#include <math.h>
int main()
{
float area, radius, width, length, a, b, c,s;
int choice;
//for circle
if(choice==1)
{
printf("Enter the radius of the Circle: ");
scanf("%f", &radius);
area = M_PI*radius*radius;
printf("The area of the circle with radius %f is %.2f", radius,
area);
}
//for rectangle
if(choice ==2)
{
printf("Enter the Width of the rectangle: ");
scanf("%f", &width);
printf("Enter the length of the rectangle: ");
scanf("%f", &length);
area = width*length;
printf("The area of the rectangle is %.2f", area);
}
//area of triangle using heron's formula
if(choice ==3)
{
printf("Enter the 3 sides of triangle eg (12 13 14): ");
scanf("%f %f %f", &a, &b, &c);
s = (a+b+c)/2;
area = sqrt(s*(s-a)*(s-b)*(s-c));
printf("The area of the triangle is %.2f", area);
}
return 0;
}
Output
**********Choose an Option:*********
1: Area of the Circle
2: Area of the Rectangle
3: Area of the Triangle
3
Enter the 3 sides of triangle eg (12 13 14): 3 7 6
The area of the triangle is 8.94
2. Remove all the break statements from Ex-1 (with switch-case construct) and try to execute
the program with few inputs. Observe the difference.
Exercise
3. Program to input number of week’s day (1-7) and translate to its equivalent name of the day
of the week (e.g. 1 to Sunday, 2 to Monday)
#include<stdio.h>
#include<conio.h>
void main()
{
int ch;
clrscr(); //to clear the screen
printf(“Enter number of week’s day(1-7):”);
scanf(“%d”,&ch);
switch(ch)
{
case 1: printf(“nSunday”);
break;
case 2: printf(“nMonday”);
break;
case 3: printf(“nTuesday”);
break;
case 4: printf(“nWednesday”);
break;
case 5: printf(“nThursday”);
break;
case 6: printf(“nFriday”);
break;
case 7: printf(“nSaturday”);
break;
}
getch(); //to stop the screen
}
4. Write a program to design a calculator that performs addition, subtraction, minus and
division operation. This program inputs two operands and an operator and then displays the
calculated results.
5. #include <stdio.h>
6.
7. int main()
8. {
9. int num1,num2;
10. float result;
11. char ch; //to store operator choice
12.
13. printf("Enter first number: ");
14. scanf("%d",&num1);
15. printf("Enter second number: ");
16. scanf("%d",&num2);
17.
18. printf("Choose operation to perform (+,-,*,/,%): ");
19. scanf(" %c",&ch);
20.
21. result=0;
22. switch(ch)
23. {
24. case '+':
25. result=num1+num2;
26. break;
27.
28. case '-':
29. result=num1-num2;
30. break;
31.
32. case '*':
33. result=num1*num2;
34. break;
35.
36. case '/':
37. result=(float)num1/(float)num2;
38. break;
39.
40. case '%':
41. result=num1%num2;
42. break;
43. default:
44. printf("Invalid operation.\n");
45. }
46.
47. printf("Result: %d %c %d = %f\n",num1,ch,num2,result);
48. return 0;
49. }
First run:
Enter first number: 10
Enter second number: 20
Choose operation to perform (+,-,*,/,%): +
Result: 10 + 20 = 30.000000
Second run:
Enter first number: 10
Enter second number: 3
Choose operation to perform (+,-,*,/,%): /
Result: 10 / 3 = 3.333333
Third run:
Enter first number: 10
Enter second number: 3
Choose operation to perform (+,-,*,/,%): >
Invalid operation.
Result: 10 > 3 = 0.000000
5. Write a program to calculate a bill of internet browsing. The conditions are given below:
Minimum Rs. 200 for up to 100 calls.
Plus, Rs. 0.60 per call for next 50 calls.
Plus, Rs. 0.50 per call for next 50 calls.
Plus, Rs. 0.40 per call for any call beyond 200 calls
#include <stdio.h>
int main()
{
int calls;
float bill;
printf("Enter number of calls :");
scanf("%d", &calls);
if (calls <= 100)
{
bill = 200;
}
else if (calls > 100 && calls <= 150)
{
calls = calls - 100;
bill = 200+(0.60 *calls);
}
else if (calls > 150 && calls <= 200)
{
calls = calls - 150;
bill = 200+(0.60 *50) + (0.50 *calls);
}
else
{
calls = calls - 200;
bill = 200 + (0.60 * 50) + (0.50 * 50) + (0.40 *
calls);
}
printf("Your bill is Rs. %0.2f", bill);
return 0;
}
6. Calculate an amount of a telephone bill for the following criteria. (Without Loop)
Calls charge per call (Rs.)
1-150 0
151-250 .9
251-400 1.2
401 onwards 1.5
*Example Program
Program to calculate the telephone bill based on the following conditions :
3. (500+) In this case first 200 call free (200-500) 1re/call and (500+) will be 2/call
For Example :600 calls will be calculated as (200*0 + 300*1 +100*2 + 200 ) = 700
(total bill)
#include <stdio.h>
#include <conio.h>
void main()
{
int calls, bill=0;
printf("Enter the number of calls");
scanf("%d",&calls);
7. Calculate amount of an electricity bill for the following criteria. (Without Loop)
Units charge per unit (Rs.)
First, 1-100 up to 0
Next, 101-200 up to 1.5
Next, 201-400 up to 2.5
401 onwards 3.5
*Example Program
You need to write a C program to calculate electricity bill using if-else
statements.
Conditions:
}
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;
}
Excercise