Saturday, September 5, 2020

Simple C Programs

C Language :-

              The C Language is developed by Dennis Ritchie for creating system applications that directly interact with the hardware devices such as drivers, kernels, etc.             C programming is considered as the base for other programming languages, that is why it is known as mother language.

         History of C language is interesting to know. Here we are going to discuss a brief history of the c language.  C programming language was developed in 1972 by Dennis Ritchie at bell laboratories of AT&T (American Telephone & Telegraph), located in the U.S.A.  Dennis Ritchie is known as the founder of the c language.

Features of C Language

C is the widely used language. It provides many features that are given below.

  1. Simple
  2. Machine Independent or Portable
  3. Mid-level programming language
  4. structured programming language
  5. Rich Library
  6. Memory Management
  7. Fast Speed
  8. Pointers
  9. Recursion
  10. Extensible

 Compilation process in c



Let's understand through an example.

 hello.c

 #include <stdio.h> 

int main() 

{     printf("Hello javaTpoint"); 

    return 0; 

}  


In the above flow diagram, the following steps are taken to execute a program:

  1. 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.
  2. 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.
  3. This assembly code is then sent to the assembler, which converts the assembly code into object code.
  4. 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.

#include <stdio.h>
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");
}

Excercise 1
SET A

1. Accept dimensions of a cylinder and print the surface area and volume (Hint: surface area = 2πr 2 + 2πrh, volume = πr 2h) 

#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)
#include<stdio.h>
#include<conio.h>
int main()
{
float cel,fer,kel;

printf("Enter Temperature in Fahrenheit :");
scanf("%f",&fer);

cel= (fer-32)/1.8 ;
printf("Celsius = %f \n",cel);

kel = (fer-32)/1.8 + 273.15 ; 
printf("Kelvin  = %f \n",kel);

return (0) ;
}

Output:
Enter Temperature in Fahrenheit :56
Celsius = 13.333333
Kelvin  = 286.483337

#include<stdio.h>
#include<conio.h>
int main()
{
float cel,fer,kel;

printf("Enter Temperature in Fahrenheit :");
scanf("%f",&fer);

cel= (fer-32)/1.8 ;
printf("Celsius = %f \n",cel);

kel = (fer-32)/1.8 + 273.15 ; 
printf("Kelvin  = %f \n",kel);

return (0) ;
}

Output:
Enter Temperature in Fahrenheit :56
Celsius = 13.333333
Kelvin  = 286.483337

#include<stdio.h>
#include<conio.h>
int main()
{
float cel,fer,kel;

printf("Enter Temperature in Fahrenheit :");
scanf("%f",&fer);

cel= (fer-32)/1.8 ;
printf("Celsius = %f \n",cel);

kel = (fer-32)/1.8 + 273.15 ; 
printf("Kelvin  = %f \n",kel);

return (0) ;
}

Output:
Enter Temperature in Fahrenheit :56
Celsius = 13.333333
Kelvin  = 286.483337


3. Accept initial velocity (u), acceleration (a) and time (t). Print the final velocity (v) and distance (s) travelled. (Hint: v = u + at, s = u + at2 )
#include<stdio.h>
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
 4. Accept inner and outer radius of a ring and print the perimeter and area of the ring (Hint: perimeter = 2 π (a+b) , area = π (a2 -b 2 ) ) 
#include <stdio.h>
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
 5. Accept two numbers and print arithmetic and harmonic mean of the two numbers (Hint: AM= (a+b)/2 , HM = ab/(a+b) ) 
#include <stdio.h>
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
 6. Accept three dimensions length (l), breadth(b) and height(h) of a cuboid and print surface area and volume (Hint : surface area=2(lb+lh+bh ), volume = lbh ) 
#include <stdio.h>
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
 7. Accept a character from the keyboard and display its previous and next character in order. Ex. If the character entered is ‘d’, display “The previous character is c”, “The next character is e”. 
#include <stdio.h>
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
Previous character: b
Next character: d
 8. Accept a character from the user and display its ASCII value.
#include <stdio.h>
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
Set B . 
 1. Accept the x and y coordinates of two points and compute the distance between the two points. 
#include <stdio.h>
#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
 2. Accept two integers from the user and interchange them. Display the interchanged numbers
#include<stdio.h>
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
 3. A cashier has currency notes of denomination 1, 5 and 10. Accept the amount to be withdrawn from the user and print the total number of currency notes of each denomination the cashier will have to give.
#include<stdio.h>
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

1 comment: