You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
// Import the standard math library.
import "math.h";// Define a generic function that calculates the average of two values.Taverage<T>(Ta,Tb){return(a+b)/2;}// Define a struct that represents a 2D point.structPoint{x:f32;y:f32;}// Define a function that calculates the distance between two points.f32distance(Pointa,Pointb){vardx=a.x-b.x;vardy=a.y-b.y;returnsqrt(dx*dx+dy*dy);}// Define a class that represents a circle.classCircle{// Define a private field to store the circle's radius.privateradius:f32;// Define a constructor that initializes the circle's radius.constructor(radius:f32){this.radius=radius;}// Define a method that calculates the area of the circle.area(): f32 {returnthis.radius*this.radius*PI;}// Define a method that calculates the circumference of the circle.circumference(): f32 {return2*this.radius*PI;}}voidmain(){// Use the `average` function to calculate the average of two numbers.vara=average(3,7);varb=average(5.5,8.5);// Print the results of the averages to the console.println(a);// 5println(b);// 7// Create two points and calculate their distance.varp1=Point{x:0.0,y:0.0};varp2=Point{x:3.0,y:4.0};vard=distance(p1,p2);// Print the distance to the console.println(d);// 5// Create a circle with radius 2.0 and calculate its area and circumference.varcircle=Circle(2.0);vararea=circle.area();varcircumference=circle.circumference();// Print the area and circumference to the console.println(area);// 12.566371println(circumference);// 12.566371}
// Import the standard input/output library.
import "stdio.h";// Define a function that calculates the factorial of a number.intfactorial(intn){if(n<=1){return1;}returnn*factorial(n-1);}// Define a function that checks if a number is prime.boolisPrime(intn){if(n<2){returnfalse;}for(vari=2;i<n;i++){if(n%i==0){returnfalse;}}returntrue;}voidmain(){// Read a number from the user.print("Enter a number: ");varn=0;scanf("%d",&n);// Calculate and print the factorial of the number.varfact=factorial(n);printf("%d! = %d\n",n,fact);// Check if the number is prime and print the result.varprime=isPrime(n);printf("%d is%s prime.\n",n,prime?"":" not");}
The text was updated successfully, but these errors were encountered:
Here are a few examples generated by ChatGPT:
The text was updated successfully, but these errors were encountered: