11
SJEM2231 STRUCTURED PROGRAMMING NAME : WAN MOHAMAD FARHAN BIN AB RAHMAN TUTORIAL 2/ LAB 2 (29 TH SEPTEMBER 2014) (Use different loop structures for problems 4 to 6, and understand how it works) QUESTION 1 Write a program that asks the user to type the width and length of a rectangle and then print the area and perimeter of the rectangle. #include <iostream> using namespace std; int main () { int width, height, area, perimeter; cout <<"Please enter width of rectangle:"; cin >> width; cout <<"Please enter height of rectangle:"; cin >> height; area=width*height; perimeter=(2*width)+(2*height); // same as 2*(height+width) cout << "The area of rectangle is : " << area << endl; cout << "The perimeter of rectangle is: " << perimeter << endl; return 0; } // Output: Please enter width of rectangle:10 Please enter height of rectangle:10 The area of rectangle is : 100 The perimeter of rectangle is: 40 QUESTION 2 Write a program that converts inches to centimeters. For example, if the user enters 16.9 for a length in inches, the output would be 42.926 cm.(One inch equals 2.54centimeters) #include <iostream> using namespace std; int main() {

C++ TUTORIAL 2

Embed Size (px)

DESCRIPTION

Condition statements and looping exercises

Citation preview

Page 1: C++ TUTORIAL 2

SJEM2231 STRUCTURED PROGRAMMING

NAME : WAN MOHAMAD FARHAN BIN AB RAHMAN

TUTORIAL 2/ LAB 2 (29TH SEPTEMBER 2014)

(Use different loop structures for problems 4 to 6, and understand how it works)

QUESTION 1

Write a program that asks the user to type the width and length of a rectangle and then

print the area and perimeter of the rectangle.

#include <iostream>

using namespace std;

int main ()

{

int width, height, area, perimeter;

cout <<"Please enter width of rectangle:";

cin >> width;

cout <<"Please enter height of rectangle:";

cin >> height;

area=width*height;

perimeter=(2*width)+(2*height); // same as 2*(height+width)

cout << "The area of rectangle is : " << area << endl;

cout << "The perimeter of rectangle is: " << perimeter << endl;

return 0;

}

// Output:

Please enter width of rectangle:10

Please enter height of rectangle:10

The area of rectangle is : 100

The perimeter of rectangle is: 40

QUESTION 2

Write a program that converts inches to centimeters. For example, if the user enters 16.9

for a length in inches, the output would be 42.926 cm.(One inch equals 2.54centimeters)

#include <iostream>

using namespace std;

int main()

{

Page 2: C++ TUTORIAL 2

float length_inch, length_cm;

cout << "Please enter length in inches: ";

cin >> length_inch;

length_cm = length_inch*2.54;

cout << "The length in centimeters is : "<< length_cm << endl;

return 0;

}

//Output :

Please enter length in inches: 16.9

The length in centimeters is : 42.926

QUESTION 3

Write a program that reads the user’s age and then prints “You are a child.” if the age <

18, “You are an adult.” if 18 ≤ age < 65, and “You are a senior citizen.” if age ≥ 65.

// Method 1, using do while-loop

#include <iostream>

using namespace std;

int main()

{

int age;

cout<< "Please enter your age:";

do

{

cin >> age;

if (age < 0)

cout << "You have not entered a valid age. Please try again:";

}

Page 3: C++ TUTORIAL 2

while(age <= 0);

if (age < 18)

cout << "You are a child"<<endl;

else if (age>=18 && age<65)

cout <<"You are an adult" << endl;

else

cout<< "You are a senior citizen" << endl;

return 0;

}

//Output_method1:

Please enter your age: -10

You have not entered a valid age. Please try again:22

You are an adult

Please enter your age:-2

You have not entered a valid age. Please try again:10

You are a child

Please enter your age:-4

You have not entered a valid age. Please try again:81

You are a senior citizen

// Method 2, using if else

#include<iostream> // allows program to perform input and output

using namespace std; // program uses names from the std namespace

int main ()

{

int age;

cout<< "Please enter your age:"; //prompt user for data

Page 4: C++ TUTORIAL 2

cin >> age; //read data from user

if (age <0)

cout << "Error! You have not entered a valid age" <<endl;

else if (age >= 0 && age < 18)

cout << "You are a child" << endl;

else if (age >= 18 && age < 65)

cout<< "You are an adult" << endl;

else

cout<< "You are a senior citizen" << endl;

return 0;

}

//Output_method2:

Please enter your age:-10

Error! You have not entered a valid age

Please enter your age:14

You are a child

Please enter your age:40

