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

Exp 6 PPS Lab

Uploaded by

hod cse
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)
20 views

Exp 6 PPS Lab

Uploaded by

hod cse
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

Ex.

No:06
Programs using decision-making constructs: if-else, goto, switch-
case, break-continue.
1. Write a C program to determine eligibility for admission to a
professional course based on the following criteria: Marks in Maths
>=65 and Marks in Phy>=55 and Marks in Chem>=50 and Total in
all three subject >=190 or Total in Maths and Physics >=140.
Aim
Write a C program to determine eligibility for admission to a professional
course based on the subject mark using If-Else condition.
Algorithm
1. Start
2. Display Eligibility Criteria through Print Statement.
3. Get the Input Marks through Scan statement.
4. Calculate total mark of subjects using if-else condition.
5. Display Total Marks.
6. Check Eligibility of mark using if else condition.
7. End

Program
#include <stdio.h> // Include the standard input/output header file.
void main()
{
int p, c, m, t, mp; // Declare variables to store marks and totals.

printf("Eligibility Criteria :\n"); // Display eligibility criteria.


printf("Marks in Maths >=65\n");
printf("and Marks in Phy >=55\n");
printf("and Marks in Chem>=50\n");
printf("and Total in all three subject >=190\n");
printf("or Total in Maths and Physics >=140\n");
printf("-------------------------------------\n");
printf("Input the marks obtained in Physics :"); // Prompt user for input.
scanf("%d", &p); // Read and store marks in 'p'.
printf("Input the marks obtained in Chemistry :");
scanf("%d", &c); // Read and store marks in 'c'.
printf("Input the marks obtained in Mathematics :");
scanf("%d", &m); // Read and store marks in 'm'.
printf("Total marks of Maths, Physics and Chemistry : %d\n", m + p + c); //
Calculate and display total marks.
printf("Total marks of Maths and Physics : %d\n", m + p); // Calculate and
display total marks.

if (m >= 65) // Check if marks in Maths are greater than or equal to 65.
if (p >= 55) // Check if marks in Physics are greater than or equal to 55.
if (c >= 50) // Check if marks in Chemistry are greater than or equal to 50.
if ((m + p + c) >= 190 || (m + p) >= 140) // Check if total marks criteria
are met.
printf("The candidate is eligible for admission.\n"); // Print eligibility
message.
else
printf("The candidate is not eligible.\n"); // Print ineligibility message.
else
printf("The candidate is not eligible.\n"); // Print ineligibility message.
else
printf("The candidate is not eligible.\n"); // Print ineligibility message.
else
printf("The candidate is not eligible.\n"); // Print ineligibility message.
}

Result
Thus eligibility of admission through mark I calculated using If-Else
Condition successfully.

2. Write a program in C to calculate and print the electricity bill of


a given customer. The customer ID, name, and unit consumed by
the user should be captured from the keyboard to display the total
amount to be paid to the customer.
The charge is as follow:
Unit Charge/unit

upto 199 @1.20

200 and above but less than 400 @1.50

400 and above but less than 600 @1.80

600 and above @2.00


If bill exceeds Rs. 400 then a surcharge of 15% will be charged and
the minimum bill should be of Rs. 100/-
Aim
Write a program in C to calculate and print the electricity bill of a
given customer using multiple If-Else condition.
Algorithm
1. Start
2. Declare necessary Variables
3. Get input such as Custid, Connm, Unit consumed and store it
accordingly.
4. Determine Charge per Unit using Multiple If-Else statement.
5. Calculate Gross Amount, Surcharge, Net Amount and display the bill.
6. End

