MrRogueKnight’s Substack

MrRogueKnight’s Substack

CS101

C Programming Lab - 4

MrRogueKnight's avatar
MrRogueKnight
Sep 03, 2024
∙ Paid
Share
Name: PRASHANT RANJAN
Roll No: 24MC3035
Email Id: 24MC3035@rgipt.ac.in
Branch: MATHEMATICS AND COMPUTING
Lab A (CS101-LabA) (My Lab) 

/*
1. Title  : Rupees and Paisa Conversion Utility - Enter Paisa Only
WAP to convert given Paisa into its equivalent Rupees and Paisa as per the following format.

Input  : Enter the Amount:550 Paisa
Output : 550 Paisa = 5 Rupees and 50 Paisa

Steps:
1. Taking input (Amount) form the user in Paisa.
2. Let's say : 550 Paisa  -> Break - 2 Parts : (550 Paisa = 5 Rupees and 50 Paisa)
3. Maths: 550 => 550/100 = 5.5 but int => 5  now we 550%100 = 55 which is Paisa.
*/
#include <stdio.h>
int main()
{
int Rupees, Paisa, Amount;                                           // Declaring the required variables
printf("Enter the Amount: ");                                        // Display the text
scanf("%d",&Amount);                                                 // Input
Rupees = Amount / 100; Paisa = Amount % 100;                         // Calculations
printf("%d Paisa = %d Rupees and %d Paisa",Amount, Rupees, Paisa);   // Output
getch();
return 0;
}
Output: 
Enter the Amount: 468952
468952 Paisa = 4689 Rupees and 52 Paisa

/*
2. Title : Time Conversion Utility - Input Seconds Only
WAP to convert given Seconds into its equivalent Hours, Minutes and Seconds as per the following format.
Input : Enter the time:7560 Seconds
Output:7560 Second = 2 Hours, 06 Minutes and 40 Seconds
Steps:
1. Declaring the variables to take the input.
2. Using simple mathematics Calculations =>  1 Hour = 60 Minutes & 1 Minute = 60 Seconds
   Time = Hours * 3600 + Minutes * 60 + Seconds;
3. Printing the Output as per the format.
*/

#include <stdio.h>

int main() {
    float Time;
    int Minutes, Seconds, Hours;
    printf("Enter the time: ");
    scanf("%f", &Time);

    Hours = Time / 3600;
    Minutes =((Time / 3600) - (Hours))*60;            // or Minutes = ( Time % 3600 ) / 60;
    Seconds =(Time -(3600 * Hours)-(Minutes * 60));   // or Time % 60;

    printf("%.0f Seconds = %d Hours, %d Minutes, and %d Seconds\n", Time, Hours, Minutes, Seconds);
    getch();
    return 0;
}
Output:
Enter the time: 59761
59761 Seconds = 16 Hours, 36 Minutes, and 1 Seconds

/*
3. Topic : Profit/Loss Calculator: Determining Seller's Profit or Loss Based on Discount
The buying price, the marker price and discount are entered through keyboard. Sometimes seller gets profit or sometimes loss depending upon the discount
WAP to determine whether the seller has made profit or incurred loss. Also determine how much profit he made or loss he incurred
Input : Set 1: Enter the buying price: 80 Enter the marker price: 100. Enter the discount: 25% Output : Set 1: Seller made a loss of 6.25%
Input : Set 2: Enter the buying price: 80 Enter the marker price: 100 Enter the discount: 10%  Output : Set 2: Seller made a profit of 12.50%

Steps :
1. Declaring and Storing the variables as listed
2. Calculation of profit or loss => (marker price * ( 1- %discount) - buying price = profit / loss

for Set 1 : marker price =  100, buying price = 80 & discount = 25%
loss = 100 *(1 - 25%)- 80 = 75 - 80 = - 5
loss % = 5 / 80 * 100
*/
#include <stdio.h>
#include<math.h>

#include <stdio.h>
#include <math.h>

int main() {
    double bp, mp, discount, t;                             // bp - buying price, mp - marker price

    printf("Enter the buying price: ");
    scanf("%lf", &bp);

    printf("Enter the marker price: ");
    scanf("%lf", &mp);

    printf("Enter the discount: ");
    scanf("%lf", &discount);

    t = (mp * (1 - discount / 100) - bp);                   // Loss
    t =  (t / bp) *100;                                     // Loss Percentage

    if (t < 0) {
        printf("Seller incurred a loss of: %.2lf%%", -t);  // Print loss
    } else {
        printf("Seller made a profit of: %.2lf%%", t);     // Print profit
    }

    getch();
    return 0;
}
Output:
Enter the buying price: 500
Enter the marker price: 655
Enter the discount: 7
Seller made a profit of: 21.83%

/*
4. Title: Swapping Numbers
Write a C program to perform swapping of two integers without using a third variable.
Input: Enter num1: 10 Enter num2: 20
Output: Before Swapping num1=10, num2=20 After Swapping num1=20, num2=10
*/
/*
Formula that can be used here are
num1 = num1 + num2;
num2 = num1 - num2;
num1 = num1 - num2;

Or

num1 = num1 ^ num2;
num2 = num1 ^ num2;
num1 = num1 ^ num2;

Or

num1 = num1 * num2;
num2 = num1 / num2;
num1 = num1 / num2;
*/
#include<stdio.h>
int main(){
    // Declare variables to store the two numbers
int num1, num2;

// Prompt the user to enter the first number
printf("Enter num1: ");
scanf("%d", &num1);

// Prompt the user to enter the second number
printf("Enter num2: ");
scanf("%d", &num2);

// Display the numbers before swapping
printf("\nBefore Swapping:\n");
printf("num1 = %d\n", num1);
printf("num2 = %d\n", num2);

// Swap the numbers without using a third variable
num1 = num1 + num2; // Add num2 to num1
num2 = num1 - num2; // Subtract num2 from the result (effectively swapping)
num1 = num1 - num2; // Subtract num2 from the result (effectively swapping)

// Display the numbers after swapping
printf("\nAfter Swapping:\n");
printf("num1 = %d\n", num1);
printf("num2 = %d\n", num2);

// Wait for the user to press a key before exiting
getch();
return 0;
}
Output:
Enter num1: 958
Enter num2: 791

