C++ PROGRAMING PLEASE HELP !! Write a computer program in C which will simulate
ID: 663057 • Letter: C
Question
C++ PROGRAMING
PLEASE HELP !!
Write a computer program in C which will simulate a calculator. Your calculator needs to support the five basic operations (addition, subtraction, multipication, division and modulus) plus primality testing (natural number is prime if it has no non-trivial divisors). Example of the output follows:
CALCULATOR MENU:
1)Addition 2)Subtraction 3)Multiplication
4)Division
5)Modulus (integers only)
6)Test if prime (integers only)
7)Exit
PLEASE CHOOSE AN OPERATION: 2
ENTER THE FIRST NUMBER: 18
ENTER THE SECOND NUMBER : 20
18-20 = -2
CALCULATOR MENU:
1)Addition
2)Subtraction
3)Multiplication
4)Division
5)Modulus (integers only)
6)Test if prime (integers only)
7) Exit
PLEASE CHOOSE AND OPERATION : 6
ENTER THE NUMBER TO BE TESTED:22
NOT PRIME: 2x11=22
CALCULATOR MENU:
1)Addition
2)Subtraction
3)Multiplication
4)Division
5)Modulus (integers only)
6) Test if prime (integers only)
7)Exit
PLEASE CHOOSE AN OPERATION: 7
GOOD BYE !
The program must be bug-free and well comented
Explanation / Answer
#include<stdio.h>
void main()
{
int add(),sub(),mult(),div(),mod(),testprime(); // declaring four functions
int o,l=1;
while(l) // This loop expression never become false
{
printf(" PLEASE CHOOSE AN OPERATION: 1)addition 2)subtraction 3)multiplication 4)division 5)modulos 6)testprime 7)exit ");
scanf("%d",&o);
switch(o)
{
case 1:
add(); // calling function
break;
case 2:
sub(); // calling function
break;
case 3:
mult(); // calling function
break;
case 4:
div(); // calling function
break;
case 5:
mod();
break;
case 6:
testprime();
break;
default:
l=0;
printf("GOOD BYE ! ");
break;// This function is used to terminate the program because in while loop exp.(1) is used
}
}
}
int add() //calling add function
{
int a,b,c;
printf("Enter two numbers");
scanf("%d%d",&a,&b);
c=a+b;
printf("%d + %d = %d",a,b,c);
getchar();
return(0); //nothing to return
}
int sub()
{
int a,b,c;
printf("Enter two numbers");
scanf("%d%d",&a,&b);
c=a-b;
printf("%d - %d= %d",a,b,c);
getchar();
return(0);
}
int mult()
{
int a,b,c;
printf("Enter two numbers");
scanf("%d%d",&a,&b);
c=a*b;
printf("%d X %d = %d",a,b,c);
getchar();
return(0);
}
int div()
{
int a,b,c;
printf("Enter two numbers");
scanf("%d%d",&a,&b);
c=a/b;
printf("%d / %d = %d",a,b,c);
getchar();
return(0);
}
int mod()
{
int a, b, d=0;
printf("Please enter first number : ");
scanf("%d", &a);
printf("Please enter second number : ");
scanf("%d", &b);
d=a%b;
printf("Modulus of entered numbers = %d",d);
getchar();
return(0);
}
int testprime()
{
int n, i, flag=0;
printf("Enter a positive integer: ");
scanf("%d",&n);
for(i=2;i<=n/2;++i)
{
if(n%i==0)
{
flag=1;
break;
}
}
if (flag==0)
printf("%d is a prime number.",n);
else
printf("%d is not a prime number.",n);
getchar();
return(0);
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.