Program
#include <stdio.h> // Include the standard input/output header file.
#include <string.h> // Include the string header file.
void main()
{
int custid, conu; // Declare variables to store customer ID and consumed
units.
float chg, surchg = 0, gramt, netamt; // Declare variables for charge,
surcharge, gross amount, and net amount.
char connm[25]; // Declare a character array to store customer name.
printf("Input Customer ID :"); // Prompt user for input of customer ID.
scanf("%d", &custid); // Read and store the customer ID.
printf("Input the name of the customer :"); // Prompt user for input of
customer name.
scanf("%s", connm); // Read and store the customer name.
printf("Input the unit consumed by the customer : "); // Prompt user for
input of consumed units.
scanf("%d", &conu); // Read and store the consumed units.
if (conu < 200)
chg = 1.20; // Set charge based on consumed units.
else if (conu >= 200 && conu < 400)
chg = 1.50; // Set charge based on consumed units.
else if (conu >= 400 && conu < 600)
chg = 1.80; // Set charge based on consumed units.
else
chg = 2.00; // Set charge based on consumed units.
gramt = conu * chg; // Calculate gross amount.
if (gramt > 300)
surchg = gramt * 15 / 100.0; // Calculate surcharge if gross amount is
greater than 300.
netamt = gramt + surchg; // Calculate net amount.
if (netamt < 100)
netamt = 100; // Set minimum net amount to 100.
printf("\nElectricity Bill\n");
printf("Customer IDNO :%d\n", custid);
printf("Customer Name :%s\n", connm);
printf("unit Consumed :%d\n", conu);
printf("Amount Charges @Rs. %4.2f per unit :%8.2f\n", chg, gramt);
printf("Surchage Amount :%8.2f\n", surchg);
printf("Net Amount Paid By the Customer :%8.2f\n", netamt);
}

Result
Thus the C program for calculating and displaying the electricity bill of a
given customer with sur charge is completed successfully.

3. Write a C program to read any day number in integer and


display the day name in word format.
Aim
Write a C program to read any day number in integer and display the day
name using switch case statement.
Algorithm
1. Start the Program
2. Declare Variables day no
3. Get user input and store it in variable.
4. Evaluate the value of day no using a switch statement and display
results
5. End the Program
Program
#include <stdio.h> // Include the standard input/output header file.
void main()
{
int dayno; // Declare a variable to store the day number.
printf("Input Day No : "); // Prompt the user to input the day number.
scanf("%d",&dayno); // Read and store the day number.
switch(dayno) // Start a switch statement based on the day number.
{
case 1:
printf("Monday \n"); // Print the corresponding day for day number 1.
break;
case 2:
printf("Tuesday \n"); // Print the corresponding day for day number 2.
break;
case 3:
printf("Wednesday \n"); // Print the corresponding day for day number 3.
break;
case 4:
printf("Thursday \n"); // Print the corresponding day for day number 4.
break;
case 5:
printf("Friday \n"); // Print the corresponding day for day number 5.
break;
case 6:
printf("Saturday \n"); // Print the corresponding day for day number 6.
break;
case 7:
printf("Sunday \n"); // Print the corresponding day for day number 7.
break;
default:
printf("Invalid day number. \nPlease try again ....\n"); // Print a message for
invalid day number.
break;
}
}

Result
Thus the program to read any day number in integer and display the day
name was completed.

4. Write a C program which computes the area of various


geometrical shapes using a menu-driven approach.

Aim
To write a C program which computes the area of various geometrical
shapes using switch statement.

Algorithm
1. Start the Program
2. Display options for the user to select the shape
3. Get the user choice and store it
4. Display the areas of various shapes using Switch statement.
5. Stop

