C Programming Lab Manual
C Programming Lab Manual
#include <stdio.h>
void main()
pe=m*9.8*h;
ke=0.5*m*(v*v);
te=ke+pe;
printf("Total Energy = %f\n Potential energy =%f\n kinetic energy=%f\n", te, pe, ke);
LAB PROGRAM-2: Write a C Program to convert Kilometres into Meters and Centimetres.
#include <stdio.h>
void main()
scanf("%f",&km);
m=km*1000;
cm=m*100;
}
LAB PROGRAM-3: Write a C program to find the greatest of 3 numbers.
#include<stdio.h>
void main()
{
int a, b, c ;
printf("Enter the values of a,b and c\n");
scanf("%d %d %d", &a, &b, &c);
if (a>b && a>c)
{
printf("A is largest ");
}
else if (b>a && b>c)
{
printf(“B is largest ");
}
else
{
printf("C is largest ");
}
}
LAB PROGRAM-4: Write a C program to count the number of lines, words and characters in
a given text.
#include <stdio.h>
void main()
char str[100];
scanf("%[^~]", str);
words++;
lines++;
words++;
else
characters++;
#include <stdio.h>
void main()
{
int a[10][10], t[10][10], r, c, i, j;
printf("Enter rows and columns of matrix: ");
scanf("%d %d", &r, &c);
printf("\nEnter elements of matrix:\n");
for(i=0; i<r; i++)
{
for(j=0; j<c; j++)
{
scanf("%d", &a[i][j]);
}
}
for(i=0; i<r; i++)
{
for(j=0; j<c; j++)
{
t[i][j] = a[j][i];
}
}
printf("\nTranspose of Matrix:\n");
for(i=0; i<r; i++)
{
for(j=0; j<c; j++)
{
printf("%d ",t[i][j]);
}
printf(“\n”);
}
}
LAB PROGRAM-6: Develop a program using pointers to compute the sum, mean and
standard deviation of all elements stored in an array of N real numbers.
#include<stdio.h>
#include<math.h>
void main ()
{
float a[20], sum1 = 0, sum2 = 0, mean, var, dev;
int i, n;
printf ("Enter no of elements:");
scanf ("%d", &n);
printf ("Enter array elements:");
for (i = 0; i < n; i++)
{
scanf ("%f", a + i);
sum1 = sum1 + * (a + i);
}
mean = sum1 / n;
for (i = 0; i < n; i++)
{
sum2 = sum2 + pow ((*(a + i) - mean), 2);
}
var = sum2 / n;
dev = sqrt (var);
printf ("Sum :%f\n", sum1);
printf ("Mean :%f\n", mean);
printf ("Variance :%f\n", var);
printf ("Deviation :%f\n", dev);
}
LAB PROGRAM-7: Design a C program to calculate the sum of array elements using pointers.
#include<stdio.h>
void main()
{
int i, n, a[10],*ptr, sum=0;
printf("Enter the no. of elements\n");
scanf("%d", &n);
printf("Enter the array elements\n");
for(i=0; i<n; i++)
{
scanf("%d",&a[i]);
}
ptr=a;
for(i=0; i<n; i++)
{
sum=sum+*ptr;
ptr++;
}
printf("Sum=%d\t",sum);
}