You are an adult

Please enter your age:78

You are a senior citizen

QUESTION 4

Write a program to find the factorial of a given number (N!).

// Using the first way of while-loop

#include <iostream>

using namespace std;

int main()

Page 5: C++ TUTORIAL 2

{

long double N, factorial;

cout<< "Please enter a number up to 170: ";

cin>> N;

factorial=1;

while ( N > 0)

/* while loop continues until test condition N>0 is true */

{

factorial= factorial*N;

--N;

}

cout<< "Factorial = " << factorial << endl;

return 0;

}

//Output :

Please enter a number up to 170: 170

Factorial = 7.25742e+306

//Using the second way of while-loop

#include<iostream>

using namespace std;

int main ()

{

long double n,N, factorial;

cout << "Enter the value of N up to 170 : ";

cin >> N;

cout << endl;

Page 6: C++ TUTORIAL 2

factorial = 1;

n= 1;

while(n <= N)

{

factorial= factorial*n;

n= n+1; // or can write as n++

}

cout << "N!= " << factorial << endl;

return 0;

}

//Output:

Enter the value of N up to 170 : 170

N!= 7.25742e+306

// Using for-loop

#include <iostream>

using namespace std;

int main()

{

long double N, int a, factorial=1; /*try use int N */

cout << "Please enter a number up to 170: ";

cin>> N;

if (N< 0)

cout << “Factorial of negative number does not exist \n”;

Page 7: C++ TUTORIAL 2

else

{

for (a=1; a<=N; a++) //for loop terminates if a > N

{

factorial= factorial*a; //same meaning as factorial*=count

}

cout<< "Factorial of N is equal to " << factorial<< endl;

}

return 0;

}

//Output:

Please enter a number up to 170: 170

Factorial of N is equal to 7.25742e+306

QUESTION 5

Write a program to find the sum of first N natural numbers. (1 + 2 + 3 + 4 + 5 + ⋯+N)

//Using while-loop

#include <iostream>

using namespace std;

int main()

{

int a, N, sum=0;

cout<<"Please enter an integer: ";

cin>> N;

a=1;

while( a <= N) // while loop terminates if a > N

{

Page 8: C++ TUTORIAL 2

sum = sum+ a; // also can be write as sum+ = a

a++; // increment a by 1

}

cout<< "Sum = " << sum << endl;

return 0;

}

//Output :

Please enter an integer: 10

Sum = 55

//Using for-loop

#include <iostream>

using namespace std;

int main()

{

int a, N, sum=0;

cout<< "Please enter an integer: " ;

cin>> N;

for(a=1; a<=N; a++) // for loop terminates if a>N

{

sum+= a; // same meaning as sum= sum+a

}

cout<< "Sum = " << sum <<endl ;

return 0;

}

//Output :

Please enter an integer: 10

Sum = 55

Page 9: C++ TUTORIAL 2

// The program below displays error message when we enters 0 or

negative number and displays the sum of natural numbers if we enters

positive number

#include <iostream>

using namespace std;

int main()

{

int a, N, sum=0;

cout << "Please enter an integer: " ;

cin >> N;

if (N <= 0)

cout<< "Cannot execute! Error !\n" ;

else

{

for(a=1; a <=N; a++) // for loop terminates if a > N

{

sum+= a; // same meaning as sum= sum+a

}

cout<< "Sum = " <<sum<< endl;

}

return 0;

}

//Output:

Please enter an integer: 10

Sum = 55

Page 10: C++ TUTORIAL 2

QUESTION 6

Write a program to the sum of a given number of squares. For example, if 5 is input, then

the program will print 55, which equals 12+ 22+ 32+ 42+ 52

//Using while-loop

#include <iostream>

using namespace std;

int main()

{

int a, int N, sum=0;

cout<< "Please enter an integer: " ;

cin >> N;

a=1;

while( a <= N) // while loop terminates if a > N

{

sum = sum+ a*a; // also can be write as sum+ = a*a

a++; // same meaning as a=a+1

}

cout << "Sum = " << sum << endl;

return 0;

}

//Output :

Please enter an integer: 5

Sum = 55

Page 11: C++ TUTORIAL 2

//Using for-loop

#include <iostream>

using namespace std;

int main()

{

int N, sum=0;

cout<< "Please enter an integer: " ;

cin>> N;

for(int a=1; a<=N; a++) // for loop terminates if a>N

{

sum+= a*a; // same meaning as sum= sum+a*a

}

cout<< "The sum of the squares is equal to " << sum <<endl ;

return 0;

}

//Output:

Please enter an integer: 5

The sum of the squares is equal to 55