Before Swapping:
num1 = 958
num2 = 791

After Swapping:
num1 = 791
num2 = 958

/**
5. Title : Maximum Value
WAP to find the largest between three numbers.
Input: Enter three numbers: 80 105 990
Output: The largest number is 990
**/
/*
Syntax used here
if (condition1) {
    // code to execute if condition1 is true
} else if (condition2) {
    // code to execute if condition2 is true
} else if (condition3) {
    // code to execute if condition3 is true
} else {
    // code to execute if none of the above conditions are true
}
*/
#include<stdio.h>
int main(){
    int num1,num2,num3;
    printf("Enter three numbers: ");
    scanf("%d" "%d" "%d", &num1,&num2,&num3);
    if (num1>num2 && num1>num3){
        printf("The largest number is %d",num1);
    } else if(num2>num3){
        printf("The largest number is %d",num2);
    }
    else{
            printf("The largest number is %d",num3);
    }
getch();
return 0;
}
Output:
Enter three numbers: 500 65 795
The largest number is 795

/**
6. Title: Finding Quotient and Remainder
WAP to perform to take two integers as input from the user and perform division of first integer by second
integer using subtraction and return quotient and remainder.
Note:first integer >= second integer. Don’t use *, /, %.
Input : Enter two numbers: 27 5
Output: Quotient: 5 Remainder: 2
**/
/*
Here's a step-by-step breakdown:

1. #include<stdio.h>: This line includes the standard input-output header file, which provides functions for input and output operations.

2. void divide(int dividend, int divisor): This line declares a function named divide that takes two integer arguments: dividend and divisor. The void keyword indicates that the function does not return any value.

3. int quotient = 0;: This line declares an integer variable quotient and initializes it to 0. This variable will store the quotient of the division.

4. while (dividend >= divisor): This line starts a while loop that continues as long as dividend is greater than or equal to divisor.

5. dividend -= divisor;: Inside the loop, this line subtracts divisor from dividend in each iteration.

6. quotient++;: This line increments the quotient variable by 1 in each iteration.

7. printf("Quotient: %d\n", quotient);: After the loop ends, this line prints the final value of quotient as the quotient of the division.

8. printf("Remainder: %d\n", dividend);: This line prints the final value of dividend as the remainder of the division.

The logic behind the function is:

- Subtract the divisor from the dividend as many times as possible (using the while loop).
- Count the number of subtractions (which gives the quotient).
- The remaining value of the dividend is the remainder.

For example, if you call the function with divide(17, 5), the output will be:


Quotient: 3
Remainder: 2


Because 17 divided by 5 is 3 with a remainder of 2.
*/
#include<stdio.h>
void divide(int dividend, int divisor) {
    int quotient = 0;
    while (dividend >= divisor) {
        dividend -= divisor;
        quotient++;
    }
    printf("Quotient: %d\n", quotient);
    printf("Remainder: %d\n", dividend);
}

int main(){
    int num1, num2;
    printf("Enter two numbers: ");

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

    if(num1>=num2){
            divide(num1, num2);
    }
    else{
         printf("Note:first integer >= second integer.");
    }

getch();
return 0;
}
Output:
Enter two numbers: 598 7
Quotient: 85
Remainder: 3

/**
7. Title: Types of Triangle
WAP to check whether the triangle is equilateral, isosceles or scalene. (Triangle consists of three sides of provided lengths n1, n2 and n3 units).

Input:   (Set1: n1=3, n2=3, n3=4) 	(Set2: n1=4, n2=4, n3=4) 	(Set3: n1=4, n2 =5, n3=7)
Output:  (Set1: Isosceles) 		(Set2: Equilateral)		(Set3: Scalene)
**/
#include<stdio.h>
int main() {
    // Declare variables to store the lengths of the three sides of the triangle
    int n1, n2, n3;

    // Prompt the user to enter the lengths of the three sides
    printf("Enter the lengths of the three sides of the triangle: ");
    scanf("%d %d %d", &n1, &n2, &n3);

    // Check if all three sides are equal (equilateral triangle)
    if (n1 == n2 && n2 == n3) {
        printf("The triangle is equilateral.");
    }
    // Check if any two sides are equal (isosceles triangle)
    else if (n1 == n2 || n2 == n3 || n1 == n3) {
        printf("The triangle is isosceles.");
    }
    // If none of the above conditions are true, it's a scalene triangle
    else {
        printf("The triangle is scalene.");
    }
    getch();
    return 0;
}
Output:
Enter the lengths of the three sides of the triangle: 56 75 19
The triangle is scalene.

Keep reading with a 7-day free trial

Subscribe to MrRogueKnight’s Substack to keep reading this post and get 7 days of free access to the full post archives.

Already a paid subscriber? Sign in
© 2025 MrRogueKnight
Privacy ∙ Terms ∙ Collection notice
Start writingGet the app
Substack is the home for great culture