C Language :-
Features of C Language
C is the widely used language. It provides many features
that are given below.
- Simple
- Machine Independent or Portable
- Mid-level programming language
- structured programming language
- Rich Library
- Memory Management
- Fast Speed
- Pointers
- Recursion
- Extensible
Let's
understand through an example.
int
main()
{ printf("Hello javaTpoint");
return 0;
}
In the
above flow diagram, the following steps are taken to execute a program:
- Firstly, the input file, i.e., hello.c, is passed to the preprocessor, and the preprocessor converts the source code into expanded source code. The extension of the expanded source code would be hello.i.
- The expanded source code is passed to the compiler, and the compiler converts this expanded source code into assembly code. The extension of the assembly code would be hello.s.
- This assembly code is then sent to the assembler, which converts the assembly code into object code.
- After the creation of an object code, the linker creates the executable file. The loader will then load the executable file for the execution.
Example :
1. A
program to check whether the input integer number is positive or negative.
void main()
{ int num;
printf("Enter a number: \n");
scanf("%d", &num);
if (num > 0)
printf("%d is a positive number \n", num);
else if (num < 0)
printf("%d is a negative number \n", num);
else
printf("0 is neither positive nor negative");
}
#include <stdio.h> #define pi 3.142 int main() { float r, h; float sa, v; printf ( "Enter the value for radius of a cylinder : \n" ); printf ( "Enter the value for height of a cylinder : \n" ); scanf ( "%f%f" , &r, &h); sa = 2 * pi * r * (r + h); v = pi * r * r * h; printf ( "Surface area of cylinder is: %f" , sa); printf ( "\nVolume of cylinder is : %f" , v); return 0; } Output :
Enter the value for radius of a cylinder : 2
Enter the value for height of a cylinder : 4
Surface area of cylinder is: 75.407997
Volume of cylinder is : 50.271999
2. Accept temperatures in Fahrenheit (F) and print it in Celsius(C) and Kelvin (K)
(Hint: C=5/9(F-32), K = C + 273.15) |
void main()
{
int sec;
float distance , initial_velocity,velocity, acceleration;
printf("Enter the time in seconds \n");
scanf("%d",&sec);
printf("Enter the initial velocity \n");
scanf("%f", &initial_velocity);
printf("Enter the acceleration \n");
scanf("%f", &acceleration);
velocity = (initial_velocity + (acceleration * sec));
distance = (initial_velocity + (acceleration * (sec*sec)));\
printf(" Total velocity is %f",velocity);
printf("\n Total distance travelled is %f", distance);
}
Output:
Enter the time in seconds
60
Enter the initial velocity
5
Enter the acceleration
7
Total velocity is 425.000000
Total distance travelled is 25205.000000
int main()
{
float out_radius,in_radius,area, perimeter;
printf("Enter inner radius of ring: ");
scanf("%f",&in_radius);
printf("Enter outer radius of ring: ");
scanf("%f",&out_radius);
perimeter= (2 * 3.14) * (in_radius + out_radius);
area= 3.14 * ((in_radius * in_radius) + (out_radius * out_radius));
printf("Area of circle: %f \nPerimeter of circle: %f\n",area,perimeter);
}
Output:
Enter inner radius of ring: 5
Enter outer radius of ring: 6
Area of circle: 191.539993
Perimeter of circle: 69.080002
int main()
{
int a,b;
float arithmetic_mean,harmonic_mean;
printf("enter two no. A and B :-");
scanf("%d%d",&a,&b);
arithmetic_mean = (a+b) /2;
harmonic_mean = (a*b) / (a+b);
printf("arithmetic mean = %f and harmonic mean = %f",arithmetic_mean,harmonic_mean);
}
Output:
enter two no. A and B :- 6 3
arithmetic mean = 4.000000 and harmonic mean = 2.000000
int main()
{
float length, width, height;
float SA, Volume;
printf("\nPlease Enter Length, Width and Height of a Cuboid\n");
scanf("%f %f %f",&length, &width, &height);
SA = 2 * (length * width + length * height + width * height);
Volume = length * width * height;
printf("\n The Surface Area of a Cuboid = %.2f\n",SA);
printf("\n The Volume of a Cuboid = %.2f\n",Volume);
}
Output:
Please Enter Length, Width and Height of a Cuboid
5 7 3
The Surface Area of a Cuboid = 142.00
The Volume of a Cuboid = 105.00
int main()
{
char ch;
printf("Enter character:\t");
scanf("%c", &ch);
printf("You entered: %c\n", ch);
printf("Previous character: %c\n", ch - 1);
printf("Next character: %c\n", ch + 1);
}
Output:
Enter character: c
You entered: c
Next character: d
int main()
{
char ch;
printf("Enter character: ");
scanf("%c", &ch);
printf("ASCII value of %c is %d \n", ch,ch);
}
Output:
Enter character: r
ASCII value of r is 114
#include <stdlib.h>
#include <math.h>
int main()
{
float distance, a, b, c, d;
printf("\nEnter The Coordinates of Point A:\n");
printf("\nX - Axis Coordinate: \t");
scanf("%f", &a);
printf("\nY - Axis Coordinate: \t");
scanf("%f", &b);
printf("\nEnter The Coordinates of Point B:\n");
printf("\nx - Axis Coordinate:\t");
scanf("%f", &c);
printf("\nY - Axis Coordinate: \t");
scanf("%f", &d);
distance = sqrt((c - a) * (c - a) + (d - b) * (d- b));
printf("\nDistance between Points A and B: %f\n", distance);
return 0;
}
Output:
Enter The Coordinates of Point A:
X - Axis Coordinate: 5
Y - Axis Coordinate: 2
Enter The Coordinates of Point B:
x - Axis Coordinate: 8
Y - Axis Coordinate: 4
Distance between Points A and B: 3.605551
int main()
{
int x, y, temp;
printf("Enter the value of x and y\n");
scanf("%d%d", &x, &y);
printf("Before Swapping\nx = %d\ny = %d\n",x,y);
temp = x;
x = y;
y = temp;
printf("After Swapping\nx = %d\ny = %d\n",x,y);
return 0;
}
Output:
Enter the value of x and y
3
6
Before Swapping
x = 3
y = 6
After Swapping
x = 6
y = 3
int main()
{
int w,x,y,z;
printf("enter withdrow amount : ");
scanf("%d",&w);
x=w/10;
w=w%10;
y=w/5;
w=w%5;
z=w;
printf("note of 10 : %d\n",x);
printf("note of 5 : %d\n",y);
printf("note of 1 : %d\n",z);
}
Output:
enter withdrow amount : 564
note of 10 : 56
note of 5 : 0
note of 1 : 4
So informative
ReplyDelete