SlideShare a Scribd company logo
1 of 45
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
SRM UNIVERSITY 15IT102L – PDD Laboratory Solution Manual
{
printf("ntEnter the value of B[%d][%d]: ",i+1,j+1);
scanf("%d",&b[i][j]);
}
}
for(i=0;i<n;i++)
{
printf("t");
for(j=0;j<n;j++)
{
c[i][j]=a[i][j]+b[i][j];
printf(" %d",c[i][j]);
printf("n");
}
}
printf("nnttMatrix A:");
printf(" ntt********nn");
for(i=0;i<n;i++)
{
printf("t");
for(j=0;j<n;j++)
printf(" %d",a[i][j]);
printf("n");
}
printf("nnttMatrix B:");
printf(" ntt********nn");
for(i=0;i<n;i++)
{
printf("t");
for(j=0;j<n;j++)
printf(" %d",b[i][j]);
printf("n");
}
printf("nnttResultant Matrix:");
printf(" ntt***************nn");
for(i=0;i<n;i++)
{
printf("t");
for(j=0;j<n;j++)
printf(" %d",c[i][j]);
printf("n");
}
getch();
}
Compiled by IT Department, SRM University, KTR Campus Page 22
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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=&sum;
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=&num;
cptr=&ch;
floptr=&x;
Compiled by IT Department, SRM University, KTR Campus Page 37
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
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
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
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
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
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
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
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

More Related Content

What's hot

C mcq practice test 4
C mcq practice test 4C mcq practice test 4
C mcq practice test 4Aman Kamboj
 
Functions & Procedures [7]
Functions & Procedures [7]Functions & Procedures [7]
Functions & Procedures [7]ecko_disasterz
 
Computer Architecture and Organization lab with matlab
Computer Architecture and Organization lab with matlabComputer Architecture and Organization lab with matlab
Computer Architecture and Organization lab with matlabShankar Gangaju
 
Csc1100 lecture02 ch02-datatype_declaration
Csc1100 lecture02 ch02-datatype_declarationCsc1100 lecture02 ch02-datatype_declaration
Csc1100 lecture02 ch02-datatype_declarationIIUM
 
Chapter 6 Balagurusamy Programming ANSI in c
Chapter 6  Balagurusamy Programming ANSI  in cChapter 6  Balagurusamy Programming ANSI  in c
Chapter 6 Balagurusamy Programming ANSI in cBUBT
 
Csc1100 lecture05 ch05
Csc1100 lecture05 ch05Csc1100 lecture05 ch05
Csc1100 lecture05 ch05IIUM
 
Chapter 2 : Balagurusamy_ Programming ANsI in C
Chapter 2 :  Balagurusamy_ Programming ANsI in CChapter 2 :  Balagurusamy_ Programming ANsI in C
Chapter 2 : Balagurusamy_ Programming ANsI in CBUBT
 
(Www.entrance exam.net)-tcs placement sample paper 2
(Www.entrance exam.net)-tcs placement sample paper 2(Www.entrance exam.net)-tcs placement sample paper 2
(Www.entrance exam.net)-tcs placement sample paper 2Pamidimukkala Sivani
 
Dam31303 dti2143 lab sheet 7
Dam31303 dti2143 lab sheet 7Dam31303 dti2143 lab sheet 7
Dam31303 dti2143 lab sheet 7alish sha
 
Core programming in c
Core programming in cCore programming in c
Core programming in cRahul Pandit
 

What's hot (20)

C mcq practice test 4
C mcq practice test 4C mcq practice test 4
C mcq practice test 4
 
Functions & Procedures [7]
Functions & Procedures [7]Functions & Procedures [7]
Functions & Procedures [7]
 
Computer Architecture and Organization lab with matlab
Computer Architecture and Organization lab with matlabComputer Architecture and Organization lab with matlab
Computer Architecture and Organization lab with matlab
 
comp2
comp2comp2
comp2
 
comp1
comp1comp1
comp1
 
Lab 1
Lab 1Lab 1
Lab 1
 
Csc1100 lecture02 ch02-datatype_declaration
Csc1100 lecture02 ch02-datatype_declarationCsc1100 lecture02 ch02-datatype_declaration
Csc1100 lecture02 ch02-datatype_declaration
 
Chapter 6 Balagurusamy Programming ANSI in c
Chapter 6  Balagurusamy Programming ANSI  in cChapter 6  Balagurusamy Programming ANSI  in c
Chapter 6 Balagurusamy Programming ANSI in c
 
I PUC CS Lab_programs
I PUC CS Lab_programsI PUC CS Lab_programs
I PUC CS Lab_programs
 
Csc1100 lecture05 ch05
Csc1100 lecture05 ch05Csc1100 lecture05 ch05
Csc1100 lecture05 ch05
 
Chapter 2 : Balagurusamy_ Programming ANsI in C
Chapter 2 :  Balagurusamy_ Programming ANsI in CChapter 2 :  Balagurusamy_ Programming ANsI in C
Chapter 2 : Balagurusamy_ Programming ANsI in C
 
(Www.entrance exam.net)-tcs placement sample paper 2
(Www.entrance exam.net)-tcs placement sample paper 2(Www.entrance exam.net)-tcs placement sample paper 2
(Www.entrance exam.net)-tcs placement sample paper 2
 
week-3x
week-3xweek-3x
week-3x
 
Dam31303 dti2143 lab sheet 7
Dam31303 dti2143 lab sheet 7Dam31303 dti2143 lab sheet 7
Dam31303 dti2143 lab sheet 7
 
Technical questions
Technical questionsTechnical questions
Technical questions
 
C Programming
C ProgrammingC Programming
C Programming
 
C programs
C programsC programs
C programs
 
Core programming in c
Core programming in cCore programming in c
Core programming in c
 
Cpph exam a_2019_s1
Cpph exam a_2019_s1Cpph exam a_2019_s1
Cpph exam a_2019_s1
 
Booth algorithm
Booth algorithmBooth algorithm
Booth algorithm
 

Viewers also liked

Internship report
Internship reportInternship report
Internship reportMomina Bibi
 
IGNOU Assembly Language Programming
IGNOU Assembly Language ProgrammingIGNOU Assembly Language Programming
IGNOU Assembly Language ProgrammingDr. Loganathan R
 
C programming-apurbo datta
C programming-apurbo dattaC programming-apurbo datta
C programming-apurbo dattaApurbo Datta
 
