Plus Minus Program in C Language
Plus minus program in C language.
In this article, you will learn how to make a plus-minus program in C language.
Example
Enter the size of the array::
6
Enter the 6 elements of the array::
4 1 -4 3 -9 0
0.500000
0.333333
0.166667
You should have knowledge of the following topics in c programming to understand this program:
- C
main()
function - C
printf()
function - C
while
loop
Source Code
// Plus Minus Program in C Language
#include <stdio.h>
int main() {
int s=0;
printf("Enter the size of the array::\n");
scanf("%d", &s);
float arr[s];
printf("Enter the %d elements of the array::\n", s);
// It will read the inputs of the array's elements + calculate the plus-minus values
int i=0;
float r1=0,r2=0,r3=0;
while (i<s) {
scanf("%f", &arr[i]);
if (arr[i] > 0) r1++;
if (arr[i] < 0) r2++;
if (arr[i] == 0) r3++;
i++;
}
r1 = r1/s;
r2 = r2/s;
r3 = r3/s;
// It will print the final output
printf("\n\n%0.6f\n%0.6f\n%0.6f\n", r1,r2,r3);
return 0;
}
Output
Enter the size of the array::
6
Enter the 6 elements of the array::
4 1 -4 3 -9 0
0.500000
0.333333
0.166667
Explanation
In this given program we have taken the inputs size of the array 6
and the array's elements 4, 1, -4, 3, -9, 0
. Then we applied the standard formula to calculate the plus-minus values of the given inputs.
After the whole calculation above program will return the 0.500000, 0.333333, 0.166667
output of the program.