0% found this document useful (0 votes)
6 views10 pages

Cha C Report 3&4

The document outlines two lab reports focused on programming in C. The first report describes a program for performing basic mathematical operations on two float variables and displaying user personal information using unformatted I/O. The second report details a program for calculating total marks, percentage, and rank for five subjects based on user input, along with a structured approach to handle input and output effectively.

Uploaded by

46prashant46
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)
6 views10 pages

Cha C Report 3&4

The document outlines two lab reports focused on programming in C. The first report describes a program for performing basic mathematical operations on two float variables and displaying user personal information using unformatted I/O. The second report details a program for calculating total marks, percentage, and rank for five subjects based on user input, along with a structured approach to handle input and output effectively.

Uploaded by

46prashant46
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/ 10

‭Lab Report -3:‬

‭ itle:‬‭Write a program to perform basic mathematical‬‭operations on two float‬


T
‭variables and display the user's personal information using unformatted I/O.‬

‭Objectives:‬‭To perform mathematical operations (addition,‬‭subtraction, multiplication,‬

‭ ivision) on two float numbers using preprocessor directives for operators and to input and‬
d
‭display personal details (name, address, age, weight, height) using unformatted I/O.‬

‭Problem Analysis:‬

‭To perform mathematical operations on two float numbers, we need three‬


‭variables: ● x and y to store the float values entered by the user.‬

‭● A variable Ch1 to store the mathematical operator received from the user using‬

‭unformatted I/O.‬

‭Additionally, a preprocessor directive is used to define the operators (+, -, *, /) as‬

‭PLUS, MINUS, MULTIPLY, and DIVIDE. These operators are then applied in the‬

c‭ alculation of the result stored in the variable z. The operations are performed‬
‭based on the operator chosen by the user.‬

‭For personal details, we use unformatted I/O to get input for the following‬
‭variables: ● Name, address (as strings)‬

‭● Age, weight, and height.‬

‭Algorithm:‬

‭1. Start‬

‭2. Declare variables x, y (float) to store input numbers.‬

‭3. Declare a character variable Ch1 to receive the mathematical operator.‬


‭4. Declare preprocessor directives for mathematical operators (PLUS, MINUS, MULTIPLY,‬

‭DIVIDE).‬

‭5. Receive input for x and y using scanf().‬

‭6. Receive input for the operator (Ch1) using unformatted I/O.‬

‭ . Perform the required arithmetic operation based on the value of‬


7
‭Ch1. 8. Store the result in z and display it.‬
‭9. Receive input for the name, address, age, weight, and height using‬
‭unformatted I/O functions.‬

‭10.Display the personal information.‬

‭11.End‬
‭Flowchart :‬

‭ ource Code :‬
S
‭#include <stdio.h>‬

‭int main() {‬
‭// Declare float variables for the numbers and for user input‬
‭float num1, num2;‬
‭char name[50];‬
‭int age;‬

‭// Get the user's name and age (unformatted input)‬


‭ rintf("Enter your name: ");‬
p
‭scanf("%s", name); // unformatted input (basic)‬

‭ rintf("Enter your age: ");‬


p
‭scanf("%d", &age); // unformatted input (basic)‬

/‭/ Get the two float numbers from the user‬


‭printf("Enter two numbers (float values): ");‬
‭scanf("%f %f", &num1, &num2);‬

/‭/ Perform and display basic mathematical operations‬


‭printf("\nBasic Mathematical Operations:\n");‬

/‭/ Addition‬
‭printf("Addition: %.2f + %.2f = %.2f\n", num1, num2, num1 + num2);‬

/‭/ Subtraction‬
‭printf("Subtraction: %.2f - %.2f = %.2f\n", num1, num2, num1 - num2);‬

/‭/ Multiplication‬
‭printf("Multiplication: %.2f * %.2f = %.2f\n", num1, num2, num1 * num2);‬

/‭/ Division (with check to avoid division by zero)‬


‭if (num2 != 0) {‬
‭printf("Division: %.2f / %.2f = %.2f\n", num1, num2, num1 / num2);‬
‭} else {‬
‭printf("Error: Division by zero is not allowed.\n");‬
‭}‬

/‭/ Display user personal information‬


‭printf("\nUser Information:\n");‬
‭printf("Name: %s\n", name);‬
‭printf("Age: %d\n", age);‬

‭return 0;‬
‭}‬

‭Output (compilation, debugging & testing)‬


