Friday, 26 June 2015

C language basic programs 2

1. //program on fundamental data types
#include<stdio.h>
#include<conio.h>
int main()
{
int a;
float b;
char c;
double d;
a=5;
b=10.2;
c='M';
d=9.325;
printf("%d %f %c %lf",a,b,c,d);
getch();
return 0;
}


output: 5 10.200000 M 9.325000



2.    //to calculate area of rectangle
#include<stdio.h>
#include<conio.h>
int main()
{
int l, b, area, perimeter;
printf("enter values of length and breadth\n");
scanf("%d %d",&l, &b);
area=l*b;
perimeter=2*(l+b);
printf("area of rectangle is %d\n",area);
printf("perimeter of rectangle is %d",perimeter);
getch();
return 0;

}



output: enter values of length and breadth
2 4
area of rectangle is 8
perimeter of rectangle is 12




3.//to calculate area of rectangle
#include<stdio.h>
#include<conio.h>
int main()
{
float r, area, perimeter;
printf("enter the value of radius of circle\n");
scanf("%f",&r);
area=3.14*r*r;
perimeter=(2*3.14*r);
printf("area of circle is %f\n",area);
printf("perimeter of circle is %f\n",perimeter);
getch();
return 0;

}


output: enter the value of radius of circle
              5
area of circle is 78.500000
perimeter of circle is 31.400000




4.//to calculate area of triangle
#include<stdio.h>
#include<conio.h>
#include<math.h>
int main()
{
int a,b,c,p;
float s,area;
printf("enter the values of a,b,c\n");
scanf("%d%d%d",&a,&b,&c);
p=a+b+c;
s=(a+b+c)/(2.0);
area=sqrt(s*(s-a)*(s-b)*(s-c));
printf("area of triangle is %f\n",area);
printf("perimeter of triangle is %d\n",p);
getch();
return 0;

}


output: enter the values of a,b,c
            2 3 4
area of triangle is 2.904737
perimeter of triangle is 9





5.//sawaping of two numbers
#include<stdio.h>
#include<conio.h>
int main()
{
int a,b,temp;
printf("enter two numbers\n");
scanf("%d %d",&a,&b);
printf("before swaping the values of a and b are %d %d\n",a,b);
temp=a;
a=b;
b=temp;
printf("after swaping the values of a and b are %d %d\n",a,b);
getch();
return 0;

}

output: enter two numbers
              3 5
before swaping the values of a and b are 3 5
after swaping the values of a and b are 5 3



No comments:

Post a Comment