0% found this document useful (0 votes)
75 views7 pages

Practical 2 PDF

This document contains code snippets from multiple C++ programs that demonstrate basic input/output operations, mathematical calculations, and unit conversions. The programs cover topics like division, modulus, area of a circle calculation, addition/subtraction/multiplication of numbers, Celsius to Fahrenheit conversion, height conversion between feet/inches and centimeters, and conversion between inches, feet, yards, meters, and centimeters. Each code snippet is followed by an expected output section, but no actual output is shown.

Uploaded by

Ng Siewmin
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)
75 views7 pages

Practical 2 PDF

This document contains code snippets from multiple C++ programs that demonstrate basic input/output operations, mathematical calculations, and unit conversions. The programs cover topics like division, modulus, area of a circle calculation, addition/subtraction/multiplication of numbers, Celsius to Fahrenheit conversion, height conversion between feet/inches and centimeters, and conversion between inches, feet, yards, meters, and centimeters. Each code snippet is followed by an expected output section, but no actual output is shown.

Uploaded by

Ng Siewmin
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/ 7

Practical 2

PART B
QUESTION 1
INPUT
#include <iostream>
using namespace std;

#define ONE 1

int main (void)


{
double num1, num2, num3;
cout << "Enter two number (separated by space): ";
cin >> num1 >> num2;

num3 = (num1 / num2) + ONE;


cout << "Answer is " << num3<<endl;

return 0;

OUTPUT

Answer depends on input data.


When the input numbers are 3.0 and 0.0, there is a division by zero which causes a non-time
error.
PART B
QUESTION 2
INPUT
#include <iostream>
#define DAYS_PER_WEEK 7
using namespace std;

int main(void)
{
int days, weeks, remaining_days;

cout << "Enter number of days: ";


cin >> days;

weeks = days / DAYS_PER_WEEK;


remaining_days = days% DAYS_PER_WEEK;

cout << "Number of week = " << weeks<<"\n";


cout << "Number of remaining day = " << remaining_days<<"\n";

return 0;

OUTPUT
PART B
QUESTION 3
INPUT
#include <iostream>
#include <cmath>
using namespace std;

#define PI 3.14159

int main(void)
{
double get_radius,area_circle;

cout << "Enter radius of the circle: ";


cin >> get_radius;

area_circle=PI*pow(get_radius,2);

cout << "Area of the circle is " << area_circle<< endl;

return 0;

OUTPUT
PART B
QUESTION 4
INPUT
#include <iostream>
using namespace std;

int main(void)
{
int num1,num2,add_result,sub_result,mul_result,mod_result;
double div_result;

cout << "Enter two numbers: ";


cin >> num1>>num2;

add_result = num1 + num2;


sub_result = num1 - num2;
mul_result = num1*num2;
mod_result = num1%num2;
div_result = (double)num1 / num2;

cout << "addition of two number is: " << add_result<<endl;


cout << "subtraction of two number is: " << sub_result << endl;
cout << "multiple of two number is: " << mul_result << endl;
cout << "modulus of two number is: " << mod_result << endl;
cout << "division of two number is: " << div_result << endl;

return 0;

OUTPUT
PART B
QUESTION 5
INPUT
#include <iostream> //FOR INPUT AND OUTPUT
#include <iomanip> //USE WHEN YOU NEED TO USE SHOWPOINT, FIXED, SETW,SETPRECISION...
using namespace std;

int main(void)
{
double cels,fahr;

cout << "Enter themperature in degree celsius: ";


cin >> cels;

fahr = 9.0 / 5.0*cels + 32;


cout << "celsius fahrenheit\n";
cout << setw(7) << fixed << setprecision(2) << cels << " ";
cout << setw(9) << fahr << endl;

return 0;

OUTPUT
PART D
QUESTION 1
INPUT
#include <iostream>
#include <iomanip>
#define INCHES_PER_FOOT 12
#define CM_PER_FOOT 30.48
using namespace std;

int main(void)
{
double feet,inches,cm;

cout << "Enter your height in feet and inches: ";


cin >> feet>>inches;

cm = (feet + inches / INCHES_PER_FOOT)*CM_PER_FOOT;


cout << "Your height in cm "<<cm<<endl;

return 0;

OUTPUT
PART D
QUESTION 2
INPUT
#include<iostream>
#include<iomanip>
#define INCHES_PER_FOOT 12
#define CM_PER_FOOT 30.48
#define INCHES_PER_METER 39.37
#define CM_PER_INCHES 2.54
#define INCHES_PER_YARD 36
using namespace std;

int main(void)
{
double feet, inches, cm, meter, yard;

cout << "Enter the measurement in inches: ";


cin >> inches;

feet = inches / INCHES_PER_FOOT;


yard = inches / INCHES_PER_YARD;
meter = inches / INCHES_PER_METER;
cm = inches * CM_PER_INCHES;

cout << "The measurement in :\n";


cout << "Feet: " << fixed << setprecision(2) << feet << endl; //WHEN FIXED +
SETPRECISION MEAN SET DP
cout << "Yard: " << yard << endl; //FIXED AND SETPRECISION ONLY HAVE TO
WRITE IN THE FIRST LINE
cout << "Meter: " << meter << endl;
cout << "Cm: " << cm << endl;

return 0;

OUTPUT

You might also like