Question

In: Computer Science

write a c program that asks the user to enter any two intger numbers and each...

write a c program that asks the user to enter any two intger numbers and each number consist 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 mnew as follows   

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

1-print grreatest 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

Solutions

Expert Solution

Code:-

#include<stdio.h>
#include<stdlib.h>
//Function to calculate gcd of two numbers
int gcd_of_numbers(int num1, int num2)
{
if (num2 == 0)
return num1;
return gcd_of_numbers(num2, num1 % num2);

}
//function to calculate odd digits sum of number
int odd_digit(int num){
   int odd_sum=0;
   int digit;
   int remainder;
   while(num>0){
       digit=num%10;//stores the each digit of number
       num=num/10;
       remainder=digit%2;//To know whether it is even or odd digit
       if(remainder==1)
           odd_sum+=digit;
   }
   return odd_sum;
}
int main(){

   int num1,num2;//variables for user input
   printf("Enter two integers that contain four digits\n");
   scanf("%d%d",&num1,&num2);
   if((num1>=1000&&num1<=9999) && (num2>=1000 && num2<=9999)){
       printf("Select what you want to do with numbers1-3\n");
       printf("1-print greatest common divisors of two numbers\n");
       printf("2-print sum of digits of each number\n");
       printf("3-print relation of odd sum of digits larger/smaller/equal\n");
       int choice;//variable for choosing option
      
       int odd_digit_sum_number1;
       int odd_digit_sum_number2;
       scanf("%d",&choice);
       switch(choice){
           case 1: printf("gcd of given two numbers is :%d\n",gcd_of_numbers(num1,num2));
                   break;
           case 2:printf("Sum of odd digits of first number: %d\n",odd_digit(num1));
                   printf("Sum of odd digits of second number: %d\n",odd_digit(num2));
                   break;
           case 3:
                   odd_digit_sum_number1=odd_digit(num1);//Call the function and store result in this variable
                   odd_digit_sum_number2=odd_digit(num2);
                   if(odd_digit_sum_number1>odd_digit_sum_number2)
                       printf("Number 1 odd digits sum= %d is larger than Number 2 odd digits sum= %d\n",odd_digit_sum_number1,odd_digit_sum_number2);
                   else if(odd_digit_sum_number1<odd_digit_sum_number2)
                       printf("Number 1 odd digits sum= %d is smaller than Number 2 odd digits sum= %d\n",odd_digit_sum_number1,odd_digit_sum_number2);
                   else
                       printf("Number 1 odd digits sum= %d is equal to Number 2 odd digits sum= %d\n",odd_digit_sum_number1,odd_digit_sum_number2);
                   break;
           default:
                   printf("Invalid Option");
       }
   }
   else{
       printf("The given user input does not contain four digits\n");
       exit(0);
   }

   return 0;
}

Output:-

Code Image:-


Related Solutions

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...
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 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 a C++ program that asks the user to enter a series of single-digit numbers with...
Write a C++ program that asks the user to enter a series of single-digit numbers with nothing separating them. Read the input as a C-string or a string object. The program should display the sum of all the single-digit numbers in the string. For example, if the user enters 2514, the program should display 12, which is the sum of 2, 5, 1, and 4. The program should also display the highest and lowest digits in the string. It is...
write a C program that asks the user to enter numbers and finds maximum among them
write a C program that asks the user to enter numbers and finds maximum among them
IN JAVA Write a complete program that asks the user to enter two real numbers from...
IN JAVA Write a complete program that asks the user to enter two real numbers from the console. If both numbers are positive print the product, if both numbers are negative print the quotient, otherwise print INVALID INPUT. Use a nested if; output should be to a dialog and console; use printf to format the console output (for real numbers specify the width and number of digits after the decimal). The output must be labeled. Follow Java conventions, indent your...
Write a C program that asks the user to enter 15 integer numbers and then store them in the array.
Write a C program that asks the user to enter 15 integer numbers and then store them in the array. Then, the program will find the second largest element in array and its index without sorting the array. For example, In this array {-55,-2,1, 2, -3, 0, 5, 9, 13, 1, 4, 3, 2, 1, 0}, the second largest element is 9 [found at index 7].
Write a program that asks the user to enter an array of random numbers, then sort...
Write a program that asks the user to enter an array of random numbers, then sort the numbers (ascending order), then print the new array, after that asks the user for a new two numbers and add them to the same array and keep the array organization. (c++ ) (using while and do while loops)
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");...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT