I need a program written in c
- User
I need a program written in c
- Assistant
Here is a basic example of a C program that takes input from the user and calculates the sum and average of two numbers.
c#include <stdio.h> int main() { int num1, num2, sum; float average; printf("Enter first number: "); scanf("%d", &num1); printf("Enter second number: "); scanf("%d", &num2); // Calculate the sum sum = num1 + num2; // Calculate the average average = (float)sum / 2; // Output results printf("Sum of the two numbers: %d\n", sum); printf("Average of the two numbers: %.2f\n", average); return 0; }To compile and run this program:
- Save the code in a file called
sum_and_average.c. - Open a terminal/command prompt.
- Navigate to the folder containing the source file.
- If you are using GCC (GNU Compiler Collection) from the command line, compile the program by entering .
- Save the code in a file called
- User
I need a program in c that calculates the first round of euroleague with teams that reads from a txt file
- Assistant
- User
write a program in c that opens a txt file, then reads its contents, checks if the file opened and then closes it
- Assistant
Here's a simple C program that opens a text file, reads its contents, checks if the file is opened and then closes it.
c#include <stdio.h> int main() { FILE *file; char filename[] = "example.txt"; int buffer; // Open the file file = fopen(filename, "r"); // Check if the file opened successfully if (file == NULL) { printf("Could not open the file: %s\n", filename); return 1; } else { printf("File opened successfully: %s\n", filename); } // Read and display the file content printf("File content:\n"); while ((buffer = fgetc(file)) != EOF) { putchar(buffer); } // Close the file fclose(file); return 0; } - User
write a program in c that finds if the length of the sides of a tringle are a pythagorean triple
- Assistant
Here is a program in C that takes the length of three sides of a triangle as input and determines if they form a Pythagorean triple:
c#include <stdio.h> int is_pythagorean_triple(int a, int b, int c) { if (a * a + b * b == c * c || a * a + c * c == b * b || b * b + c * c == a * a) { return 1; } else { return 0; } } int main() { int side1, side2, side3; // Input three sides printf("Enter the length of the first side: "); scanf("%d", &side1); printf("Enter the length of the second side: "); scanf("%d", &side2); printf("Enter the length of the third side: "); scanf("%d", &side3); // Check if the sides form a Pythagorean triple int result = is_pythagorean_triple(side1, side2, side3); if (result) { printf("The given sides form a Pythagorean triple.\n"); } else { printf("The given sides do not form a Pythagorean triple.\n"); } return 0; }