watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqvz3v1z2xlmjaxma==/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/ Dissolve/70/gravity/center ">
1. Code
#include <iostream>int main () { using namespace std; int num1; int num2; int total=0; cout << "Please enter the start number \ n"; Cin >> NUM1; cout << "Please enter the end number \ n"; Cin >> num2; for (NUM1; num1<=num2; num1++) Total = Num1 + total; cout << num1 << "and" << num2 << "integer and as" << total <<endl; return 0;}
Execution results
2. Code
#include <iostream>int main () { using namespace std; Double total = 0.0; double in; cout << "Please enter Number:"; Cin >> in; while (in = 0) {Total + = in; cout << "Total number of inputs:" << full << "\ n"; cout << "Please enter the next number:"; Cin >> in; } cout << "Execution End"; return 0;}
Execution results
3. Code
#include <iostream>int main () { using namespace std; Double daphne=100; Double cleo=100; int year=1; while (Daphne >= Cleo) { Daphne + = 100*0.1; Cleo + = cleo*0.05; cout << "<< year <<", Daphne investment value "<< Daphne <<" Cleo Investment Value "<< Cleo <<endl ; year++; } cout << "<< year <<", when Cleo exceeds Daphne investment value "<< Endl; return 0;}
Execution results
4. Code
#include <iostream>const char *months[12] = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"}; int main () { using namespace std; int sales[12]; int total; for (int i=1; i<=12; i++) { cout << Please enter << months[i-1] << "Sales quantity:"; CIN >> sales[i-1]; } for (int j=0; j<12; j + +) {Total = total+sales[j]; } cout << "Total sales:" << totals <<endl; return 0;}
Execution results
5. Code
#include <iostream>const char *months[12] = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"}; int main () { using namespace std; int sales[3][12]; int total[3] = {0}; Be sure to initialize. Otherwise the initial value is not 0 int sum; for (int a=1;a<=3;a++) {for (int i=1; i<=12; i++) { cout << "Please enter" << a << "year" < ;< months[i-1] << "Sales quantity:"; CIN >> sales[a-1][i-1]; } } for (int b=0, b<3; b++) {for (int j=0; j<12; j + +) { total[b] = total[b]+sales[b][j]; } sum = sum + total[b]; cout << "<< b+1 <<" annual total sales of << total[b] <<endl; } cout << "Total sales:" << sum <<endl; return 0;}
Execution results
6. Code
<pre name= "code" class= "CPP" >//be sure to add while (Cin.get ()! = ' \ n '); #include <iostream>using namespace Std;const int LEN = 60;struct car{ char Brand[len]; int year;}; int main () { int num; cout << "How many cars does you wish to catalog?"; CIN >> num; while (Cin.get ()! = ' \ n '); Car *ps = new Car[num]; for (int i=0;i<num;i++) { cout << "Car #" << (i+1) << ": \ n"; cout << "Please enter the Make:"; Cin.getline (Ps[i].brand, LEN); cout << "Please enter the year made:"; Cin >> Ps[i].year; while (Cin.get ()! = ' \ n '); } cout << "Here is your collection:\n"; for (int i=0; i<num; i++) { cout << ps[i].year << "" << Ps[i].brand <<endl; } Delete [] PS; return 0;}
Execution results
watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqvz3v1z2xlmjaxma==/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/ Dissolve/70/gravity/center ">
7. Code
#include <iostream> #include <cstring>const int str_len=60;int main () { using namespace std; Char Words[str_len]; int count=0; cout << "Enter words (to stop,type, Word done): \ n"; while (Cin >> words && strcmp ("Done", words)) ++count; cout << "entered a total of" << count << "words. \ n" <<endl; return 0;}
Execution results
8. Code
#include <iostream> #include <string>int main () { using namespace std; string words; int count=0; cout << "Enter words {to stop,type the word done}:\n"; while (Cin >> words && words! = "Done") ++count; cout << "entered a total of" << count << "words. \ n"; return 0;}
Execution results
9. Code
#include <iostream>int main () { using namespace std; int row; cout << "Enter number of row:"; Cin >> Row; for (int i=0, i<row; i++) {for (int j=row-i; j>1; j--) { cout << "."; } for (int k=0; k<=i; k++) { cout << "*"; } cout << "\ n"; } return 0;}
Execution results
Copyright notice: This article Bo Master original article. Blog, not reproduced without consent.
C + + Primer post-lesson programming problem