C++ PROGRAM EXAMPLES AND SAMPLE OUTPUTS
For you to develop the skills in formulating c++ programs, try to examine and analyze each program below. It would be
better if you will encode this program on code:block or any c++ compiler apps, then build and run the program for you to
learn how the output will be displayed. If your program displays the same with the result shown below, then you are
successful.. I encourage you then to examine, analyze, encode, test and run the sample programs below.
1. Write a program in C++ to display a welcome text in a separate line.
Sample Solution:
C++ Code : Sample Output:
#include <iostream> Print a welcome text in a separate line:
using namespace std; ----------------------------------------------
Welcome to
int main() w3resource.com
{
cout << "\n\n Print a welcome text in a separate line :\n"; Note:
cout << "----------------------------------------------\n"; *** cout << statement displays text
cout << " Welcome to \n" ; within the double quotation (“ ”)
cout << " w3resource.com "<<endl ; ***\n within the quotation means move
return 0; to the next line
}
or you can just use <<endl before ending the statement instead of \n
#include <iostream>
using namespace std;
int main()
{
cout << "\n\n Print a welcome text in a separate line : " <<endl ;
cout << "----------------------------------------------" <<endl ;
cout << " Welcome to " <<endl ;
cout << " w3resource.com "<<endl ;
return 0;
}
2. Write a program in C++ to display the sum of 29 and 30.
Sample Solution: C++ Code : Sample Output:
#include <iostream> Print the sum of two numbers:
using namespace std; -----------------------------------
The sum of 29 and 30 is : 59
int main()
{
cout << "\n\n Print the sum of two numbers :\n";
cout << "-----------------------------------\n";
cout << " The sum of 29 and 30 is : "<< 29+30 <<"\n\n" ;
}
3. Write a program in C++ to print the sum of 29 and 30 using variables.
Sample Solution: C++ Code : Sample Output:
#include <iostream> Print the sum of two numbers:
using namespace std; -----------------------------------
The sum of 29 and 30 is : 59
Sample Solution:
int main() This is how local
{ variables are declared
C++ Code
cout :
<< "\n\n Print the sum of two numbers :\n";
For you to develop the skills in formulating c++ programs, try to examine and analyze each program below. It would be
better if you will encode this program on code:block or any c++ compiler apps, then build and run the program for you to
learn how the output will be displayed. If your program displays the same with the result shown below, then you are
successful.. I encourage you then to examine, analyze, encode, test and run the sample programs below.
1. Write a program in C++ to display a welcome text in a separate line.
Sample Solution:
C++ Code : Sample Output:
#include <iostream> Print a welcome text in a separate line:
using namespace std; ----------------------------------------------
Welcome to
int main() w3resource.com
{
cout << "\n\n Print a welcome text in a separate line :\n"; Note:
cout << "----------------------------------------------\n"; *** cout << statement displays text
cout << " Welcome to \n" ; within the double quotation (“ ”)
cout << " w3resource.com "<<endl ; ***\n within the quotation means move
return 0; to the next line
}
or you can just use <<endl before ending the statement instead of \n
#include <iostream>
using namespace std;
int main()
{
cout << "\n\n Print a welcome text in a separate line : " <<endl ;
cout << "----------------------------------------------" <<endl ;
cout << " Welcome to " <<endl ;
cout << " w3resource.com "<<endl ;
return 0;
}
2. Write a program in C++ to display the sum of 29 and 30.
Sample Solution: C++ Code : Sample Output:
#include <iostream> Print the sum of two numbers:
using namespace std; -----------------------------------
The sum of 29 and 30 is : 59
int main()
{
cout << "\n\n Print the sum of two numbers :\n";
cout << "-----------------------------------\n";
cout << " The sum of 29 and 30 is : "<< 29+30 <<"\n\n" ;
}
3. Write a program in C++ to print the sum of 29 and 30 using variables.
Sample Solution: C++ Code : Sample Output:
#include <iostream> Print the sum of two numbers:
using namespace std; -----------------------------------
The sum of 29 and 30 is : 59
Sample Solution:
int main() This is how local
{ variables are declared
C++ Code
cout :
<< "\n\n Print the sum of two numbers :\n";