Question

This is a c++ question we are not using namespace std at the top Subtraction +...

This is a c++ question

we are not using namespace std at the top

Subtraction + Decision and Loop

This is a twist on the previous exercise that will help you review loops and decision structures. You will again ask the user to enter two numbers. However, you will ALWAYS subtract the smaller number from the larger number to ensure that you never get a negative number for an answer. You do this by checking the numbers and switching them if they are not in the right order (larger then smaller). All of this checking, switching, subtracting, and output of the answer should occur in a function.

((( THIS WAS THE PREVIOUS EXERCISE

Function Basics - Arguments and Parameters

This is another easy exercise to test your knowledge of the fundamentals. In main(), ask the user to enter two integers. Pass those two integers to a function that will subtract one number from another. This function must also output the answer to the user.

Output:

Enter two integers (separated by a space) and this program will subtract them: [user enters: 5 7]

5 - 7 = -2

Notes and Hints:

1) From now on, you must use a function prototype for all programs that use functions. Don't expect me to ask for it in each exercise. )))

Finally, ask the user if they would like to run the program again. By now, you should know exactly what type of loop to use.

Output:

Enter two integers (separated by a space) and this program will subtract the smaller from the larger: [user enters: 7 5]

7 - 5 = 2
Do you want to run this program again? [user enters: y]

Enter two integers (separated by a space) and this program will subtract the smaller from the larger:   [user enters: 5 7]

7 - 5 = 2
Do you want to run this program again? [user enters: n]

Notes and Hints:

1) As always, make sure you accept an upper or lower case 'Y'

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include<iostream>
void subtract(int x,int y)
{
int sub;
if(x<y) //if it's true then swap the values of x and y
{
int t = x;
x=y;
y=t;
}
sub = x-y;
printf("\nAbsolute subtraction of %d and %d is: %d",x,y,sub);
}
int main()
{
int a,b;
char c='y';
while(c=='y' || c=='Y') //loops until user provides something else other than 'y' or 'Y'
{
printf("\nEnter two integers:\n");
scanf("%d",&a);
scanf(" %d",&b);
subtract(a,b);
printf("\nDo you want to continue?");
scanf(" %c",&c); //this loop continues if c is either 'y' or 'Y'
}
}

OUTPUT:

Enter two integers: Absolute subtraction of 2 and 1 is: 1 Do you want to continue?y Enter two integers: Absolute subtraction

Add a comment
Know the answer?
Add Answer to:
This is a c++ question we are not using namespace std at the top Subtraction +...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • This is a C++ question we do not use namespace std at the beginning of the...

    This is a C++ question we do not use namespace std at the beginning of the program 9. Arrays: Functions and Reading From File Although this code is a little long, the strategy is straightforward and it will really help you practice functions. I have provided a large amount of detail to help you out. In a nutshell, you're going to read some numbers from a file (into an array) and then ask the user for a number that will...

  • C Programming // only using #include<stdio.h> #include<stdlib.h> A rational number is any number that can be...

    C Programming // only using #include<stdio.h> #include<stdlib.h> A rational number is any number that can be expressed as a fraction of two integers. Write a program that reads a rational number from the user and display it as a mixed numeral. Example run 1: Enter the numerator and denominator separated by a space> 22 7 22/7 = 3 1/7 Example run 2: Enter the numerator and denominator separated by a space> 3 4 3/4 = 0 3/4

  • This is a c++ question note: not using  namespace std; at the beginning of the program Writing...

    This is a c++ question note: not using  namespace std; at the beginning of the program Writing Data to a File This program will write a series of letters, starting with 'A', to an external file (letters.txt). The user will decide how many letters in total get saved to the file. ** IMPORTANT: The test cases will evaluate your code against .txt files that I uploaded. You do NOT have to upload your own txt files. Input: Including 'A', how many...

  • #include <iostream> using namespace std; int main() {    } Write a program that, given a...

    #include <iostream> using namespace std; int main() {    } Write a program that, given a user-specified integer, prints out the next 10 integers (in increasing order; each on its own line). For example, given an input of 3, the console content following a run of the completed program will look as follows: Enter a number: 3 4 5 6 7 8 9 10 11 12 13

  • In this program, you will be using C++ programming constructs, such as overloaded functions. Write a...

    In this program, you will be using C++ programming constructs, such as overloaded functions. Write a program that requests 3 integers from the user and displays the largest one entered. Your program will then request 3 characters from the user and display the largest character entered. If the user enters a non-alphabetic character, your program will display an error message. You must fill in the body of main() to prompt the user for input, and call the overloaded function showBiggest()...

  • use C++ please 1. Vowels and Consonants Write a program that asks the user to input...

    use C++ please 1. Vowels and Consonants Write a program that asks the user to input three different integers. Write a function called numberStyle for this program that will accept each integer (one at a time) and return the following . If the integer is even, return 1 . If the integer is odd, return -1 . If the integer is zero, return O In main, after calling the function output the appropriate message describing the integers ing the function...

  • 1. Write a C++ program to output a framed greeting. The program will produce five lines...

    1. Write a C++ program to output a framed greeting. The program will produce five lines of output. The first line begins the frame. It is a sequence of * characters as long as the person's name, plus some characters to match the salutation ("Hello, "), plus a space and an * at each end. The line after that will be an appropriate number of spaces with an * at each end. The third line is an *, a space,...

  • Complete the Python program below that performs the following operations. First prompt the user to input...

    Complete the Python program below that performs the following operations. First prompt the user to input two integers, n and m. If n<m, then print the odd positive integers that are less than m (in order, on a single line, separated by spaces). If man, then print the even positive integers that are less than n (in order, on a single line, separated by spaces). If neem, then print nothing. For instance, if the user enters 5 followed by 10,...

  • Write a C++ program that repeatedly collects positive integers from the user, stopping when the user...

    Write a C++ program that repeatedly collects positive integers from the user, stopping when the user enters a negative number or zero. After that, output the largest positive number entered. A sample run should appear on the screen like the text below. Enter a number: 3 Enter a number: 10 Enter a number: 2 Enter a number: -213 Output: The largest positive number you entered was 10.

  • write in python Create a program that will ask the user for a list of integers,...

    write in python Create a program that will ask the user for a list of integers, all on one line separated by a slash. construct a list named "adj_numbers" which contains the users input numbers after subtracting 2 from each number. then print the resulting list. the input statement and print statement are given to you in the starting code. a sample run of the program is as follows: Enter a list of integers, separated by slashes: 7/3/6/8 resulting list:...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT