Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
72 views

C Programming Lab Manual

The document contains 8 programs written in C programming language to demonstrate various concepts: 1) Calculate mechanical energy of a particle given mass, height, and velocity. 2) Convert kilometers to meters and centimeters. 3) Find the greatest of 3 numbers. 4) Count lines, words, and characters in given text. 5) Find transpose of a 2x2 matrix. 6) Calculate sum, mean, and standard deviation of array elements using pointers. 7) Calculate sum of array elements using pointers. 8) Implement string operations like compare, concatenate, and find length using functions.

Uploaded by

harshagowda0464
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
72 views

C Programming Lab Manual

The document contains 8 programs written in C programming language to demonstrate various concepts: 1) Calculate mechanical energy of a particle given mass, height, and velocity. 2) Convert kilometers to meters and centimeters. 3) Find the greatest of 3 numbers. 4) Count lines, words, and characters in given text. 5) Find transpose of a 2x2 matrix. 6) Calculate sum, mean, and standard deviation of array elements using pointers. 7) Calculate sum of array elements using pointers. 8) Implement string operations like compare, concatenate, and find length using functions.

Uploaded by

harshagowda0464
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

Introduction to C

Programming Lab Manual


(BESCK104EL)
LAB PROGRAM-1: Write a C Program to find Mechanical Energy of a particle using E =
mgh+1/2 mv2.

#include <stdio.h>

void main()

float m, g, h, v, te, ke, pe;

printf("Enter mass, height and velocity: ");

scanf("%f %f %f", &m, &h, &v);

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()

float km, m, cm;

printf("Enter the kilometer: ");

scanf("%f",&km);

m=km*1000;

cm=m*100;

printf("Kilometer = %.2f\n Meter =%.2f\n Centimeter=%.2f\n", km, m, cm);

}
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];

int i, words=1, lines=1, characters=0;


printf("Enter the Text: ");

scanf("%[^~]", str);

for(i=0; str[i]!='\0'; i++)

if(str[i] == ' ')

words++;

else if(str[i] == '\n')

lines++;

words++;

else

characters++;

printf("Total number of words : %d\n",words);

printf("Total number of lines : %d\n",lines);

printf("Total number of characters : %d\n",characters);

LAB PROGRAM – 5: Implement a C program to find the transpose of 2X2 matrix.

#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);
}

LAB PROGRAM – 8: Write functions to implement string operations such as compare,


concatenate, string length. Convince the parameter passing techniques.
#include <stdio.h>
#include<string.h>
void length(char[]);
void compare(char[], char[]);
void concate(char[],char[]);
void main()
{
char str1[50], str2[50];
int ch;
while(1)
{
printf("\n 1. length\t 2. compare\t 3.concat\n");
printf("Enter your choice: ");
scanf("%d",&ch);
switch(ch)
{
case 1:printf("Enter String to find length: ");
scanf("%s",str1);
length(str1);
break;
case 2:printf("Enter String-1: ");
scanf("%s",str1);
printf("Enter String-2: ");
scanf("%s",str2);
compare(str1,str2);
break;
case 3:printf("Enter String-1: ");
scanf("%s",str1);
printf("Enter String-2: ");
scanf("%s",str2);
concate(str1,str2);
break;
default: printf(“Invalid Choice”);
break;
}
}
}
void length(char str1[])
{
int len;
len=strlen(str1);
printf("Length = %d",len);
}
void compare(char str1[], char str2[])
{
int i;
i=strcmp(str1,str2);
if(i==0)
printf("Equal");
else
printf("Not equal");
}
void concate(char str1[], char str2[])
{
strcat(str1,str2);
printf("Concatenated String = %s",str1);
}

You might also like