Write a program that executes various operations on three numbers. The numbers a
ID: 3624703 • Letter: W
Question
Write a program that executes various operations on three numbers. The numbers areof type int and initialized to -1. In case the user enters a negative number as input, ask
the user to enter data again after displaying the following message:
ERROR: Incorrect data, try again.
The program should display a user menu, reads one out of 9 options (numbers 1 – 9
only) executes the suitable operation and goes back to display the menu, until the user
chooses option 9 (Quit).
Here is the menu:
******** Main menu ********
1 : Input the numbers
2 : Display the numbers
3: Sort the numbers
4 : Display the average
5 : Display the min and max
6 : Display Prime or Not
7 : Display Perfect or Not
8 : Initialize the numbers
9 : Quit
**************************
Please, enter your choice >
In case there is an error in the command choice, the program asks the user to try again
after displaying the following message: ERROR: Incorrect command, try again.
In case the numbers are initialized to -1 and the user chooses commands 3-7 the
following message should appear: ERROR: No numbers entered.
Explanation / Answer
#include <stdio.h>
int displayMenu(void);
void displayNumbers(void); // function decleration
void sortNumbers(void); // function decleration
double avgNumbers (void); // function decleration
int minNumber (void); // function decleration
int maxNumber (void); // function decleration
int prime (int x); // function decleration
int perfect(int x); // function decleration
void init(void); // function decleration
int a,b,c;
int main()
{char z;
int min,max,sp,pra,prb,prc,pira,pirb,pirc,ilz;
double av;
displayMenu(); // functoin call
scanf ("%c",&z);
ilz=0;//!!!
while ( z!=9)
{
switch (z)
{ case '1': printf("Please enter 3 positive numbers");
scanf("%d %d %d", &a,&b,&c);
while (a<0 || b<0 || c<0)
{ printf("ERROR: incorect number, try again");
scanf("%d %d %d", &a,&b,&c);
}
ilz=1;
printf ("Please enter a number: ");
sp=1;
scanf (" %c",&z);
break;
case '2': if (ilz ==0)
{printf("ERROR no numbers entred ");
printf ("Please enter a number: ");
scanf (" %c",&z);
break;
}
displayNumbers(); // functoin call
printf ("Please enter a number: ");
scanf (" %c",&z);
break;
case '3': if (sp==0 || ilz==0)
{printf("ERROR no numbers entred ");
printf ("Please enter a number: ");
scanf (" %c",&z);
break;
}
else
sortNumbers(); // functoin call
printf("%d %d %d",a,b,c);
printf ("Please enter a number: ");
scanf (" %c",&z);
break;
case '4': if (sp==0 || ilz==0)
{printf("ERROR no numbers entred ");
printf ("Please enter a number: ");
scanf (" %c",&z);
break;
}
else
av = avgNumbers(); // functoin call
printf("The average is : %.2f ",av);
printf ("Please enter a number: ");
scanf (" %c",&z);
break;
case '5': if (sp==0 || ilz==0)
{printf("ERROR no numbers entred ");
printf ("Please enter a number: ");
scanf (" %c",&z);
break;
}
else
{min = minNumber();// functoin call
max = maxNumber();// functoin call
printf("The maximum number is: %d ", max);
printf("The minimum number is: %d ",min);
}
printf ("Please enter a number: ");
scanf (" %c",&z);
break;
case '6': if (sp==0 || ilz==0)
{printf("ERROR no numbers entred ");
printf ("Please enter a number: ");
scanf (" %c",&z);
break;
}
else
{pra =prime(a);
prb =prime(b);
prc =prime(c);
if (pra ==1)
printf("The number %d is prime ",a);
else
printf("The number %d is not prime ",a);
if (prb ==1)
printf("The number %d is prime ",b);
else
printf("The number %d is not prime ",b);
if (prc ==1)
printf("The number %d is prime ",c);
else
printf("The number %d is not prime ",c);
}
printf ("Please enter a number: ");
scanf (" %c",&z);
break;
case '7': if (sp==0 || ilz==0)
{printf("ERROR no numbers entred ");
printf ("Please enter a number: ");
scanf (" %c",&z);
break;
}
else
{pira =perfect(a);
pirb =perfect(b);
pirc =perfect(c);
}
if (pira ==1)
printf("The number %d is perfect ",a);
else
printf("The number %d is not perfect ",a);
if (pirb ==1)
printf("The number %d is perfect ",b);
else
printf("The number %d is not perfect ",b);
if (pirc ==1)
printf("The number %d is perfect ",c);
else
printf("The number %d is not perfect ",c);
printf ("Please enter a number: ");
scanf (" %c",&z);
break;
case '8': if (ilz == 0)
{printf("ERROR no numbers entred ");
printf ("Please enter a number: ");
scanf (" %c",&z);
break;
}
init();
printf("%d %d %d",a,b,c);
printf ("Please enter a number: ");
sp=0;
scanf (" %c",&z);
break;
case '9': break;
default: printf("ERROR: Incorrect command, try again: ");
scanf (" %c",&a);
break;
}
}
return 0;
}
int displayMenu(void)// function definition
{
printf("1: Input the numbers ");
printf("2: Display the numbers ");
printf("3: Sort the numbers ");
printf("4: Display the average ");
printf("5: Display the min and max ");
printf("6: Display prime or not ");
printf("7: Display perfect or not ");
printf("8: Initilize the numbers ");
printf("9: Quit ");
printf("********************** ");
printf ("Please enter your choice > ");
return 0;
}
void displayNumbers (void) // function definition
{
printf("b =%d c =%d d =%d ", a,b,c);
}
void sortNumbers(void) // function definition
{
int min,mid,max;
if(c<b)
{ if (a<c)
{ min=a;
mid=c;
max=b;
a=min;
b=mid;
c=max;
}
else
{ if (a<b)
{ min=c;
mid=a;
max=b;
a=min;
b=mid;
c=max;
}
else
{ min=c;
mid=b;
max=a;
a=min;
b=mid;
c=max;
}
}
}
else if (c>b)
{ if (a<b)
{ min=a;
mid=b;
max=c;
a=min;
b=mid;
c=max;
}
else
{ if (c<a)
{ min=b;
mid=a;
max=c;
a=min;
b=mid;
c=max;
}
else
{ min=b;
mid=c;
max=a;
a=min;
b=mid;
c=max;
}
}
}
}
double avgNumbers(void) // function definition
{
double sum;
double avge;
sum = a+ b+ c;
avge = sum/3;
return (avge);
}
int maxNumber (void) // function definition
{
if (a>b && a>c)
return (a);
else if (b>a && b>c)
return (b);
else
return (c);
}
int minNumber (void) // function definition
{
if (a<b && a<c)
return (a);
else if (b<a && b<c)
return (b);
else
return (c);
}
int prime (int x) // function definition
{
int count,c;
count=2;
while (count != x)
{ c = x % count;
if ( c==0)
break;
count++;
}
if (count== x )
return (1);
else
return (0);
}
int perfect (int x) // function definition
{
int count,div,b;
count=1;
div=0;
while (count <x)
{
b = x % count;
if (b==0)
div = div + count;
count++;
}
if ( div==x)
return (1);
else
return (0);
}
void init(void) // function definition
{ a=-1;
b=-1;
c=-1;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.