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 ton C++ Programmingn"; 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 : 7 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 ornPress 2 for Nonn"; cin>> value; if(value==1)
{ 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 2 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 before setprecision() to ensure that when we set the setprecision(0), it will round off to integer, if we make setprecision(1), setprecision(2), etc ; it will round off to 1 decimal place ,2 decimal places and so on respectively */ cout<< "Do you want to continue with other decimal number?n"; cout<<"Press 1 for Yes ornPress 2 for Nonn"; 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 or Press 2 for No 2

C++ TUTORIAL 1

  • 1.
    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
  • 2.
    #include <iostream> usingnamespace 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 ton C++ Programmingn"; return 0; } //Output : Hello, welcome to C++ programming
  • 3.
    QUESTION 3 Writea 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
  • 4.
    QUESTION 4 Writea 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";
  • 5.
    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 : 7 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; }
  • 6.
    //Output_truncate : Pleaseenter 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 ornPress 2 for Nonn"; cin>> value; if(value==1)
  • 7.
    { 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?
  • 8.
    Press 1 forYes 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 2 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;
  • 9.
    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 before setprecision() to ensure that when we set the setprecision(0), it will round off to integer, if we make setprecision(1), setprecision(2), etc ; it will round off to 1 decimal place ,2 decimal places and so on respectively */ cout<< "Do you want to continue with other decimal number?n"; cout<<"Press 1 for Yes ornPress 2 for Nonn"; 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; } }
  • 10.
    //Output_round off methodusing 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 or Press 2 for No 2