Source Code: C Program To Calculate Circumference of Circle
#include<stdio.h>
int main()
{
const float PI = 3.14;
float r, c;
printf("Enter Radius of the Circle\n");
scanf("%f", &r);
c = 2 * PI * r;
printf("Circumference of the Circle is %f\n", c);
return 0;
}
Output 1: Enter Radius of the Circle 10 Circumference of the Circle is 62.800003
Output 2: Enter Radius of the Circle 41 Circumference of the Circle is 257.480011
User Input: Enter Radius and Height of the Cylinder 2 5
Output: Volume of Cylinder is 62.831856
Logic To Calculate Volume of Cylinder
We ask the user to enter values for radius and height of the Cylinder. If user enters 2m and 5m. Then we use the formula to calculate the Volume of Cylinder:
volume = (PI x Radius2) x height;
User input: radius = 2m; height = 5m;
PI value is 3.14159265359;
volume = PI x Radius2 x height; volume = 3.14 x (2m)2 x (5m); volume = 3.14 x 4(m)2 x 5(m); volume = 3.14 x 20(m)3; volume = 62.8 m3;
So volume of cylinder is 62.8 cubic meter.
Video Tutorial: C Program To Calculate Volume of Cylinder
Source Code: C Program To Calculate Volume of Cylinder
#include<stdio.h>
int main()
{
const float PI = 3.14159265359;
float r, h, volume;
printf("Enter Radius and Height of the Cylinder\n");
scanf("%f%f", &r, &h);
volume = PI * r * r * h;
printf("Volume of Cylinder is %f\n", volume);
return 0;
}
Output 1: Enter Radius and Height of the Cylinder 2 5 Volume of Cylinder is 62.831856
Output 2: Enter Radius and Height of the Cylinder 2 3 Volume of Cylinder is 37.699112
Given the coordinates(cx, cy) of center of a circle and its radius, write a C program that will determine whether a point(x, y) lies inside the Circle, on the Circle or outside the Circle. (Hint: Use sqrt() and pow() functions)
Note: Center Point – (cx, cy); We need to find the position of point (x, y);
Logic To Check whether Point Lies Inside, Outside or On The Circle
First we need to calculate the distance of the point(x, y) from the center(cx, cy) of the circle. Next we need to compare the distance with the radius of the Circle.
Conditions To Determine The Position of the Point(x, y) 1. Distance is greater than radius: point is outside the Circle. 2. Distance is less than radius : point is inside the Circle. 3. Distance is equal to the radius: point is on the Circle.
Source Code: C Program To Check If Point Lies Inside, Outside or On The Circle
#include < stdio.h >
#include < math.h >
int main()
{
float cx, cy, radius, x, y, distance;
printf("Enter the center point(cx, cy)\n");
scanf("%f%f", &cx, &cy);
printf("Enter radius of the circle\n");
scanf("%f", &radius);
printf("Enter the point(x, y) to check its position\n");
scanf("%f%f", &x, &y);
distance = sqrt( pow( (x - cx), 2 ) + pow( (y - cy), 2 ) );
if(distance < radius)
{
printf("Point (%0.2f, %0.2f) is inside the Circle\n", x, y);
}
else if(distance > radius)
{
printf("Point (%0.2f, %0.2f) is outside the Circle\n", x, y);
}
else
{
printf("Point (%0.2f, %0.2f) is on the Circle\n", x, y);
}
return 0;
}
Output 1: Enter the center point(cx, cy) 0 0 Enter radius of the circle 6 Enter the point(x, y) to check its position 2 2 Point (2.00, 2.00) is inside the Circle
Output 2: Enter the center point(cx, cy) 0 0 Enter radius of the circle 6 Enter the point(x, y) to check its position 12 6 Point (12.00, 6.00) is outside the Circle
Here I’m drawing a circle with a red fill and incrementing it’s x-axis value each time it is called.
JavaScript file: x-axis movement control myScript.js
1
2
3
4
5
var r = 50;
var x = -r;
if( x >= context.canvas.width + r)
x = -r;
var r = 50;
var x = -r;
if( x >= context.canvas.width + r) x = -r;
Here the x-axis value is tracked each time and is set back to -50 ( minus radius of the circle ) once the circle moves out of the x-axis or the canvas width.
window.onload = canvas;
function canvas()
{
var myCanvas = document.getElementById("myCanvas");
if( myCanvas && myCanvas.getContext("2d") )
{
var context = myCanvas.getContext("2d");
var r = 50;
var x = -r;
setInterval(function(){
context.fillStyle = "#000";
context.fillRect(0, 0, context.canvas.width, context.canvas.height);
context.beginPath();
context.fillStyle = "red";
context.arc(x++, context.canvas.height/2, r, 0, 2 * Math.PI);
context.fill();
if( x >= context.canvas.width + r)
x = -r;
}, 10);
}
}
window.onload = canvas;
function canvas()
{
var myCanvas = document.getElementById("myCanvas");
if( myCanvas && myCanvas.getContext("2d") )
{
var context = myCanvas.getContext("2d");
var r = 50;
var x = -r;
setInterval(function(){
context.fillStyle = "#000";
context.fillRect(0, 0, context.canvas.width, context.canvas.height);
context.beginPath();
context.fillStyle = "red";
context.arc(x++, context.canvas.height/2, r, 0, 2 * Math.PI);
context.fill();
if( x >= context.canvas.width + r) x = -r;
}, 10);
}
}
Here we use the setInterval() method to iterate/loop through the anonymous function and each time the background of the canvas is set to black(#000 or #000000) by drawing a black rectangle across the entire canvas width and height. For every call the value of x increments by 1, hence the circle moves 1px away from its previous position, until it reaches the end of canvas – after which it is reset back to -r value.