LAB PROGRAMS
LAB PROGRAMS
1. Write a C program to print a block F using hash (#), where the F has a height of six
characters and width of five and four characters.
PROGRAM :
#include<stdio.h>
int main( )
printf("######\n");
printf("#\n");
printf("#\n");
printf("#####\n");
printf("#\n");
printf("#\n");
printf("#");
return 0;
OUPTUT :
2 )Write a C program to compute the perimeter and area of a rectangle with a height of
7 inches and width of 5 inches.
PROGRAM:
#include <stdio.h>
int main( )
int a,p;
a=7*5;
p=2*(7+5);
return 0;
OUTPUT:
Perimeter of a rectangle is 24
3) Write a C program to display multiple variables.
PROGRAM :
#include<stdio.h>
int main( )
char c='V';
int i=678;
float f=12.23;
double d=12.123456789;
printf("Character = %c\n",c);
printf("Integer = %d\n",i);
printf("Float = %f\n",f);
printf("Double = %lf\n",d);
return 0;
Output:
Character = V
Integer = 678
Float = 12.230000
Double = 12.123457
EXERCISE 2
PROGRAM:
#include<stdio.h>
#include<math.h>
int main( )
{
int x1,x2,y1,y2;
int x,y;
float d;
printf("Enter the coordinates of first point(x1,y1):");
scanf("%d%d",&x1,&y1);
printf("Enter the coordinates of the second point(x2,y2):");
scanf("%d%d",&x2,&y2);
x = (x2-x1);
y = (y2-y1);
d = sqrt((x*x)+(y*y));
printf("Distance = %.2f\n",d);
return 0;
}
Output:
Distance = 2.24
2) Write a C program that accepts 4 integers p, q, r, s from the user where r and s are
positive and p is even. If q is greater than r and s is greater than p and if the sum of r
and s is greater than the sum of p and q print "Correct values", otherwise print
"Wrong values".
PROGRAM:
#include <stdio.h>
int main()
int p, q, r, s;
scanf("%d", &p);
scanf("%d", &q);
scanf("%d", &r);
scanf("%d", &s);
if((q > r) && (s > p) && ((r+s) > (p+q)) && (r > 0) && (s > 0) && (p%2 == 0))
{
printf("\nCorrect values\n");
}
else
{
printf("\nWrong values\n");
}
return 0;
}
OUTPUT :
Correct values
ii)
Wrong values
EXERCISE 3
PROGRAM:
int main()
char str[20];
gets(str);
return 0;
OUTPUT :
123abcd
Integer: 123
abcdef
Integer: 0
2) Write a program in C which is a Menu-Driven Program to compute the area of the
various geometrical shape
PROGRAM :
#include <stdio.h>
void main ()
int choice,r,l,w,b,h;
float area;
scanf("%d",&choice);
switch(choice)
case 1:
scanf("%d",&r);
area=3.14*r*r;
break;
case 2:
scanf("%d%d",&l,&w);
area=l*w;
break;
case 3:
scanf("%d%d",&b,&h);
area=0.5*b*h;
break;
OUTPUT :
PROGRAM:
#include <stdio.h>
int main()
int n, i;
scanf("%d", &n);
if (n < 0)
else
fact *= i;
return 0;
OUTPUT:
i) Enter an integer: -5
Factorial of 15 = 1307674368000
Exercise 4:
1) Write a program in C to display the n terms of even natural number and their sum.
PROGRAM:
#include<stdio.h>
main()
int n,i,sum;
scanf("%d",&n);
sum=0;
i=1;
do
printf("%d ",2*i);
sum=sum+(2*i);
i=i+1;
}while(i<=n);
OUTPUT:
PROGRAM :
#include<stdio.h>
main()
{
int n,i;
float sum=0.0;
printf("\nEnter n value:");
scanf("%d",&n);
printf("\nThe given harmonic series : ");
for(i=1;i<=n;i++)
{
printf("1/%d +",i);
sum=sum+(1.0/i);
}
printf("\b");
printf("= %f",sum);
}
OUTPUT:
i) Enter n value:5
The given harmonic series : 1/1 +1/2 +1/3 +1/4 +1/5 = 2.283334
The given harmonic series : 1/1 +1/2 +1/3 +1/4 +1/5 +1/6 +1/7 +1/8 +1/9 +1/10 +1/11 +1/12
= 3.103211
3) Write a C program to check whether a given number is an Armstrong number or not.
PROGRAM:
#include<stdio.h>
main()
int n,temp,rem,sum=0;
printf("\nEnter number:");
scanf("%d",&n);
temp=n;
while(n!=0)
rem=n%10;
sum=sum+(rem*rem*rem);
n=n/10;
if(temp==sum)
printf("\nArmstrong Number");
else
OUTPUT :
i) Enter number:153
Armstrong Number
PROGRAM :
#include <stdio.h>
int main()
int i, j;
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
ctr=0;
if (i!=j)
if(a[i]==a[j])
ctr++;
}
if(ctr==0)
printf("%d ",a[i]);
OUTPUT:
5 6 3
2
1
12
89
89
2 1 3 12