Question

Using c++.. 1. Write a program to find the sum(), Subtraction(), Multiplication(), Division() operations using Switch...

Using c++.. 1. Write a program to find the sum(), Subtraction(), Multiplication(), Division() operations using Switch statement and functions. 2. Write a program to find the summation of N numbers. Use two functions. One function will take the input from user and the other will perform the summation from 1 to N. 3. Write a program to find the factorial of a number. Use two functions. One function will take the input from user and the other will perform the factorial. 4. Write a program using functions to find the ‘Even’ or ‘Odd’ number Submit your source code and output screen capture.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

QUESTION 1 C++ CODE:

#include<iostream>
#include<iomanip>

using namespace std;

//function to add two numbers
int addition(int x,int y)
{
   return x+y;
}

//function to subtract two numbers
int subtraction(int x,int y)
{
   return x-y;
}

//function to multiply two numbers
int multiplication(int x,int y)
{
   return x*y;
}

//function to divide two numbers
double division(int x,int y)
{
   return (double)x/y;
}

//main function
int main()
{
   //variable for storing the choice
   int choice;
  
   //variable to store the results of operations and input
   int x,y,r,r2,r3;
   double r4;
  
   //infinite looping
   while(1)
   {
       //displaying the menu
       cout<<"\n1. Addition"       ;
       cout<<"\n2. Subtraction";
       cout<<"\n3. Multiplication";
       cout<<"\n4. Divison";
       cout<<"\n5. Exit";
      
       //getting the choice from the user
       cout<<"\nEnter your choice: ";
       cin>>choice;
      
       //choosing the action based on the input
       switch(choice)
       {
          
           // for addition
           case 1: cout<<"Enter the first number: ";
                   cin>>x;
                   cout<<"Enter the second number: ";
                   cin>>y;
                   r=addition(x,y);
                   cout<<"\nThe sum of "<<x<<" and "<<y<<" is "<<r<<endl;
                   break;
          
           //subtraction
           case 2: cout<<"Enter the first number: ";
                   cin>>x;
                   cout<<"Enter the second number: ";
                   cin>>y;
                   r2=subtraction(x,y);
                   cout<<"\nThe subtraction of "<<y<<" from "<<x<<" is "<<r2<<endl;
                   break;
                  
           // multiplication
           case 3: cout<<"Enter the first number: ";
                   cin>>x;
                   cout<<"Enter the second number: ";
                   cin>>y;
                   r3=multiplication(x,y);
                   cout<<"\nThe multiply of "<<x<<" and "<<y<<" is "<<r3<<endl;
                   break;
          
           //division
           case 4: cout<<"Enter the first number: ";
                   cin>>x;
                   cout<<"Enter the second number: ";
                   cin>>y;
                   r4=division(x,y);
                   cout<<"\nThe division of "<<x<<" by "<<y<<" is ";
                   cout<<fixed << setprecision(2)<<r4<<endl;
                   break;
                  
           //for closing the application      
           case 5: cout<<"\nClosing the application...";
                   exit(0);
                   break;
          
           default:cout<<"\nEnter 1,2,3,4, or 5";
              
                  
       }
      
   }

}

SCREENSHOT FOR OUTPUT:

QUESTION 2 C++ CODE:

#include<iostream>

using namespace std;

//function to find the sum of n numbers
int summation(int n)
{
   int sum=0,i=0;
  
   //loop to find the sum of n numbers
   for(i=1;i<=n;i++)
   {
       sum=sum+i;
   }
  
   return sum;
}

//function to get the input from the user
int get_input()
{
   int n;
  
   //getting input from the user
   cout<<"Enter a number: ";
   cin>>n;
  
   //return the value
   return n;
}
int main()
{
   //calling the functions
   int n=get_input();
   int r=summation(n);

   //printing the sum of n numbers
   cout<<"The sum of "<<n<<" numbers is "<<r;
   return 0;
}

SCREENSHOT FOR OUTPUT:

QUESTION 3 C++ CODE:

#include<iostream>

using namespace std;

//function to find the factorial of n numbers
int factorial(int n)
{
   int product=1,i=0;
  
   if (n==0)
       return 1;
  
   //loop to find the factorial of n numbers
   for(i=1;i<=n;i++)
   {
       product=product*i;
   }
  
   return product;
}

//function to get the input from the user
int get_input()
{
   int n;
  
   //getting input from the user
   cout<<"Enter a number: ";
   cin>>n;
  
   //return the value
   return n;
}
int main()
{
   //calling the functions
   int n=get_input();
   int r=factorial(n);

   //printing the sum of n numbers
   cout<<"The factorial of "<<n<<" is "<<r;
   return 0;
}

SCREENSHOT FOR OUTPUT:

QUESTION 4 C++ CODE:

#include<iostream>

using namespace std;

//function to a number odd or even
void odd_even(int n)
{
   if (n % 2 ==0)
   {
       cout<<"The given number "<<n<<" is even"<<endl;
   }
   else
   {
           cout<<"The given number "<<n<<" is odd"<<endl;
   }
}

//function to get the input from the user
int get_input()
{
   int n;
  
   //getting input from the user
   cout<<"Enter a number: ";
   cin>>n;
  
   //return the value
   return n;
}
int main()
{
   //calling the functions
   int n=get_input();
   odd_even(n);
}

SCREENSHOT FOR OUTPUT:

Add a comment
Know the answer?
Add Answer to:
Using c++.. 1. Write a program to find the sum(), Subtraction(), Multiplication(), Division() operations using Switch...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT