C Program to implement Linear regression algorithm.

Linear Regression  is the predicting the value of one scalar variable(y) using the explanatory another variable(x). Linear regression  is represented by the equation Y = a + bX, where X is the explanatory variable and Y is the scalar variable. The slope of the line is b, and a is the intercept. For linear list square modeling, Linear regression is very helpful. Read more about C Programming Language.

* You can use all the programs on www.c-program-example.com
* for personal and learning purposes. For permissions to use the
* programs for commercial purposes,
* contact [email protected]
* To find more C programs, do visit www.c-program-example.com
* and browse!
* Happy Coding

#include "stdio.h"
#include "conio.h"
#include "math.h"
#include "string.h"

float mean(float *a, int n);
void deviation(float *a, float mean, int n, float *d, float *S);

void main() {
float a[20], b[20], dx[20], dy[20];
float sy = 0, sx = 0, mean_x = 0, mean_y = 0, sum_xy = 0;
float corr_coff = 0, reg_coff_xy = 0, reg_coff_yx = 0;
char type_coff[7];
int n = 0, i = 0;


printf("Enter the value of n: ");
scanf("%d", &n);
printf("Enter the values of x and y:n");
for (i = 0; i < n; i++)
scanf("%f%f", &a[i], &b[i]);
mean_x = mean(a, n);
mean_y = mean(b, n);
deviation(a, mean_x, n, dx, &sx);
deviation(b, mean_y, n, dy, &sy);

for (i = 0; i < n; i++)
sum_xy = sum_xy + dx[i] * dy[i];
corr_coff = sum_xy / (n * sx * sy);
printf("Enter the type of regression coefficient as 'x on y' or 'y on x': ");

if (strcmp(type_coff, "x on y") == 1) {
reg_coff_xy = corr_coff * (sx / sy);
printf("nThe value of linear regression coefficient is %f",
} else if (strcmp(type_coff, "y on x") == 1) {
reg_coff_yx = corr_coff * (sy / sx);
printf("nThe value of linear regression coefficient is %f",
} else
printf("nEnter the correct type of regression coefficient.");

float mean(float *a, int n) {
float sum = 0, i = 0;
for (i = 0; i < n; i++)
sum = sum + a[i];
sum = sum / n;
return (sum);

void deviation(float *a, float mean, int n, float *d, float *s) {
float sum = 0, t = 0;
int i = 0;
for (i = 0; i < n; i++) {
d[i] = a[i] - mean;
t = d[i] * d[i];
sum = sum + t;
sum = sum / n;
*s = sqrt(sum);

Read more Similar C Programs
Learn C Programming

Simple C Programs

You can easily select the code by double clicking on the code area above. To get regular updates on new C programs, you can Follow @c_program You can discuss these programs on our Facebook Page. Start a discussion right now,

our page! Share this program with your Facebook friends now! by liking it

(you can send this program to your friend using this button) Like to get updates right inside your feed reader? Grab our feed!

To browse more C Programs visit this link
(c) www.c-program-example.com

Leave a Reply