Tải bản đầy đủ (.docx) (5 trang)

GIẢI PHƯƠNG TRÌNH bậc HAI ỨNG DỤNG NGÔN NGỮ c

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (78.34 KB, 5 trang )

GIẢI PHƯƠNG TRÌNH BẬC HAI ỨNG DỤNG NGÔN NGỮ C
#include <stdio.h>
#include <math.h>
void main()
{
float a, b, c, delta;
printf("\nGiai phuong trinh bac hai AXý + BX + C = 0");
printf("\nCho biet ba he so A B C : ");
scanf("%f%f%f", &a, &b, &c);
delta = b * b - 4 * a * c;
if (delta<0)
printf("Phuong trinh vo nghiem");
else if (delta == 0)
printf("Phuong trinh co nghiem kep x1 = x2 = %f", -b/
(2*a));
else
{
printf("Phuong trinh co hai nghiem phan biet\nx1 = %f",
(-b + sqrt(delta))/(2*a));
printf("\nx2 = %f", (-b - sqrt(delta))/(2*a));
}
getch();
}
MA PHƯƠNG
#include <stdio.h>
#include <conio.h>
// func declaration
void matrix( int n );
// main()
int main(void)
{


int n;

// input until it's valid.
do
{
printf("\n Plz input size of matrix [ odd size & n
< 20 ]: n = ");
scanf("%d",&n);
if ( n % 2 == 0 ) printf("\n Invalid input value
Plz re-input \n");
}
while ( n % 2 == 0 );

if ( n > 20 ) { n = 19 ; // in case of n is greater
than 20
printf("\n %d is greater than 20 & set to be
default as 19 .",n ); } // end if

// call matrix()
matrix(n);
// stop to watch
getch();
return 0;
}
// function matrix(int n)
void matrix( int n )
{
int a[20][20];
int i, j, row, col, count = 1;
int old_row, old_col, sum = 0;


// set starting value of array
for ( i = 0 ; i < n ; i++ )
for ( j = 0 ; j < n ; j++ )
a[i][j] = 0;

// set the 1st value to start
row = 0; col = (n-1) / 2;

while ( count < n*n + 1 )
{
a[row][col] = count++ ; // set value for
elements
old_row = row ; old_col = col; // save the
last addresses
// define whether going out of array
row -= 1; if ( row == -1 ) row = n - 1;
col += 1; if ( col == n ) col = 0;
// in case of already having number
if ( a[row][col] != 0 )
{
row = old_row + 1;
col = old_col;
} // end if
} // end while
// print result
printf("\n");
for ( i = 0 ; i < n ; i++ )
{
for ( j = 0 ; j < n ; j++ )

printf("%4d",a[i][j]);
printf("\n");
} // end for

// calculate sum
for ( j = 0 ; j < n ; j++ )
sum += a[0][j];
printf("\n Sum of each row - column - diagonal
line is : %d " , sum);

return;
}

×