C++ Functions: 1. Write A C++ Program That Will Display The Calculator Menu
C++ Functions: 1. Write A C++ Program That Will Display The Calculator Menu
The program will prompt the user to choose the operation choice (from 1 to 5). Then
it asks the user to input two integer vales for the calculation. See the sample below.
MENU
1. Add
2. Subtract
3. Multiply
4. Divide
5. Modulus
Enter your choice: 1
Enter your two numbers: 12 15
Result: 27
Continue? y
The program also asks the user to decide whether he/she wants to continue the
operation. If he/she input ‘y’, the program will prompt the user to choose the
operation gain. Otherwise, the program will terminate.
Solution:
#include <cstdlib>
#include <iostream>
#include<iomanip>
void displaymenu(){
cout<<"==================================================="<<"\
n";
cout<<" MENU "<<"\n";
cout<<"==================================================="<<"\
n";
cout<<" 1.Add"<<"\n";
cout<<" 2.Subtract"<<"\n";
cout<<" 3.Multiply"<<"\n";
cout<<" 4.Divide"<<"\n";
cout<<" 5.Modulus"<<"\n";
}
int Add(int a,int b){
return(a+b);
}
cout<<"\nPress y or Y to continue:";
cin>>confirm;
}while(confirm=='y'||confirm=='Y');
system("PAUSE");
return EXIT_SUCCESS;
}
#include <stdio.h>
Write a program in C to find the square of any number using the function.
#include <stdio.h>
int main()
int num;
double n;
printf("------------------------------------------------\n");
scanf("%d", &num);
n = square(num);
return 0;
Copy
Sample Output:
Function : find square of any number :
------------------------------------------------
Input any number for square : 20
The square of 20 is : 400.00
Write a program in C to swap two numbers using the function.
#include<stdio.h>
int n1,n2;
printf("\n\n Function : swap two numbers using function :\n");
printf("------------------------------------------------\n");
printf("Input 1st number : ");
scanf("%d",&n1);
printf("Input 2nd number : ");
scanf("%d",&n2);
int tmp;
tmp = *p; // tmp store the value of n1
*p=*q; // *p store the value of *q that is value of n2
*q=tmp; // *q store the value of tmp that is the value of n1
}
Write a program in C to check a given number is even or odd using the function.
#include <stdio.h>
//if the least significant bit is 1 the number is odd and 0 the number
is even
int checkOddEven(int n1)
{
return (n1 & 1);//The & operator does a bitwise and,
}
int main()
{
int n1;
printf("\n\n Function : check the number is even or odd:\n");
printf("------------------------------------------------\n");
printf("Input any number : ");
scanf("%d", &n1);
int fact(int);
void main()
{
int sum;
sum=fact(1)/1+fact(2)/2+fact(3)/3+fact(4)/4+fact(5)/5;
printf("\n\n Function : find the sum of 1!/1+2!/2+3!/3+4!/4+5!/5
:\n");
printf("---------------------------------------------------------
-\n");
printf("The sum of the series is : %d\n\n",sum);
}
int fact(int n)
{
int num=0,f=1;
while(num<=n-1)
{
f =f+f*num;
num++;
}
return f;
}