Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Exercise 1: Print
- Write a C++ program to print the following lines:
- You are 10 years old.
- You are too young to play the game.
- Answer:
- #include "stdafx.h"
- #include <iostream>
- using namespace std;
- int main()
- {
- cout<< "You are 10 years old." <<endl;
- cout<< "You are too young to play the game." <<endl;
- int anynumber;
- cin >> anynumber;
- return 0;
- }
- Exercise 2: Print *
- Write five C++ statements to print the asterisk pattern as shown below.
- *****
- *****
- *****
- *****
- *****
- Answer:
- #include "stdafx.h"
- #include <iostream>
- using namespace std;
- int main()
- {
- cout<< "*****" <<endl;
- cout<< "*****" <<endl;
- cout<< "*****" <<endl;
- cout<< "*****" <<endl;
- cout<< "*****" <<endl;
- int anynumber;
- cin >> anynumber;
- return 0;
- }
- Exercise 3: declarations
- Write a C++ program to declare two integers, one float variables and assign 10, 15, and 12.6 to them respectively. It then prints these values on the screen.
- Answer:
- #include "stdafx.h"
- #include <iostream>
- using namespace std;
- int main()
- {
- int number1 = 10;
- int number2 = 15;
- float number3 = 12.6;
- cout <<"number1: " <<number1 <<endl;
- cout <<"number2: " <<number2 <<endl;
- cout <<"number3: " <<number3 <<endl;
- int anynumber;
- cin >> anynumber;
- return 0;
- }
- Exercise 4: Output
- Write a C++ program to prompt the user to input her/his name and print this name on the screen, as shown below. The text from keyboard can be read by using cin>> and to display the text on the screen you can use cout<<.
- Hello Sok! .
- Answer:
- #include "stdafx.h"
- #include <iostream>
- using namespace std;
- int main()
- {
- char name[80];
- cout <<"Please enter your name: ";
- cin >> name;
- cout <<"Hello "<<name <<endl;
- int anynumber;
- cout <<"Enter any key to continue: ";
- cin >> anynumber;
- return 0;
- }
- Exercise 5: Input and Output
- Write a C++ program to prompt the user to input 3 integer values and print these values in forward and reversed order, as shown below.
- Please enter your 3 numbers: 12 45 78
- Your numbers forward:
- 12
- 45
- 78
- Your numbers reversed:
- 78
- 45
- 12
- Answer:
- #include "stdafx.h"
- #include <iostream>
- using namespace std;
- int main()
- {
- int number1;
- int number2;
- int number3;
- cout<<"Please enter your 3 numbers:";
- cin>>number1>>number2>>number3;
- cout<<"\nYour numbers forward:\n";
- cout<<number1<< endl;
- cout<<number2<< endl;
- cout<<number3<< endl;
- cout<<"Your numbers reversed:\n";
- cout<<number3<< endl;
- cout<<number2<< endl;
- cout<<number1<< endl;
- int anynumber;
- cout <<"Enter any key to continue: ";
- cin >> anynumber;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement