0% found this document useful (0 votes)
55 views

1CSM1_PPSC_T4_Week4_2024_25_Solutions

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

1CSM1_PPSC_T4_Week4_2024_25_Solutions

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

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING (AI & ML)

KAKATIYA INSTITUTE OF TECHNOLOGY & SCIENCE, WARANGAL


(An Autonomous Institute under Kakatiya University, Warangal)

Tutorial 4 (T4)
(UNIT – II)

U24AI104 –
Course Code – Name Programming for Branch – Section CSE (AI & ML)
Problem Solving with C
Tutorial Sheet posted on Tutorial class scheduled 01.10.2024 (B1) and
27.09.2024
CourseWeb on on 03.10.2024 (B2)
Decision Making and Branching: Simple if, if-else, Nested if, Else if ladder,
Topics covered
switch case

Problem
Class Answer the following questions. CO CDLL
No
T4 1. Develop a C program to find the final electricity bill for a customer as CO2 [Ap]
per below norms.

Generate Electricity Bill as per the below tabulation


Up to 100 units
0 to 50 units Rs. 1.45
50+ to 100 units Rs. 2.50
100 to 200 units
0 to 100 units Rs. 3.30
100+ to 200 units Rs. 4.30
More than 200 units
0 to 200 units Rs. 5.00
200+ to 300 units Rs. 7.20
300+ to 400 units Rs. 8.50
400+ to 800 units Rs. 9.00
800+ units Rs. 9.50
Solution #include <stdio.h>

int main() {
int units;
float bill = 0;

printf("Enter the total units consumed: ");


scanf("%d", &units);

if (units <= 100) {


// Up to 100 units
if (units <= 50) {
bill = units * 1.45;
} else {
bill = (50 * 1.45) + ((units - 50) * 2.50);
}
} else if (units <= 200) {
// 100 to 200 units
bill = (100 * 3.30) + ((units - 100) * 4.30);
} else if (units <= 300) {
// 200 to 300 units
bill = (200 * 5.00) + ((units - 200) * 7.20);
} else if (units <= 400) {
// 300 to 400 units
bill = (200 * 5.00) + (100 * 7.20) + ((units - 300) * 8.50);
} else if (units <= 800) {
// 400 to 800 units
bill = (200 * 5.00) + (100 * 7.20) + (100 * 8.50) + ((units - 400) * 9.00);
} else {
// More than 800 units
bill = (200 * 5.00) + (100 * 7.20) + (100 * 8.50) + (400 * 9.00) + ((units
- 800) * 9.50);
}

printf("The final electricity bill is: Rs. %.2f\n", bill);

return 0;
}
2. Develop a C program to display the day of the week when entered CO2 [Ap]
number between 0 to 6 as follows using switch statement compulsory.
When entered 0 has to display “SUNDAY” and exit.
When entered 1 has to display “MONDAY”, When entered 2 has to
display “TUESDAY”, When entered 3 has to display
“WEDNESDAY”, When entered 4 has to display “THURSDAY”,
When entered 5 has to display “FRIDAY”, and When entered 6 has to
display “SATURDAY”.
Solution: #include <stdio.h>