Program
#include <stdio.h>
void main ()
{
int choice,r,l,w,b,h,s;
float area;
printf("Input 1 for area of circle\n");
printf("Input 2 for area of rectangle\n");
printf("Input 3 for area of triangle\n");
printf("Input 4 for area of square\n");
printf("Input your choice : ");
scanf("%d",&choice);
switch(choice)
{
case 1:
printf("Input radious of the circle : ");
scanf("%d",&r);
area=3.14*r*r;
break;
printf("The area is : %f\n",area);
case 2:
printf("Input length and width of the rectangle : ");
scanf("%d%d",&l,&w);
area=l*w;
break;
printf("The area is : %f\n",area);
case 3:
printf("Input the base and hight of the triangle :");
scanf("%d%d",&b,&h);
area=.5*b*h;
break;
printf("The area is : %f\n",area);
case 4:
printf("Input the side of the square :");
scanf("%d",&s);
area=s*s;
break;
printf("The area is : %f\n",area);
default:
printf("please choose from the given options.");
break;
}
Result
Thus C program to computes the area of various geometrical shapes using
a menu-driven approach is completed successfully.

5. Write a program in C which is a Menu-Driven Program to perform


a simple calculation.
Aim
Write a C Program to perform a simple calculation with Menu-Driven
approach using switch statement.
Algorithm
1. Start
2. Declare Variables for simple calculation
3. Get the input from user.
4. Display the menu option to perform calculation
5. Process User Choice using Switch Statement and display result.
6. Stop

Program
#include <stdio.h> // Include the standard input/output header file.
void main() {
int num1,num2,opt; // Declare variables to store user input and operation
choice.
// Prompt user for two integers and store them.
printf("Enter the first Integer :");
scanf("%d",&num1);
printf("Enter the second Integer :");
scanf("%d",&num2);
// Display the menu for operation choice.
printf("\nInput your option :\n");
printf("1-Addition.\n2-Substraction.\n3-Multiplication.\n4-Division.\n5-Exit.\
n");
scanf("%d",&opt); // Read and store the user's choice.
switch(opt) { // Start a switch statement based on the user's choice.
case 1:
printf("The Addition of %d and %d is: %d\n",num1,num2,num1+num2); //
Perform addition and print result.
break;
case 2:
printf("The Substraction of %d and %d is: %d\n",num1,num2,num1-num2);
// Perform subtraction and print result.
break;
case 3:
printf("The Multiplication of %d and %d is: %d\
n",num1,num2,num1*num2); // Perform multiplication and print result.
break;
case 4:
if(num2==0) {
printf("The second integer is zero. Divide by zero.\n"); // Handle division by
zero.
} else {
printf("The Division of %d and %d is : %d\n",num1,num2,num1/num2); //
Perform division and print result.
}
break;
case 5:
break; // Exit the program.
default:
printf("Input correct option\n"); // Display error message for invalid input.
break;
}
}

Result
Thus the C Program to perform a simple calculation using Menu-
Driven approach done using switch statement.

6. Write a C program to find whether a given number is odd or even


using goto statement.

Aim
Write a C program to find whether a given number is odd or even using
goto statement.

Algorithm
1. Start
2. Declare Variables for simple calculation
3. Get the input from user.
4. Process the input using goto Statement and display the output.
5. Stop.
Program
#include<stdio.h>
#include<stdlib.h>
void main()
{
int num;
printf("Enter a number\n");
scanf("%d", &num);
if (num % 2 == 0)
goto even;
else
goto odd;
even:
printf("%d is even\n", num);
exit(0);
odd:
printf("%d is odd\n", num);
}

Result
Thus the program to find whether a given number is odd or even using goto
statement is completed.

7. Write a C program to print the values using break and continue


statement.
Aim
Write a C program to print the values using break and continue
statement.

Algorithm
1. Start the Program
2. Display options for the user to select the shape
3. Continue Loop work based on condition.
4. Display the message.
5. Break loop work based on condition
6. Stop.

Program
#include <stdio.h>
int main() {
printf("Using continue statement:\n");

// Loop to demonstrate continue


for (int i = 1; i <= 10; i++) {
if (i == 5) {
continue; // Skip the iteration when i is 5
}
printf("%d\n", i); // Print the current value of i
}

printf("\nUsing break statement:\n");

// Loop to demonstrate break


for (int i = 1; i <= 10; i++) {
if (i == 8) {
break; // Terminate the loop when i is equal to 8
}
printf("%d\n", i); // Print the current value of i
}

return 0;
}

Result
Thus C program to print the values using break and continue statement
completed.

You might also like