Basic C Programming Set 2

Example.6. C program to calculate and print area and perimeter of the rectangle?

#include <stdio.h>
int main()
{
int l,b,a,p;
printf("Enter length and width of rectangle ");
scanf("%d %d",&l,&b);
a=l*b;
p=2*(l+b);
printf("area = %d perimeter = %d",a,p);
return 0;
}

Output:

Enter length and width of rectangle 2
3
area = 6 perimeter = 10

Example.7. C program to calculate and print area and circumference of a circle.

#include <stdio.h>
#define pi 3.1415
int main()
{
float r,a,c;
printf("Enter radius of circle ");
scanf("%f",&r);
a=pi*r*r;
c=2*pi*r;
printf("area = %f circumference = %f",a,c);
return 0;
}

Output:

Enter radius of circle 2.3
area = 16.618534 circumference = 14.450900

Example.8. C program to take input for 4 numbers calculate and print their sum, product, and average?

#include <stdio.h>
int main()
{
float a,b,c,d,s,p,av;
printf("Enter 4 nos ");
scanf("%f %f %f %f",&a,&b,&c,&d);
s=a+b+c+d;
p=a*b*c*d;
av=(a+b+c+d)/4;
printf("sum = %f prod = %f average = %f",s,p,av);
return 0;
}

Output:

Enter 4 nos 10
20
30
40
sum = 100.000000 prod = 240000.000000 average = 25.000000

C Language Programming Tutorial

C Language Tutorial Home     Introduction to C Language     Tokens     If Condition      goto statement and Labelname     Switch Statements     For loop     While Loop     Do while loop     break and continue     Functions     Recursion     Inbuild Functions     Storage Classes     Preprocessor     Arrays     Pointers     Structures and Unions     File Handling     Projects