Design a program called cscd255hw2.c which reads in a non-negative integer (0 is
ID: 3627279 • Letter: D
Question
Design a program called cscd255hw2.c which reads in a non-negative integer (0 is non-negative) from the user. The user will then be prompted with a menu of choices (this menu should be repetitively displayed until the user chooses to quit): You must implement #1 and #5 and TWO of #2, #3,and #4.Your menu will include these choices (stub out the one you don’t implement.)
1. Enter a new number
2. Print the number of odd digits, even digits and zeros in the integer
3. Print the prime numbers between 2 and the integer
4. Print the sum of the digits of the integer
5. Quit the program
PROGRAM PARTICULARS:
· When the program starts up, ask the user for the positive integer. After the user enters the positive integer, display the above menu. Remember the user can choose to do #2, #3, and #4 on the same number. Meaning, once you have the number from the user do not make the user enter a new number each time. The user can keep the same number until the user selects option 1. I have added a sample output of what the program should execute like. (See Below)
· There must be error checking on the input integer: if it is negative, the program will print an error message and re-prompt. This process will continue until valid input is entered. You may assume an integer of some form will be entered by the user.
· There must be error checking on the menu choice entered: if the user enters a choice not on the menu, the program will print an error message, re-display the menu and re-prompt. This process will continue until valid input is entered.
· No string (char arrays) or any other kinds of array variables are allowed.
· No built-in methods for integer manipulation are allowed. You may use pow( ) if you feel it is necessary.
· You may assume that no integer entered will be greater than the maximum integer size for type int.
· You must use functions for this assignment. The main function will be very small, it will contain function calls and a do while loop.
Explanation / Answer
please rate - thanks
#include <stdio.h>
int getnumber();
void digits(int);
void prime(int);
void sum(int);
int menu();
int main()
{int n,choice;
n=getnumber();
choice=menu();
do
{if(choice==1)
n=getnumber();
else if(choice==2)
digits(n);
else if(choice==3)
prime(n);
else
sum(n);
choice=menu();
}while(choice!=5);
return 0;
}
int getnumber()
{int n;
printf("Enter a non-negative integer: ");
scanf("%d",&n);
while(n<0)
{printf("Invalid entry ");
printf("Enter a non-negative integer: ");
scanf("%d",&n);
}
return n;
}
void digits(int n)
{printf("I've chosen not to do this function ");
}
void prime(int n)
{int line=0,factors,i,j;
printf("The prime numbers between 1 and %d are: ",n);
for(j=2;j<=n;j++)
{factors=0;
for(i=2;i<j;i++)
{if(j%i==0)
factors++;
}
if(factors==0)
printf("%d ",j);
}
printf(" ");
}
void sum(int n)
{int sum=0;
printf("the sum of the digits of %d is ",n);
while(n>0)
{sum+=(n%10);
n/=10;
}
printf("%d ",sum);
}
int menu()
{int n;
do
{
printf(" What would you like to do? ");
printf("1. Enter a new number ");
printf("2. Print the number of odd digits, even digits and zeros in the integer ");
printf("3. Print the prime numbers between 2 and the integer ");
printf("4. Print the sum of the digits of the integer ");
printf("5. Quit the program ");
printf("Enter a number 1-5: ");
scanf("%d",&n);
if(n<1||n>5)
printf("Invalid entry ");
}while(n<1||n>5);
return n;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.