‭Conclusion & Discussion:‬

‭The provided C program successfully performs mathematical operations (addition,‬

‭subtraction, multiplication, and division) using preprocessor directives and unformatted I/O‬
‭for receiving the operator. The program also takes personal information using unformatted‬
‭I/O and displays the data. The unformatted I/O operations work as intended for string input,‬
‭although care should be taken to avoid buffer overflow issues when using gets().‬
‭Lab Report -4:‬

‭Title:‬‭Write a program to calculate and display‬‭the total marks, percentage, and rank for 5‬
‭subjects (Physics, Chemistry, Math, English, and Biology) based on the obtained marks.‬
‭Objectives:‬

‭ o calculate the total marks, percentage, and display the rank of each subject and‬
T
‭the overall rank based on the given marks for 5 subjects.‬

‭Problem Analysis:‬

‭To solve the problem, we need to input the marks for 5 subjects (Physics, Chemistry, Math,‬

‭English, and Biology). The total marks and percentage will be calculated based on the input‬
‭marks, and the rank of each subject will be determined by categorising the marks into ranks‬

s‭ uch as fail, pass, second division, first division, distinction, and extraordinary‬
‭based on the percentage ranges.‬

‭Algorithm:‬

‭1. Start‬

‭2. Declare variables to store the marks of 5 subjects, total marks, percentage,‬
‭and ranks for each subject.‬

‭3. Take input from the user for marks of Physics, Chemistry, Math, English, and Biology.‬

‭4. Calculate the total marks by summing the marks of all subjects.‬

‭5. Calculate the percentage as (total marks / 500) * 100.‬

‭6. Determine the rank for each subject based on the percentage:‬

‭○ Marks < 40%: Fail‬

‭○ Marks between 40% and 55%: Pass/Third Division‬

‭○ Marks between 55% and 65%: Second Division‬

‭○ Marks between 65% and 80%: First Division‬

‭○ Marks between 80% and 95%: Distinction‬

‭○ Marks between 95% and 100%: Extraordinary‬

‭7. Display the total marks, percentage, and the rank for each subject.‬

‭8. Display the overall rank based on the percentage obtained.‬

‭9. End‬
‭Flowchart :‬

‭ ource Code :‬
S
‭#include <stdio.h>‬

‭int main() {‬
‭// Declare variables for marks and total‬
‭float physics, chemistry, math, english, biology;‬
‭float totalMarks, percentage;‬
‭char rank[20];‬

/‭/ Ask the user to input marks for 5 subjects‬


‭printf("Enter marks for Physics: ");‬
‭scanf("%f", &physics);‬
p‭ rintf("Enter marks for Chemistry: ");‬
‭scanf("%f", &chemistry);‬

p‭ rintf("Enter marks for Math: ");‬


‭scanf("%f", &math);‬

p‭ rintf("Enter marks for English: ");‬


‭scanf("%f", &english);‬

p‭ rintf("Enter marks for Biology: ");‬


‭scanf("%f", &biology);‬

/‭/ Calculate total marks and percentage‬


‭totalMarks = physics + chemistry + math + english + biology;‬
‭percentage = (totalMarks / 500) * 100;‬

/‭/ Determine the rank based on percentage‬


‭if (percentage >= 90) {‬
‭sprintf(rank, "A+");‬
‭} else if (percentage >= 80) {‬
‭sprintf(rank, "A");‬
‭} else if (percentage >= 70) {‬
‭sprintf(rank, "B");‬
‭} else if (percentage >= 60) {‬
‭sprintf(rank, "C");‬
‭} else if (percentage >= 50) {‬
‭sprintf(rank, "D");‬
‭} else {‬
‭sprintf(rank, "F");‬
‭}‬

/‭/ Display the total marks, percentage, and rank‬


‭printf("\nTotal Marks: %.2f / 500\n", totalMarks);‬
‭printf("Percentage: %.2f%%\n", percentage);‬
‭printf("Rank: %s\n", rank);‬

‭return 0;‬
‭}‬
‭Output (compilation, debugging & testing)‬

‭Conclusion & Discussion:‬

‭The refined C program effectively addresses the requirements by providing a structured and‬
‭maintainable codebase. The use of functions enhances readability and facilitates easier‬

‭debugging and future modifications. Input validation ensures that the program handles‬
‭unexpected or incorrect user inputs gracefully, preventing potential runtime errors.‬

You might also like