In: Computer Science
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
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