Advertisement
Guest User

Untitled

a guest
Jan 25th, 2015
260
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.73 KB | None | 0 0
  1. Exercise 1: Print
  2.  
  3. Write a C++ program to print the following lines:
  4. You are 10 years old.
  5. You are too young to play the game.
  6.  
  7. Answer:
  8.  
  9. #include "stdafx.h"
  10. #include <iostream>
  11. using namespace std;
  12.  
  13. int main()
  14. {
  15. cout<< "You are 10 years old." <<endl;
  16. cout<< "You are too young to play the game." <<endl;
  17.  
  18. int anynumber;
  19. cin >> anynumber;
  20.  
  21.  
  22. return 0;
  23. }
  24.  
  25.  
  26. Exercise 2: Print *
  27. Write five C++ statements to print the asterisk pattern as shown below.
  28.  
  29. *****
  30. *****
  31. *****
  32. *****
  33. *****
  34.  
  35. Answer:
  36.  
  37. #include "stdafx.h"
  38. #include <iostream>
  39. using namespace std;
  40.  
  41. int main()
  42. {
  43. cout<< "*****" <<endl;
  44. cout<< "*****" <<endl;
  45. cout<< "*****" <<endl;
  46. cout<< "*****" <<endl;
  47. cout<< "*****" <<endl;
  48.  
  49. int anynumber;
  50. cin >> anynumber;
  51.  
  52.  
  53. return 0;
  54. }
  55.  
  56.  
  57.  
  58.  
  59. Exercise 3: declarations
  60.  
  61. 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.
  62.  
  63. Answer:
  64.  
  65. #include "stdafx.h"
  66. #include <iostream>
  67. using namespace std;
  68.  
  69. int main()
  70. {
  71. int number1 = 10;
  72. int number2 = 15;
  73. float number3 = 12.6;
  74.  
  75. cout <<"number1: " <<number1 <<endl;
  76. cout <<"number2: " <<number2 <<endl;
  77. cout <<"number3: " <<number3 <<endl;
  78.  
  79. int anynumber;
  80. cin >> anynumber;
  81.  
  82. return 0;
  83. }
  84.  
  85. Exercise 4: Output
  86.  
  87. 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<<.
  88. Hello Sok! .
  89.  
  90. Answer:
  91.  
  92. #include "stdafx.h"
  93. #include <iostream>
  94. using namespace std;
  95.  
  96.  
  97. int main()
  98. {
  99. char name[80];
  100. cout <<"Please enter your name: ";
  101. cin >> name;
  102. cout <<"Hello "<<name <<endl;
  103.  
  104. int anynumber;
  105. cout <<"Enter any key to continue: ";
  106. cin >> anynumber;
  107.  
  108. return 0;
  109. }
  110.  
  111. Exercise 5: Input and Output
  112. 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.
  113.  
  114.  
  115. Please enter your 3 numbers: 12 45 78
  116. Your numbers forward:
  117. 12
  118. 45
  119. 78
  120.  
  121. Your numbers reversed:
  122. 78
  123. 45
  124. 12
  125.  
  126. Answer:
  127. #include "stdafx.h"
  128. #include <iostream>
  129.  
  130. using namespace std;
  131.  
  132. int main()
  133. {
  134. int number1;
  135. int number2;
  136. int number3;
  137. cout<<"Please enter your 3 numbers:";
  138. cin>>number1>>number2>>number3;
  139.  
  140. cout<<"\nYour numbers forward:\n";
  141. cout<<number1<< endl;
  142. cout<<number2<< endl;
  143. cout<<number3<< endl;
  144. cout<<"Your numbers reversed:\n";
  145. cout<<number3<< endl;
  146. cout<<number2<< endl;
  147. cout<<number1<< endl;
  148.  
  149. int anynumber;
  150. cout <<"Enter any key to continue: ";
  151. cin >> anynumber;
  152.  
  153. return 0;
  154. } 
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement