Question

In: Computer Science

Write a C program that asks the user to enter any two integernumbers, and each...


Write a C program that asks the user to enter any two integer numbers, and each number consists of four-digits. Your program should check whether the numbers are four digits or not and in case they are not a four digit number, the program should print a message and exit, otherwise it should do the following:

Print a menu as follows:

Select what you want to do with the number 1-3:

1- Print Greatest Common Divisor (GCD) of the two numbers.

2- Print sum of odd digits of each number.

3- Print relation of odd sum of digits larger/smaller/equal.

The user should enter a value 1 to 3 which should be processed using a switch statement.

Your program should include at least two functions (you may use more):

1- Function find_GCD which takes a two integer numbers and returns the GCD number.

2- Function find_odd_sum which takes a four-digit number and returns the sum of its odd digits.

VERY IMPORTANT:

1. Turn in your assignment by replying to the course coordinator’s assignment on ITC and attaching your code file (main.c).

2. You must include your full name, student id number, and lab section number in a comment at the beginning of your main.c code file.

Example of a Sample Runs:

Sample run 1:

Enter any two four digit numbers

1348295643

134829 is not a four digit number, goodbye

Sample run 2:

Enter any two four digit numbers

12356743

Select what you want to do with the number 1-3:

1- Print the GCD.

2- Print sum of odd digits for the two numbers.

3- Print relation of odd sum of digits larger/smaller/equal

2

Sum of odd digits of 1235 is 9

Sum of odd digits of 6743 is 10

Sample run 3:

Enter any four digit number

18312145

Select what you want to do with the number 1-3:

1- Print the GCD.

2- Print sum of odd digits for the two numbers.

3- Print relation of odd sum of digits larger/smaller/equal

3

Sum of odd digits in 1831 = 5 is less than sum of odd digits in 2145 = 6

Sample run 4:

Enter any four-digit number

21323612

Select what you want to do with the number 1-3:

1- Print the GCD.

2- Print sum of odd digits for the two numbers.

3- Print relation of odd sum of digits larger/smaller/equal

3

Sum of odd digits in 2132 is equal to sum of odd digits in 3612 = 4

Solutions

Expert Solution

Code:

#include 
#include 

int find_GCD(int a, int b)
{
    if (b == 0)
        return a;
    return find_GCD(b, a % b); 
}

int find_odd_sum(int a){
  int sum=0;
  int count=1;
  int digit=0;
  while(a>0){
    digit = a%10;
    if(count%2!=0)sum=sum+digit;
    a=a/10;
    count++;
  }
  return sum;
}

int main() {
  int a, b;
  printf("Enter any two four digit numbers\n");
  scanf("%d%d",&a,&b);
  if(a<1000 || a>9999){
    printf("%d is not a four digit number, goodbye",a);
    exit(0);
  }else if(b<1000 || b>9999){
    printf("%d is not a four digit number, goodbye",b);
    exit(0);
  }else{
    printf("Select what you want to do with the number 1-3:\n");
    printf("1- Print the GCD.\n2- Print sum of odd digits for the two numbers.\n3- Print relation of odd sum of digits larger/smaller/equal\n");
    int choice;
    scanf("%d",&choice);
    switch(choice){
      case 1:
            printf("The GCD of %d and %d is %d\n",a,b,find_GCD(a,b));
            break;
      case 2:
            printf("Sum of odd digits of %d is %d\n",a,find_odd_sum(a));
            printf("Sum of odd digits of %d is %d\n",b,find_odd_sum(b));
            break;
      case 3:
            if(a>b){
              printf("Sum of odd digits in %d = %d is less than sum of odd digits in %d = %d",a,find_odd_sum(a),b,find_odd_sum(b));
            }else if(a


Related Solutions

Write a program that asks the user to enter the total precipitation for each of the...
Write a program that asks the user to enter the total precipitation for each of the twelve months of the year into a list. The program should calculate and display the total precipitation for the year, the average monthly precipitation, and the months with the highest and lowest precipitation amounts. FYI: precipitation is measured in inches. Assume that precipitation amounts are floating-point numbers. You will need a list to store the precipitation amounts and another list to store the names...
Question Write a C program that asks the user to enter two integers x and n....
Question Write a C program that asks the user to enter two integers x and n. Then the program computes xn (=x * x * x …… (n times)) using for loop. and give me an output please use printf and scanf #include int main(void) {     //Declare required variables             //read two integers x , n from the keyboard                 //compute xn using for loop                     printf("< Your name >\n");...
Write a C++ program that asks the user to enter the monthly costs for the following...
Write a C++ program that asks the user to enter the monthly costs for the following expenses incurred from operating your automobile: loan payment, insurance, gas, oil, tires, and maintenance. The program should then display the total monthly cost of these expenses, and a projected total annual cost of these expenses. Label each cost. The labels should be left aligned and have a column width of 30 characters. The cost should be aligned right and displayed with two decimal places...
Write a C program that loops and asks a user to enter a an alphanumeric phone...
Write a C program that loops and asks a user to enter a an alphanumeric phone number and converts it to a numeric one. No +1 at the beginning. You can put all code in one quiz1.c file or put all functions except main in phone.c and phone.h and include it in quiz1.c Submit your *.c and .h files or zipped project */ #pragma warning (disable: 4996) //windows #include <stdio.h> #include <string.h> #include <stdbool.h> #include <ctype.h> enum { MaxLine =...
C++ write a program that asks the user to enter the hours and rate then calculate...
C++ write a program that asks the user to enter the hours and rate then calculate the gross pay for an employee, the program should test if the hours are regular (40), any hour more than 40 should be paid with the overtime rate: 1.5*rate. The program should ask repeatedly the user if he/she wants to continue: y or n, if the user types y, then the program should ask for the hours and rate for another employee then display...
write a program in c++ that asks the user to enter their 5 test scores and...
write a program in c++ that asks the user to enter their 5 test scores and calculates the most appropriate mean. Have the results print to a text file and expected results to print to screen.
In the space provided below write a C++ program that asks the user to enter their...
In the space provided below write a C++ program that asks the user to enter their quarterly earnings for the past two years stores the data in a 2-dimensional array. The program then computes both the annual earnings as well as the total earning and prints the results along with the 2-dimensional array on screen as well as onto a file.
C++. Write a program that asks the user to enter a single word and outputs the...
C++. Write a program that asks the user to enter a single word and outputs the series of ICAO words that would be used to spell it out. The corresponding International Civil Aviation Organization alphabet or ICAO words are the words that pilots use when they need to spell something out over a noisy radio channel. See sample screen output for an example: Enter a word: program Phonetic version is: Papa Romeo Oscar Golf Romeo Alpha Mike The specific requirement...
Write a C++ program that asks the user to enter in three numbers and displays the...
Write a C++ program that asks the user to enter in three numbers and displays the numbers in ascending order. If the three numbers are all the same the program should tell the user that all the numbers are equal and exits the program. Be sure to think about all the possible cases of three numbers. Be sure to test all possible paths. Sample Runs: NOTE: not all possible runs are shown below. Sample Run 1 Welcome to the order...
In the space provided below write a ******C program********* that asks the user to enter their...
In the space provided below write a ******C program********* that asks the user to enter their quarterly earnings for the past two years stores the data in a 2-dimensional array. The program then computes both the annual earnings as well as the total earning and prints the results along with the 2-dimensional array on screen. Using the embed icon shown above, also include screenshots demoing the execution of your program. Please write carefully. Thank you
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT