1. Program which prints block F using # character
void main()
{
clrscr();
printf("#####\n");
printf("#\n####\n");
printf("#\n#\n#\n");
getch();
}
2. Program which computes perimeter and area of a rectangle
void main()
{
int height=7,width=5;
int perimeter,area;
clrscr();
perimeter=2*(height+width);
area=height*width;
printf("Perimeter of rectangle is %d inches\n",perimeter);
printf("Area of rectangle is %d square inches\n",area);
}
3. Program which displays multiple variables
void main()
{
char cvar='P';
int ivar=234;
float fvar=567.25f;
double dvar=123456789.1234567;
clrscr();
printf("Character variable cvar = %c\n",cvar);
printf("Integer variable ivar = %d\n",ivar);
printf("Float variable fvar = %f\n",fvar);
printf("Double variable dvar = %g\n",dvar);
getch();
}
4. Program which calculates distance between two points
#include<math.h>
void main()
{
int x1,y1,x2,y2;
float dist;
clrscr();
printf("Enter the co-ordinates of first point: ");
scanf("%d %d",&x1,&y1);
printf("Enter the co-ordinates of second point: ");
scanf("%d %d",&x2,&y2);
dist=sqrt(pow(x2-x1,2)+pow(y2-y1,2));
printf("Distance between the points is %.2f units\n",dist);
getch();
}
5. Program which checks the relation among 4 variables
void main()
{
int p,q,r,s;
clrscr();
printf("Enter 4 integers for p q r s : ");
scanf("%d %d %d %d",&p,&q,&r,&s);
if( (r>0) && (s>0) && (p%2==0) )
{
if( (q>r) && (s>p) && ((r+s)>(p+q)) )
printf("Corect Values");
else
printf("Wrong Values");
}
else
printf("Wrong Values");
getch();
}
6. Program which converts string to long integer
#include <stdlib.h>
void main()
{
long l;
char *lstr = "98765432";
clrscr();
l = atol(lstr);
printf("String = %s Integer = %ld\n", lstr, l);
getch();
}
7. Program which compute area of various geometrical shapes
void main()
{
int choice,b,h,r;
float area;
clrscr();
do
{
printf("\nArea Calculation\n");
printf("1.Rectangle\n");
printf("2.Triangle\n");
printf("3.Circle\n");
printf("4.Exit\n");
printf("Enter your choice: ");
scanf("%d",&choice);
switch(choice)
{
case 1: printf("Enter length & breadth of rectangle: ");
scanf("%d %d",&h,&b);
area=h*b;
printf("Area of Rectangle is %.2f square units\n",area);
break;
case 2: printf("Enter base & height of triangle: ");
scanf("%d %d",&b,&h);
area=0.5*b*h;
printf("Area of Triangle is %.2f square units\n",area);
break;
case 3: printf("Enter radius of circle: ");
scanf("%d",&r);
area=(22/7.0)*r*r;
printf("Area of Circle is %.2f square units\n",area);
break;
case 4: break;
default:printf("Invalid choice\n");
}
}while(choice!=4);
}
8. Program which calculates factorial of a number
void main()
{
int i,n;
long f=1;
clrscr();
printf("Enter a positive integer: ");
scanf("%d",&n);
for(i=1;i<=n;i++)
f=f*i;
printf("\nFactorial of %d is %ld",n,f);
getch();
}
9. Program which displays n even natural numbers and their sum
void main()
{
int n,i,sum=0,count=0;
clrscr();
printf("How many even numbers to display? ");
scanf("%d",&n);
for(i=2;count<n;count++,i+=2)
{
printf("%d ",i);
sum=sum+i;
}
printf("\nSum of the first %d even natural numbers is %d",n,sum);
getch();
}
10. Program which displays n terms of Harmonic series.1 + 1/2 + 1/3 + 1/4 + 1/5 + .... 1/n and their sum*/
void main()
{
int n,i;
float sum=1.0;
clrscr();
printf("How many terms of Harmonic Series? ");
scanf("%d",&n);
printf("\n1");
for(i=2;i<=n;i++)
{
printf(" + 1/%d",i);
sum=sum+(1.0/i);
}
printf("\n\nSum of %d terms of the Harmonic Series is %.2f ",n,sum);
getch();
}
11. Program which checks whether a number is Armstrong number or not
void main()
{
int n,t,r,sum=0;
clrscr();
printf("Enter an integer ");
scanf("%d",&n);
t=n;
while(t>0)
{
r=t%10;
sum=sum+r*r*r;
t=t/10;
}
if(sum==n)
printf("\n%d is Armstrong Number",n);
else
printf("\n%d is not Armstrong Number",n);
getch();
}
12. Program which prints all unique elements in an array
void main()
{
int arr[10],freq[10];
int i,j,count;
clrscr();
printf("Enter elements in array\n");
for(i=0;i<10;i++)
{
printf("Enter an integer ");
scanf("%d",&arr[i]);
freq[i]=-1;
}
for(i=0;i<10;i++)
{
count=1;
for(j=i+1;j<10;j++)
{
if(arr[i]==arr[j])
{
count++;
freq[j]=0;
}
}
if(freq[i]!=0)
freq[i]=count;
}
printf("\nUnique elements in the array are\n\n");
for(i=0;i<10;i++)
{
if(freq[i]==1)
printf("%d ",arr[i]);
}
getch();
}
No comments:
Post a Comment