Write a program that reads a number greater than or equal to 1,000 from the user and prints it with a comma separating the thousands. Here is a sample dialog; the user input is in boldface:
Please enter an integer >= 1000: 23456
23,456
#include < iostream> using namespace std; int main(){ int n; cout << "type a number greater or equal to 1000 : "; cin >> n; // Goal: define n1 = 23 and n2 = 455 int n1 = n / 1000; // 23455 / 1000 = 23.455 -> 23 because it is an integer vs. integer int n2 = n % 1000; // 455 // 2 3 // 1000 | 2 3 4 5 5 // ________________ // 2 3 4 5 // 2 0 0 0 // ________________ // 3 4 5 5 // 3 0 0 0 // ________________ // 4 5 5 // want to print out 23,455 cout << n1 << "," << n2 << endl; return 0; }
Write a program that reads a number between 1,000 and 999,999 from the user, where the user enters a comma in the input. Then print the number without a comma. Here is a sample dialog; the user input is in boldface:
Please enter an integer between 1,000 and 999,999: 23,456
23456
Hint: Read the input as a string. Measure the length of the string. Suppose it contains n characters. Then extract substrings consisting of the first n â 4 characters and the last three characters.
#include < iostream> #include < string> using namespace std; int main(){ string n; cout << "type a number between 1,000 and 999,999 : "; cin >> n; // suppose the input we get is 23,456 // n = "3,456" int num_of_char = n.length(); // 6 string n1 = n.substr(num_of_char-3); // 456 string n2 = n.substr(0,num_of_char-4); // 23 cout << n1 << n2 << endl; // will print out 23456 return 0; }
Write a program that prints all solutions to the quadratic equation $ax^2 + bx + c$. Read in a, b, c and use the quadratic formula. If the discriminant $b^2 - 4ac$ is negative, display a message stating that there are no solutions.
#include < iostream > using namespace std; int main() { double a,b,c; cout << "type a : "; cin >> a; cout << "type b : "; cin >> b; cout << "type c : "; cin >> c; // b^2 - 4ac double discriminant = b*b - 4*a*c; if(discriminant < 0){ cout << "no solution" << endl; } else if(discriminant == 0){ cout << "repeating roots" << endl; } else{ cout << "two distinct roots" << endl; } }
Write a program that translates a letter grade into a number grade. Letter grades are A, B, C, D, and F, possibly followed by + or â. Their numeric values are 4, 3, 2, 1, and 0. There is no F+ or Fâ. A + increases the numeric value by 0.3, a â decreases it by 0.3. However, an A+ has value 4.0.
Enter a letter grade: B-
The numeric value is 2.7.
#include < iostream > #include < string > using namespace std; int main(){ string grade; cout << "Enter a letter grade: "; cin >> grade; if(grade == "A+"){ cout << 4 << endl; } else if(grade == "A"){ cout << 4 << endl; } else if(grade == "A-"){ cout << 3.7 << endl; } else if(grade == "B+"){ cout << 3.3 << endl; } else if(grade == "B"){ cout << 3 << endl; } else if(grade == "B-"){ cout << 2.7 << endl; } else if(grade == "C+"){ cout << 2.3 << endl; } else if(grade == "C"){ cout << 2 << endl; } else if(grade == "C-"){ cout << 1.7 << endl; } else if(grade == "D+"){ cout << 1.3 << endl; } else if(grade == "D"){ cout << 1 << endl; } else if(grade == "D-"){ cout << 0.7 << endl; } else if(grade == "F"){ cout << 0 << endl; } return 0; }
#include < iostream > #include < string > using namespace std; int main(){ string grade; cout << "Type grade : "; cin >> grade; string alphabet = grade.substr(0,1); double plus_minus = 0; if( grade.length() == 2 ){ if( grade.subtr(1,1) == "+"){ plus_minus = 0.3; } else{ // "-" case plus_minus = -0.3; } } if( alphabet == "A" ){ cout << fmin(4,4 + plus_minus) << endl; } else if( alphabet == "B" ){ cout << 3 + plus_minus << endl; } else if( alphabet == "C" ){ cout << 2 + plus_minus << endl; } else if( alphabet == "D" ){ cout << 1 + plus_minus << endl; } else if( alphabet == "F" ){ cout << 0 << endl; } else{ cout << "Wrong Input!" <