Advertisement
jhnksr

doc hadj

Nov 1st, 2023 (edited)
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.75 KB | Source Code | 0 0
  1. #include <iostream>
  2. #include <iomanip>
  3. #include <string.h>
  4. #include <math.h>
  5. using namespace std;
  6.  
  7. int a, i, upperLimit, number = 1, step = 1;
  8. bool mainchoice;
  9. void data1() {
  10.  
  11.     int upperLimit, digit;
  12.     cout << "Enter upper limit: ";
  13.     cin >> upperLimit;
  14.  
  15.     for (int i = 1; i <= upperLimit; i++)
  16.     {
  17.         digit = i % 10;
  18.         if (digit == 1 || digit == 2 || digit == 4 || digit == 8) {
  19.             cout << i << " ";
  20.         }
  21.     }
  22.  
  23. }
  24.  
  25. void data2() {
  26.  
  27.     int upper_limit, digit;
  28.     cout << "Enter upper limit: ";
  29.     cin >> upper_limit;
  30.  
  31.     for (int i = 1; i <= upper_limit; i++) {
  32.         digit = i % 4;
  33.         if (digit == 1 || digit == 3)
  34.             cout << i << " ";
  35.     }
  36.  
  37. }
  38.  
  39. void data3() {
  40.  
  41.  
  42.     cout << "Enter upper limit: ";
  43.     cin >> upperLimit;
  44.  
  45.     upperLimit *= 2;
  46.     int number = 1, i = 1;
  47.     while (number <= upperLimit) {
  48.         if (i % 2 == 1) {
  49.             cout << number << " ";
  50.         }
  51.         else {
  52.             cout << i + 2 << " ";
  53.         }
  54.         number += i;
  55.         i++;
  56.     }
  57. }
  58.  
  59.  
  60. void data4()
  61.  
  62. {
  63.     int n;
  64.     double sum = 0.0;
  65.     cout << "Enter n: ";
  66.     cin >> n;
  67.  
  68.     if (n <= 0) {
  69.         cout << "Invalid input. Please enter a positive integer." << endl;
  70.     }
  71.  
  72.     int i = 1;
  73.     bool firstTerm = true; // determine if it's the first term in the series
  74.  
  75.     cout << "Sum of the series: ";
  76.  
  77.     do {
  78.         if (!firstTerm) {
  79.             cout << " + ";
  80.         }
  81.         else {
  82.             firstTerm = false;
  83.         }
  84.  
  85.         cout << i << "/" << i + 1;
  86.  
  87.         sum += static_cast<double>(i) / (i + 1);
  88.         i++;
  89.     } while (i <= n);
  90.  
  91.     cout << " = " << fixed << setprecision(3) << sum << endl;
  92.  
  93. }
  94.  
  95.  
  96. void data5() {
  97.     int num, digit;
  98.     cout << "Enter a number: ";
  99.     cin >> num;
  100.     if (num == 0) {
  101.         cout << "zero " << endl;
  102.     }
  103.     const char* words[] = { "zero ", "one ", "two ", "three ", "four ", "five ", "six ", "seven ", "eight ", "nine " };
  104.     string reverseWords = "";
  105.  
  106.     while (num > 0) {
  107.         digit = num % 10;
  108.         num /= 10;
  109.         reverseWords = words[digit] + reverseWords;
  110.     }
  111.  
  112.     cout << "Output: " << reverseWords << endl;
  113. }
  114.  
  115. int main() {
  116.     while (mainchoice = true)
  117.     {
  118.     int choice = 0;
  119.  
  120.     cout << "Choose one:\n1. Sequence of 1, 2, 4, and 8 Generator\n2. Modulo 4\n3. Alternating Numbers\n4. '1 / 2 + 2 / 3 + 3 / 4 ...'\n5. Numbers to Words\n6. Exit. " << endl;
  121.     cout << "Choice: ";
  122.     cin >> choice;
  123.  
  124.     switch (choice)
  125.     {
  126.     case 1:
  127.         system("cls");
  128.         data1();
  129.         cout << "\n\n\n";
  130.         break;
  131.     case 2:
  132.         system("cls");
  133.         data2();
  134.         cout << "\n\n\n";
  135.         break;
  136.     case 3:
  137.         system("cls");
  138.         data3();
  139.         cout << "\n\n\n";
  140.         break;
  141.     case 4:
  142.         system("cls");
  143.         data4();
  144.         cout << "\n\n\n";
  145.         break;
  146.     case 5:
  147.         system("cls");
  148.         data5();
  149.         cout << "\n\n\n";
  150.         break;
  151.     case 6:
  152.         system("cls");
  153.         cout << "Program Exited!";
  154.         cout << "\n\n\n";
  155.         return 0;
  156.     default:
  157.         cout << "Please Enter a valid option.";
  158.         cout << "\n\n\n";
  159.         break;
  160.     }
  161. }
  162.             return 0;
  163.        
  164. }
  165. /*(use for)
  166.  Enter upper limit: 50
  167. 1 2 4 8 11 12 14 18 21 22 24 28 31 32 34 38 41 42 44 48
  168.  
  169.  (use for)
  170.  Enter upper limit: 50
  171.  1 3 9 13 18 19 22 24 30 34 39 40 43 45
  172.  
  173.  (use while)
  174.  Enter upper limit: 50
  175.  1 3 3 6 5 11 7 18 9 27 11 38 13 51 15 66 17 83 19
  176.  
  177. (use do while)
  178.  Enter n: 10
  179. sum of the series
  180.  1/2+2/3+3/4+4/5+5/6+6/7+7/8+8/9+10/11 =. 7.980
  181.  
  182.  (use while loop)
  183.  Enter a number: 2193
  184.  Output
  185.  three nine one two
  186.  
  187.  Enter a number: 1020
  188.  Output
  189.  zero two zero one
  190. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement