Practical Viewer

Practical Programs

IT Practical Solution

1 Programs Found
1 Program 1
Aim (Write on the ruler page)
Write a program in C++ with a ratio class using member functions like assign() function to initialize its member data integer numerator and denominator, convert() function to convert the ratio into double, invert() function to get the inverse of the ratio and print() function to print the ratio and its reciprocal.

Program (Start from new page after AIM)
#include<iostream.h>
#include<conio.h>

class ratio {
public:
    void assign(int, int);
    double convert();
    void invert();
    void print();
private:
    int num, den;
};

void main() {
    ratio x;
    x.assign(22, 7);
    cout << "x=";
    x.print();
    cout << "=" << x.convert() << "\n";
    x.invert();
    cout << "1/x=";
    x.print();
    cout << "\n";
    getch();
}

void ratio::assign(int numerator, int denominator) {
    num = numerator;
    den = denominator;
}

double ratio::convert() {
    return double((num) / den);
}

void ratio::invert() {
    int temp = num;
    num = den;
    den = temp;
}

void ratio::print() {
    cout << num << "/" << den;
    getch();
}

Output (Stick on blank page opposite to code)
Download Output