tampurus

SGS lab 1

Oct 1st, 2023
688
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.41 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. int main()
  6. {
  7.     while (true)
  8.     {
  9.         int num;
  10.         cout << "Enter 1 for Hello World :  \nEnter 2 for Calculation : \nEnter 3 for Swapping of 2 numbers : \nEnter 4 for Swapping of 3 numbers : \nEnter 5 for Fibonacci series : \nEnter 6 for Square of number : \nEnter 7 for Area of triangle : \nEnter 8 for Area of circle : \nEnter 9 for Simple interest : \nEnter 10 for checking Leap year or not : \nEnter 11 for Check character vowel or not : \nEnter 12 for Table of any number :- \nEnter 13 for Prime number :- \n\nEnter any number from above stated = ";
  11.         cin >> num;
  12.  
  13.         switch (num)
  14.         {
  15.             case 1:
  16.                 cout << "Hello World";
  17.                 break;
  18.             case 2:
  19.             {
  20.                 int a1, a2, val;
  21.                 cout << "Enter any two numbers = ";
  22.                 cin >> a1 >> a2;
  23.                 cout << "Enter 1 for Addition :- \nEnter 2 for Subtraction :- \nEnter 3 for Multiplication :- \nEnter 4 for Division :- \nEnter 5 for Modulus (Remainder) :- \nEnter 6 for All Calculation :- \n\nPlease Enter = ";
  24.                 cin >> val;
  25.    
  26.                 switch (val)
  27.                 {
  28.                 case 1:
  29.                     cout << "\nAddition = " << a1 + a2;
  30.                     break;
  31.                 case 2:
  32.                     cout << "\nSubtraction = " << a1 - a2;
  33.                     break;
  34.                 case 3:
  35.                     cout << "\nMultiplication = " << a1 * a2;
  36.                     break;
  37.                 case 4:
  38.                     if (a2 != 0)
  39.                         cout << "\nDivision = " << a1 / a2;
  40.                     else
  41.                         cout << "\nDivision by zero is not allowed";
  42.                     break;
  43.                 case 5:
  44.                     if (a2 != 0)
  45.                         cout << "\nModulus (Remainder) = " << a1 % a2;
  46.                     else
  47.                         cout << "\nModulus (Remainder) by zero is not allowed";
  48.                     break;
  49.                 case 6:
  50.                     cout << "\nAddition = " << a1 + a2;
  51.                     cout << "\nSubtraction = " << a1 - a2;
  52.                     cout << "\nMultiplication = " << a1 * a2;
  53.                     if (a2 != 0)
  54.                         cout << "\nDivision = " << a1 / a2;
  55.                     else
  56.                         cout << "\nDivision by zero is not allowed";
  57.                     if (a2 != 0)
  58.                         cout << "\nModulus (Remainder) = " << a1 % a2;
  59.                     else
  60.                         cout << "\nModulus (Remainder) by zero is not allowed";
  61.                     break;
  62.                 default:
  63.                     cout << "Invalid number";
  64.                     break;
  65.                 }
  66.             }
  67.             break;
  68.             case 3:
  69.             {
  70.                 int b1, b2, choice;
  71.                 cout << "\nEnter 1 for Swapping with a third variable :- \nEnter 2 for Swapping without a third variable :- \n\nNow Enter here= ";
  72.                 cin >> choice;
  73.                 int b3;
  74.                 switch (choice)
  75.                 {
  76.                 case 1:
  77.                     cout << "Enter the value of a and b = ";
  78.                     cin >> b1 >> b2;
  79.                     b3 = b1;
  80.                     b1 = b2;
  81.                     b2 = b3;
  82.                     cout << "a=" << b1 << "\nb=" << b2;
  83.                     break;
  84.                 case 2:
  85.                     cout << "Enter the value of a and b = ";
  86.                     cin >> b1 >> b2;
  87.                     b1 = b1 + b2;
  88.                     b2 = b1 - b2;
  89.                     b1 = b1 - b2;
  90.                     cout << "a=" << b1 << "\nb=" << b2;
  91.                     break;
  92.                 default:
  93.                     cout << "Invalid choice";
  94.                     break;
  95.                 }
  96.             }
  97.             break;
  98.             case 4:
  99.             {
  100.                 int n1, n2, n3;
  101.                 cout << "Enter the value of a, b, and c = ";
  102.                 cin >> n1 >> n2 >> n3;
  103.                 int n4 = n1;
  104.                 n1 = n2;
  105.                 n2 = n3;
  106.                 n3 = n4;
  107.                 cout << "a=" << n1 << "\nb=" << n2 << "\nc=" << n3;
  108.             }
  109.             break;
  110.             case 5:
  111.             {
  112.                 int d1 = 0, d2 = 1, d3, d4, d5;
  113.                 cout << "Enter the number of values to be printed for the Fibonacci series = ";
  114.                 cin >> d5;
  115.                 cout << "Fibonacci Series :-" << endl;
  116.                 cout << d1 << endl
  117.                      << d2;
  118.                 for (d4 = 3; d4 <= d5; d4++)
  119.                 {
  120.                     d3 = d1 + d2;
  121.                     d1 = d2;
  122.                     d2 = d3;
  123.                     cout << endl << d3;
  124.                 }
  125.             }
  126.             break;
  127.             case 6:
  128.             {
  129.                 int e;
  130.                 cout << "Enter any number you want the square of = ";
  131.                 cin >> e;
  132.                 cout << "Square of " << e << " = " << e * e;
  133.             }
  134.             break;
  135.             case 7:
  136.             {
  137.                 float base, height, area1;
  138.                 cout << "Enter the value of base and height = ";
  139.                 cin >> base >> height;
  140.                 area1 = (base * height) / 2;
  141.                 cout << "Area of Triangle = " << area1;
  142.             }
  143.             break;
  144.             case 8:
  145.             {
  146.                 float area, radius;
  147.                 cout << "Enter the value of the radius = ";
  148.                 cin >> radius;
  149.                 area = (22 * radius * radius) / 7;
  150.                 cout << "Area of Circle = " << area;
  151.             }
  152.             break;
  153.             case 9:
  154.             {
  155.                 float p, r, t, si;
  156.                 cout << "Enter the value of P, R, and T = ";
  157.                 cin >> p >> r >> t;
  158.                 si = (p * r * t) / 100;
  159.                 cout << "Simple Interest = " << si;
  160.             }
  161.             break;
  162.             case 10:
  163.             {
  164.                 int year;
  165.                 cout << "Enter any Year = ";
  166.                 cin >> year;
  167.                 if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))
  168.                     cout << year << " is a Leap Year";
  169.                 else
  170.                     cout << year << " is not a Leap Year";
  171.             }
  172.             break;
  173.             case 11:
  174.             {
  175.                 char ch;
  176.                 cout << "Enter any character = ";
  177.                 cin >> ch;
  178.                 switch (ch)
  179.                 {
  180.                 case 'a':
  181.                 case 'e':
  182.                 case 'i':
  183.                 case 'o':
  184.                 case 'u':
  185.                 case 'A':
  186.                 case 'E':
  187.                 case 'I':
  188.                 case 'O':
  189.                 case 'U':
  190.                     cout << " " << ch << " is a Vowel";
  191.                     break;
  192.                 default:
  193.                     cout << " " << ch << " is not a Vowel";
  194.                     break;
  195.                 }
  196.             }
  197.             break;
  198.             case 12:
  199.             {
  200.                 int n;
  201.                 cout << "Enter any number you want the table of = ";
  202.                 cin >> n;
  203.                 for (int i = 1; i <= 10; i++)
  204.                 {
  205.                     cout << n << "*" << i << "=" << n * i << endl;
  206.                 }
  207.             }
  208.             break;
  209.             case 13:
  210.             {
  211.                 int n;
  212.                 cout << "Enter any number = ";
  213.                 cin >> n;
  214.                 if(n<=1){
  215.                     cout<<"Not prime \n";
  216.                     break;
  217.                 }
  218.                 bool check = true;
  219.                 for(int i=2 ; i<= (n/2) ; i++){
  220.                     if(n%i==0){
  221.                         check = false;
  222.                         break;
  223.                     }
  224.                        
  225.                 }
  226.                 if(check){
  227.                     cout<<n<<" is prime\n";
  228.                 }
  229.                 else{
  230.                     cout<<n<<" is not prime\n";
  231.                 }
  232.             }
  233.             break;
  234.             default:
  235.                 cout << "Please enter a valid number";
  236.                 break;
  237.         }
  238.  
  239.         cout << "\nDo you want to continue? (y/n): ";
  240.         char choice;
  241.         cin >> choice;
  242.         if (choice != 'y' && choice != 'Y')
  243.             break;
  244.     }
  245.  
  246.     return 0;
  247. }
  248.  
Advertisement
Add Comment
Please, Sign In to add comment