High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
c programing
1. SRM UNIVERSITY 15IT102L – PDD Laboratory Solution Manual
INDEX
Compiled by IT Department, SRM University, KTR Campus Page 1
EX.
NO Ex. Date EXERCISES
1 Study of Unix commands
2 Basic Programs in C
3
a) Swapping of two numbers using temporary variable
b) Swapping of two numbers without using temporary variable
Decision making – Branching
4 Greatest of three numbers
5
Use of Switch...Case statements - Counting number of vowels
and digits in a string
Decision making - Looping
6 Sum of digits
7 Generation of prime numbers
8 Generation of Fibonacci Series
Functions
9 Factorial of a given number using Function
10 Functions with Static data type
Arrays
11 Matrix Addition
12
a) Matrix Transpose
b) Matrix Multiplication
Strings
13 Palindrome checking
14 Removal of duplicate string from the given sentence
Structures
15 Sorting of items using array of structures
16 User-defined data type using structure
Pointers
17
Basic Programs on pointers
18 Display sum array using pointers
19 Sorting of names using array of pointers
20 Using pointers compute mean and standard deviation
2. SRM UNIVERSITY 15IT102L – PDD Laboratory Solution Manual
EX. No. : 2 BASIC C PROGRAMS
SAMPLE 1: Write a program to print Simple Interest.
/* program to find simple interest */
#include<stdio.h>
int main( )
{
float p,n,r,si;
clrscr();
printf(“Enter principle, Number of years and Rate of Interest :n”);
scanf(%f%f%f”,&p,&n,&r);
si=(p*n*r/100);
printf(“The Simple Interest is : %2.2f: ”,si);
return 0;
}
OUTPUT:-
Enter principle, Number of years and Rate of Interest:
5000 5 10
The Simple Interest is: 2500.00
SAMPLE 2: Write a program to generate Sales slip.
/************** SALES SLIP ***************/
#include <stdio.h>
int main( )
{
float purchase, tax_amt, total, rate=0.125;
clrscr();
printf("n Amount of purchase: ");
scanf("%f",&purchase);
tax_amt = purchase * rate;
total = purchase + tax_amt;
printf("n Purchase is : %0.2f ",purchase);
printf("n Tax : %0.2f",tax_amt);
printf("n Total : %0.2f",total);
return 0;
}
OUTPUT 1:
Amount of purchase: 500
Purchase is: 500.00
Tax : 62.50
Total : 562.50
OUTPUT 2:
Amount of purchase: 1773
Compiled by IT Department, SRM University, KTR Campus Page 2
3. SRM UNIVERSITY 15IT102L – PDD Laboratory Solution Manual
Purchase is: 1773.00
Tax : 221.62
Total : 1994.62
RESULT:
Thus the command options in the UNIX environment have been studied and
basic C programs are compiled and executed adopting UNIX commands.
EX. No. : 3 SWAPPING OF TWO NUMBERS
Compiled by IT Department, SRM University, KTR Campus Page 3
4. SRM UNIVERSITY 15IT102L – PDD Laboratory Solution Manual
AIM
To write a C program to swap two numbers using temporary variable and without
using temporary variable.
ALGORITHM
1. Start
2. Read two numbers A, B
3. Copy A to TEMP
4. Copy B to A
5. Copy TEMP to B
6. Print A, B
7. Calculate A=A+B, B=A-B, A=A-B
8. Display A, B
9. Stop
PROGRAM
/***************** SWAPPING OF TWO NUMBERS *****************/
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int a,b,temp;
printf("Enter Two numbers");
scanf("%d %d",&a,&b);
printf("nSwapping Using Temporary Variablen");
printf("n Before Swap A=%d B=%d",a,b);
temp=a;
a=b;
b=temp;
printf("nAfter Swap A=%d B=%d",a,b);
Compiled by IT Department, SRM University, KTR Campus Page 4
5. SRM UNIVERSITY 15IT102L – PDD Laboratory Solution Manual
printf("nSwapping Without Using Temporary Variablen");
printf("n Before Swap A=%d B=%d",a,b);
a=a+b;
b=a-b;
a=a-b;
printf("nAfter Swap A=%d B=%d",a,b);
getch();
}
OUTPUT
Enter Two numbers 3 2
Swapping Using Temporary Variable
Before Swap A=3 B=2
After Swap A=2 B=3
Swapping Without Using Temporary Variable
Before Swap A=2 B=3
After Swap A=3 B=2
RESULT
Thus swapping of two numbers using temporary variable and without using
temporary variable was done and successfully executed.
EX. No. : 4 GREATEST OF THREE NUMBERS
Compiled by IT Department, SRM University, KTR Campus Page 5
6. SRM UNIVERSITY 15IT102L – PDD Laboratory Solution Manual
AIM:
To write a C program to find the largest of three numbers using conditional and
IF statement.
ALGORITHM
1. Start
2. Read three numbers A, B, C
3. Compare whether A is greater than B and C using conditional operator
4. If it is equal then print “A is greater”
5. If step 3 is false check whether B is greater than A and C using conditional
operator.
6. If it is true print “B is greater”
7. If step 5 is false print “C is greater”
8. Stop
PROGRAM
/***** GREATEST OF THREE NUMBERS USING
IF AND CONDITIONAL OPERATOR *****/
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int a,b,c;
printf("enter 3 numbers");
scanf("%d %d %d",&a,&b,&c);
printf("n Using conditional");
printf("n");
Compiled by IT Department, SRM University, KTR Campus Page 6
7. SRM UNIVERSITY 15IT102L – PDD Laboratory Solution Manual
(a>b&&a>c)?printf("A is larger"):(b>a&&b>c)?printf("B is larger"):printf("C is
larger");
printf("n Using IF n");
if(a>b && a>c)
printf("A is largest");
else
if(b>a&&b>c)
printf("B is largest");
else
printf("C is largest");
getch();
}
OUTPUT
Enter 3 numbers 2 5 6
Using conditional
C is larger
Using IF
C is largest
RESULT
Thus the largest of three numbers using conditional and if statement was
successfully executed.
Compiled by IT Department, SRM University, KTR Campus Page 7
8. SRM UNIVERSITY 15IT102L – PDD Laboratory Solution Manual
EX. No. : 5 COUNTING NUMBER OF VOWELS AND DIGITS IN A STRING
AIM
To write a C program to count the number of vowels and digits in a given string
using switch case statement.
ALGORITHM
1. Start
2. Read a string CH
3. Loop until CH is equal to new line
4. Check whether the entered character is vowel or a digit using switch case
5. If it is vowel increment VOWEL
6. If it is digit increment DIGIT
7. End loop
8. Print VOWEL, DIGIT
9. Stop
PROGRAM
/**** COUNTING NUMBER OF VOWELS AND DIGITS
IN A STRING USING SWITCH CASE ****/
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
char ch;
int digits=0,vowels=0;
printf("Enter a string with digitsn");
while((ch=getchar())!='n')
{
switch(ch)
Compiled by IT Department, SRM University, KTR Campus Page 8
9. SRM UNIVERSITY 15IT102L – PDD Laboratory Solution Manual
{
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
digits++;
break;
case 'a':case 'A':
case 'e':case 'E':
case 'i':case 'I':
case 'o':case 'O':
case 'u':case 'U':
vowels++;
break;
}
}
printf("n No. of vowels in the given string %d",vowels);
printf("n No. of digits in the given string %d",digits);
getch();
}
Compiled by IT Department, SRM University, KTR Campus Page 9
10. SRM UNIVERSITY 15IT102L – PDD Laboratory Solution Manual
OUTPUT
Enter a string with digits
COMPTER PROGRAMMING 123
No. of vowels in the given string 5
No. of digits in the given string 3
RESULT
Thus a C program to count the number of vowels and digits using switch case
was compiled and executed successfully.
Compiled by IT Department, SRM University, KTR Campus Page 10
11. SRM UNIVERSITY 15IT102L – PDD Laboratory Solution Manual
EX. No. : 6 SUM OF DIGITS
AIM:
To write a C program to result the sum of digits of given number.
ALGORITHM:
1. Start the program.
2. Initialize s=0
3. Read the given number in variable N
4. Repeat while(n!=0)
5. Set, x=n%10
6. Assign, s=s+x
7. Set, n=n/10
8. Print sum
9. Stop
PROGRAM
/******************** SUM OF DIGITS ***********************/
#include<stdio.h>
#include<conio.h>
void main()
{
long int n,s=0,x;
clrscr();
printf("n Enter the number:");
scanf("%ld", &n);
while(n!=0)
{
x=n%10;
s=s+x;
n=n/10;
}
printf(" The sum of the digits is %ld",s);
getch();
}
Compiled by IT Department, SRM University, KTR Campus Page 11
12. SRM UNIVERSITY 15IT102L – PDD Laboratory Solution Manual
OUTPUT 1:
Enter the number: 786
The sum of the digits is 21
OUTPUT 2:
Enter the number: 104278048
The sum of the digits is 12
RESULT:
Thus the C program to result sum of digits of given number was compiled and
executed successfully.
Compiled by IT Department, SRM University, KTR Campus Page 12
13. SRM UNIVERSITY 15IT102L – PDD Laboratory Solution Manual
EX. No. : 7 GENERATION OF PRIME NUMBERS
AIM:
To write a C program to generate all prime numbers in a given range.
ALGORITHM:
1: Start the program.
2: Get the range of prime numbers N
3: Check if N= =1 if equal go to step 4
4: Print number is not valid to generate prime.
5: By using for loop check (i<=N).
6: Assign c=0 check the process j<=i using for loop.
5: Check for the condition (i%j==0) then c=c+1.
6: Check for the condition whether c=2, then display the value.
7: Stop
PROGRAM:
/* * * * * * * * * PRIME NUMBERS * * * * * * * * * * */
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int n,p,i,j,c;
printf("Enter the limit");
scanf("%d",&n);
printf("n The prime numbers are");
for (i=1; i<=n; i++)
{
Compiled by IT Department, SRM University, KTR Campus Page 13
14. SRM UNIVERSITY 15IT102L – PDD Laboratory Solution Manual
c=0;
for(j=1;j<=n;j++)
{
if((i%j)==0)
{
c=c+1;
}
}
if(c= =2)
{
printf("n%d",i);
}
}
getch();
}
OUTPUT :
Enter the limit: 10
The prime numbers are:
2
3
5
7
RESULT:
Thus the C program to generate prime numbers in the given range was compiled
and executed successfully.
Compiled by IT Department, SRM University, KTR Campus Page 14
15. SRM UNIVERSITY 15IT102L – PDD Laboratory Solution Manual
EX. No. : 8 GENERATION OF FIBONACCI SERIES
AIM:
To write a c program to generate the Fibonacci series.
ALGORITHM:
1. Start
2. Initialize FIB1 = 0, FIB2 = 1, COUNTER = 2
3. Read NUMBERS
4. If NUMBERS<3 stop the program
5. Print FIB2
6. Loop until COUNTER<=NUMBERS
7. Increment COUNTER by 1
8. FIB3=FIB1+FIB2
9. Print FIB3
10. Copy FIB2 to FIB1
11. Copy FIB3 to FIB2
12. End loop
13. Stop
PROGRAM:
/*************FIBONACCI SERIES ***************/
#include <stdio.h>
#include<conio.h>
#include<process.h>
void main()
{
Compiled by IT Department, SRM University, KTR Campus Page 15
16. SRM UNIVERSITY 15IT102L – PDD Laboratory Solution Manual
clrscr();
int fib1 = 0,fib2 = 1,fib3,numbers,counter = 2;
printf("How many Fibonacci number you need ? : ");
scanf("%d",&numbers);
if (numbers < 3)
exit(0);
printf("%d ",fib2);
do
{
counter++;
fib3 = fib1 + fib2;
printf("n%d",fib3);
fib1 = fib2;
fib2 = fib3;
} while (counter <= numbers);
getch();
}
OUTPUT:
How many Fibonacci number you need? : 5
1
1
2
3
5
RESULT
Thus the Fibonacci series for a given range was compiled and executed
successfully.
Compiled by IT Department, SRM University, KTR Campus Page 16
17. SRM UNIVERSITY 15IT102L – PDD Laboratory Solution Manual
EX. No. : 9 FACTORIAL OF A GIVEN NUMBER USING FUNCTION
AIM
To write a C program to result factorial of given number using function.
ALGORITHM
1. Start
2. Read the number in variable a
3. Call the declared function fact_func() with actual parameter 'a’
4. Receive the return value from function in variable n
5. Print n
6. Stop
Function fact_func()
1. Get the value in formal parameter f
2. Initialize fact = 1
3. Repeat step 3 and 4 until f>=1
4. fact=fact*f
5. decrement the variable f by 1
6. return fact value after condition fails in step3
PROGRAM
/******************** FACTORIAL using Function ********************/
#include<stdio.h>
#include<conio.h>
main()
{
long int a,n;
int fact_func(int);
clrscr();
printf("n Enter the number:");
scanf("%ld", &a);
n=fact_func(a);
printf(" %ld! Factorial is %ld", a, n);
getch();
}
Compiled by IT Department, SRM University, KTR Campus Page 17
18. SRM UNIVERSITY 15IT102L – PDD Laboratory Solution Manual
int fact_func( int f)
{
int fact=1;
while (f>=1)
{
fact=(f*fact);
f--;
}
return fact;
}
OUTPUT 1:
Enter the number: 6
6! Factorial is 720
OUTPUT 2:
Enter the number: 7
7! Factorial is 5040
RESULT
Thus the C program to result factorial of given number using function was
compiled and executed successfully.
Compiled by IT Department, SRM University, KTR Campus Page 18
19. SRM UNIVERSITY 15IT102L – PDD Laboratory Solution Manual
EX. No. : 10 FUNCTIONS WITH STATIC DATA TYPE
AIM
To write a C program using functions with static data type.
ALGORITHM
1. Start
2. Call function f1()
3. Print “after first call”
4. Call function f1()
5. Print “after second call”
6. Call function f1()
7. Print “after third call”
8. Stop
Function f1()
7. Initialize k= 0 as static and j=10
8. Print the value of k and j
9. Calculate k=k+10
PROGRAM
/********* FUNCTIONS WITH STATIC DATA TYPE*********/
#include <stdio.h>
int g = 10;
main()
{
void f1();
clrscr();
f1();
printf(" after first call n");
f1();
printf(" after second call n");
f1();
printf(" after third call n");
getch();
}
Compiled by IT Department, SRM University, KTR Campus Page 19
20. SRM UNIVERSITY 15IT102L – PDD Laboratory Solution Manual
void f1()
{
static int k=0;
int j = 10;
printf("nValue of k = %d and j = %d",k,j);
k=k+10;
j=j+10;
}
OUTPUT
Value of k = 0 and j = 10 after first call
Value of k = 10 and j = 10 after second call
Value of k = 20 and j = 10 after third call
RESULT
Thus the C program using functions with static data type was compiled and
executed successfully.
Compiled by IT Department, SRM University, KTR Campus Page 20
21. SRM UNIVERSITY 15IT102L – PDD Laboratory Solution Manual
EX. No. : 11 MATRIX ADDITION
AIM:
To write a C program to perform the Matrix Addition.
ALGORITHM
1. Start
2. Read the order of matrix M,N
3. Read the value of two matrices A,B using loop
4. Set loop i=0 to m and j=0 to N
5. Calculate c[i][j]=a[i][j]+b[i][j]
6. End loop
7. Print c[i][j]
8. Stop
PROGRAM
MATRIX ADDITION
#include<stdio.h>
#include<conio.h>
void main()
{
int n,i,j,a[3][3],b[3][3],c[3][3];
clrscr();
printf("ntEnter the order of matrix: ");
scanf("%d",&n);
printf("n");
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
printf("ntEnter the value of A[%d][%d]: ",i+1,j+1);
scanf("%d",&a[i][j]);
}
}
printf("n");
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
Compiled by IT Department, SRM University, KTR Campus Page 21
23. SRM UNIVERSITY 15IT102L – PDD Laboratory Solution Manual
OUTPUT:
Enter the order of matrix: 3
Enter the value of A[1][1]: 1
Enter the value of A[1][2]: 2
Enter the value of A[1][3]: 3
Enter the value of A[2][1]: 4
Enter the value of A[2][2]: 5
Enter the value of A[2][3]: 6
Enter the value of A[3][1]: 7
Enter the value of A[3][2]: 8
Enter the value of A[3][3]: 9
Enter the value of B[1][1]: 9
Enter the value of B[1][2]: 8
Enter the value of B[1][3]: 7
Enter the value of B[2][1]: 6
Enter the value of B[2][2]: 5
Enter the value of B[2][3]: 4
Enter the value of B[3][1]: 3
Enter the value of B[3][2]: 2
Enter the value of B[3][3]: 1
Matrix A:
********
1 2 3
4 5 6
7 8 9
Matrix B:
********
9 8 7
6 5 4
3 2 1
Resultant Matrix:
***************
10 10 10
10 10 10
10 10 10
RESULT
Thus the C program to perform the matrix addition was compiled and
successfully executed.
Compiled by IT Department, SRM University, KTR Campus Page 23
24. SRM UNIVERSITY 15IT102L – PDD Laboratory Solution Manual
EX. No. : 12 MATRIX OPERATIONS
AIM:
To write a C program to perform the Matrix Multiplication and Transpose
operations.
ALGORITHM
Muliplication
1. Start
2. Read the order of matrix M,N
3. Read the value of two matrices A,B
4. Initialize c[i][j]=0
5. Set loop i=0 to M j=0 to N and k=0 to N
6. Calculate c[i][j]=c[i][j]+a[i][k]*b[k][j]
7. End loop
8. Print c[i][j]
9. Stop
Transpose
1. Start
2. Read the order of matrix M,N
3. Read the value of two matrices A,B
4. Set loop i=0 to M and j=0 to N
5. Assign t[i][j]=a[j][i]
6. End loop
7. Print t[i][j]
8. Stop
PROGRAM
/*** MATRIX OPERATIONS - MULTIPLICATION & TRANSPOSE ***/
#include<stdio.h>
#include<conio.h>
void main()
{
int n,i,j,a[3][3],b[3][3],c[3][3],m,k;
clrscr();
printf("Enter the order of two Matrices");
scanf("%d %d",&m,&n);
Compiled by IT Department, SRM University, KTR Campus Page 24
25. SRM UNIVERSITY 15IT102L – PDD Laboratory Solution Manual
printf("n Enter the Matrix A");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
scanf("%d",&a[i][j]);
}
printf("n");
}
printf("n Enter the Matrix B");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
scanf("%d",&b[i][j]);
}
printf("n");
}
printf("Matrix Multiplicationn");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
c[i][j]=0;
for(k=0;k<n;k++)
{
c[i][j]=c[i][j]+a[i][k]*b[k][j];
}
}
}
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
printf("t%d",c[i][j]);
}
printf("n");
}
printf("Transpose of matrix An");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
Compiled by IT Department, SRM University, KTR Campus Page 25
26. SRM UNIVERSITY 15IT102L – PDD Laboratory Solution Manual
printf("t%d",a[j][i]);
}
printf("n");
}
getch();
}
OUTPUT
Enter the order of two Matrices 3 3
Enter the Matrix A
4 1 4
4 4 4
4 1 4
Enter the Matrix B
2 2 2
2 2 2
2 2 2
Matrix Multiplication
18 18 18
24 24 24
18 18 18
Transpose of matrix A
4 4 4
1 4 1
4 4 4
RESULT
Thus the C program to perform the matrix operations was compiled and
successfully executed.
Compiled by IT Department, SRM University, KTR Campus Page 26
27. SRM UNIVERSITY 15IT102L – PDD Laboratory Solution Manual
EX. No. : 13 PALINDROME CHECKING
AIM
To write a C program to perform given string is palindrome or not a palindrome.
ALGORITHM
1. Start
2. Read the given string in variable s1
3. Calculate the length of string s1 using strlen function and store it in variable cnt
4. Store the reversed string in another string r1 using loop
5. Until s1[cnt] reaches NULL character, store s1[cnt] = r1[index]
6. Assign, r1[index]=NULL character
7. Compare strings s1 and r1, strcmp returns 0 when the strings are equal
8. Or a negative integer when s1 is less than s2,
9. Or a positive integer if s1 is greater than s2,
10. If compare result is zero, print string s1 is palindrome or not a palindrome
11. Stop
PROGRAM
/******************* PALINDROME *****************/
#include<stdio.h>
#include<string.h>
main ()
{
int i,cnt=0;
char s1[20],r1[20];
clrscr();
printf("n Enter the string:");
scanf("%s",&s1);
cnt=stringlen(s1);
for(i=0;s1[i]!='0';i++)
{
cnt--;
r1[i]=s1[cnt];
}
r1[i]='0';
if(strcmp(s1,r1) = =0)
Compiled by IT Department, SRM University, KTR Campus Page 27
28. SRM UNIVERSITY 15IT102L – PDD Laboratory Solution Manual
printf("n %s is palindromen",s1);
else
printf("n %s is not a palindrome",s1);
getch();
}
int stringlen(char s1[])
{
int cn = 0;
while(s1[cn]!='0')
cn++;
return(cn);
}
OUTPUT 1:
Enter the string: hello
hello is not a palindrome
OUTPUT 2:
Enter the string: madam
madam is palindrome
RESULT
Thus the C program to check given string is palindrome or not a palindrome was
compiled and executed successfully.
Compiled by IT Department, SRM University, KTR Campus Page 28
29. SRM UNIVERSITY 15IT102L – PDD Laboratory Solution Manual
EX. No. : 14 REMOVING DUPLICATE STRING FROM A GIVEN SENTENCE
AIM:
To write a C program to remove the duplicate string from the given sentence.
ALGORITHM
1. Start
2. Read the sentence in bin variable from the user
3. Separate the words and store it in temp variable which is two dimension array
4. Compare the consecutive words in temp upto the word count
5. If it is not equal print the word else do not print that word
6. Repeat 4 and 5 until all the words in temp are compared
7. Stop
PROGRAM
/**** REMOVE DUPLICATE STRING FROM A GIVEN SENTENCE ****/
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <conio.h>
void main(void)
{
char temp[10][20];
char bin[100];
int index=0;
int word=0,len=0;
int loop,chain;
clrscr();
printf("nEnter the sentence: ");
gets(bin);
while(bin[index]!=NULL)
{
if(isalpha(bin[index]) || isdigit(bin[index]))
{
Compiled by IT Department, SRM University, KTR Campus Page 29
30. SRM UNIVERSITY 15IT102L – PDD Laboratory Solution Manual
temp[word][len]=bin[index];
len++;
}
else if(isalpha(bin[index-1]) || isdigit(bin[index-1]))
{
temp[word][len]=NULL;
word++;len=0;
}
index++;
}
temp[word][len]=bin[index];
word++;
printf("nThe output data is: ");
for(index=0;index<word;index++)
if(strcmp(temp[index],temp[index+1])!=0)
printf("%s ",temp[index]);
getch();
}
OUTPUT
Enter the sentence: Hi Hi have a nice nice day
The output data is: Hi have a nice day
RESULT
Thus a C program to remove the duplicate string from the given sentence was
compiled and executed successfully.
Compiled by IT Department, SRM University, KTR Campus Page 30
31. SRM UNIVERSITY 15IT102L – PDD Laboratory Solution Manual
EX. No. : 15 ARRAY OF STRUCTURES
AIM
To write a C program to create an array of structures for a list of items with the
following details
Item_Code Item_Name
102 Paste-Colgate
102 Paste-Pepsodent
102 Paste-Close-up
101 Soap-Cinthol
101 Soap-Lux
101 Soap-Hammam
101 Soap-Dove
Arrange the set of items in ascending order of its Item_Code and descending
order of its Item_Name as given below
Item_Code Item_Name
101 Soap-Lux
101 Soap-Hamam
101 Soap-Dove
101 Soap-Cinthol
102 Paste-Pepsodent
102 Paste-Colgate
102 Paste-Close-up
ALGORITHM
1. Start
2. Define structure item with the members
Item_code : integer
Item_name : character
end item
3. Read the given details in item_code and item_name
4. Set loop i=0 to 6 and j=1 to 6
5. Check if a[i].item_code<a[j].item_code then assign
6. Temp=a[i].item_code
Compiled by IT Department, SRM University, KTR Campus Page 31
32. SRM UNIVERSITY 15IT102L – PDD Laboratory Solution Manual
7. A[i].item_code=a[j].item_code
8. A[j].item_code=temp
9. End loop i,j
10. Set loop i=0 to 6 and j=1 to 6
11. Check if a[i].item_name is less then a[j].item_code
12. Then assign a[i].item_name to temp
13. Assign a[j].item_name to a[i].item_name
14. Assign temp to a[j].item_name
15. End loop
16. Print item_code and item_name using loop
17. Stop
PROGRAM
/******************* ARRAY OF STRUCTURES ******************/
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
clrscr();
int i,j,temp;
char temp1[30];
struct item
{
int item_code;
char item_name[30];
}a[7];
printf("Enter the item code and item name");
for(i=0;i<7;i++)
{
scanf("%d %s",&a[i].item_code,&a[i].item_name);
}
for(i=0;i<7;i++)
{
for(j=i+1;j<7;j++)
{
if(a[i].item_code>a[j].item_code)
{
temp=a[i].item_code;
a[i].item_code=a[j].item_code;
a[j].item_code=temp;
Compiled by IT Department, SRM University, KTR Campus Page 32
33. SRM UNIVERSITY 15IT102L – PDD Laboratory Solution Manual
}
}
}
for(i=0;i<7;i++)
{
for(j=i+1;j<7;j++)
{
if(strcmp(a[i].item_name,a[j].item_name)<0)
{
strcpy(temp1,a[i].item_name);
strcpy(a[i].item_name,a[j].item_name);
strcpy(a[j].item_name,temp1);
}
}
}
printf("Item_Code ttt Item_Name");
for(i=0;i<7;i++)
{
printf("n");
printf("%d tt %s",a[i].item_code,a[i].item_name);
}
getch();
}
OUTPUT
Enter the item code and item name
102 Paste-Colgate
102 Paste-Pepsodent
102 Paste-Close-up
101 Soap-Cinthol
101 Soap-Lux
101 Soap-Hamam
101 Soap-Dove
Item_Code Item_Name
101 Soap-Lux
101 Soap-Hamam
101 Soap-Dove
101 Soap-Cinthol
102 Paste-Pepsodent
102 Paste-Colgate
102 Paste-Close-up
RESULT
Thus the list of items using array of structures was successfully compiled and
executed.
Compiled by IT Department, SRM University, KTR Campus Page 33
34. SRM UNIVERSITY 15IT102L – PDD Laboratory Solution Manual
EX. No. : 16 USER-DEFINED DATA TYPE USING STRUCTURE
AIM
To write a C program to create a user defined data type using structure to accept
a list of N numbers of students with their names and roll numbers and arrange them in an
alphabetical order and write the details in the file.
ALGORITHM
1. Start
2. Create a user-defined structure tag with the members
name: character
rno: integer
3. Open the file details .txt in writing mode
4. Read no for the number of student details
5. Read all the student details in name, rno
6. Before sorting print the student details and same time write it in the file details.txt
7. Using function sort(), sort the names upto no count
8. After sorting print the student details and same time write it in the file
9. Stop
PROGRAM
/********* USER-DEFINED DATA TYPE USING STRUCTURE ********/
#include<stdio.h>
#include<string.h>
#include<conio.h>
struct tag
{
char name[10];
int rno;
};
typedef struct tag node;
node s[10],t[10];
void main()
{
FILE *fp;
Compiled by IT Department, SRM University, KTR Campus Page 34
35. SRM UNIVERSITY 15IT102L – PDD Laboratory Solution Manual
int no,i;
fp=fopen("details.txt","w");
clrscr();
fflush(stdin);
printf("n Enter the Number of students:");
scanf("%d",&no);
for(i=0;i<no;i++)
{
printf("n Enter the name of student %d=",i+1);
fflush(stdin);
gets(s[i].name);
printf("n Enter the roll number=");
scanf("%d",&s[i].rno);
t[i]=s[i];
}
printf("n Before Sorting...n");
fprintf(fp," Before Sorting...n");
fprintf(fp,"nRoll_No Student_Namen");
for(i=0;i<no;i++)
{
printf("%3d %10sn",t[i].rno,t[i].name);
fprintf(fp,"%d t %sn",t[i].rno,t[i].name);
}
printf(" After Sorting...n");
fprintf(fp,"nn After Sorting...n");
fprintf(fp,"nRoll_No Student_Namen");
sort(no);
for(i=0;i<no;i++)
{
printf("%3d %10sn",s[i].rno,s[i].name);
fprintf(fp,"%d t %sn", s[i].rno,s[i].name);
}
printf("nThe data are written successfully to the file");
fclose(fp);
getch();
}
sort(int no)
{
int i,j;
node temp;
for(i=0;i<no;i++)
{
for(j=i+1;j<no;j++)
{
Compiled by IT Department, SRM University, KTR Campus Page 35
36. SRM UNIVERSITY 15IT102L – PDD Laboratory Solution Manual
if(strcmp(s[i].name,s[j].name) > 0)
{
temp=s[i];
s[i]=s[j];
s[j]=temp;
}
}
}
}
OUTPUT
Enter the Number of students: 2
Enter the name of student 1=Sundar
Enter the roll number=30
Enter the name of student 3=Reshma
Enter the roll number=25
Before Sorting...
30 Sundar
25 Reshma
After Sorting...
25 Reshma
30 Sundar
The data are written successfully to the file
RESULT
Thus the C program to accept a list of N numbers of students with their names
and roll numbers are arranged in an alphabetical order and successfully written to the file
using user defined data type using structure.
Compiled by IT Department, SRM University, KTR Campus Page 36
37. SRM UNIVERSITY 15IT102L – PDD Laboratory Solution Manual
EX. No. : 17 BASIC PROGRAMS ON POINTERS
AIM
To write a C program to demo on Pointer variables.
ALGORITHM
1. Start
2. Initialize the variables.
3. Assign the address for dereferencing the variable to pointer variable after pointer
declaration.
4. Print out the values based on address referencing and dereferencing operator.
5. Stop
Program1: To illustrate pointer declaration
#include< stdio.h >
void main( )
{
int *ptr;
int sum;
sum=45;
ptr=∑
printf (“n Sum is %dn”, sum);
printf (“n The sum pointer is %u”, ptr);
}
OUTPUT:
Sum is 45
The sum pointer is 65522
Program2: To display the contents of the variable their address using pointer variable
#include< stdio.h >
void main( )
{
int num, *intptr;
float x, *floptr;
char ch, *cptr;
num=123;
x=12.34;
ch=’a’;
intptr=#
cptr=&ch;
floptr=&x;
Compiled by IT Department, SRM University, KTR Campus Page 37
38. SRM UNIVERSITY 15IT102L – PDD Laboratory Solution Manual
printf(“Num %d stored at address %un”,*intptr, intptr);
printf(“Value %f stored at address %un”,*floptr, floptr);
printf(“Character ‘%c’ stored at address %un”,*cptr, cptr);
}
OUTPUT:
Num 123 stored at address 65524
Value 12.340000 stored at address 65520
Character ‘a’ stored at address 65519
Program3: To display the contents of array using pointer
#include<stdio.h>
void main( )
{
int *ptr, a[10];
int i,j=0,n;
printf(“n Enter the limit of the array:”);
scanf(“%d”,&n);
printf(“n Enter the array elements:”);
for(i=0;i< n;i++)
scanf(“%d”,&a[i]);
printf(“n Array elements are”);
for(ptr=a;ptr< (a+n);ptr++)
printf(“n Value of a[%d]=%d stored at address %u”, j++,*ptr, ptr);
}
OUTPUT:
Enter the limit of the array: 4
Enter the array elements
56 7 25 42
Array elements are:
Value of a[0]=56 stored at address 65502
Value of a[1]=7 stored at address 65504
Value of a[2]=25 stored at address 65506
Value of a[3]=42 stored at address 65508
RESULT
Thus the C program to perform basic understanding of pointers was compiled
and executed successfully.
Compiled by IT Department, SRM University, KTR Campus Page 38
39. SRM UNIVERSITY 15IT102L – PDD Laboratory Solution Manual
EX. No. : 18 DISPLAY SUM OF ARRAY USING POINTERS
AIM
To write a C program to Display sum of array using pointers
ALGORITHM
1. Start
2. Read n elements from the user and store it in the array.
3. store the address of the array into the pointer.
4. Set loop i=0 to i=n
5. Fetch the value from the location pointer by pointer variable.
6. Using De-referencing pointer can get the value at address.
7. Perform the addition
8. Repeat until n
9. Print the sum value
10. Stop
PROGRAM
/****** DISPLAY SUM OF ARRAY USING POINTERS ******/
#include<stdio.h>
#include<conio.h>
void main() {
int numArray[10];
int i, sum = 0;
int *ptr;
printf("nEnter 10 elements : ");
for (i = 0; i < 10; i++)
scanf("%d", &numArray[i]);
ptr = numArray; /* a=&a[0] */
for (i = 0; i < 10; i++) {
sum = sum + *ptr;
ptr++;
Compiled by IT Department, SRM University, KTR Campus Page 39
40. SRM UNIVERSITY 15IT102L – PDD Laboratory Solution Manual
}
printf("The sum of array elements : %d", sum);
}
RESULT
Thus the C program to compute sum of array using pointers was compiled and executed
successfully.
Compiled by IT Department, SRM University, KTR Campus Page 40
41. SRM UNIVERSITY 15IT102L – PDD Laboratory Solution Manual
EX. No. : 19 SORTING OF NAMES USING ARRAY OF POINTERS
AIM
To write a C program to sort the given names using array of pointers.
ALGORITHM
1. Start
2. Read n for number of names to be sorted
3. Read the names
4. Set loop i=0 to n and j=1 to n
5.Compare the first name with second name if is greater than 0
6. Interchange the first name and the second name using third variable
7. Repeat until all the names are sorted
8. Print the sorted names
9. Stop
PROGRAM
/****** SORTING OF NAMES USING ARRAY OF POINTERS ******/
#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<stdlib.h>
#define MAX 10
void main()
{
clrscr();
char *names[MAX],temp[15],*temp1;
int n,i,j,len;
printf("Enter the no. of names to be sorted");
scanf("%d",&n);
*names=NULL;
fflush(stdin);
printf("enter the names one by onen");
for(i=0;i<n;i++)
{
gets(temp);
len=strlen(temp);
*(names+i)=(char*)malloc(len+1);
strcpy(*(names+i),temp);
Compiled by IT Department, SRM University, KTR Campus Page 41
42. SRM UNIVERSITY 15IT102L – PDD Laboratory Solution Manual
}
for(i=0;i<n;i++)
for(j=i+1;j<n;j++)
{
if(strcmp(*(names+i),*(names+j))>0)
{
temp1=*(names+i);
*(names+i)=*(names+j);
*(names+j)=temp1;
}
}
printf("n the sorted list of names:n");
for(i=0;i<n;i++)
printf("%sn",*(names+i));
getch();
}
OUTPUT
Enter the no. of names to be sorted: 5
Enter the names one by one:
Selva
Deebiga
Murugan
Marie
Lakshmi
The sorted list of names:
Deebiga
Lakshmi
Marie
Murugan
Selva
RESULT
Thus the C program to sort the given names using array of pointers was compiled
and executed successfully.
Compiled by IT Department, SRM University, KTR Campus Page 42
43. SRM UNIVERSITY 15IT102L – PDD Laboratory Solution Manual
EX. No. : 20 USING POINTERS COMPUTE MEAN AND STANDARD
DEVIATION
AIM
To write a C program to compute mean and standard deviation
ALGORITHM
1. Declare an integer array , x[50];
2. Initialize the temporary variables to compute the mean value and standard
deviation values.
3. read the input array.
4. compute the mean and standard deviation on the stored values using *
operators.
5.Print the computed values, mean, standard deviation.
6. Stop the execution of the program.
PROGRAM
/** COMPUTE MEAN AND STANDARD DEVIATION USING POINTERS ***/
#include <stdio.h>
#include <math.h>
void main()
{
int x [50]; /* max. 50 elements in array x */
int n; /*number of elements in array x */
int sum, mean, sd; /* sum, mean and standard deviation.*/
int i;
clrscr();
/* read data */
printf("How many numbers (max 50)? ");
scanf("%d", &n);
printf ("Enter numbers: ");
Compiled by IT Department, SRM University, KTR Campus Page 43
44. SRM UNIVERSITY 15IT102L – PDD Laboratory Solution Manual
for(i = 0; i<n; i++)
{
scanf("%d", (x+i));
sum += *(x+i);
}
/* calculate mean */
sum= 0;
for(i = 0; i < n; i++)
{
sum+= *(x+i);
}
mean = sum / n;
/* calculate standard deviation */
sum= 0;
for(i = 0; i < n; i++)
{
sum+= (*(x+i) - mean) * (*(x+i) - mean);
}
sd = sqrt(sum / n);
printf("Mean = %dn", mean);
printf("Standard Deviation: %dn", sd);
getch();
}
Compiled by IT Department, SRM University, KTR Campus Page 44
45. SRM UNIVERSITY 15IT102L – PDD Laboratory Solution Manual
RESULT
Thus the C program to compute mean and standard deviation was compiled and
executed successfully.
Compiled by IT Department, SRM University, KTR Campus Page 45