int main() {
int day;

printf("Enter a number between 0 and 6: ");


scanf("%d", &day);

switch (day) {
case 0:
printf("SUNDAY\n");
break;
case 1:
printf("MONDAY\n");
break;
case 2:
printf("TUESDAY\n");
break;
case 3:
printf("WEDNESDAY\n");
break;
case 4:
printf("THURSDAY\n");
break;
case 5:
printf("FRIDAY\n");
break;
case 6:
printf("SATURDAY\n");
break;
default:
printf("Invalid input! Please enter a number between 0 and
6.\n");
}
return 0;
}
3. Develop a C program to calculate commission for the input value of CO2 [Ap]
sales amount.
Commission is calculated as per the following rules:
Commission is NIL for sales amount Rs. 5000.
Commission is 2% for sales when sales amount is > Rs. 5000and <=
Rs. 10000.
Commission is 5% for sales amount > Rs. 10000.
Solution: #include <stdio.h>

int main() {
float sales, commission;

printf("Enter the sales amount: ");


scanf("%f", &sales);

if (sales <= 5000) {


commission = 0;
} else if (sales > 5000 && sales <= 10000) {
commission = sales * 0.02;
} else {
commission = sales * 0.05;
}

printf("The commission for sales amount Rs. %.2f is Rs. %.2f\n",


sales, commission);

return 0;
}
4. Develop a C program to find whether a given number is even or odd. CO2 [Ap]
(Hint: Use % operator or bitwise & operator)
Solution: #include <stdio.h>

int main() {
int number;

printf("Enter an integer: ");


scanf("%d", &number);

// Method 1: Using modulus operator


if (number % 2 == 0) {
printf("Using modulus operator: %d is even.\n", number);
} else {
printf("Using modulus operator: %d is odd.\n", number);
}

// Method 2: Using bitwise AND operator


if ((number & 1) == 0) {
printf("Using bitwise operator: %d is even.\n", number);
} else {
printf("Using bitwise operator: %d is odd.\n", number);
}

return 0;
}
5. Develop a C program to find largest of three numbers using ternary CO2 [Ap]
operatory.
Solution: #include <stdio.h>

int main() {
int num1, num2, num3, largest;

printf("Enter three numbers: ");


scanf("%d %d %d", &num1, &num2, &num3);

// Using nested ternary operator to find the largest


largest = (num1 > num2) ?
((num1 > num3) ? num1 : num3) :
((num2 > num3) ? num2 : num3);

printf("The largest number is: %d\n", largest);

return 0;
}
6. Develop a C program to find the nature of the roots of quadratic CO2 [Ap]
equation using switch statement.
Solution: #include <stdio.h>
#include <math.h>

int main() {
float a, b, c, discriminant;

printf("Enter coefficients a, b, and c of the quadratic equation (ax^2


+ bx + c = 0): ");
scanf("%f %f %f", &a, &b, &c);

// Calculate the discriminant


discriminant = (b * b) - (4 * a * c);

// Determine the nature of roots using switch statement


switch ((discriminant > 0) ? 1 : (discriminant == 0) ? 0 : -1) {
case 1:
printf("The roots are real and distinct.\n");
break;
case 0:
printf("The roots are real and equal.\n");
break;
case -1:
printf("The roots are complex and imaginary.\n");
break;
default:
printf("Error in calculating discriminant.\n");
}

return 0;
}
7. Develop a C program that will take the amount as input from the user CO2 [Ap]
and print minimum number of notes (Rs. 500, 100, 50, 20, 10, 5, 2, 1)
required for the amount.
Example
Input
Input amount: 575
Output
Total number of notes:
500: 1
100: 0
50: 1
20: 1
10: 0
5: 1
2: 0
1: 0
Solution: #include <stdio.h>

int main() {
int amount, count500, count100, count50, count20, count10, count5,
count2, count1;

printf("Enter the amount: ");


scanf("%d", &amount);

// Calculate the number of each denomination


count500 = amount / 500;
amount %= 500;

count100 = amount / 100;


amount %= 100;

count50 = amount / 50;


amount %= 50;

count20 = amount / 20;


amount %= 20;

count10 = amount / 10;


amount %= 10;

count5 = amount / 5;
amount %= 5;

count2 = amount / 2;
amount %= 2;

count1 = amount / 1;

// Print the result


printf("Total number of notes:\n");
printf("500: %d\n", count500);
printf("100: %d\n", count100);
printf("50: %d\n", count50);
printf("20: %d\n", count20);
printf("10: %d\n", count10);
printf("5: %d\n", count5);
printf("2: %d\n", count2);
printf("1: %d\n", count1);

return 0;
}
8. Apply the concept of if else construct to find the output of the CO2 [Ap]
following program?

#include<stdio.h>
void main()
{
if( 10 > 9 )
printf("Singapore\n");
else if(4%2 == 0)
printf("England\n");
printf("Poland");

}
Solution: Singapore
Poland
9. Develop a C program to swap three numbers using temporary CO1 [Ap]
variable and without temporary variable.
Solution: Program 1: Swap Three Numbers Using a Temporary Variable

#include <stdio.h>

int main() {
int a, b, c, temp;

printf("Enter three numbers (a, b, c): ");


scanf("%d %d %d", &a, &b, &c);

// Swapping using a temporary variable


temp = a; // Store the value of a in temp
a = b; // Assign the value of b to a
b = c; // Assign the value of c to b
c = temp; // Assign the value of temp (original a) to c

printf("After swapping:\n");
printf("a: %d\n", a);
printf("b: %d\n", b);
printf("c: %d\n", c);

return 0;
}

Program 2: Swap Three Numbers Without Using a Temporary


Variable

#include <stdio.h>

int main() {
int a, b, c;

printf("Enter three numbers (a, b, c): ");


scanf("%d %d %d", &a, &b, &c);

// Swapping without a temporary variable


a = a + b + c; // Sum of all three numbers
b = a - (b + c); // b now gets the original value of a
c = a - (b + c); // c now gets the original value of b
a = a - (b + c); // a now gets the original value of c

printf("After swapping:\n");
printf("a: %d\n", a);
printf("b: %d\n", b);
printf("c: %d\n", c);
return 0;
}

Course Faculty:
Dr. Narasimha Reddy Soora
HoD and Professor,
Dept of CSE (AI & ML)

You might also like