Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 4

Workshop 4

Ex3: Implement a program to calculate the quadratic equation ax +bx+c=0. Ask the user to enter
2

three coefficients a, b, c. The program calculates and prints the solution to the screen.

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

void giaiPTBac2(float a, float b, float c);


int main(int argc, char *argv[]) {
float a, b, c;
    printf("Nhap he so bac 2, a = ");
    scanf("%f", &a);
    printf("Nhap he so bac 1, b = ");
    scanf("%f", &b);
    printf("Nhap hang so tu do, c = ");
    scanf("%f", &c);
    giaiPTBac2(a, b, c);
    return 1;
}
    
void giaiPTBac2(float a, float b, float c) {
    
    if (a == 0) {
        if (b == 0) {
            printf("Phuong trinh vo nghiem!");
        } else {
            printf("Phuong trinh co mot nghiem: x = %f", (-c / b));
        }
        return;
    }
   
    float delta = b*b - 4*a*c;
    float x1;
    float x2;
  
    if (delta > 0) {
        x1 = (float) ((-b + sqrt(delta)) / (2*a));
        x2 = (float) ((-b - sqrt(delta)) / (2*a));
        printf("Phuong trinh co 2 nghiem la: x1 = %f va x2 = %f", x1, x2);
    } else if (delta == 0) {
        x1 = (-b / (2 * a));
        printf("Phong trinh co nghiem kep: x1 = x2 = %f", x1);
    } else {
        printf("Phuong trinh vo nghiem!");
    }
}
Ex4: Implement a program. Input positive integer n, check if n is prime or not? Eg: 7 is prime, 9
is not prime.
#include <stdio.h>
#include <stdlib.h>

int checkprime(int n);


int main(){
    int n;
    printf("\nNhap n = ");
    scanf("%d", &n);
    checkprime(n);
    return 0;
    
}
int checkprime(int n){
   
    int i,key=0;
    for( i= 2; i <= n/2; i++){
        if(n%i == 0){
            key=1;
            break;
        }
    }
    if(key ==1){
        printf("\n%d khong phai la so nguyen to(it is not a prime number)", n);
    }else{
        printf("\n%d la so nguyen to(it is a prime number)", n);
    }
    return n;
}
Ex5: Implement a program . Enter a positive integer n, check if n is a perfect square number or
not? Eg. perfect squares: 1, 4, 9, 25, 36, 49,…

(Note: If the user enters an invalid number, the program will ask to re-enter it.)

#include <stdio.h>
#include <stdlib.h>

int squarecheck(int j);


int main() {
   
    int j;
   
    printf("\nNhap j = ");
    scanf("%d", &j);
    squarecheck(j);
   
    return 0;
}
int squarecheck(int j){
    int i = 0;
    goto c;
    a:
    printf("\nVui long nhap j lai= ");
    scanf("%d", &j);
    c:
    while(i*i <= j){
        if(i*i == j){
            printf("\n%d la so chinh phuong!(j is a perfect square number )\n", j);
            goto b;
        
        }
        ++i;
    }
    printf("\n%d khong phai so chinh phuong!(j is not a perfect square number )\n", j);
    goto a;
    b:
       
    return 0;
}

You might also like