0% found this document useful (0 votes)
28 views8 pages

Lab 5

The document contains several C programming exercises, including calculating areas of geometric shapes, translating days of the week, designing a calculator, and calculating bills for internet usage, telephone, and electricity. Each exercise includes code snippets and example outputs demonstrating the functionality of the programs. The document serves as a practical guide for learning basic programming concepts and conditional statements in C.

Uploaded by

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

Lab 5

The document contains several C programming exercises, including calculating areas of geometric shapes, translating days of the week, designing a calculator, and calculating bills for internet usage, telephone, and electricity. Each exercise includes code snippets and example outputs demonstrating the functionality of the programs. The document serves as a practical guide for learning basic programming concepts and conditional statements in C.

Uploaded by

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

1.

Program to calculate an area of a circle, a rectangle or a triangle depending on user’s choice

#include <stdio.h>
#include <math.h>

int main()
{
float area, radius, width, length, a, b, c,s;
int choice;

printf("**********Choose an Option:*********\n ");


printf("1: Area of the Circle\n ");
printf("2: Area of the Rectangle\n ");
printf("3: Area of the Triangle\n ");

//input the geometry shape


scanf("%d", &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;
}

Enter number of calls :350


Your bill is Rs. 315.00

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 :

1. (0-200) first 200 calls are free

2. (200-500) Calls between 200 to 500 will be charged 1 /Call.

3. (500+) In this case first 200 call free (200-500) 1re/call and (500+) will be 2/call

4. 200 is rental in the final bill

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);

if(calls<=200) // first if condition for free calls upto 200


bill=0;
if(calls>200&& calls<=500) // if condtion for 200 calls free +1 re/call on rest
calls
bill=200*0+ (calls-200)*1;
if(calls>500)
bill=200*0+300*1+(calls-500)*2; // if calls 200 free+ (200-500)->1Re/Call
(500+)->2Re/call
bill=bill+200; // 200 rental is added.
printf("the final bill =%d",bill);
}

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:

 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.
#include<stdio.h>
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;
}

8. Calculate discount in rupees for the following criterion. (Without Loop)


Cost price discount
>=800 25%
500-800 20%
<500 no discount (0%)

Excercise

You might also like