Lab Manual PPSC
Lab Manual PPSC
Tech I - I Sem1
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>
/* height and width of a rectangle in inches */
int width;
int height;
int area;
int perimeter;
int main()
{
height = 7;
width = 5;
perimeter = 2*(height + width);
printf("Perimeter of the rectangle = %d inches\n", perimeter);
area = height * width;
printf("Area of the rectangle = %d square inches\n", area); 2
return(0);
}
Output:
Perimeter of the rectangle = 24 inches
Area of the rectangle = 35 square inches
#include <stdio.h>
#include <math.h>
int main() {
float x1, y1, x2, y2, gdistance;
printf("Input x1: ");
scanf("%f", &x1);
printf("Input y1: ");
scanf("%f", &y1);
printf("Input x2: ");
scanf("%f", &x2);
printf("Input y2: ");
scanf("%f", &y2);
gdistance = ((x2-x1)*(x2-x1))+((y2-y1)*(y2-y1));
printf("Distance between the said points: %.4f", sqrt(gdistance));
printf("\n");
return 0;
}
Output:
Input x1: 25
Input y1: 15
Input x2: 35
Input y2: 10
Distance between the said points: 11.1803
2. Write a C program that accepts 4 integers p, q, r, s from the user where q, 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;
printf("\nInput the first integer: ");
scanf("%d", &p);
4
printf("\nInput the second integer: ");
scanf("%d", &q);
printf("\nInput the third integer: ");
scanf("%d", &r);
printf("\nInput the fourth integer: ");
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:
Input the first integer: 25
Wrong values
-------------------------------------------------------------------------------------------------
Week 3
1.Write a C program to convert a string to a long integer.
#include<stdio.h>
#include<stdlib.h>
int main ()
{
char buffer[] = "2016 40a0b0 -1101110100110111100110 0x5abfff";
char * ptr_end;
long int i1, i2, i3, i4;
i1 = strtol (buffer,&ptr_end,10);
i2 = strtol (ptr_end,&ptr_end,16);
i3 = strtol (ptr_end,&ptr_end,2);
i4 = strtol (ptr_end,NULL,0);
printf ("\nIn decimals: %ld, %ld, %ld, %ld.\n\n", i1, i2, i3, i4);
return 0; 5
}
printf("\nFactorial of %d is = %ld",num,fact);
return 0;
}
Output:
Enter an integer number: 7
Factorial of 7 is = 5040
-------------------------------------------------------------------------------------------------
Week 4
1.Write a program in C to display the n terms of even natural number and their sum
#include <stdio.h>
void main()
{
int i,n,sum=0;
Output:
Input number of terms : 5
2. Write a program in C to display the n terms of harmonic series and their sum. The
series is : 1 + 1/2 + 1/3 + 1/4 + 1/5 ... 1/n terms
#include <stdio.h>
void main()
{
int i,n;
float s=0.0;
printf("Input the number of terms : ");
scanf("%d",&n);
printf("\n\n");
for(i=1;i<=n;i++)
{
if(i<n)
{
printf("1/%d + ",i);
s+=1/(float)i;
}
if(i==n)
{
printf("1/%d ",i);
s+=1/(float)i;
}
}
printf("\nSum of Series upto %d terms : %f \n",n,s);
}
Output:
Input the number of terms : 5
5
6
7
The individual array elements are :
Arr[0]=3
Arr[1]=4
Arr[2]=5
Arr[3]=6
Arr[4]=7
printf("%d ",arr2[i]);
}
printf("\nThe Odd elements are :\n");
for(i=0;i<k;i++)
{
printf("%d ", arr3[i]);
}
printf("\n\n");
}
Output:
Separate odd and even integers in separate arrays:
-------------------------------------------------------------------
Input the number of elements to be stored in the array :5
Input 5 elements in the array :
element - 0 : 25
element - 1 : 47
element - 2 : 42
element - 3 : 56
element - 4 : 32
{
for(j=i+1; j<n; j++)
{
if(arr1[j] <arr1[i])
{
tmp = arr1[i];
arr1[i] = arr1[j];
arr1[j] = tmp;
}
}
}
printf("\nElements of array in sorted ascending order:\n");
for(i=0; i<n; i++)
{
printf("%d ", arr1[i]);
}
printf("\n\n");
}
Output:
sort elements of array in ascending order :
----------------------------------------------
Input the size of array : 5
Input 5 elements in the array :
element - 0 : 2
element - 1 : 7
element - 2 : 4
element - 3 : 5
element - 4 : 9
void main()
{
int arr1[50][50],brr1[50][50],crr1[50][50],i,j,k,r1,c1,r2,c2,sum=0;
//multiplication of matrix
for(i=0;i<r1;i++)
for(j=0;j<c2;j++)
crr1[i][j]=0;
for(i=0;i<r1;i++) //row of first matrix
{
for(j=0;j<c2;j++) //column of second matrix
{
sum=0;
for(k=0;k<c1;k++)
sum=sum+arr1[i][k]*brr1[k][j];
crr1[i][j]=sum;
}
}
printf("\nThe multiplication of two matrices is : \n");
for(i=0;i<r1;i++)
{
printf("\n");
for(j=0;j<c2;j++)
{
printf("%d\t",crr1[i][j]);
}
}
}
printf("\n\n");
}
Output:
Multiplication of two Matrices :
----------------------------------
Input the rows and columns of first matrix : 2 14
2
element - [1],[0] : 7
element - [1],[1] : 8
1 2
3 4
The Second matrix is :
5 6
7 8
The multiplication of two matrices is :
19 22
43 50
{
int arr1[50][50],brr1[50][50],i,j,k=0,r,c;
printf("\n\nTranspose of a Matrix :\n");
printf("---------------------------\n");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
brr1[j][i]=arr1[i][j];
}
}
Output:
Transpose of a Matrix :
---------------------------
1 2
3 4
----------------------------------------------------------------------------------------------
Week 7
1. Write a program in C to search an element in a row wise and column wise sorted
matrix.
#include <stdio.h>
int searchElement(int arr2D[4][4], int n, int x)
{
int i = 0, j = n-1;
while ( i < n && j >= 0 )
{
if ( arr2D[i][j] == x )
{
printf("\nThe element Found at the position in the matrix is: %d, %d", i, j);
return 1;
}
if ( arr2D[i][j] < x )
j--;
else
i++;
}
printf("\nThe given element not found in the 2D array.");
return 0;
}
int main()
{
int arr2D[4][4] = { {15, 23, 31, 39},
{18, 26, 36, 43},
{25, 28, 37, 48},
{30, 34, 39, 50},
};
int i,j,v;
v=37;
//------------- print original array ------------------
printf("The given array in matrix form is : \n"); 17
for(i = 0; i < 4; i++)
{
for (j=0;j<4;j++)
{
printf("%d ", arr2D[i][j]);
}
printf("\n");
}
//------------------------------------------------------
Output:
The given array in matrix form is :
15 23 31 39
18 26 36 43
25 28 37 48
30 34 39 50
The given value for searching is: 37
The element Found at the position in the matrix is: 2, 2
void main()
{
char str[100]; /* Declares a string of size 100 */
int l,i;
m o c . e c r u o s e r 3 w
-----------------------------------------------------------------------------------------------------------
Week 8
1. Write a program in C to compare two string without using string library functions.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
void main()
{
char str1[str_size], str2[str_size];
int flg=0;
int i=0;
i++;
}
if(str1[i-1] == '\0' && str2[i-1]=='\0') 19
flg=0;
else if(str1[i] > str2[i])
flg=1;
else if(str1[i] < str2[i])
flg=-1;
if(flg == 0)
{
printf("\nThe length of both strings are equal and \nalso both strings are equal.\n\n");
}
else if(flg == -1)
{
printf("\nThe length of the first string is smaller than second.\n\n");
}
else
{
printf("\nThe length of the first string is greater than second.\n\n");
}
}
Output:
Compare two string whether they are equal or not :
------------------------------------------------------
Input the 1st string : This is first string
Input the 2nd string : This is first string
void main()
{
char str1[100], str2[100];
int i;
printf("\n\nCopy one string into another string :\n"); 20
printf("-----------------------------------------\n");
printf("Input the string : ");
fgets(str1, sizeof str1, stdin);
str2[i] = str1[i];
i++;
}
struct course
{
int marks;
char subject[30];
};
int main() 21
{
struct course *ptr;
int i, noOfRecords;
printf("Enter number of records: ");
scanf("%d", &noOfRecords);
// Allocates the memory for noOfRecords structures with pointer ptr pointing to the base
address.
printf("Displaying Information:\n");
return 0;
}
Output:
Enter number of records: 2
Enter name of the subject and marks respectively:
Programming
22
Enter name of the subject and marks respectively:
Structure
33
Displaying Information:
Programming 22
Structure 33
Output:
Pointer : How to handle the pointers in the program :
------------------------------------------------------------
Here in the declaration ab = int pointer, int m= 29
Address of m : 0x7fff24a3f8bc
Value of m : 29
#include <stdio.h>
int main(void)
{
//normal variable
//pointer variable
int *ptr;
//pointer initialization
ptr = #
return 0;
}
Output:
value of num = 100
Address of num: 9505c134
Address of ptr: 9505c138
2. Write a program in C to add two numbers using pointers
#include <stdio.h>
#include<conio.h>
main()
{
int x=10,y=20,*p1,*p2,z;
p1=&x;
p2=&y;
Z=(*p1)+(*p2);
printf(“addition of two pointers is z=%d”,z); 24
getch();
}
Output :
addition of two pointers is 30.
-----------------------------------------------------------------------------------------------
Week 11
1.Write a program in C to add numbers using call by reference .
Program:
#include <stdio.h>
long addTwoNumbers(long *, long *);
int main()
{
long fno, sno, *ptr, *qtr, sum;
if(element==NULL)
{
printf(" No memory is allocated.");
exit(0);
}
printf("\n");
for(i=0;i<n;++i)
{
printf(" Number %d: ",i+1);
scanf("%f",element+i);
}
for(i=1;i<n;++i)
{
if(*element<*(element+i))
*element=*(element+i);
}
printf(" The Largest element is : %.2f \n\n",*element);
return 0;
}
Output:
Pointer : Find the largest element using Dynamic Memory Allocation :
-------------------------------------------------------------------------
Input total number of elements(1 to 100): 5
Number 1: 5
Number 2: 7
Number 3: 2
Number 4: 9 26
Number 5: 8
The Largest element is : 9.00
--------------------------------------------------------------------------------------------------
Week 12
1. Write a program in C to swap elements using call by reference .
Program:
#include <stdio.h>
#include <conio.h>
main()
{
int x,y;
clrscr();
printf(“enter x,y values:”);
scanf(“%d%d”,&x,&y);
swap(&x,&y);
getch();
}
int main()
{
char s[1000];
int i,vowels=0,consonants=0;
for(i=0;s[i];i++)
{
if((s[i]>=65 && s[i]<=90)|| (s[i]>=97 && s[i]<=122)) 27
{
if(s[i]=='a'|| s[i]=='e'||s[i]=='i'||s[i]=='o'||s[i]=='u'||s[i]=='A'||s[i]=='E'||s[i]=='I'||s[i]=='O'
||s[i]=='U')
vowels++;
else
consonants++;
}
printf("vowels = %d\n",vowels);
printf("consonants = %d\n",consonants);
return 0;
}
Output:
Enter the string : hello world
vowels = 3
consonants = 7
---------------------------------------------------------------------------------------------------
Week 13
1. Write a program in C to show how a function returning pointer .
Program:
#include<stdio.h>
#include<conio.h>
main()
{
int a=5;
printf(“a=%d\n”,a);
inc(&a);
printf(“a=%d\n”,a);
}
inc(int *a)
{
a=a+1;
printf(“a=%d\n”,a);
}
int main()
{
int n, i, *ptr, sum = 0;
return 0;
}
---------------------------------------------------------------------------------------------------
Week 14
1. Write a C program to find sum of n elements entered by user. To perform this
program, allocate memory dynamically using calloc( ) function. Understand the
difference between the above two programs
program:
#include <stdio.h> 29
#include <stdlib.h>
int main()
{
int n, i, *ptr, sum = 0;
printf("Enter number of elements: ");
scanf("%d", &n);
Output:
Function : convert decimal to binary :
-------------------------------------------
Input any decimal number : 65
2. Write a program in C to get the largest element of an array using the function
Program:
#include<stdio.h>
#define MAX 100
int main()
{
int arr1[MAX],mxelem,i;
printf(" Input the number of elements to be stored in the array :");
scanf("%d",&n);
Output:
Function : get largest element of an array :
-------------------------------------------------
Input the number of elements to be stored in the array :5
Input 5 elements in the array :
element - 0 : 1
element - 1 : 2
element - 2 : 3
element - 3 : 4
element - 4 : 5
The largest element in the array is : 5
-----------------------------------------------------------------------------------------------------
Week 16
1.Write a program in C to append multiple lines at the end of a text file.
program:
#include <stdio.h>
int main ()
{
FILE * fptr;
int i,n;
char str[100];
char fname[20];
char str1;
test line 1
test line 2
test line 3
test line 4
test line 5
test line 6
test line 7
2. Write a program in C to copy a file in another name. 34
program:
#include <stdio.h>
#include <stdlib.h>
void main()
{
FILE *fptr1, *fptr2;
char ch, fname1[20], fname2[20];
fptr1=fopen(fname1, "r");
if(fptr1==NULL)
{
printf(" File does not found or error in opening.!!");
exit(1);
}
printf(" Input the new file name : ");
scanf("%s",fname2);
fptr2=fopen(fname2, "w");
if(fptr2==NULL)
{
printf(" File does not found or error in opening.!!");
fclose(fptr1);
exit(2);
}
while(1)
{
ch=fgetc(fptr1);
if(ch==EOF)
{
break;
}
else
{
fputc(ch, fptr2);
}
} 35
printf(" The file %s copied successfully in the file %s. \n\n",fname1,fname2);
fclose(fptr1);
fclose(fptr2);
getchar();
}
Output:
Copy a file in another name :
----------------------------------
Input the source file name : test.txt
Input the new file name : test1.txt
The file test.txt copied successfully in the file test1.txt.
void main()
{
int status;
char fname[20];
printf("\n\n Remove a file from the disk :\n");
printf("----------------------------------\n");
printf(" Input the name of file to delete : ");
scanf("%s",fname);
status=remove(fname);
if(status==0)
{
printf(" The file %s is deleted successfully..!!\n\n",fname);
}
else
{
printf(" Unable to delete file %s\n\n",fname);
}
}
Output:
Remove a file from the disk :
----------------------------------
Input the name of file to delete : test.txt
The file test.txt is deleted successfully..!!
************************ PPSC LAB COMPLETED************************