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

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.
1/Write a C program that asks the user to enter 5 nums and adds each number...
1/Write a C program that asks the user to enter 5 nums and adds each number to a total. The program should then display the total. To add to the total, the program must call the function void sum(int value, int *ptotal) passing the value the user entered and the address of the total (a local variable in main). The function should add the value to the total. Note that the function is void and does not return anything. Don't...
a). Write a program that asks the user to enter an integer N and prints two...
a). Write a program that asks the user to enter an integer N and prints two integers, root and power, such that 1 < power < 6 and N = root ** power. If no such pair of integers exists, it should print a message to that effect. There are two loops, one for power and one for root. Order the loops so that if N = 64, then your program find that N = 8 ** 2 rather than...
in C++, Write a program that asks the user to enter 6 numbers. Use an array...
in C++, Write a program that asks the user to enter 6 numbers. Use an array to store these numbers. Your program should then count the number of odd numbers, the number of even numbers, the negative, and positive numbers. At the end, your program should display all of these counts. Remember that 0 is neither negative or positive, so if a zero is entered it should not be counted as positive or negative. However, 0 is an even number....
Write an interactive C program that asks a user to enter a sentence from the keyboard,...
Write an interactive C program that asks a user to enter a sentence from the keyboard, and prints the sentence with all the words spelled backwards. Note: you may assume that each sentence will have at most 50 characters and each word is separated by a space. Sample code execution: bold information entered by user enter a sentence: hello ece 175 class olleh ece 571 ssalc continue (q to quit)? y enter a sentence: May the force be with you...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT