You are on page 1of 14

Department of Computer Science and Engineering

Institute of Technical Education & Research, SOA, Deemed to be University

W EEK -E ND A SSIGNMENT-02
Operating Systems Workshop (CSE 3541)

Problem Statement:
Experiment with selection structures; if, if-else, if-else if-else and switch statements to develop applications.

Assignment Objectives:
To become familiar with one of the control structures, selection, out of sequence, selection, and repetition
kinds of control structure.

Instruction to Students (If any):


Students are required to write his/her own program by avoiding any kind of copy from any sources.
Additionally, They must be able to realise the outcome of that question in relevant to systems pro-
gramming. You may use additional pages on requirement.

Programming/ Output Based Questions:


1. Find the value that is assigned to x when y is 10.0.
Code snippet: 1(a)
Output
x = 25.0;
if(y != (x - 10.0))
x = x - 10.0; The value of x=15.000000
else
x = x / 2.0;

Code snippet: 1(b)


if(y < 15.0) Output
if(y >= 0.0)
x = 5 * y;
else
x = 2 * y; The value of x=50.000000
else
x = 3 * y;

Code snippet: 1(c) Output


if (y < 15.0 && y >= 0.0)
x = 5 * y; The value of x=50.000000
else
x = 2 * y;

2. Write C statements to carry out the following.

1
Department of Computer Science and Engineering
Institute of Technical Education & Research, SOA, Deemed to be University

Output
if it has the item then
Enter an item: 5
If item is nonzero, then multiply product by item The product is 5.
and save the result in product ; otherwise, skip if it doesn't contain any item
the multiplication. In either case, print the value Enter an item: 0
of product. Declare the appropriate type and ini- The product is 1.
tialize, if required.

3. Correct the following if statement; assume the indentation is correct.


Output
if (deduct < balance);
float deduct=5000;
balance = balance - deduct; float balance=15000;
printf("New balance is %.2f\n", balance); if (deduct < balance)
{
else; balance = balance - deduct;
printf("New balance is %.2f\n", balance);
printf("Deduction of %.2f refused.\n", }
deduct); else
{
printf("Would overdraw account.\n"); printf("Deduction of %.2f refused.\n",deduct);
printf("Would overdraw account.\n");
printf("Deduction = %.2f Final balance = %.2f",deduct, balance);
printf("Deduction = %.2f Final balance = %.2f", }
deduct, balance);

4. Write an interactive program that contains an if statement that may be used to compute the area of a
square ( area = side2 ) or a circle ( area = pi × radius2 ) after prompting the user to type the first
character of the figure name (S or C).

Write/paste your code here t


#include <stdio.h>

#define PI 3.14159265358979323846

int main() {
char figure;
float side, radius, area;

printf("Enter the first character of the figure name (S or C): ");


scanf("%c", &figure);

if (figure == 'S') {
printf("Enter the side of the square: ");
scanf("%f", &side);

area = side * side;


} else if (figure == 'C') {
printf("Enter the radius of the circle: ");
scanf("%f", &radius);

area = PI * radius * radius;


} else {
printf("Invalid figure name.\n");
return 1;
}

printf("The area of the figure is %f.\n", area);

return 0;}

5. Write a nested if statement for the decision diagrammed in the accompanying flowchart. Use a
multiple-alternative if for intermediate decisions where possible.
2
Department of Computer Science and Engineering
Institute of Technical Education & Research, SOA, Deemed to be University

Space for Program and output t


#include <stdio.h>
Output
#define PI 3.14159265358979323846
Enter the ph value
int main() { 7
int ph,ph1,ph2,ph3; Neutral
printf("Enter the ph value\n"); Enter the ph value
scanf("%d",&ph);
if(ph>7) 5
{ Acidic
Enter the ph value
if(ph<12)
{ 1
printf("Alkaline\n"); Very Acidic
} Enter the ph value
else
{ -1
printf("Very Alkaline\n"); Very Acidic
} Enter the ph value
}
15
Very Alkaline
else
{
if(ph==7)
{
printf("Neutral\n");
}
else
{
if(ph>2 && ph<7)
{
printf("Acidic\n");
}
else
{
printf("Very Acidic\n");
}
}
}
return 0;

3
Department of Computer Science and Engineering
Institute of Technical Education & Research, SOA, Deemed to be University

6. Implement the following decision table using a nested if statement. Assume that the grade point
average is within the range 0.0 through 4.0.

Grade Point Average Transcript Message


0.00.99 Failed semesterregistration suspended
1.01.99 On probation for next semester
2.02.99 (no message)
3.03.49 Deans list for semester
3.54.00 Highest honors for semester

Space for Program and output t

#include <stdio.h>

int main() {
float gpa;

printf("Enter your GPA: ");


scanf("%f", &gpa);

if (gpa < 0.0 || gpa > 4.0) {


printf("Invalid GPA.\n");
return 1;
}

if (gpa < 1.0) {


printf("Failed semester; registration suspended.\n");
} else if (gpa < 2.0) {
printf("On probation for next semester.\n");
} else if (gpa < 3.0) {

} else if (gpa < 3.5) {


printf("Dean's list for semester.\n");
} else {
printf("Highest honors for semester.\n");
}

return 0;
}
Output

Enter your GPA: 0

Failed semester; registration suspended

Enter your GPA: 1.25

On probation for next semester.

Enter your GPA: 2.3

Enter your GPA: 3.02

Dean's list for semester.

Enter your GPA: 3.52

Highest honors for semester.

4
Department of Computer Science and Engineering
Institute of Technical Education & Research, SOA, Deemed to be University

7. Implement the following decision table using a multiple-alternative if statement. Assume that the
wind speed is given as an integer.

Wind Speed (mph) Category


below 25 not a strong wind
2538 strong wind
3954 gale
5572 whole gale
above 72 hurricane

Space for Program and output t

#include <stdio.h> Output

Enter wind speed (mph): 20


int main() {
Category: not a strong wind
int wind_speed;
Enter wind speed (mph): 28
printf("Enter wind speed (mph): ");
Category: strong wind
scanf("%d", &wind_speed);
Enter wind speed (mph): 44
if (wind_speed < 25) { Category: gale
printf("Category: not a strong wind\n"); Enter wind speed (mph): 69
} else if (wind_speed >= 25 && wind_speed <= 38) { Category: whole gale
printf("Category: strong wind\n"); Enter wind speed (mph): 82
} else if (wind_speed >= 39 && wind_speed <= 54) { Category: hurricane
printf("Category: gale\n");

} else if (wind_speed >= 55 && wind_speed <= 72) {

printf("Category: whole gale\n");

} else {

printf("Category: hurricane\n");

return 0;

5
Department of Computer Science and Engineering
Institute of Technical Education & Research, SOA, Deemed to be University

8. What will be printed by this carelessly constructed switch statement if the value of color is ’R’?
switch (color) { / break statements missing /
Output
* *
case ’R’:
printf("red\n");
case ’B’: red
printf("blue\n"); blue
case ’Y’:
printf("yellow\n"); yellow
}

9. Determine life expectancy of a standard light bulb given the input watts; 35, 45, 76, 120 respectively.
switch (watts) {
case 25:
Output
life = 2500;
break;
case 40:
case 60: It will not print anything as
life = 1000;
break;
the print statement is not
case 75: mentioned as well as given
case 100: input watts is not match any
life = 750; of the cases
break;
default:
life = 0;
}

10. C relational and equality operators give a result of 1 for true and 0 for false. Evaluate the following
expression for different values of x. Also write the statement to avoid such common error.
if (0 <= x <= 4) Test cases and output t
printf("Condition is true\n");
(a) x=5
Common Error Correction (b) x=15
if (x>=0 && x <= 4){ (c) x=34
(d) x=-20
printf("Condition is true\n");
} (e) x=-45

11. Evaluate the following code snippet for different values of x.


printf("Enter x \n");
scanf("%d",&x);
if (x = 10)
printf("x is 10");
printf("Differentiate: == and =");
else Test cases and output t
printf(" simply incorrect results");
(a) x=5
Common Error correction in if statement (b) x=15
For every test case it will always print the if (c) x=34
condition as in the if statement x=10 so any (d) x=-20
value of x is assign with 10 so it will only print (e) x=-45
if statement condition not the else ones
6
Department of Computer Science and Engineering
Institute of Technical Education & Research, SOA, Deemed to be University

12. Write a switch statement that assigns to the variable lumens the expected brightness of a standard
light bulb whose wattage has been stored in watts. Assign 1 to lumens if the value of watts is not in
the table.Use this table:

Watts Brightness (in Lumens)


15 125
25 215
40 500
60 880
75 1000
100 1675

Space for Program and output t


#include <stdio.h>
Output
Enter the wattage of the light bulb: 15
int main() {

int watts, lumens; The brightness of the light bulb is 125 lumens.

printf("Enter the wattage of the light bulb: ");

scanf("%d", &watts);

switch (watts) {
case 15:
lumens = 125;
break;

case 25:
lumens = 215;
break;

case 40:
lumens = 500;
break;

case 60:
lumens = 880;
break;

case 75:
lumens = 1000;
break;

case 100:
lumens = 1675;
break;

default:
lumens = 1;
break;

printf("The brightness of the light bulb is %d lumens.\n", lumens);

return 0;

7
Department of Computer Science and Engineering
Institute of Technical Education & Research, SOA, Deemed to be University

13. Keiths Sheet Music needs a program to implement its music teachers discount policy. The program is
to prompt the user to enter the purchase total and to indicate whether the purchaser is a teacher. The
store plans to give each customer a printed receipt, so your program is to create a nicely formatted file
called receipt.txt. Music teachers receive a 10% discount on their sheet music purchases unless the
purchase total is $100 or higher. In that case, the discount is 12%. The discount calculation occurs
before addition of the 5% sales tax. Here are two sample output filesone for a teacher and one for a
nonteacher.
Total purchases $122.00
Teacher’s discount (12%) 14.64
Discounted total 107.36
Sales tax (5%) 5.37
Total $112.73
Total purchases $24.90
Sales tax (5%) 1.25
Total $26.15

Note: to display a % sign, place two % signs in the format string:


printf("%d%%", SALES_TAX);

To write the output in the file receipt.txt use output redirection, ./a.out > receipt.txt
Space for Program and output t

#include <stdio.h>

#define TEACHER_DISCOUNT_RATE 0.12 // 12% discount for teachers

#define NORMAL_DISCOUNT_RATE 0.10 // 10% discount for non-teachers

#define SALES_TAX_RATE 0.05 // 5% sales tax rate

int main() {

double total_purchases, discount, discounted_total, sales_tax, final_total;

int is_teacher;

printf("Enter the total purchases: $");

scanf("%lf", &total_purchases);

printf("Is the purchaser a teacher? (1 for Yes, 0 for No): ");

scanf("%d", &is_teacher);

if (is_teacher) {

if (total_purchases >= 100.0) {

discount = total_purchases * TEACHER_DISCOUNT_RATE;

} else {

discount = total_purchases * NORMAL_DISCOUNT_RATE;

} else {

discount = total_purchases * NORMAL_DISCOUNT_RATE;

8
Department of Computer Science and Engineering
Institute of Technical Education & Research, SOA, Deemed to be University

Space for Program and output t

discounted_total = total_purchases - discount;

sales_tax = discounted_total * SALES_TAX_RATE;

final_total = discounted_total + sales_tax;

printf("Total Purchase:%.2lf\n",total_purchases);

printf("Discounted Total:%.2lf\n",discounted_total);

printf("Sales Tax:%.2lf\n",sales_tax);

printf("Final Total:%.2lf\n",final_total);

return 0;

Output

Enter the total purchases: $ 15000

Is the purchaser a teacher? (1 for Yes, 0 for No):

Total Purchase:15000.00

Discounted Total:13200.00

Sales Tax:660.00

Final Total:13860.00

9
Department of Computer Science and Engineering
Institute of Technical Education & Research, SOA, Deemed to be University

14. A particular cell phone plan includes 50 minutes of air time and 50 text messages for $15.00 a month.
Each additional minute of air time costs $0.25, while additional text messages cost $0.15 each. All
cell phone bills include an additional charge of $0.44 to support 911 call centers, and the entire bill
(including the 911 charge) is subject to 5 percent sales tax.
Write a program that reads the number of minutes and text messages used in a month from the user.
Display the base charge, additional minutes charge (if any), additional text message charge (if any),
the 911 fee, tax and total bill amount. Only display the additional minute and text message charges if
the user incurred costs in these categories. Ensure that all of the charges are displayed using 2 decimal
places.

Space for Program and output t


#include <stdio.h>

#define BASE_CHARGE 15.00

#define ADDITIONAL_MINUTE_CHARGE 0.25

#define ADDITIONAL_TEXT_MESSAGE_CHARGE 0.15

#define NINE_ONE_ONE_FEE 0.44

#define SALES_TAX_RATE 0.05

int main() {

int minutes_used, text_messages_used;

double additional_minute_charge, additional_text_message_charge, nine_one_one_fee, tax, total_bill_amount;

printf("Enter the number of minutes used: ");

scanf("%d", &minutes_used);

printf("Enter the number of text messages used: ");

scanf("%d", &text_messages_used);

if (minutes_used > 50) {

additional_minute_charge = (minutes_used - 50) * ADDITIONAL_MINUTE_CHARGE;

} else {

additional_minute_charge = 0.0;

}
if (text_messages_used > 50) {
additional_text_message_charge = (text_messages_used - 50) * ADDITIONAL_TEXT_MESSAGE_CHARGE;

} else {

additional_text_message_charge = 0.0;

nine_one_one_fee = NINE_ONE_ONE_FEE;

tax = (BASE_CHARGE + additional_minute_charge + additional_text_message_charge + nine_one_one_fee) * SALES_TAX_RATE;


total_bill_amount = BASE_CHARGE + additional_minute_charge + additional_text_message_charge + nine_one_one_fee + tax;
printf("Base charge: $%.2f\n", BASE_CHARGE);

if (additional_minute_charge > 0.0) {

printf("Additional minutes charge: $%.2f\n", additional_minute_charge);

10
Department of Computer Science and Engineering
Institute of Technical Education & Research, SOA, Deemed to be University

Space for Program and output t


if (additional_text_message_charge > 0.0) {

printf("Additional text message charge: $%.2f\n", additional_text_message_charge);

printf("911 fee: $%.2f\n", nine_one_one_fee);

printf("Tax: $%.2f\n", tax);

printf("Total bill amount: $%.2f\n", total_bill_amount);

return 0;

Output

Enter the number of minutes used: 60

Enter the number of text messages used: 55

Base charge: $15.00

Additional minutes charge: $2.50

Additional text message charge: $0.75

911 fee: $0.44

Tax: $1.07

Total bill amount: $20.76

11
Department of Computer Science and Engineering
Institute of Technical Education & Research, SOA, Deemed to be University

15. Write a program that determines the day number (1 to 366) in a year for a date that is provided as
input data. As an example, January 1, 1994, is day 1. December 31, 1993, is day 365. December 31,
1996, is day 366, since 1996 is a leap year. A year is a leap year if it is divisible by four, except that
any year divisible by 100 is a leap year only if it is divisible by 400. Your program should accept the
month, day, and year as integers. Include a function leap that returns 1 if called with a leap year, 0
otherwise.
Space for Program and output t
#include <stdio.h>

int leap(int year) {

if (year % 400 == 0) {

return 1;

} else if (year % 100 == 0) {

return 0;

} else if (year % 4 == 0) {

return 1;

} else {

return 0;

int day_number(int month, int day, int year) {

int days_in_year = 365;

if (leap(year)) {

days_in_year++;

int days_in_previous_months = 0;

for (int i = 1; i < month; i++) {

days_in_previous_months += (i == 2 && leap(year)) ? 29 : (i < 8) ? 31 : 30;

int day_number = days_in_previous_months + day;

return day_number;

int main() {

int month, day, year;

printf("Enter the month: ");

scanf("%d", &month);

printf("Enter the day: ");

scanf("%d", &day);

printf("Enter the year: ");

scanf("%d", &year);

12
Department of Computer Science and Engineering
Institute of Technical Education & Research, SOA, Deemed to be University

Space for Program and output t

int day_number = day_number(month, day, year);

printf("The day number is %d.\n", day_number);

return 0;

Output

Enter the month: 1

Enter the day: 1

Enter the year: 1994

The day number is 1.

Enter the month: 12

Enter the day: 31

Enter the year: 1993

The day number is 365.

Enter the month: 12

Enter the day: 31

Enter the year: 1996

The day number is 366.

13
Department of Computer Science and Engineering
Institute of Technical Education & Research, SOA, Deemed to be University

16. A triangle can be classified based on the lengths of its sides as equilateral, isosceles or scalene. All
three sides of an equilateral triangle have the same length. An isosceles triangle has two sides that are
the same length, and a third side that is a different length. If all of the sides have different lengths then
the triangle is scalene. Write a program that reads the lengths of the three sides of a triangle from the
user. Then display a message that states the triangles type.
Space for Program and output t

#include <stdio.h>

int main() {

int side1, side2, side3;

printf("Enter the lengths of the three sides of the triangle: ");

scanf("%d %d %d", &side1, &side2, &side3);

if (side1 == side2 && side2 == side3) {

printf("The triangle is equilateral.\n");

else if (side1 == side2 || side1 == side3 || side2 == side3) {

printf("The triangle is isosceles.\n");

else {

printf("The triangle is scalene.\n");

return 0;

Output

Enter the lengths of the three sides of the triangle: 5 5 5

The triangle is equilateral.

Enter the lengths of the three sides of the triangle: 5 5 6

The triangle is isosceles.

Enter the lengths of the three sides of the triangle: 5 6 7

The triangle is scalene.

14

You might also like