A program can accept a two numbers then find the average and output the result of the two numbers


An average of set of numbers is their sum divided by their quantity. It can be defined as −

Show

average = sum of all values / number of values

Here we shall learn how to programmatically calculate average.

Algorithm

Algorithm of this program is very easy −

START Step 1 → Collect integer values in an array A of size N Step 2 → Add all values of A Step 3 → Divide the output of Step 2 with N Step 4 → Display the output of Step 3 as average STOP

Pseudocode

Lets write pseudocode for the driven algorithm −

procedure average() Array A Size N FOR EACH value i of A sum ← sum + A[i] END FOR average = sum / N DISPLAY average end procedure

Implementation

Implementation of this algorithm is given below −

#include <stdio.h> int main() { int i,total; int a[] = {0,6,9,2,7}; int n = 5; total = 0; for(i = 0; i < n; i++) { total += a[i]; } printf("Average = %f\n", total/(float)n); return 0; }

Output

Output of the program should be −

Average = 4.800000

mathematical_programs_in_c.htm

This program takes in two integers x and y as a screen input from the user.

The sum and average of these two integers are calculated and outputted using the cout command.

Example:

#include <iostream> using namespace std; int main(){ int x,y,sum; float average; cout << "Enter 2 integers : " << endl; cin>>x>>y; sum=x+y; average=sum/2; cout << "The sum of " << x << " and " << y << " is " << sum << "." << endl; cout << "The average of " << x << " and " << y << " is " << average << "." << endl; }

Program Output:

A program can accept a two numbers then find the average and output the result of the two numbers

Explanation:

In this program, you will get to know about how within a program, you take two integer variables and find the sum and average of these two integer variables and display the output. So, first of all, you have to include the iostream header file using the "include" preceding by # which tells that hat the header file needs to be process before compilation, hence named preprocessor directive. And then, for removing naming conflict you can use namespace statement within a program.

Next is int main(). As you know that all the execution of any C++ program starts from main() function. So the main() function is defined with return type as integer. Now, you have to take three integer type variables name 'x', 'y' and 'sum'. Then you have to take another floating type variable name 'average'. Now using the cout statement prints the message "Enter 2 integers : " Next the cin statement takes the 2 values from the user and put them in x and y respectively. Then the addition of x and y is assigned to variable 'sum'. Also, the average variable is assigned with the value of sun divided by two (which is the formula for mean of two numbers).

Then the cout statements are use to display the calculated value of sum and average. The system("PAUSE") is used to pause the console of the system. And finally the return 0; statement is used to return an integer type value back to main().

A program can accept a two numbers then find the average and output the result of the two numbers
report this ad


Page 2

A right-angled triangular array of natural numbers is called Floyd triangle.

Building a Floyd's triangle in C++ is quite easy, but you must have the understanding of how for loop works.

Example:

#include <iostream> using namespace std; int main() { int n, i, c, a = 1; cout << "Enter the number of rows of Floyd's triangle to print: "; cin >> n; for (i = 1; i <= n; i++) { for (c = 1; c <= i; c++) { cout << a; a++; } cout << endl; } return 0; }

Program Output:

A program can accept a two numbers then find the average and output the result of the two numbers

Explanation:

This program is used to print the right angled Floyd's triangle. For this you just need to understand the for-loop. So, first of all, you have to include the iostream header file using the "include" preceding by # which tells that the header file needs to be process before compilation, hence named preprocessor directive. Now, for removing naming conflict you can use namespace statement within a program.

Then the main() function is declared with return type as integer. Now you have to declare four integer type variables name as 'n', 'i', 'c' and 'a' and assign the value of 'a' as 1. Then the cout statement is used to display the message - "Enter the number of rows of Floyd's triangle to print: ". The cin statement eventually takes the value from the user and stores it in variable n.

Now, you have to use 2 for-loops in the form of nested for. First for loop will count using the variable i as 1and checks the condition i <= n. Again the second nested for-loop goes from c=1 till c <=i.

Then the cout statement is used within nested for to print all the numbers and increments the value of a by 1. After the inner loop gets executed, the cout takes the printing cursor to the next line.

A program can accept a two numbers then find the average and output the result of the two numbers
report this ad


Page 3

It is a basic C++ program that sums two given numbers. It takes two integer inputs from the user and performs arithmetic operations on them. Next, it assigns the output to a variable and then prints it on the screen.

Example C++ Program:
#include <iostream> using namespace std; int main() { /* Variable Declaration and Initialization. */ int input1, input2, output; /* Taking user input */ cout << "Please enter two integer: "; cin >> input1 >> input2; /* Adding the two numbers */ output = input1 + input2; /* Prints the output to the screen */ cout << input1 << " + " << input2 << " = " << output; return 0; }

Program Output:

Please enter two integer: 10 5 10 + 5 = 15
A program can accept a two numbers then find the average and output the result of the two numbers
report this ad


Page 4

This C++ program finds the average of the numbers given by the user. It takes the input from the user on how many numbers have to get the average; Then, it takes the input of those numbers. After receiving the information, it divides the sum of the numbers by the count of the numbers. Next, it assigns the output to a variable and then prints it on the screen.

#include <iostream> using namespace std; int main() { /* Variable Declaration and Initialization. */ int n, i; float num[100], sum = 0.0, average; /* Taking user input */ cout << "How many numbers do you want to get the average of? "; cin >> n; /* Validation - The number must be within the range */ while (n > 100 || n <= 0) { cout << "Please enter numbers in the range (1 to 100)." << endl; cout << "Enter the number again: "; cin >> n; } /* Taking user input */ for (i = 0; i < n; ++i) { cout << i + 1 << ". Enter a number: "; cin >> num[i]; sum += num[i]; } /* Find avarage */ average = sum / n; /* Prints the output to the screen */ cout << "Average = " << average; return 0; }

Program Output:

How many numbers do you want to get the average of? 150 Please enter numbers in the range (1 to 100). Enter the number again: 5 1. Enter a number: 1 2. Enter a number: 2 3. Enter a number: 3 4. Enter a number: 4 5. Enter a number: 5 Average = 3
A program can accept a two numbers then find the average and output the result of the two numbers
report this ad