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

C Programs for Basic Calculations

Lab practical pps

Uploaded by

jacky421421z
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)
11 views8 pages

C Programs for Basic Calculations

Lab practical pps

Uploaded by

jacky421421z
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

PROGRAM - 1

AIM: WAP that accepts the marks of 5 subjects and finds the sum and percentage
marks obtained by the student.

CODE:

#include<stdio.h>

void main()

int BEE, PPS,PHYSICS,MATHS,EVS,sum ;

float per;

printf("Enter marks of BEE=");

scanf("%d",&BEE);

printf("Enter marks of PPS=");

scanf("%d",&PPS);

printf("Enter marks of PHYSICS=");

scanf("%d",&PHYSICS);

printf("Enter marks of MATHS=");

scanf("%d",&MATHS);

printf("Enter marks of EVS=");

scanf("%d",&EVS);

sum=BEE+PPS+PHYSICS+MATHS+EVS;

printf("Sum of marks=%d",sum);

per=(float)sum/5;

printf("\nPercentage of marks=%f",per);

}
OUTPUT:
PROGRAM - 2

AIM: WAP that calculates the Simple Interest and Compound Interest. The Principal,
Amount, Rate of Interest and Time are entered through the keyboard.

CODE:

#include<stdio.h>

#include<math.h>

void main()

float p, r, t, a, si, ci;

printf("Enter Principle=");

scanf("%f",&p);

printf("Enter Rate=");

scanf("%f",&r);

printf("Enter Time=");

scanf("%f",&t);

si=(p*r*t)/100;

printf("Simple Interest=%f",si);

a = p*(pow((1 + r / 100), t));

ci = a - p;

printf("\nCompound Interest=%f",ci);

OUTPUT:
PROGRAM - 3

AIM: WAP to calculate the area and circumference of a circle.


CODE:

#include <stdio.h>

#define PI 3.14159

