0% found this document useful (0 votes)
47 views

Sjem2231 Structured Programming Name: Wan Mohamad Farhan Bin Ab Rahman Tutorial 1/ Lab 1 (22 SEPTEMBER 2014)

This document contains the code solutions to 5 questions posed in a structured programming lab assignment. The questions involve writing programs to: 1) print a welcome message, 2) print a sentence in single and multiple lines, 3) calculate mathematical operations on two integers, 4) calculate operations by taking user input, and 5) convert a float to an integer using different rounding methods. The code solutions demonstrate the use of input/output streams, variables, data types, mathematical and rounding functions.

Uploaded by

Anugrah Pangeran
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)
47 views

Sjem2231 Structured Programming Name: Wan Mohamad Farhan Bin Ab Rahman Tutorial 1/ Lab 1 (22 SEPTEMBER 2014)

This document contains the code solutions to 5 questions posed in a structured programming lab assignment. The questions involve writing programs to: 1) print a welcome message, 2) print a sentence in single and multiple lines, 3) calculate mathematical operations on two integers, 4) calculate operations by taking user input, and 5) convert a float to an integer using different rounding methods. The code solutions demonstrate the use of input/output streams, variables, data types, mathematical and rounding functions.

Uploaded by

Anugrah Pangeran
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

SJEM2231 STRUCTURED PROGRAMMING

NAME : WAN MOHAMAD FARHAN BIN AB RAHMAN


TUTORIAL 1/ LAB 1 (22nd SEPTEMBER 2014)

QUESTION 1
Write a program to print the word Welcome.
#include <iostream>
using namespace std;
int main()
{
cout << "Welcome \n";
return 0;
}

//Output :
Welcome

QUESTION 2
Write a program to print a sentence in a single line and in three lines as like
below.
Hello, welcome to C++ Programming
and
Hello,
welcome to
C++ Programming

#include <iostream>
using namespace std;
int main()
{
cout <<

"Hello, welcome to C++ Programming \n" ;

return 0;
}

//Output :
Hello, welcome to C++ Programming

#include <iostream>
using namespace std;
int main()
{
cout << "Hello,\n welcome to\n C++ Programming\n";
return 0;
}

//Output :
Hello,
welcome to
C++ programming

QUESTION 3
Write a program that prints the sum, difference and product of two integers.
Initialize the integers with the values 14 and 8.
#include <iostream>
using namespace std;
int main()
{
int a,b ;
int sum, difference, product;
a=14;
b=8;
sum= a+b;
difference= a-b;
product= a*b;
cout << sum <<"\n";
cout << difference <<"\n";
cout << product <<"\n;
return 0;
}

// Output:
22
6
112

QUESTION 4
Write a program that prints the sum, difference, product, quotient, and
remainder of two numbers that are input interactively.
#include <iostream>
using namespace std;
int main()
{
int a,b;
int sum, diff, product, quotient, remainder;

cout << "Please enter a value of a:


cin

" ;

>> a;

cout << "Please enter a value of b:


cin

" ;

>> b;

sum=a+b;
diff=a-b;
product=a*b;
quotient=a / b;
remainder = a%b;

cout << The sum of a and b is :

<< sum <<"\n";

cout << The difference of a and b is :

<< diff <<"\n";

cout << The product of a and b is :

<< product <<"\n";

cout << The quotient of a and b is :

<< quotient<<"\n";

cout << The remainder of a and b is : << remainder<<"\n";

return 0;
}

//Output :
Please enter a value of a: 34
Please enter a value of b: 27
The sum of a and b is : 61
The difference of a and b is :

The product of a and b is : 918


The quotient of a and b is : 1
The remainder of a and b is : 7

QUESTION 5
Write a program to read the floating point number and convert it to integer

//Using truncate method


#include <iostream>
using namespace std;
int main()
{
float x;
int y;
cout<<"Please enter a decimal number : ";
cin >> x;
y=x;
cout<<"The integer number you will get is : "<< y << endl;
return 0;
}

//Output_truncate :
Please enter a decimal number : 45.77
The integer number you will get is : 45

//Using round off method directly :


#include <iostream>
#include <cmath>
using namespace std;

double round(double value)


{
return(floor(value+0.5));
}
int main()
{
double x,value;
int y;
write:
cout <<"Please enter a decimal number : ";
cin >>x;
y = round(x);
cout<<"The integer number that you will get after round off is :
"<< y<<endl;
cout<<"Do you want to continue with other decimal number? \n";
cout<<"Press 1 for Yes
cin>> value;
if(value==1)

or\nPress 2 for No\n\n";

{
goto write;
}
else if(value==2)
{
return 0;
}
else
{
cout<<"Error! Please understand the COMMAND!!" <<endl;
goto write;
}
}

//Output_roundoff to integer directly

Please enter a decimal number : 56.44


The integer number that you will get after round off is : 56
Do you want to continue with other decimal number?
Press 1 for Yes

or

Press 2 for No

1
Please enter a decimal number : 1000.87
The integer number that you will get after round off is : 1001
Do you want to continue with other decimal number?

Press 1 for Yes

or

Press 2 for No

8
Error! Please understand the COMMAND!!
Please enter a decimal number : 194.499
The integer number that you will get after round off is : 194
Do you want to continue with other decimal number?
Press 1 for Yes

or

Press 2 for No

Press any key to continue

//Round off method by using setprecision() to set the decimal places :

#include <iostream>
#include <cmath>

//nothing happened if we eliminate this cmath

#include <iomanip>

// Input output manipulator, use setprecision()

using namespace std;

int main()
{
long double x,value;
int y;

write:
cout << "Please enter a decimal number : ";
cin >>x;
y = long double (x);
cout<< "The integer number you will get is : ";
cout<< fixed << setprecision (0) << x <<endl;
/* fixed is used
setprecision(0),
setprecision(1),
place ,2 decimal

before setprecision() to ensure that when we set the


it will round off to integer, if we make
setprecision(2), etc ; it will round off to 1 decimal
places and so on respectively */

cout<< "Do you want to continue with other decimal number?\n";


cout<<"Press 1 for Yes

or\nPress 2 for No\n\n";

cin>> value;
if(value==1)
{
goto write;

// goto (statement);

}
else if(value==2)
{
return 0;
}
else
{
cout<< "Error! Please understand the COMMAND!!" <<endl;
goto write;
}
}

//Output_round off method using setprecision()

Please enter a decimal number : 454535.435


The integer number you will get is : 454535
Do you want to continue with other decimal number?
Press 1 for Yes

or

Press 2 for No

1
Please enter a decimal number : 44.78
The integer number you will get is : 45
Do you want to continue with other decimal number?
Press 1 for Yes

or

Press 2 for No

1
Please enter a decimal number : 0.999993
The integer number you will get is : 1
Do you want to continue with other decimal number?
Press 1 for Yes
Press 2 for No

or

You might also like