Create the following program in Visual Studio in C. Write a small program that c
ID: 3603259 • Letter: C
Question
Create the following program in Visual Studio in C.
Write a small program that calculates the sum and the difference of two integers with three user-defined functions:
//takes two integer arguments and returns the sum of the two arguments
int CalculateSum (int num1, int num2);
//takes two integer arguments and returns the difference of the two arguments
int CalculateDifference(int num1, int num2);
//takes two integer arguments and two integer pointer arguments
//1. calculate the sum and stores the result in sumptr
//2. calculate the difference and store the result in diffPtr
void CalculateBoth(int num1, int num2, int*sumPtr, int *diffPtr);
Call all three functions from main.
Print all the results inside the function definitions (value at) and ALSO print the results again back in the main function after the function has been called.
Main function logic (and hints):
1. Declare variables num1, num2, sum1, diff1, sum2, diff2
2. Ask and get 2 numbers from the user (num1 and num2)
3. Pass num1 and num2 to the function CalculateSum and it will return the sum to the variable sum1
4. Print sum1 onto the screen
5. Pass num1 and num2 to the function CalculateDifference and it will return the difference to the variable diif1
6.Print diff1 onto the screen
7. Pass num1 and num2 and “address of” sum2 and “address of” diff2 to the function CalculateBoth
8. Print sum2 and diff2 results onto the screen
Explanation / Answer
int checkPrimeNumber(int n);
int main()
{
int n1, n2, i, flag;
printf("Enter two positive integers: ");
scanf("%d %d", &n1, &n2);
printf("Prime numbers between %d and %d are: ", n1, n2);
for(i=n1+1; i<n2; ++i)
{
// i is a prime number, flag will be equal to 1
flag = checkPrimeNumber(i);
if(flag == 1)
printf("%d ",i);
}
return 0;
}
// user-defined function to check prime number
int checkPrimeNumber(int n)
{
int j, flag = 1;
for(j=2; j <= n/2; ++j)
{
if (n%j == 0)
{
flag =0;
break;
}
}
return flag;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.