int main() {

float radius, area, circumference;

// Accept input for radius

printf("Enter the radius of the circle: ");

scanf("%f", &radius);

// Calculate area and circumference

area = PI * radius * radius;

circumference = 2 * PI * radius;

// Display area and circumference

printf("\nArea of the circle = %.2f\n", area);

printf("Circumference of the circle = %.2f\n", circumference);

return 0;

OUTPUT:
PROGRAM - 4

AIM: WAP that accepts the temperature in Centigrade and converts into Fahrenheit using
the formula C/5=(F-32)/9

CODE:

#include <stdio.h>

int main() {

float celsius, fahrenheit;

// Accept input for temperature in Celsius

printf("Enter the temperature in Celsius: ");

scanf("%f", &celsius);

// Calculate temperature in Fahrenheit

fahrenheit = (celsius * 9.0 / 5.0) + 32.0;

// Display temperature in Fahrenheit

printf("\nTemperature in Fahrenheit = %.2f\n", fahrenheit);

return 0;

OUTPUT:
PROGRAM - 5

AIM: WAP that swaps values of two variables.


CODE:

#include <stdio.h>

int main() {

int num1, num2, temp;

// Accept input for num1 and num2

printf("Enter the value of num1: ");

scanf("%d", &num1);

printf("Enter the value of num2: ");

scanf("%d", &num2);

// Swap values using a third variable

temp = num1;

num1 = num2;

num2 = temp;

// Display swapped values

printf("\nAfter swapping:\n");

printf("num1 = %d\n", num1);

printf("num2 = %d\n", num2);

return 0;

OUTPUT:
PROGRAM - 6

AIM: WAP that checks whether the two numbers entered by the user are
equal or not.
CODE:

#include <stdio.h>

int main() {

int num1, num2;

// Accept input for num1 and num2

printf("Enter the first number: ");

scanf("%d", &num1);

printf("Enter the second number: ");

scanf("%d", &num2);

// Check if num1 and num2 are equal

if (num1 == num2) {

printf("\nThe two numbers are equal.\n");

else {

printf("\nThe two numbers are not equal.\n");

return 0;

OUTPUT:
PROGRAM - 7

AIM: WAP to find the greatest of three numbers.


CODE:

#include <stdio.h>

int main() {

int num1, num2, num3, greatest;

// Accept input for num1, num2, and num3

printf("Enter the first number: ");

scanf("%d", &num1);

printf("Enter the second number: ");

scanf("%d", &num2);

printf("Enter the third number: ");

scanf("%d", &num3);

// Find the greatest number

greatest = num1;

if (num2 > greatest) {

greatest = num2;

if (num3 > greatest) {

greatest = num3;

// Display the greatest number

printf("\nThe greatest number is: %d\n", greatest);

return 0;

OUTPUT:

Common questions

Powered by AI

To determine the greatest number among three numbers, the program must check each number against the current greatest value. Initially, we assume num1 is the greatest. If num2 is greater than the greatest, we update the greatest to num2. Similarly, if num3 is greater than the greatest, it becomes the new greatest. The conditions are implemented through a series of if statements: `if (num2 > greatest) { greatest = num2; } if (num3 > greatest) { greatest = num3; }` .

Understanding the formula C/5=(F-32)/9 is crucial when converting between Celsius and Fahrenheit because it provides the mathematical relationship required to accurately perform the conversion. The formula derives from the relationship between the Celsius and Fahrenheit scales, where the factor 5/9 accounts for the differing scales' increments. The rearranged formula used for practical conversion is fahrenheit = (celsius * 9.0 / 5.0) + 32.0, which simplifies the conversion process .

To calculate the percentage of marks from the sum of marks obtained in five subjects, you divide the sum of all the marks by the total number of subjects and multiply by 100 (implicitly through floating point calculation). The formula used is: percentage = (sum / total number of subjects). In the given code, percentage = sum / 5, assuming each subject's maximum score is the same .

Defining constants like PI is crucial when computing the area and circumference of a circle because it ensures precision and correctness in these calculations. The value of PI (π) is a mathematical constant representing the ratio of a circle's circumference to its diameter. By defining PI (e.g., as #define PI 3.14159), the circle computations become reliable and maintainable across different parts of a program, allowing for changes in the value of PI in a single location if higher precision is desired .

Preprocessor directives like #define are used to create symbolic constants or macros that the compiler replaces with specified values before compilation. For example, `#define PI 3.14159` substitutes PI with 3.14159 throughout the program. The advantages include improved code readability, easier maintenance, and the ability to update values in one location rather than finding and replacing each instance manually, leading to a lower chance of errors across a codebase .

To determine equality between two numbers, the program uses a simple logical comparison: `if (num1 == num2)`. This method compares the memory values of num1 and num2, and if they are equal, the program concludes that the two numbers are equal. This method is effective because it directly evaluates the stored values without ambiguity or additional processing, ensuring it operates efficiently and rapidly for simple equality checks .

The compound interest calculation accounts for the effect of interest compounding over time by using the formula A = P * (1 + r/100)^t. This formula incorporates the principal amount (P), rate of interest (r), and time (t), where interest is added to the principal at the end of each period, and future interest calculations include this accumulated interest. The effect of compounding increases as either the interest rate or the number of periods t increases. Compound interest (ci) is calculated by subtracting the original principal from the accumulated amount: ci = A - P .

Type casting in the calculation of percentage from total marks when using integers is essential to maintain precision in floating-point operations. Without type casting, integer division would result in an integer truncated result, potentially leading to inaccurate percentage calculations. Casting the sum to a float before division ensures that the division operation takes place in floating-point arithmetic, resulting in a precise and correct percentage calculation .

Separating input, processing, and output operations in a program structure is beneficial for clarity, maintainability, and debugging. This separation follows the logic structure, where input gathers necessary data, processing manipulates and computes based on this data, and output delivers the results. Such organization allows each section to be independently modified or debugged without affecting others, improving modularity and simplifying complexity in larger codebases .

Using a temporary variable in swapping ensures that the original values of the variables are not overwritten. By storing one value in a temporary variable, it provides a placeholder to reassign the swapped values accurately. This method prevents data loss as it maintains a copy of one variable while the swap is performed. For example: temp = num1; num1 = num2; num2 = temp .

You might also like