Machine design 5 (md) Mechanical Engineering handwritten classes notes (study...
Machine design 5 (md) Mechanical Engineering handwritten classes notes (study...Machine design 5 (md) Mechanical Engineering handwritten classes notes (study...
Machine design 5 (md) Mechanical Engineering handwritten classes notes (study...Khagendra Gautam
 
Engineering mechanics 1 handwritten classes notes (study materials) for IES P...
Engineering mechanics 1 handwritten classes notes (study materials) for IES P...Engineering mechanics 1 handwritten classes notes (study materials) for IES P...
Engineering mechanics 1 handwritten classes notes (study materials) for IES P...Khagendra Gautam
 
Mathematics 1 handwritten classes notes (study materials) for IES PSUs GATE
Mathematics 1 handwritten classes notes (study materials) for IES PSUs GATEMathematics 1 handwritten classes notes (study materials) for IES PSUs GATE
Mathematics 1 handwritten classes notes (study materials) for IES PSUs GATEKhagendra Gautam
 
Design of machine elements
Design of machine elementsDesign of machine elements
Design of machine elementsbenchmate
 
Error analysis presentation
Error analysis presentationError analysis presentation
Error analysis presentationGeraldine Lopez
 
Computer Generation Presentation
Computer Generation PresentationComputer Generation Presentation
Computer Generation PresentationJayesh Modi
 
Research & Reviews: A Journal of Toxicology vol 6 issue 3
Research & Reviews: A Journal of Toxicology vol 6 issue 3Research & Reviews: A Journal of Toxicology vol 6 issue 3
Research & Reviews: A Journal of Toxicology vol 6 issue 3STM Journals
 
Electiva iv, act1, 3 corte
Electiva iv, act1, 3 corteElectiva iv, act1, 3 corte
Electiva iv, act1, 3 corteRAFAEL QUIARAGUA
 
Research & Reviews A Journal of Health Professions vol 6 issue 3
Research & Reviews A Journal of Health Professions vol 6 issue 3Research & Reviews A Journal of Health Professions vol 6 issue 3
Research & Reviews A Journal of Health Professions vol 6 issue 3STM Journals
 

Viewers also liked (19)

Internship report
Internship reportInternship report
Internship report
 
IGNOU Assembly Language Programming
IGNOU Assembly Language ProgrammingIGNOU Assembly Language Programming
IGNOU Assembly Language Programming
 
C programming-apurbo datta
C programming-apurbo dattaC programming-apurbo datta
C programming-apurbo datta
 
Machine design 5 (md) Mechanical Engineering handwritten classes notes (study...
Machine design 5 (md) Mechanical Engineering handwritten classes notes (study...Machine design 5 (md) Mechanical Engineering handwritten classes notes (study...
Machine design 5 (md) Mechanical Engineering handwritten classes notes (study...
 
Structure in C
Structure in CStructure in C
Structure in C
 
Engineering mechanics 1 handwritten classes notes (study materials) for IES P...
Engineering mechanics 1 handwritten classes notes (study materials) for IES P...Engineering mechanics 1 handwritten classes notes (study materials) for IES P...
Engineering mechanics 1 handwritten classes notes (study materials) for IES P...
 
Mathematics 1 handwritten classes notes (study materials) for IES PSUs GATE
Mathematics 1 handwritten classes notes (study materials) for IES PSUs GATEMathematics 1 handwritten classes notes (study materials) for IES PSUs GATE
Mathematics 1 handwritten classes notes (study materials) for IES PSUs GATE
 
Design of machine elements
Design of machine elementsDesign of machine elements
Design of machine elements
 
Error analysis presentation
Error analysis presentationError analysis presentation
Error analysis presentation
 
Computer Generation Presentation
Computer Generation PresentationComputer Generation Presentation
Computer Generation Presentation
 
Estudiante
EstudianteEstudiante
Estudiante
 
El agua
El aguaEl agua
El agua
 
parts of speech
parts of speechparts of speech
parts of speech
 
slide share
slide shareslide share
slide share
 
لکنت زبان
لکنت زبانلکنت زبان
لکنت زبان
 
Get started with dropbox
Get started with dropboxGet started with dropbox
Get started with dropbox
 
Research & Reviews: A Journal of Toxicology vol 6 issue 3
Research & Reviews: A Journal of Toxicology vol 6 issue 3Research & Reviews: A Journal of Toxicology vol 6 issue 3
Research & Reviews: A Journal of Toxicology vol 6 issue 3
 
Electiva iv, act1, 3 corte
Electiva iv, act1, 3 corteElectiva iv, act1, 3 corte
Electiva iv, act1, 3 corte
 
Research & Reviews A Journal of Health Professions vol 6 issue 3
Research & Reviews A Journal of Health Professions vol 6 issue 3Research & Reviews A Journal of Health Professions vol 6 issue 3
Research & Reviews A Journal of Health Professions vol 6 issue 3
 

Similar to c programing

C and Data structure lab manual ECE (2).pdf
C and Data structure lab manual ECE (2).pdfC and Data structure lab manual ECE (2).pdf
C and Data structure lab manual ECE (2).pdfjanakim15
 
Programming in C Lab
Programming in C LabProgramming in C Lab
Programming in C LabNeil Mathew
 
Cs291 assignment solution
Cs291 assignment solutionCs291 assignment solution
Cs291 assignment solutionKuntal Bhowmick
 
C decision making and looping.
C decision making and looping.C decision making and looping.
C decision making and looping.Haard Shah
 
Mid term sem 2 1415 sol
Mid term sem 2 1415 solMid term sem 2 1415 sol
Mid term sem 2 1415 solIIUM
 
Important C program of Balagurusamy Book
Important C program of Balagurusamy BookImportant C program of Balagurusamy Book
Important C program of Balagurusamy BookAbir Hossain
 
Fy secondsemester2016
Fy secondsemester2016Fy secondsemester2016
Fy secondsemester2016Ankit Dubey
 
Fy secondsemester2016
Fy secondsemester2016Fy secondsemester2016
Fy secondsemester2016Ankit Dubey
 
Fy secondsemester2016
Fy secondsemester2016Fy secondsemester2016
Fy secondsemester2016Ankit Dubey
 
LAB_MANUAL_cpl_21scheme-2.pdf
LAB_MANUAL_cpl_21scheme-2.pdfLAB_MANUAL_cpl_21scheme-2.pdf
LAB_MANUAL_cpl_21scheme-2.pdfRavinReddy3
 
Md university cmis 102 week 4 hands on lab new
Md university cmis 102 week 4 hands on lab newMd university cmis 102 week 4 hands on lab new
Md university cmis 102 week 4 hands on lab neweyavagal
 
Md university cmis 102 week 4 hands on lab new
Md university cmis 102 week 4 hands on lab newMd university cmis 102 week 4 hands on lab new
Md university cmis 102 week 4 hands on lab newLast7693
 
Md university cmis 102 week 4 hands on lab new
Md university cmis 102 week 4 hands on lab newMd university cmis 102 week 4 hands on lab new
Md university cmis 102 week 4 hands on lab newscottbrownnn
 
Datastructure notes
Datastructure notesDatastructure notes
Datastructure notesSrikanth
 

Similar to c programing (20)

C and Data structure lab manual ECE (2).pdf
C and Data structure lab manual ECE (2).pdfC and Data structure lab manual ECE (2).pdf
C and Data structure lab manual ECE (2).pdf
 
Programming in C Lab
Programming in C LabProgramming in C Lab
Programming in C Lab
 
LMmanual.pdf
LMmanual.pdfLMmanual.pdf
LMmanual.pdf
 
Cs291 assignment solution
Cs291 assignment solutionCs291 assignment solution
Cs291 assignment solution
 
Cp manual final
Cp manual finalCp manual final
Cp manual final
 
C decision making and looping.
C decision making and looping.C decision making and looping.
C decision making and looping.
 
Mid term sem 2 1415 sol
Mid term sem 2 1415 solMid term sem 2 1415 sol
Mid term sem 2 1415 sol
 
Important C program of Balagurusamy Book
Important C program of Balagurusamy BookImportant C program of Balagurusamy Book
Important C program of Balagurusamy Book
 
C-LOOP-Session-2.pptx
C-LOOP-Session-2.pptxC-LOOP-Session-2.pptx
C-LOOP-Session-2.pptx
 
CP Handout#4
CP Handout#4CP Handout#4
CP Handout#4
 
Fy secondsemester2016
Fy secondsemester2016Fy secondsemester2016
Fy secondsemester2016
 
Fy secondsemester2016
Fy secondsemester2016Fy secondsemester2016
Fy secondsemester2016
 
Fy secondsemester2016
Fy secondsemester2016Fy secondsemester2016
Fy secondsemester2016
 
Unit-IV.pptx
Unit-IV.pptxUnit-IV.pptx
Unit-IV.pptx
 
LAB_MANUAL_cpl_21scheme-2.pdf
LAB_MANUAL_cpl_21scheme-2.pdfLAB_MANUAL_cpl_21scheme-2.pdf
LAB_MANUAL_cpl_21scheme-2.pdf
 
C++ in 10 Hours.pdf.pdf
C++ in 10 Hours.pdf.pdfC++ in 10 Hours.pdf.pdf
C++ in 10 Hours.pdf.pdf
 
Md university cmis 102 week 4 hands on lab new
Md university cmis 102 week 4 hands on lab newMd university cmis 102 week 4 hands on lab new
Md university cmis 102 week 4 hands on lab new
 
Md university cmis 102 week 4 hands on lab new
Md university cmis 102 week 4 hands on lab newMd university cmis 102 week 4 hands on lab new
Md university cmis 102 week 4 hands on lab new
 
Md university cmis 102 week 4 hands on lab new
Md university cmis 102 week 4 hands on lab newMd university cmis 102 week 4 hands on lab new
Md university cmis 102 week 4 hands on lab new
 
Datastructure notes
Datastructure notesDatastructure notes
Datastructure notes
 

Recently uploaded

Coefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxCoefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxAsutosh Ranjan
 
Introduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptxIntroduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptxupamatechverse
 
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVHARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVRajaP95
 
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...Soham Mondal
 
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Christo Ananth
 
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINEMANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINESIVASHANKAR N
 
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...srsj9000
 
GDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentationGDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentationGDSCAESB
 
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 
Software Development Life Cycle By Team Orange (Dept. of Pharmacy)
Software Development Life Cycle By  Team Orange (Dept. of Pharmacy)Software Development Life Cycle By  Team Orange (Dept. of Pharmacy)
Software Development Life Cycle By Team Orange (Dept. of Pharmacy)Suman Mia
 
Introduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptxIntroduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptxupamatechverse
 
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escortsranjana rawat
 
the ladakh protest in leh ladakh 2024 sonam wangchuk.pptx
the ladakh protest in leh ladakh 2024 sonam wangchuk.pptxthe ladakh protest in leh ladakh 2024 sonam wangchuk.pptx
the ladakh protest in leh ladakh 2024 sonam wangchuk.pptxhumanexperienceaaa
 
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCollege Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCall Girls in Nagpur High Profile
 
Analog to Digital and Digital to Analog Converter
Analog to Digital and Digital to Analog ConverterAnalog to Digital and Digital to Analog Converter
Analog to Digital and Digital to Analog ConverterAbhinavSharma374939
 
Introduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxIntroduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxupamatechverse
 
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escortsranjana rawat
 

Recently uploaded (20)

Coefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxCoefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptx
 
Introduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptxIntroduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptx
 
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVHARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
 
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
 
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
 
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINEMANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
 
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
 
GDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentationGDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentation
 
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
Software Development Life Cycle By Team Orange (Dept. of Pharmacy)
Software Development Life Cycle By  Team Orange (Dept. of Pharmacy)Software Development Life Cycle By  Team Orange (Dept. of Pharmacy)
Software Development Life Cycle By Team Orange (Dept. of Pharmacy)
 
Introduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptxIntroduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptx
 
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
 
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
 
the ladakh protest in leh ladakh 2024 sonam wangchuk.pptx
the ladakh protest in leh ladakh 2024 sonam wangchuk.pptxthe ladakh protest in leh ladakh 2024 sonam wangchuk.pptx
the ladakh protest in leh ladakh 2024 sonam wangchuk.pptx
 
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCollege Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
 
Analog to Digital and Digital to Analog Converter
Analog to Digital and Digital to Analog ConverterAnalog to Digital and Digital to Analog Converter
Analog to Digital and Digital to Analog Converter
 
Roadmap to Membership of RICS - Pathways and Routes
Roadmap to Membership of RICS - Pathways and RoutesRoadmap to Membership of RICS - Pathways and Routes
Roadmap to Membership of RICS - Pathways and Routes
 
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCRCall Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
 
Introduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxIntroduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptx
 
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
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
  • 22. SRM UNIVERSITY 15IT102L – PDD Laboratory Solution Manual { printf("ntEnter the value of B[%d][%d]: ",i+1,j+1); scanf("%d",&b[i][j]); } } for(i=0;i<n;i++) { printf("t"); for(j=0;j<n;j++) { c[i][j]=a[i][j]+b[i][j]; printf(" %d",c[i][j]); printf("n"); } } printf("nnttMatrix A:"); printf(" ntt********nn"); for(i=0;i<n;i++) { printf("t"); for(j=0;j<n;j++) printf(" %d",a[i][j]); printf("n"); } printf("nnttMatrix B:"); printf(" ntt********nn"); for(i=0;i<n;i++) { printf("t"); for(j=0;j<n;j++) printf(" %d",b[i][j]); printf("n"); } printf("nnttResultant Matrix:"); printf(" ntt***************nn"); for(i=0;i<n;i++) { printf("t"); for(j=0;j<n;j++) printf(" %d",c[i][j]); printf("n"); } getch(); } Compiled by IT Department, SRM University, KTR Campus Page 22
  • 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=&sum; 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=&num; 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