Type the code below into Visual Studio and run it. Describe what is happening he
ID: 3808446 • Letter: T
Question
Type the code below into Visual Studio and run it. Describe what is happening here. Now, add a new function in this program. Perhaps you want to subtract the two numbers or multiply them, or find the greater or lesser than values. Make sure to upload your code and that it contains the original add function as well as the one you created.
#include <stdio.h>
int add(int x, int y); // function prototype
int main()
{
int a, b;
int sum; //declares the variables used in the program
printf(“Enter the first number :”); //prompt to input the first number
scanf(“%d”, &a); // stores the first number in variable a
printf(“Enter the second number:”); //prompt to input the first number
scanf(“%d, &b): // stores the second number in variable b
sum = add(a,b); //function call
printf(“The sum of two numbers is: %d ”, sum); //displays the sum after executing add function
printf(“The value stored in the variable sum is: %d ”,sum); //displays the value store in the variable
sum
return 0;
}
int add(int x, int y) //function definition
{
Return x +y;
}
Explanation / Answer
Hi
I have fixed all compilation issues and added new method for finding diference.
#include <stdio.h>
int add(int x, int y); // function prototype
int main()
{
int a, b;
int sum, diff; //declares the variables used in the program
printf("Enter the first number :"); //prompt to input the first number
scanf("%d", &a); // stores the first number in variable a
printf("Enter the second number:"); //prompt to input the first number
scanf("%d", &b); // stores the second number in variable b
sum = add(a,b); //function call
printf("The sum of two numbers is: %d ", sum); //displays the sum after executing add function
printf("The value stored in the variable sum is: %d ",sum); //displays the value store in the variable
diff = subtract(a,b); //function call
printf("The difference of two numbers is: %d ", diff); //displays the diff after executing add function
return 0;
}
int add(int x, int y) //function definition
{
return x +y;
}
int subtract (int x, int y) //function definition
{
return x -y;
}
Output:
sh-4.2$ gcc -o main *.c
sh-4.2$ main
Enter the first number :4
Enter the second number:2
The sum of two numbers is: 6
The value stored in the variable sum is: 6
The difference of two numbers is: 2
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.